245
|
1 # Generate 'perfect' sine wave sounds
|
|
2
|
|
3 # Design notes: produces ~= 0.5s raw CDDA audio - 44100 Hz, 16 bit signed values
|
|
4 # Input is freq in Hz - 440 for A, etc. - must be an integer
|
|
5 # Output is written the file beep<freq>.pcm
|
|
6 # Convert to ogg with oggenc -r <file>
|
|
7
|
|
8 import sys
|
|
9 import math
|
|
10 import struct
|
|
11
|
|
12
|
|
13 CDDA_RATE = 44100
|
|
14 MAX = 105*256 # Max value for sine wave
|
|
15
|
|
16 def gen_sine(freq):
|
|
17 filename = 'beep%s.pcm' % freq
|
|
18 # We need to generate freq cycles and sample that CDDA_RATE times
|
|
19 samples_per_cycle = CDDA_RATE / freq
|
|
20 data = []
|
|
21 for x in range(samples_per_cycle):
|
|
22 rad = float(x) / samples_per_cycle * 2 * math.pi
|
|
23 y = MAX * math.sin(rad)
|
|
24 data.append(struct.pack('<i', y))
|
|
25 output = open(filename, 'w')
|
|
26 for x in range(freq/2):
|
|
27 output.write(''.join(data))
|
|
28 output.close()
|
|
29
|
|
30
|
|
31 if __name__ == "__main__":
|
|
32 freq = int(sys.argv[1])
|
|
33 gen_sine(freq)
|