2

I am using sounddevice, scipy and playsound in my project. I have a file named record.py whose source is given below :

import sounddevice as sd
from scipy.io.wavfile import write
from playsound import playsound

fs = 44100  # this is the frequency sampling; also: 4999, 64000
seconds = 5  # Duration of recording

myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
print("Starting: Speak now!")
sd.wait()  # Wait until recording is finished
print("finished")
write('output.wav', fs, myrecording)  # Save as WAV file
playsound("output.wav")

The above given code works fine, but I have a problem. You may have noticed the seconds variable in the source, whose value is set to 5. Therefore, after 5sec the recording stops automatically and saves itself as output.wav and then gets played. Is there any way such that I didn't require to define the duration, that means the user can record the audio as long as she/he wants. Also by pressing the Shift key she/he can pause as well as resume the recording, and finally stop it by pressing Enter, after which the file automatically saves and gets played. So can this be done ?

Machavity
  • 30,841
  • 27
  • 92
  • 100
MOD ER HACKS
  • 197
  • 1
  • 9
  • 2
    Sure, this can be done. As a starting point, you can have a look at the [rec_unlimited.py](https://github.com/spatialaudio/python-sounddevice/blob/master/examples/rec_unlimited.py) example. – Matthias Jan 08 '20 at 08:38

0 Answers0