Mercurial > boomslang
comparison pyntnclick/tools/gen_sound.py @ 854:79b5c1be9a5e default tip
Remove pyntnclick, it's its own library, now
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sat, 21 Jun 2014 22:06:09 +0200 |
parents | f95830b58336 |
children |
comparison
equal
deleted
inserted
replaced
852:f95830b58336 | 854:79b5c1be9a5e |
---|---|
1 # Generate 'perfect' sine wave sounds | |
2 | |
3 # Design notes: produces ~= (use requested) s raw CDDA audio - 44100 Hz | |
4 # 16 bit signed values | |
5 # Input is freq in Hz - 440 for A, etc. - must be an integer | |
6 # Output is written the file beep<freq>.pcm | |
7 # Convert to ogg with oggenc -r <file> | |
8 | |
9 import sys | |
10 import math | |
11 import struct | |
12 | |
13 CDDA_RATE = 44100 | |
14 MAX = 105 * 256 # Max value for sine wave | |
15 | |
16 | |
17 def gen_sine(freq, secs): | |
18 filename = 'beep%s.pcm' % freq | |
19 # We need to generate freq cycles and sample that CDDA_RATE times | |
20 samples_per_cycle = CDDA_RATE / freq | |
21 data = [] | |
22 for x in range(samples_per_cycle): | |
23 rad = float(x) / samples_per_cycle * 2 * math.pi | |
24 y = MAX * math.sin(rad) | |
25 data.append(struct.pack('<i', y)) | |
26 output = open(filename, 'w') | |
27 for x in range(int(freq * secs)): | |
28 output.write(''.join(data)) | |
29 output.close() | |
30 return filename | |
31 | |
32 | |
33 def usage(): | |
34 print 'Unexpected input' | |
35 print 'Usage: gen_sound.py <freq> [<length>]' | |
36 print ' where <freq> is the frequency in Hz (integer)' | |
37 print ' and [<length>] is the time is seconds (float)' | |
38 | |
39 if __name__ == "__main__": | |
40 try: | |
41 freq = int(sys.argv[1]) | |
42 except Exception, exc: | |
43 usage() | |
44 print 'Error was: %s' % exc | |
45 sys.exit(1) | |
46 | |
47 if len(sys.argv) > 2: | |
48 try: | |
49 secs = float(sys.argv[2]) | |
50 except Exception, exc: | |
51 usage() | |
52 print 'Error was: %s' % exc | |
53 sys.exit(1) | |
54 else: | |
55 secs = 0.25 | |
56 output = gen_sine(freq, secs) | |
57 print 'Wrote sample to %s' % output |