X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=tools%2Fgen_sound.py;h=7b13a819204f76d7635fcd38b6b31fc78a9734c4;hb=0b89694e222a490fd93ca6f7dfe0045d10b15dd6;hp=6efc05ebff69587062fe13d326db171a9b28ab5a;hpb=306a448b525dbda5caed96aa7e009d0e8ca83fcc;p=naja.git diff --git a/tools/gen_sound.py b/tools/gen_sound.py old mode 100644 new mode 100755 index 6efc05e..7b13a81 --- a/tools/gen_sound.py +++ b/tools/gen_sound.py @@ -1,5 +1,7 @@ +#!/usr/bin/env python + # Generate imperfect sine waves -# +# # Design notes. Produces ~= (user requested) s of raw audio # We're aiming for an 8-bit'ish effect, so we're going with # 8125 Hz, 8 bit sampling, but faking it out to @@ -11,44 +13,60 @@ import math import struct OUTPUT_RATE = 8125 -MAX = 95 +DEFAULT_VOL = 95 + -def gen_sine(freq, secs): +def gen_sine(freq, secs, volume): filename = 'beep%s.pcm' % freq # We generate freq cycles and sample that OUTPUT_RATE times - per_cycle = OUTPUT_RATE / freq + per_cycle = OUTPUT_RATE // freq data = [] for x in range(per_cycle): rad = float(x) / per_cycle * 2 * math.pi - y = 256 * int(MAX * math.sin(rad)) + y = 256 * int(volume * math.sin(rad)) data.extend([struct.pack(' []' - print ' where is the frequency in Hz (int)' - print ' and [] is the time in seconds (float) - default 0.25' + print ('Unexpected input') + print ('Usage gen_sound [] []') + print (' where is the frequency in Hz (int)') + print (' [] is the time in seconds (float) - default 0.25') + print (' and [] is the volume (integer between 0 and 127)' + ' - default %s' % DEFAULT_VOL) if __name__ == "__main__": try: - freq = int(sys.argv[1]) - if len(sys.argv) > 2: - secs = float(sys.argv[2]) - else: - secs = 0.25 - except Exception, exc: - usage() - print 'Error was', exc - sys.exit(1) - - gen_sine(freq, secs) - + freq = int(sys.argv[1]) + if len(sys.argv) > 2: + secs = float(sys.argv[2]) + else: + secs = 0.25 + if len(sys.argv) > 3: + volume = int(sys.argv[3]) + else: + volume = DEFAULT_VOL + except Exception as exc: + usage() + print ('Error was: %s' % exc) + sys.exit(1) + + if volume > 128 or volume < 0: + usage() + print ('Invalid volume: %s' % volume) + sys.exit(1) + + if freq > 2000 or freq < 100: + usage() + print ('Invalid freq: %s' % volume) + sys.exit(1) + + gen_sine(freq, secs, volume)