Advertisement
Guest User

Updated Python One-Line Player

a guest
Nov 14th, 2011
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. #   Examples:
  4. #
  5. #   Linux (-d plays immediately via /dev/dsp):
  6. #   python playit.py -d '(t>>7|t|t>>6)*10+4*(t&t>>13|t>>6)'
  7. #
  8. #   WAV compatible OS (-w writes to file):
  9. #   python playit.py -w -o output.wav '(t>>7|t|t>>6)*10+4*(t&t>>13|t>>6)'
  10. #
  11.  
  12. import sys, optparse, wave
  13.  
  14. parser = optparse.OptionParser(
  15.     usage='%prog [options] t-expr',
  16.     description='''
  17.    Based on "Experimental one-line algorithmic music":
  18.    http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html
  19.    ''')
  20. parser.set_defaults(outfile=None, start=0, length=30, wrapper=None)
  21. parser.add_option('-s', '--start', dest='start',
  22.                   type='int', help='Set starting position in seconds')
  23. parser.add_option('-l', '--length', dest='length',
  24.                   type='int', help='Set length in seconds')
  25. parser.add_option('-d', '--dsp', dest='outfile', action='store_const',
  26.                   const='/dev/dsp', help='Send to /dev/dsp audio output')
  27. parser.add_option('-w', '--wave', dest='wrapper', action='store_const',
  28.                   const='wav', help='Add a WAV wrapper')
  29. parser.add_option('-o', '--output', dest='outfile', action='store',
  30.                   help='Send output to specified file instead of stdout')
  31. opts, args = parser.parse_args()
  32.  
  33. if len(args) != 1:
  34.     parser.print_help()
  35.     sys.exit(20)
  36. t_expr, = args
  37.  
  38. outfile = opts.outfile
  39. if outfile:
  40.     output = open(outfile, 'wb')
  41. else:
  42.     output = sys.stdout
  43. wrapper = opts.wrapper
  44. if wrapper == 'wav':
  45.     output = wave.open(output)
  46.     output.setnchannels(1)
  47.     output.setsampwidth(1)
  48.     output.setframerate(8000)
  49.     writer = output.writeframes
  50. else:
  51.     writer = output.write
  52. start = opts.start
  53. length = opts.length
  54.  
  55. t_start = 8000 * int(start)
  56. t_end = t_start + 8000 * int(length)
  57. stream = eval(
  58.     '("".join(chr(int(%s) & 0xFF) for t in xrange(s, s + 8000)) '
  59.                                  ' for s in xrange(%d, %d, 8000))'
  60.     % (t_expr, t_start, t_end))
  61. for chunk in stream:
  62.     writer(chunk)
  63.  
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement