Mercurial > boomslang
diff pyntnclick/tools/gen_sound.py @ 589:ebc48b397fd5 pyntnclick
Turn rect_drawer into a command line option
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Sat, 11 Feb 2012 17:07:52 +0200 |
parents | tools/gen_sound.py@15713dfe555d |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyntnclick/tools/gen_sound.py Sat Feb 11 17:07:52 2012 +0200 @@ -0,0 +1,57 @@ +# Generate 'perfect' sine wave sounds + +# Design notes: produces ~= (use requested) s raw CDDA audio - 44100 Hz +# 16 bit signed values +# Input is freq in Hz - 440 for A, etc. - must be an integer +# Output is written the file beep<freq>.pcm +# Convert to ogg with oggenc -r <file> + +import sys +import math +import struct + +CDDA_RATE = 44100 +MAX = 105 * 256 # Max value for sine wave + + +def gen_sine(freq, secs): + filename = 'beep%s.pcm' % freq + # We need to generate freq cycles and sample that CDDA_RATE times + samples_per_cycle = CDDA_RATE / freq + data = [] + for x in range(samples_per_cycle): + rad = float(x) / samples_per_cycle * 2 * math.pi + y = MAX * math.sin(rad) + data.append(struct.pack('<i', y)) + output = open(filename, 'w') + for x in range(int(freq * secs)): + output.write(''.join(data)) + output.close() + return filename + + +def usage(): + print 'Unexpected input' + print 'Usage: gen_sound.py <freq> [<length>]' + print ' where <freq> is the frequency in Hz (integer)' + print ' and [<length>] is the time is seconds (float)' + +if __name__ == "__main__": + try: + freq = int(sys.argv[1]) + except Exception, exc: + usage() + print 'Error was: %s' % exc + sys.exit(1) + + if len(sys.argv) > 2: + try: + secs = float(sys.argv[2]) + except Exception, exc: + usage() + print 'Error was: %s' % exc + sys.exit(1) + else: + secs = 0.25 + output = gen_sine(freq, secs) + print 'Wrote sample to %s' % output