bastibe/SoundCard

loopback record audio from speaker return zero array

sparkstrike opened this issue · 5 comments

First of all, thanks so much for your work on it!
I tried to record audio from speaker, it return a array with correct shape, but all element is zero: [[0, 0], [0, 0], ...], and the data return immediately (not 5 second) . It is strange that it works well yesterday and record by pc's physical microphone (not loopback).

environment
PC: DELL G3 notebook computer; OS: win11; python3.7 and SoundCard v0.4.2

import soundcard as sc

OUTPUT_FILE_NAME = "out.wav"  # file name.
SAMPLE_RATE = 16000  # [Hz]. sampling rate.
RECORD_SEC = 5 #  [sec]. duration recording audio.

speaker = sc.default_speaker()  # it is my speaker
print(speaker)  # <Speaker 扬声器 (Realtek(R) Audio) (2 channels)>

micer = sc.get_microphone(id=str(speaker.name), include_loopback=True)  # loopback record

with micer.recorder(samplerate=SAMPLE_RATE) as mic:
    data = mic.record(numframes=SAMPLE_RATE * RECORD_SEC)
    print(data)  # data is a zero array, but data.shape is correct ???????


""" output
<Speaker 扬声器 (Realtek(R) Audio) (2 channels)>
[[0. 0.]
 [0. 0.]
 [0. 0.]
 ...
 [0. 0.]
 [0. 0.]
 [0. 0.]]
"""

The other strangenesss is that it return valid audio array when play a music, but return [0] array when a few seconds after the music stops playing. The gif show it. (The RECORD_SEC change to 2 second)

1

Could you explain in a bit more detail in which case it returns zeros of the right shape immediately? This is not supposed to happen.

I seem to have the same problem when using the microphone to record on Mac.

default_mic = sc.default_microphone()
samplerate = 48000
seconds = 20 
data = default_mic.record(samplerate=samplerate, numframes=samplerate*20)
print(data,len(data),max(data))
[<Microphone MacBook Pro Microphone (1 channels)>, <Microphone Microsoft Teams Audio (2 channels)>, <Microphone WebexMediaAudioDevice (2 channels)>]
0: MacBook Pro Microphone
1: Microsoft Teams Audio
2: WebexMediaAudioDevice
[[-0.]
 [-0.]
 [-0.]
 ...
 [-0.]
 [-0.]
 [-0.]] 960000 [-0.]

@wolfiex this does not seem related to loopback recording, so this is a different problem. Please open a new issue.

@bastibe
This issue may be the same as #166 , because I saw same output when I opened that issue.
Using the latest master branch may resolve this issue.

@sparkstrike
Could you try using the latest version of master branch instead of the released version?