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

Crackly audio on some mp3 files (suspected bug in resampler) #584

Open
Chazoshtare opened this issue Jun 7, 2024 · 10 comments
Open

Crackly audio on some mp3 files (suspected bug in resampler) #584

Chazoshtare opened this issue Jun 7, 2024 · 10 comments

Comments

@Chazoshtare
Copy link

I'm writing a project that converts text to speech using Google TTS. The mp3 files it returns have some audible crackling noise in them while played with rodio. It's not an issue with the file itself, as it plays normally everywhere else, such as in music players and the like.
I tried:

  • playing it through stream_handle or a sink
  • decoding it with different decoders
  • playing it through different audio interfaces and outputs
  • all that on Linux and Windows

In all cases it was the same, so I'm rather convinced it's a problem on rodio's side. I'm attaching a simple project with a sample sound that demonstrates the issue.
rodio-crackling-issue.tar.gz

@dvdsk
Copy link
Collaborator

dvdsk commented Jun 8, 2024

First off all thanks for the demonstrator project, that saves me a some time :)

Now the bad news, I can not reproduce it locally on my machine, which makes things a bit more difficult.

Lets start by checking if there could be an issue in cpal. Could you try the beep example from cpal?

If that works without issues ill see if I can find some more systems to try the demonstrator project on and reproduce the crackling.

@Chazoshtare
Copy link
Author

The beep example you provided produces a clean beep. Generally, rodio plays most of the sounds I have without any problems, I only observe the issue with mp3 files returned by google TTS.
Just in case so you could hear what it's like on my side, I recorded how it sounds using a loopback from my interface.
crackles.zip

@dvdsk
Copy link
Collaborator

dvdsk commented Jun 10, 2024

The beep example you provided produces a clean beep. Generally, rodio plays most of the sounds I have without any problems, I only observe the issue with mp3 files returned by google TTS. Just in case so you could hear what it's like on my side, I recorded how it sounds using a loopback from my interface. crackles.zip

That is pretty interesting (useful?) actually since... I do not hear the crackling in that recording either...

Ill be honest I have no idea at this point. And without a way to replicate it on my side I can not really try things out. If you need to get this working fast your best bet is probably to try kira (a different rust audio lib) or build something on the symphonia play example.

On the other hand I am open to trying things on your side until we get a better idea (if we ever do, this one is really confusing me).

Have you tried lowering your volume? Maybe something in your system is amplifying things too much (digital amplification can lead to crackling).

If you recode the mp3 file as wav with something like audacity then play it back with rodio, do you still get the crackling? (to find out if it is mp3 specific).

Does the crackling occur using the symphonia play example liked above (its a little cli you can use to play audio)?

@Chazoshtare
Copy link
Author

The crackles are definitely there in that recording, you can even see them on a spectrogram.
Top is the example sound with crackles, how it sounds while played with rodio, bottom is original, how it should sound.
spectrogram

As a sanity check, I also asked multiple people with different audio setups to confirm they can hear them. Are you sure you're playing the example loud enough for them to be audible? Or, since their sounds are in high frequency range, that your audio setup plays them at all?
I'm 100% sure it's not relevant to volume. I'll try recoding the sound to different formats and post more information.

@dvdsk
Copy link
Collaborator

dvdsk commented Jun 10, 2024

The crackles are definitely there in that recording, you can even see them on a spectrogram. Top is the example sound with crackles, how it sounds while played with rodio, bottom is original, how it should sound.

spectrogram, good idea! That I can reproduce :)

Or, since their sounds are in high frequency range, that your audio setup plays them at all?

lets hope there is a low pass filter in there, or I'm going deaf a bit early 😅

I'll try recoding the sound to different formats and post more information.

That should answer if its the mp3 decoder or more fundamental issue in how rodio resamples audio

@Chazoshtare
Copy link
Author

Just checked, playing the sound with symphonia-play produces a clean sound, no issues there. I recoded the original file to different formats and sample rates, I'm attaching a few files you can try. The results on my setup were:

  • [original] mono, mp3, 24000hz - crackles
  • mono, ogg, 24000hz - crackles, even worse actually
  • mono, wav, 24000hz - no issues, plays perfectly
  • mono, mp3, 44100hz - no issues, plays perfectly
  • mono, ogg, 44100hz - no issues, plays perfectly

My first guess is that the SamplesConverter from convert_samples() might be the source of the issue here. The default output opened by the sample project I provided is a stereo/44100hz output in my case, so possibly the noise is introduced while
converting the samples to that format. That doesn't really explain why the wav version plays perfectly in both cases, but I have no idea for that yet.
crackling-examples.tar.gz

@dvdsk dvdsk added the bug label Jun 17, 2024
@dvdsk dvdsk changed the title Crackly audio on some mp3 files Crackly audio on some mp3 files (suspected bug in resampler) Jun 17, 2024
@dvdsk
Copy link
Collaborator

dvdsk commented Jun 17, 2024

Unfortunately I'm not familiar with that part of the code base and this will probably be a subtle mistake in there somewhere. I'll be honest I do not really have the time to work on finding it.

Thanks for narrowing it down, I had hoped it would be a simple issue, unfortunately not.
Now lets hope someone has time to dive into this!

@dvdsk
Copy link
Collaborator

dvdsk commented Jun 23, 2024

might be worth it just swapping rodio's resampler out for: https://github.com/HEnquist/rubato. Its MIT licensed and they seem to have spend a lot of time on optimizing. Could also solve this issue or allow is to send it upstream to them :)

@dvdsk
Copy link
Collaborator

dvdsk commented Nov 18, 2024

So the interpolater/resampler in rodio is fine however it prioritizes speed/efficiency over audio quality. Thats mostly okay but for some resampling rates that leads to crackly audio. We need to offer a slower more hifi resampler and make that the default.

I mentioned rebato above, Dasp also features a nice hifi resampler: https://docs.rs/dasp/latest/dasp/interpolate/sinc/struct.Sinc.html

@PetrGlad
Copy link
Contributor

PetrGlad commented Nov 29, 2024

Unfortunately I could not hear the crackles myself also but that may be caused by my hearing being worn out. However I do see some spurious vertical lines on the spectrogram. Maybe actual conversion parameters can be send to tracing to help with the debugging.

As a temporary fix I'd try to use some low pass filter after up-sampling and see if it helps. I am not sure if the output quality will be sufficient then. But if I found it correctly, in the example above the rate conversion is from 2400 to 44100 so low-pass will not discard any actual details there.

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

No branches or pull requests

3 participants