Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # Examples:
- #
- # Linux (-d plays immediately via /dev/dsp):
- # python playit.py -d '(t>>7|t|t>>6)*10+4*(t&t>>13|t>>6)'
- #
- # WAV compatible OS (-w writes to file):
- # python playit.py -w -o output.wav '(t>>7|t|t>>6)*10+4*(t&t>>13|t>>6)'
- #
- import sys, optparse, wave
- parser = optparse.OptionParser(
- usage='%prog [options] t-expr',
- description='''
- Based on "Experimental one-line algorithmic music":
- http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html
- ''')
- parser.set_defaults(outfile=None, start=0, length=30, wrapper=None)
- parser.add_option('-s', '--start', dest='start',
- type='int', help='Set starting position in seconds')
- parser.add_option('-l', '--length', dest='length',
- type='int', help='Set length in seconds')
- parser.add_option('-d', '--dsp', dest='outfile', action='store_const',
- const='/dev/dsp', help='Send to /dev/dsp audio output')
- parser.add_option('-w', '--wave', dest='wrapper', action='store_const',
- const='wav', help='Add a WAV wrapper')
- parser.add_option('-o', '--output', dest='outfile', action='store',
- help='Send output to specified file instead of stdout')
- opts, args = parser.parse_args()
- if len(args) != 1:
- parser.print_help()
- sys.exit(20)
- t_expr, = args
- outfile = opts.outfile
- if outfile:
- output = open(outfile, 'wb')
- else:
- output = sys.stdout
- wrapper = opts.wrapper
- if wrapper == 'wav':
- output = wave.open(output)
- output.setnchannels(1)
- output.setsampwidth(1)
- output.setframerate(8000)
- writer = output.writeframes
- else:
- writer = output.write
- start = opts.start
- length = opts.length
- t_start = 8000 * int(start)
- t_end = t_start + 8000 * int(length)
- stream = eval(
- '("".join(chr(int(%s) & 0xFF) for t in xrange(s, s + 8000)) '
- ' for s in xrange(%d, %d, 8000))'
- % (t_expr, t_start, t_end))
- for chunk in stream:
- writer(chunk)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement