3

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Gerhard
  • 1,925
  • 3
  • 15
  • 24
  • Your code looks fine. Do you get sound from the mic in other applications? – Matthias Dec 12 '18 at 16:55
  • I have the same or very similar issue. My configuration is Thinkpad L520, Ubuntu. The only way how can I read from sound card is to disable internal microphone in BIOS. This bug is not related directly to sounddevice. I can't record anything with arecord utility nor with pyaudio. – rooobertek Apr 13 '19 at 18:25

2 Answers2

4

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

0

try this code

import sounddevice as sd
from scipy.io.wavfile import write

fs=44100
duration=5
print("recording...............")


record_voice=sd.rec(int(duration * fs),samplerate=fs,channels=2)
sd.wait()       
write("sound.wav",fs,record_voice)

arvind8
  • 92
  • 1
  • 11