I generaed a beep sound in Python, that exists for 5ms, and repeats after every 1s, for next 10s.
The codes are as such:
## Import modules
import time
import sys
import winsound
import soundfile as sf
## Set frequency and duration of beep sound
frequency = 37
duration = 5 # duration of beep sound is 5ms
for i in range(1,10): # create beep sound for 10s
## Create beep sound for 5 ms, after every 1 s
sys.stdout.write('\r\a{i}'.format(i=i))
sys.stdout.flush()
winsound.Beep(frequency, duration)
time.sleep(1) # Repeat beep sound after 1s
Now, I want to save this pattern in a .wav file. Hence, I changed the codes as such:
## Import modules
import time
import sys
import winsound
import soundfile as sf
## Set frequency and duration of beep sound
frequency = 37
duration = 5 # duration of beep sound is 5ms
for i in range(1,10): # create beep spund for 10 s
## Create beep sound for 5 ms, after every 1 s
sys.stdout.write('\r\a{i}'.format(i=i))
sys.stdout.flush()
winsound.Beep(frequency, duration)
time.sleep(1)
## Save the beep spund as a .wav file
sf.write("Beep.wav", winsound.Beep(frequency, duration))
However, I keep geeting errors.
Can somebady please let me know how do I save this in a .wav file ?
The error is this:
write() missing 1 required positional argument: 'samplerate'
from the docs here: https://pysoundfile.readthedocs.io/en/latest/
we can see that the function requires 3 arguments:
sf.write('new_file.flac', data, samplerate)
The code in the question has provided only two arguements.
The 3 arguements are:
It samplerate
is missing.
With the docs given, this works:
## Import modules
import time
import sys
import numpy as np
import winsound
import soundfile as sf
## Set frequency and duration of beep sound
frequency = 37
duration = 5 # duration of beep sound is 5ms
for i in range(1,10): # create beep spund for 10 s
## Create beep sound for 5 ms, after every 1 s
sys.stdout.write('\r\a{i}'.format(i=i))
sys.stdout.flush()
winsound.Beep(frequency, duration)
time.sleep(1)
## Save the beep spund as a .wav file
file = 'G:\\My Drive\\darren\\02_programming\python\\tests\\Beep.wav'
with sf.SoundFile(file, 'w', 44100, 2, 'PCM_24') as f:
f.write(np.random.randn(10, 2))