Skip to content

Commit 3be0911

Browse files
committed
add a wav file writer example
1 parent 2107702 commit 3be0911

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

wav_writer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import random
2+
import struct
3+
import wave
4+
5+
6+
sample_rate = 44100.0 # hertz
7+
duration = 1.0 # seconds
8+
frequency = 440.0 # hertz
9+
10+
wav = wave.open("sample.wav", "w")
11+
wav.setnchannels(1) # mono
12+
wav.setsampwidth(2)
13+
wav.setframerate(sample_rate)
14+
15+
# Create a wav file _ hissy noise
16+
for i in range(2000):
17+
value = random.randint(-32767, 32767)
18+
data = struct.pack('<h', value)
19+
wav.writeframesraw(data)
20+
21+
wav.close()

0 commit comments

Comments
 (0)