Mercurial > boomslang
annotate tools/gen_sound.py @ 546:ad4d6ffd25d7
update tags
author | convert-repo |
---|---|
date | Sat, 21 Jun 2014 17:38:39 +0000 |
parents | 5b9f371c2bb8 |
children | 15713dfe555d |
rev | line source |
---|---|
245 | 1 # Generate 'perfect' sine wave sounds |
2 | |
537
5b9f371c2bb8
PEP-8 cleanup of tools
Stefano Rivera <stefano@rivera.za.net>
parents:
249
diff
changeset
|
3 # Design notes: produces ~= (use requested) s raw CDDA audio - 44100 Hz |
5b9f371c2bb8
PEP-8 cleanup of tools
Stefano Rivera <stefano@rivera.za.net>
parents:
249
diff
changeset
|
4 # 16 bit signed values |
245 | 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 | |
537
5b9f371c2bb8
PEP-8 cleanup of tools
Stefano Rivera <stefano@rivera.za.net>
parents:
249
diff
changeset
|
13 CDDA_RATE = 44100 |
5b9f371c2bb8
PEP-8 cleanup of tools
Stefano Rivera <stefano@rivera.za.net>
parents:
249
diff
changeset
|
14 MAX = 105 * 256 # Max value for sine wave |
245 | 15 |
16 | |
249 | 17 def gen_sine(freq, secs): |
245 | 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') | |
249 | 27 for x in range(int(freq * secs)): |
245 | 28 output.write(''.join(data)) |
29 output.close() | |
30 | |
31 | |
32 if __name__ == "__main__": | |
33 freq = int(sys.argv[1]) | |
249 | 34 if len(sys.argv) > 2: |
35 secs = float(sys.argv[2]) | |
36 else: | |
37 secs = 0.25 | |
38 gen_sine(freq, secs) |