SoundCard icon indicating copy to clipboard operation
SoundCard copied to clipboard

Unable to record audio on MacOS

Open TnTora opened this issue 10 months ago • 9 comments

macOS 15.2 (x86)

I'm trying to run the following script to test this module but it seems I am failing to get proper audio.

import soundcard as sc
import soundfile as sf

default_mic = sc.default_microphone()

data = default_mic.record(samplerate=48000, numframes=48000*5)
print(data)
sf.write(file="out.wav", data=data, samplerate=48000)

Here is the result of print(data)

[[ 0.00875521  0.00859588]
 [ 0.01109303  0.01090191]
 [ 0.01041745  0.01023482]
 ...
 [-0.00246532 -0.00247982]
 [-0.00246533 -0.00247982]
 [-0.00246533 -0.00247983]]

Listening to the output file I hear a few frames of proper audio followed by a short high pitched noise and then silence. I tried using the internal mic, an external mic and a virtual one (blackhole) but got the same result with all of them.

Something I noticed while testing recording system audio using blackhole as a mic is that if audio is already playing when I run the script I get something like the array above. If I start the script and then immediately play audio I get

[[-0. -0.]
 [-0. -0.]
 [-0. -0.]
 ...
 [-0. -0.]
 [-0. -0.]
 [-0. -0.]]

I tried recording the same system audio as above using the library sounddevice and got proper audio, this is the numpy array

[[ 2.1381495e-03  1.9403318e-03]
 [ 3.4074080e-03  3.1864089e-03]
 [ 4.5137000e-03  4.3309033e-03]
 ...
 [ 1.0739344e-05  2.2570629e-04]
 [ 2.0904181e-04  4.3157060e-04]
 [-2.0285620e-04  3.7671925e-05]]

Am I missing something?

TnTora avatar Feb 23 '25 03:02 TnTora