0

I am trying the Python Sounddevice lib to stream audio from the microphone

self.audio_streamer = sd.Stream(device=self.input_device, channels=self.channels,
                                        samplerate=self.sampling_rate, dtype='int16',
                                        callback=self.update_audio_feed, blocksize=self.audio_block_size,
                                        latency='low')```



def update_audio_feed(self, indata, outdata, frames, time, status):
        print("update_audio_feed")
        if status:
            print(status, file=sys.stderr)

        print(indata)
        outdata.fill(0)


Output :

The indata is an array with 0's always from the callback.
update_audio_feed
[[0]
 [0]
 [0]
 ...
 [0]
 [0]
 [0]]

Sounddevice is detectingt the mic fine but not getting the signal :
Device Info: {'name': 'MacBook Pro Microphone', 'hostapi': 0, 'max_input_channels': 1, 'max_output_channels': 0, 'default_low_input_latency': 0.04852607709750567, 'default_low_output_latency': 0.01, 'default_high_input_latency': 0.05868480725623583, 'default_high_output_latency': 0.1, 'default_samplerate': 44100.0}
Sampling rate: 44100.0
vinzzz
  • 2,084
  • 2
  • 14
  • 23

3 Answers3

1

The issue on my mac was a security/ permissions issue . When I tried running the python script through Visual Studio Console it did not work... But when I tried the mac Terminal it prompted for the microphone and everything started to work..

More details here : https://www.reddit.com/r/MacOS/comments/9lwyz0/mojave_not_privacy_settings_blocking_all_mic/

https://github.com/spatialaudio/python-sounddevice/issues/267

vinzzz
  • 2,084
  • 2
  • 14
  • 23
0

I've been using sounddevice without major issues on a number of Macs for a few months now.

Firstly, have you tried the wire.py example? That works out of the box for me.

Two things that I noticed in your code:

  • I haven't tried specifying the blocksize. I have only used the default value of 0. I could well believe that may be causing you issues.
  • You've specified a "low" latency Stream. At the very least on OSX10.13 this produces very unstable audio (lots of input underflows). If stable audio is important to you, I would recommend you consider latency options higher than "high". For reference, Audacity uses 100ms and obtains stable audio. Also, input underflows often mean indata is filled with zeros.

For those interested in this problem in the future, you may wish to look at the issue posted on sounddevice at GitHub.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Matthew Walker
  • 2,527
  • 3
  • 24
  • 30
0

I had the same issue on MacOS, but I was running the script from vscode. Actually vscode wouldn't ask for microphone permission, so it will assume it has this permission (which it didn't) and you will get an empty array.

Switched to running the script from terminal and everything changed, I got a permission request and everything went well.

Catalin Pirvu
  • 175
  • 2
  • 8