Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63c547a

Browse files
committed
Audio
1 parent 00581d8 commit 63c547a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,28 +1968,28 @@ def read_wav_file(filename):
19681968

19691969
### Write Frames to WAV File
19701970
```python
1971-
def write_to_wav_file(filename, frames_int):
1971+
def write_to_wav_file(filename, frames_int, mono=True):
19721972
frames_short = (pack('<h', a) for a in frames_int)
19731973
with wave.open(filename, 'wb') as wf:
1974-
wf.setnchannels(1)
1974+
wf.setnchannels(1 if mono else 2)
19751975
wf.setsampwidth(2)
19761976
wf.setframerate(44100)
19771977
wf.writeframes(b''.join(frames_short))
19781978
```
19791979

19801980
### Examples
1981-
#### Saves a sine wave to a WAV file:
1981+
#### Saves a sine wave to a mono WAV file:
19821982
```python
19831983
from math import pi, sin
19841984
frames_f = (sin(i * 2 * pi * 440 / 44100) for i in range(100000))
19851985
frames_i = (int(a * 30000) for a in frames_f)
19861986
write_to_wav_file('test.wav', frames_i)
19871987
```
19881988

1989-
#### Adds noise to a WAV file:
1989+
#### Adds noise to a mono WAV file:
19901990
```python
19911991
from random import randint
1992-
add_noise = lambda value: max(-32768, min(32768, value + randint(-500, 500)))
1992+
add_noise = lambda value: max(-32768, min(32767, value + randint(-500, 500)))
19931993
frames_i = (add_noise(a) for a in read_wav_file('test.wav'))
19941994
write_to_wav_file('test.wav', frames_i)
19951995
```

0 commit comments

Comments
 (0)