From d5b40ebebac451779deb4631972d3076b2b02d45 Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 15 May 2014 20:58:01 +0200 Subject: [PATCH] Add volume parameter to gen_sound --- tools/gen_sound.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tools/gen_sound.py b/tools/gen_sound.py index 6efc05e..bf2c10d 100644 --- a/tools/gen_sound.py +++ b/tools/gen_sound.py @@ -13,14 +13,14 @@ import struct OUTPUT_RATE = 8125 MAX = 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 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 'Usage gen_sound [] []' print ' where is the frequency in Hz (int)' - print ' and [] is the time in seconds (float) - default 0.25' + print ' [] is the time in seconds (float) - default 0.25' + print ' and [] is the volume (integer between 0 and 127) - default %s' % MAX if __name__ == "__main__": @@ -45,10 +46,25 @@ if __name__ == "__main__": secs = float(sys.argv[2]) else: secs = 0.25 + if len(sys.argv) > 3: + volume = int(sys.argv[3]) + else: + volume = MAX except Exception, exc: usage() print 'Error was', exc sys.exit(1) - gen_sine(freq, secs) + 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) -- 2.34.1