@@ -1968,28 +1968,28 @@ def read_wav_file(filename):
1968
1968
1969
1969
### Write Frames to WAV File
1970
1970
``` python
1971
- def write_to_wav_file (filename , frames_int ):
1971
+ def write_to_wav_file (filename , frames_int , mono = True ):
1972
1972
frames_short = (pack(' <h' , a) for a in frames_int)
1973
1973
with wave.open(filename, ' wb' ) as wf:
1974
- wf.setnchannels(1 )
1974
+ wf.setnchannels(1 if mono else 2 )
1975
1975
wf.setsampwidth(2 )
1976
1976
wf.setframerate(44100 )
1977
1977
wf.writeframes(b ' ' .join(frames_short))
1978
1978
```
1979
1979
1980
1980
### Examples
1981
- #### Saves a sine wave to a WAV file:
1981
+ #### Saves a sine wave to a mono WAV file:
1982
1982
``` python
1983
1983
from math import pi, sin
1984
1984
frames_f = (sin(i * 2 * pi * 440 / 44100 ) for i in range (100000 ))
1985
1985
frames_i = (int (a * 30000 ) for a in frames_f)
1986
1986
write_to_wav_file(' test.wav' , frames_i)
1987
1987
```
1988
1988
1989
- #### Adds noise to a WAV file:
1989
+ #### Adds noise to a mono WAV file:
1990
1990
``` python
1991
1991
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 )))
1993
1993
frames_i = (add_noise(a) for a in read_wav_file(' test.wav' ))
1994
1994
write_to_wav_file(' test.wav' , frames_i)
1995
1995
```
0 commit comments