Voice recorder in python step by step with example
Voice recorder in python step by step with example
Here is a step-by-step guide on how to create a simple voice recorder using Python:
Install the sounddevice and numpy libraries by running pip install sounddevice numpy in your command line.
Create a new Python script and import the sounddevice and numpy libraries by adding the following lines at the top of your script:
Copy code
import sounddevice as sd
import numpy as np
Set the recording parameters, such as the sample rate, number of channels, and the length of the recording. For example, you can set the sample rate to 44100 Hz and the number of channels to 1 (mono) for a 10-second recording:
Copy code
fs = 44100 # Sample rate
channels = 1 # Number of channels
duration = 10 # Recording duration in seconds
Use the sounddevice.rec() function to start the recording. The function takes a few parameters, such as the number of samples to record and the sample rate, and returns the recorded data as a NumPy array. The following code snippet shows how to record for the specified duration:
Copy code
recording = sd.rec(int(fs * duration), samplerate=fs, channels=channels)
Use the sounddevice.wait() function to wait until the recording is finished.
Copy code
sd.wait()
Save the recorded data to a file using the numpy.save() function. You can save the data in a file format such as WAV. Here's an example of how to do this:
Copy code
np.save("recording.npy", recording) # Save the recorded data to a .npy file
You can also use the scipy.io.wavfile.write() function to write the recording data to a .wav file.
Copy code
from scipy.io.wavfile import write
write("recording.wav", fs, recording)
#To play the recorded sound
Copy code
sd.play(recording)
This is just an example of how you can create a simple voice recorder using Python. The code can be adjusted and expanded upon to include additional functionality such as the ability to pause and resume recording, or the ability to record in different file formats.
Comments
Post a Comment