python-sounddevice (Part 2)
python-sounddevice is going to record audio from your microphone and store it as a NumPy array. If you’d like to convert a NumPy array to a WAV file, you’ll want to use a module from
SciPy to do so. Let’s go ahead and install
SciPy to get started.
Going to the text editor, you’re going to
import sounddevice as sd, and then
from scipy.io.wavfile, and you’ll just want to
import write. So before, when we were playing audio,
python-sounddevice obtained a lot of information from the WAV file itself. Since you’re going to be creating a WAV file, you’ll need to set a sample rate, so using
fs set the sample rate to
44100, and then a duration, so
seconds, and let’s go
3. Now it’s time to create the recording object, so just go ahead and say something like
myrecording and equal this to
sd.rec() (record), and pass in an integer of
seconds times your sample rate.
01:13 This will give you how many samples are going to be captured.
And then for
samplerate, just pass in that value again, and
channels=2. All right, let me hide this EXPLORER so we can see a little bit better.
You’ll want to wait until the recording is complete before you write anything, so throw in
sd.wait(), and then go ahead and
write(), and we’ll just call this
'output.wav'. And you’ll want to pass it in your sample rate, and then that actual recording object.
All right, let’s test this out. So down in the terminal, let’s run
python_sounds. Okay. On my other screen, I got a notice to get access to the microphone, so I’m just going to say OK.
And we’ll see if this records! Okay, it seemed like it was about three seconds, so let’s take a look at the EXPLORER again. And there’s
Let’s use one of the libraries from before and see if this worked. I’m just going to comment out everything there, and
from playsound import playsound,
and then just grab
Let’s run that again and see if it worked. “And we’ll see if this records.” Awesome! So that went to the microphone on my laptop, but you can see that we were able to record something using
Pretty cool! So,
python-sounddevice is a good high-level recording option to use. In the next video, we’re going to take a look at
pyaudio, which you may remember was a little bit more complex, but a very powerful library for dealing with audio in Python.
Stereo! Two channels will get you one for a left and right speaker, while one channel produces a mono output.
Hi, I tried this code on my mac,
fs = 44100 seconds = 3 my_recording = sd.rec(int(seconds * fs), samplerate=fs, channels=2) sd.wait() write('output.wav', fs, my_recording) playsound('output.wav')
but it gives me
raise PortAudioError(errormsg, err) sounddevice.PortAudioError: Error opening InputStream: Invalid number of channels [PaErrorCode -9998]
Then I tried to change channels to 1, it works, but it didn’t record my sound. You know how to fix this issue? Thanks.
Have you been able to record sound with any of the other scripts in this course yet? MacOS has become a lot more strict with microphone permissions recently and it may be playing a role. Try running it with two channels but from Terminal and see if that helps you out (I have to do with with some programs to prompt the “Allow ______ to use the microphone?” request)
I tried to run the sounddevice in my regular terminal, and it still didn’t work. I also tried to install pyaudio by pipenv, but it gives me the new error message.
Installing dependencies from Pipfile.lock (611ef3)… An error occurred while installing pyaudio==0.2.11 --hash=sha256:0d92f6a294565260a282f7c9a0b0d309fc8cc988b5ee5b50645634ab9e2da7f7 --hash=sha256:259bb9c1363be895b4f9a97e320a6017dd06bc540728c1a04eb4a7b6fe75035b --hash=sha256:2a19bdb8ec1445b4f3e4b7b109e0e4cec1fd1f1ce588592aeb6db0b58d4fb3b0 --hash=sha256:51b558d1b28c68437b53218279110db44f69f3f5dd3d81859f569a4a96962bdc --hash=sha256:589bfad2c615dd4b5d3757e763019c42ab82f06fba5cae64ec02fd7f5ae407ed --hash=sha256:8f89075b4844ea94dde0c951c2937581c989fabd4df09bfd3f075035f50955df --hash=sha256:93bfde30e0b64e63a46f2fd77e85c41fd51182a4a3413d9edfaf9ffaa26efb74 --hash=sha256:cf1543ba50bd44ac0d0ab5c035bb9c3127eb76047ff12235149d9adf86f532b6 --hash=sha256:f78d543a98b730e64621ebf7f3e2868a79ade0a373882ef51c0293455ffa8e6e! Will try again.
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2) AttributeError: ‘module’ object has no attribute ‘rec’
sounddevice.PortAudioError: Error opening InputStream: Invalid number of channels [PaErrorCode -9998]
Become a Member to join the conversation.
Pygator on March 1, 2020
channels=2 means what exactly?