Mercurial > pyntnclick
comparison tools/gen_sound.py @ 249:913257638e6f
Add length parameter
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Fri, 27 Aug 2010 15:51:50 +0200 |
parents | f87a3485d927 |
children | 5b9f371c2bb8 |
comparison
equal
deleted
inserted
replaced
248:37f892b59c4b | 249:913257638e6f |
---|---|
1 # Generate 'perfect' sine wave sounds | 1 # Generate 'perfect' sine wave sounds |
2 | 2 |
3 # Design notes: produces ~= 0.5s raw CDDA audio - 44100 Hz, 16 bit signed values | 3 # Design notes: produces ~= (use requested) s raw CDDA audio - 44100 Hz, 16 bit signed values |
4 # Input is freq in Hz - 440 for A, etc. - must be an integer | 4 # Input is freq in Hz - 440 for A, etc. - must be an integer |
5 # Output is written the file beep<freq>.pcm | 5 # Output is written the file beep<freq>.pcm |
6 # Convert to ogg with oggenc -r <file> | 6 # Convert to ogg with oggenc -r <file> |
7 | 7 |
8 import sys | 8 import sys |
11 | 11 |
12 | 12 |
13 CDDA_RATE = 44100 | 13 CDDA_RATE = 44100 |
14 MAX = 105*256 # Max value for sine wave | 14 MAX = 105*256 # Max value for sine wave |
15 | 15 |
16 def gen_sine(freq): | 16 def gen_sine(freq, secs): |
17 filename = 'beep%s.pcm' % freq | 17 filename = 'beep%s.pcm' % freq |
18 # We need to generate freq cycles and sample that CDDA_RATE times | 18 # We need to generate freq cycles and sample that CDDA_RATE times |
19 samples_per_cycle = CDDA_RATE / freq | 19 samples_per_cycle = CDDA_RATE / freq |
20 data = [] | 20 data = [] |
21 for x in range(samples_per_cycle): | 21 for x in range(samples_per_cycle): |
22 rad = float(x) / samples_per_cycle * 2 * math.pi | 22 rad = float(x) / samples_per_cycle * 2 * math.pi |
23 y = MAX * math.sin(rad) | 23 y = MAX * math.sin(rad) |
24 data.append(struct.pack('<i', y)) | 24 data.append(struct.pack('<i', y)) |
25 output = open(filename, 'w') | 25 output = open(filename, 'w') |
26 for x in range(freq/2): | 26 for x in range(int(freq * secs)): |
27 output.write(''.join(data)) | 27 output.write(''.join(data)) |
28 output.close() | 28 output.close() |
29 | 29 |
30 | 30 |
31 if __name__ == "__main__": | 31 if __name__ == "__main__": |
32 freq = int(sys.argv[1]) | 32 freq = int(sys.argv[1]) |
33 gen_sine(freq) | 33 if len(sys.argv) > 2: |
34 secs = float(sys.argv[2]) | |
35 else: | |
36 secs = 0.25 | |
37 gen_sine(freq, secs) |