mediadevices icon indicating copy to clipboard operation
mediadevices copied to clipboard

Distorted audio capture on linux.

Open dezi opened this issue 2 years ago • 5 comments

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

dezi avatar May 07 '23 22:05 dezi

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.

  • Hardware: Raspberry Pi 4
  • OS: Ubuntu 20.04 Server
  • Audio device: USB AUDIO CODEC

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)

schrockwell avatar Nov 03 '23 20:11 schrockwell

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

kadharsh avatar Nov 24 '23 06:11 kadharsh

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

  • linux-imx 4.14.98
  • USB Mic
  • PulseAudio

thx

Lien03 avatar Dec 22 '23 10:12 Lien03

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.

kenzoi avatar Feb 22 '24 19:02 kenzoi

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

dezi avatar Apr 03 '24 05:04 dezi