Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distorted audio capture on linux. #500

Open
dezi opened this issue May 7, 2023 · 5 comments
Open

Distorted audio capture on linux. #500

dezi opened this issue May 7, 2023 · 5 comments

Comments

@dezi
Copy link

dezi commented May 7, 2023

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
Copy link

schrockwell commented Nov 3, 2023

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)

@kadharsh
Copy link

kadharsh commented Nov 24, 2023

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
Copy link

Lien03 commented Dec 22, 2023

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

  • linux-imx 4.14.98
  • USB Mic
  • PulseAudio

thx

@kenzoi
Copy link

kenzoi commented Feb 22, 2024

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
Copy link
Author

dezi commented Apr 3, 2024

schrockwell: I have also experienced the

[ALSA] poll() failed.

Bug.

You will find my solution here:

mackron/miniaudio#836

Regards, dezi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants