X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=tools%2Fgen_sound.py;h=94bf3d12cd59d7891ece2dc2674a901737eaf982;hb=a1c561e26fae79ce8a9570c9404975a8a0367d65;hp=db37d8267325d187ed9ffdfce8ec5c861e8be35a;hpb=00a560f0ecaa4dde4e194b6f9cba089ad9a7e8b6;p=naja.git diff --git a/tools/gen_sound.py b/tools/gen_sound.py old mode 100644 new mode 100755 index db37d82..94bf3d1 --- a/tools/gen_sound.py +++ b/tools/gen_sound.py @@ -1,70 +1,45 @@ -# 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. +#!/usr/bin/env python +from subprocess import Popen, PIPE +import os import sys -import math -import struct -OUTPUT_RATE = 8125 -DEFAULT_VOL = 95 +def gen_raw(description): + for chunk in description: + for blob in chunk.raw(): + yield blob -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(volume * math.sin(rad)) - data.extend([struct.pack(' [] []') - 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) +def write(basename, description): + return encode('%s.ogg' % basename, gen_raw(description)) -if __name__ == "__main__": - try: - 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) +def main(): + sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + from data.sounds import SOUNDS + from naja.gen_sound import Chunk - if freq > 2000 or freq < 100: - usage() - print ('Invalid freq: %s' % volume) - sys.exit(1) + sounds = SOUNDS.keys() + if len(sys.argv) > 1: + sounds = sys.argv[1:] + for sound in sounds: + description = SOUNDS[sound] + if isinstance(description, Chunk): + description = (description,) + write(sound, description) - gen_sine(freq, secs, volume) + +if __name__ == '__main__': + main()