From 306a448b525dbda5caed96aa7e009d0e8ca83fcc Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 15 May 2014 20:16:14 +0200 Subject: [PATCH] Add beep generating tool --- tools/gen_sound.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tools/gen_sound.py diff --git a/tools/gen_sound.py b/tools/gen_sound.py new file mode 100644 index 0000000..6efc05e --- /dev/null +++ b/tools/gen_sound.py @@ -0,0 +1,54 @@ +# 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 +# CDDA output (44100 Hz, 16 bit signed) for easier conversion to ogg +# by multiply the value by 256 (after roundin) and repeating it 4 times. + +import sys +import math +import struct + +OUTPUT_RATE = 8125 +MAX = 95 + +def gen_sine(freq, secs): + 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)) + data.extend([struct.pack(' []' + print ' where is the frequency in Hz (int)' + print ' and [] is the time in seconds (float) - default 0.25' + + +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) + -- 2.34.1