pion / mediadevices

Go implementation of the MediaDevices API.
https://pion.ly/
MIT License
521 stars 120 forks source link

Distorted audio capture on linux. #500

Open dezi opened 1 year ago

dezi commented 1 year ago

The problem is in pkg/driver/microphone/microphone.go lines 133...

onRecvChunk := func(_, chunk []byte, framecount uint32) {
    select {
    case <-cancelCtx.Done():
    case m.chunkChan <- chunk:
    }
}

Callback function of malgo audio capture. The chunk buffer is send to channel. Malgo will reuse the buffer while it is beeing processed.

You have to make a copy of chunk before sending it to the channel.

onRecvChunk := func(_, chunk []byte, framecount uint32) {
    newChunk := make([]byte, len(chunk))
    copy(newChunk, chunk)
    select {
    case <-cancelCtx.Done():
    case m.chunkChan <- newChunk:
    }
}

Please fix. Regards, dezi

schrockwell commented 8 months ago

I have the same problem, and I can confirm that this does partially fix the microphone driver. Where there was once a lot of distortion, there is now none! But unfortunately, the audio completely stops after a few seconds.

The audiotest driver works great.

EDIT: I have tracked down the issue with audio stopping to something involving miniaudio or ALSA. I added a LogProc to the device, and found that the Stop callback was being called after the log statements:

[ALSA] poll() failed.
[ALSA] Dropping capture device...
[ALSA] Dropping capture device successful.
[ALSA] Preparing capture device...
[ALSA] Preparing capture device successful.

I could not get any more information on why poll() failed, except that this error is being raised from miniaudio.

I installed PulseAudio and ran the pulseaudio daemon so that miniaudio would pick that backend instead, and now it's working without any problems! (after the patch was applied above)

kadharsh commented 7 months ago

I faced the same problem of severely distorted audio, and this solution worked for me. Initially, ALSA wasn't providing any audio output; although I could record audio using arecord, it wasn't being sent to the other side. After installing PulseAudio, the audio started working, but the quality remained heavily distorted. This fix by @dezi effectively resolved the distortion issue.

My hardware was a Rpi 4 running raspian os bookworm and a USB audio card

Lien03 commented 6 months ago

I am also experiencing issues with distorted audio, this worked for me.

thx

kenzoi commented 4 months ago

I had the same problem when using my bluetooth earbud some months ago, copy the buffer worked at that time. (with a usb headphone it was working fine without any patch in the same pc).

But now it's working fine for me without any patch, I don't know if it was an update on my Fedora or on pion/mediadevices.

dezi commented 3 months ago

schrockwell: I have also experienced the

[ALSA] poll() failed.

Bug.

You will find my solution here:

https://github.com/mackron/miniaudio/issues/836

Regards, dezi