Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Create a Screen recorder using Python
Next article icon

Python can be used to perform a variety of tasks. One of them is creating a voice recorder. We can use python'ssounddevice module to record and play audio. This module along with thewavioor thescipymodule provides a way to save recorded audio.

Installation:

sounddevice:This module provides functions to play and record NumPy arrays containing audio signals. Let'sinstall it by running the following command:

pip install sounddevice.

We can use eitherwavioandscipyto save the recorded audio in file format.We will see both of them here.

To installwavio:

pip install wavio

To installscipy:

pip install scipy

Now, we are done with installing the required modules. So, let's write the code.

Getting Started

First, import the required libraries.

# import required libraries
import sounddevice as sd
from scipy.io.wavfile import write
import wavio as wv

Now, before starting the recorder, we have to declare a few variables. The first one is the sampling frequency of the audio (in most cases this will be 44100 or 48000 frames per second) and the second is recording duration. We have to specify the duration in seconds so that it stops recording after that duration.

So, let's declare them too.

# Sampling frequency
freq = 44100

# Recording duration
duration = 5

Now, we are ready to start the recorder. It will create a NumPy array of the recorded audio.

Channels - The position of each audio source within the audio signal is known a channel.

Here number of channels can be 1 or 2 only.

# Start recorder with the given values of
# duration and sample frequency
recording = sd.rec(int(duration * freq),
samplerate=freq, channels=2)

# Record audio for the given number of seconds
sd.wait()

Now, we are done with recording the audio. So, let's save it. To save the audio file, we can either use thescipymodule or thewaviomodule. Let's go through them one by one.

Using Scipy:

We will use the write function from scipy.io.wavfile to convert the NumPy array to an audio file.

# This will convert the NumPy array to an audio
# file with the given sampling frequency
write("recording0.wav", freq, recording)

Using wavio:

We can also use the write function from thewaviolibrary.

# Convert the NumPy array to audio file
wv.write("recording1.wav", recording, freq, sampwidth=2)

Complete Code:

Python
# import required librariesimportsounddeviceassdfromscipy.io.wavfileimportwriteimportwavioaswv# Sampling frequencyfreq=44100# Recording durationduration=5# Start recorder with the given values# of duration and sample frequencyrecording=sd.rec(int(duration*freq),samplerate=freq,channels=2)# Record audio for the given number of secondssd.wait()# This will convert the NumPy array to an audio# file with the given sampling frequencywrite("recording0.wav",freq,recording)# Convert the NumPy array to audio filewv.write("recording1.wav",recording,freq,sampwidth=2)

Create a Voice Recorder using Python
Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp