-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('<i', y)] * 4)
- output = open(filename, 'w')
- # This is correct because OUTPUT_RATE = CDDA rate / 4 and we repeat
- # the samples 4 times, so this works out to CDDA rate
- for x in range(int(freq * secs)):
- output.write(''.join(data))
- output.close()
- print 'Wrote output to %s' % filename
-
-
-def usage():
- print 'Unexpected input'
- print 'Usage gen_sound <freq> [<length>]'
- print ' where <freq> is the frequency in Hz (int)'
- print ' and [<length>] 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)
-
+
+
+def gen_raw(description):
+ for chunk in description:
+ for blob in chunk.raw():
+ yield blob
+
+
+def encode(filename, data):
+ print "Writing %s" % filename
+ p = Popen(('oggenc', '-o', filename, '--raw', '--raw-chan=1',
+ '--quiet', '-'),
+ stdin=PIPE, cwd='data/sounds')
+ for blob in data:
+ p.stdin.write(blob)
+ p.stdin.close()
+ assert p.wait() == 0
+
+
+def write(basename, description):
+ return encode('%s.ogg' % basename, gen_raw(description))
+
+
+def main():
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
+ from data.sounds import SOUNDS
+ from naja.gen_sound import Chunk
+
+ 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)
+
+
+if __name__ == '__main__':
+ main()