bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
689 stars 70 forks source link

mic isn't recording audio #136

Closed fvviz closed 2 years ago

fvviz commented 3 years ago

This is the code (I've taken it from the docs)

import soundcard as sc
import numpy

# get a list of all speakers:
speakers = sc.all_speakers()
# get the current default speaker on your system:
default_speaker = sc.default_speaker()
# get a list of all microphones:
mics = sc.all_microphones()
# get the current default microphone on your system:
default_mic = sc.default_microphone()

data = default_mic.record(samplerate=48000, numframes=48000)
default_speaker.play(data/numpy.max(data), samplerate=48000)

when I run the code the sound does not play. At first I thought this was a problem with the speaker object but then I tried playing a saved wav file separately using the speaker object and it worked but when I try to play the mic input there is no sound output, also there is this warning

/Users/faizrahim/PycharmProjects/voice-lag-simul/writejson.py:15: RuntimeWarning: invalid value encountered in true_divide
  default_speaker.play(data/numpy.max(data), samplerate=48000)

The data variable when printed returns an array with nothing other than 0.0

[[0.]
 [0.]
 [0.]
 ...
 [0.]
 [0.]
 [0.]]
bastibe commented 3 years ago

What sort of mic are you using? Pure zeros usually mean that there is no microphone connected. What operating system are you running?

fvviz commented 3 years ago

I am running macOS Big Sur on the late 2020 M1 MacBook Pro. The mics I'm using are the default laptop mic and an AirPods mic. Both of these mics worked on PyAudio but won't work on Soundcard

bastibe commented 3 years ago

What does print(mic) say, exactly? Does it change if you supply a channel map?

fvviz commented 3 years ago

when I'm on my laptop mic print(default_mic) gives me <Microphone MacBook Pro Microphone (1 channels)> when I'm on my AirPods it gives me <Microphone Faizs AirPods (1 channels)>. And what is a channel map though?

bastibe commented 3 years ago

Thank you. That output looks normal. Honestly, I have no idea what is causing this issue. But macOS has silently changed its audio APIs multiple times in the past, so I wouldn't put it past them to have done so again.

Pure zeros are usually macOS's way of telling you that no microphone is connected. Apparently now it is also used to signal some kind of error condition. I'm afraid I can't help you beyond this, since I don't have a modern Mac to test on.

But the code is pretty straight-forward, so you might be able to debug it yourself. Start with the _Recorder object in coreaudio.py and use print-debugging as any runtime-debugger would mess up macOS's callback model. If you do look into this, I'll try to help you as best I can!

alfonsocv12 commented 2 years ago

Hi, Bastibe I have the same issue with a Scarlett 8I6 macOS Big Sur also I change the sample rate to 44100 because using logic to record defaults to that. Also when I hardcode 48khz into the Focusrite software Logic stops monitoring of input. I check on _Recorder object to see If I get somewhere.

alfonsocv12 commented 2 years ago

The problem is Microphone Permission. I just added Microphone permission to Iterm and the problem is solved

bastibe commented 2 years ago

Thank you for figuring that one out! So if I understand you correctly, this only affects users of a non-default Terminal app?

Would you like to contribute the exact steps necessary to the README?

alfonsocv12 commented 2 years ago

Yes the real problem is the most common places where people run their scripts on macos is from vscode (that has a well documented bug since macos Mojave) and iterm that also doesn't ask for permission.

I try to fix this at script level so that soundcard ask for it regardless of the top application in the meantime I add the workaround to the readme.

alfonsocv12 commented 2 years ago

Add issue to readme https://github.com/bastibe/SoundCard/pull/148

petri commented 2 years ago

given there's a note in readme now, perhaps this could be closed?

alfonsocv12 commented 2 years ago

I'm ok with closing this