-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsine.py
More file actions
62 lines (41 loc) · 1.4 KB
/
Copy pathsine.py
File metadata and controls
62 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""A test of generating a WAV file with Python."""
import sys
import wave
import math
import random
import struct
# yay stereo sound
NUM_CHANNELS = 2
# number of bytes in a single audio sample
SAMPLE_WIDTH = 2
# Looks like ye olde 44.1 kHz sampling rate?
FRAMERATE = 44100
# What's the highest possible sample value?
BITS_IN_SAMPLE = SAMPLE_WIDTH * 8 - 1
MAX_SAMPLE_VALUE = 2 ** BITS_IN_SAMPLE - 1
help_msg = '''Usage: %s <output.wav> <length> <frequency> <amplitude>
length - length of WAV file in seconds.
frequency - pitch of sine wave in hertz (integer).
amplitude - volume of sine wave as a float - 0 is silent, 1 is max.''' % sys.argv[0]
def main():
if len(sys.argv) < 5:
print()
exit(2)
num_seconds = int(sys.argv[2])
f = wave.open(sys.argv[1], 'w')
f.setnchannels(NUM_CHANNELS)
f.setsampwidth(SAMPLE_WIDTH)
f.setframerate(FRAMERATE)
frequency = int(sys.argv[3])
amplitude = math.ceil(float(sys.argv[4]) * MAX_SAMPLE_VALUE)
amplitude = int(amplitude)
for i in range(0, num_seconds * 44100):
time_in_secs = i / 44100.0
value = amplitude * math.sin(2 * math.pi * frequency * time_in_secs)
packed_value = struct.pack('h', int(value))
# Outputting the same value once for each audio channel, I think?
f.writeframes(packed_value)
f.writeframes(packed_value)
f.close()
if __name__ == '__main__':
main()