Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

python-sounddevice (Part 2)

Pygator on March 1, 2020

channels=2 means what exactly?

Joe Tatusko RP Team on March 2, 2020

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.

Joe Tatusko RP Team on March 26, 2020

Hi TJ,

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)

Hi, Joe

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.

achk on Aug. 19, 2020

myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2) AttributeError: ‘module’ object has no attribute ‘rec’

achk on Aug. 19, 2020

sounddevice.PortAudioError: Error opening InputStream: Invalid number of channels [PaErrorCode -9998]

Become a Member to join the conversation.