Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to record sound with sounddevice

I am unable to record audio using sounddevice in python. This code worked before on an older version of Mac OS.

The python3 code is as follows:

import sounddevice as sd
import numpy as np
fs = 48000
duration=5
rec = sd.rec(int(duration * fs), samplerate=fs, channels=1, blocking=True)
print(rec)

The output is

array([[0.],
       [0.],
       [0.],
       ...,
       [0.],
       [0.],
       [0.]], dtype=float32)

Although not all values are shown here, I have confirmed that they are all zero. When the sound records correctly, most of the these values are non-zero.

I have confirmed that the devices are set correctly:

sd.default.device # Output is [2, 3]

python3 -m sounddevice
  0 DisplayPort, Core Audio (0 in, 2 out)
  1 DisplayPort, Core Audio (0 in, 2 out)
> 2 MacBook Pro Microphone, Core Audio (1 in, 0 out)
< 3 MacBook Pro Speakers, Core Audio (0 in, 2 out)

Is this something to do with permissions in Mac OS? I'm using Mojave.

Thank you.

like image 450
Gerhard Avatar asked Oct 16 '25 13:10

Gerhard


1 Answers

I had a similar problem because vscode's terminal wasn't asking permission from the OS to use the microphone. Once I launched it on a regular terminal and accepted the request, it worked just fine in both terminals.

Hope I could help

like image 76
Julian Weber Avatar answered Oct 19 '25 02:10

Julian Weber