linux icon indicating copy to clipboard operation
linux copied to clipboard

Alsa over HDMI goes async or gives crackling

Open kosirdavid opened this issue 13 years ago • 44 comments

I've posted on forum about this bug: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=48769

After I talked to some gstreamer guys, it seams that this bug is related to alsa drives for Raspberry Pi.

Long story in short, changing "buffer-time" property of gstreamer alsasink makes sound late (if buffer-time is high) or makes crackling noise (if buffer-time is lower).

On other platforms, there is no need to change "buffer-time" property and there are no issues with gstreamer alsasink (this bug can be reproduced only with Raspberry).

Is there any chance someone can reproduce this, give an explanation or even fix?

kosirdavid avatar Jul 02 '13 01:07 kosirdavid

Can you try this build: https://dl.dropboxusercontent.com/u/3669512/temp/start.elf

Note: this is test build with a fixed 256M/256M gpu/arm memory split.

It should drop queued audio data when requested.

I don't know if I agree that there shouldn't be any audio played while data is being submitted. As the alsa driver is handled by a separate processor with queued messages, then it takes some time for those messages to arrive during which time we are suposed to be outputting audio. Just because a different alsa card with a local dma buffer is able to fill and halt its buffer almost instantly doesn't mean that's the correct behaviour.

I would agree that no samples should be output after the final:

snd_pcm_drop(playback_handle);

but I'm not sure if they were before my update.

popcornmix avatar Nov 23 '13 18:11 popcornmix

I've tested new start.elf and it is good in 95% of time.

In other 5% I hear strange noise, like crackling. I've recorded it with phone, you can get it here http://goo.gl/j6AlhC

kosirdavid avatar Nov 25 '13 11:11 kosirdavid

@10robinho is the recorded noise something new? is the updated start.elf preferable to default one? What can I do to reproduce the noise you heard? hdmi or analogue output? Do you hear noise from aplay?

popcornmix avatar Nov 25 '13 13:11 popcornmix

I will try to test the new image tomorrow, but it might be that I can make it on wednesday only. Will let you know the results with VLC then.

julianscheel avatar Nov 25 '13 13:11 julianscheel

@popcornmix

is the recorded noise something new?

No, but before your fix, it was much much more ofter, like 8/10 times.

is the updated start.elf preferable to default one?

Yes, I just replaced them.

What can I do to reproduce the noise you heard?

I've used Gstreamer program and paused->play->pause->play-> ... video from disk.

I believe same thing can be see with VLC, but I haven't tried.

hdmi or analogue output?

HDMI

Do you hear noise from aplay?

I didn't test it much, as I don't know how can I reproduce it with aplay.

Now, I will try to reproduce it with dennishamester's program and report if I managed to do it.

BTW, sorry for closing and reopening issue, I'm clumsy :)

kosirdavid avatar Nov 25 '13 13:11 kosirdavid

@10robinho Before you spend too much time with the test program: We are not very sure that this is a good testcase anymore...

julianscheel avatar Nov 25 '13 14:11 julianscheel

The drain fix from the test build is now in latest rpi-update firmware.

Is there still a problem, and is there a test case to show the problem?

popcornmix avatar Nov 27 '13 23:11 popcornmix

I am developing my own audio player application for the Raspberry Pi and I faced the exact same problem that is described in this issue. Doing a lot of play-pause-play-pause-... operations sometimes cause periodic crackling in the audio output.

My pause implementation simply calls snd_pcm_drop() on the ALSA handle to drop everything from the output buffer to stop playing. Removing the snd_pcm_drop() call from my sources fixes the problem perfectly.

One more addition to the report is that I have the same problem on the analog output of the Pi as well.

I am using a recent Pi firmware but if you need more information about it feel free to ask me.

giszo avatar Jan 16 '14 16:01 giszo

@giszo If you can produce a simple test program that clearly does something wrong, then I'd like to see it.

popcornmix avatar Jan 16 '14 17:01 popcornmix

It took a little bit of time but I was able to reproduce the issue with the following application: http://minimaldiff.hu/zeppelin/test.c Be warned that the source of the given test application is just a quickly hacked application. :) The interesting parts:

  • init_alsa() function -> ALSA library initialization
  • handle_input function() line 155 & 157 -> playback is stopped here
  • main() function line 192 & 215 -> decoded audio is played here.

You will need libmpg123 to be able to compile the application with something like this: "gcc -O2 -lasound -lmpg123 test.c -o test" An mp3 file with 44100Hz sampling rate and 2 channels is also needed. To start the application simply do "./test your_music.mp3"

By pressing space multiple times the tester will start and stop playing of the specified file. Escape can be used to quit from the application. My test scenario is something like this:

  • press space -> playback starts
  • wait a few (~3-5 seconds) -> make sure played audio is fine
  • press space -> playback pauses
  • wait a sec
  • repeat from the beginning ...

Sometimes the issue pops up right after the second try, sometimes it requires a lot of pressing on space, but I can reproduce it aytime on my Pi. When I hear the periodic glitches in the audio its period is somewhere between 500ms and 1s.

Let me know if you need further assistance.

giszo avatar Jan 16 '14 20:01 giszo

@giszo I could compile your code, and after a few pause/unpause cycles I got the glitches. I'll look into it.

popcornmix avatar Jan 17 '14 11:01 popcornmix

One more observation I did yesterday that may help, but I am not totally sure about it: if I moved the snd_pcm_prepare() call to be executed only when playback is started, the problem didn't show up ... or I was not trying it hard enough. :)

So I would think that the problem is caused by calling snd_pcm_drop() and snd_pcm_prepare() right after each other.

giszo avatar Jan 17 '14 12:01 giszo

@popcornmix did you have time to look into this issue?

giszo avatar Jan 20 '14 17:01 giszo

I've been looking at this for the last few hours, and not getting very far. The samples always look plausible all the way through the chain. If you send silence rather than music the noise doesn't seem to occur (although as the test is intermittent, I've frequently been drawing incorrect conclusions). I'd deciding the reporting of how many samples were consumed was probably causing the problem, and calculating them different ways seemed to help, but it was just chance...

I've finally found a smoking gun. Just before the noise I get a buffer with an odd number of bytes. That's pretty surprising - I'd assume that alsa deals in 2 bytes per sample and 2 channels, so I'd always see multiples of 4. I think after the odd byte buffer, we play that buffer endian reversed causing the noise. The next buffer is off too, which flips it back causing the noise/good cycles.

It's too late now, but I've got something concrete to debug in the next day or so, so hopefully will have a fix soon.

popcornmix avatar Jan 30 '14 01:01 popcornmix

It is nice to hear that there is some promising progress. :)

giszo avatar Jan 31 '14 19:01 giszo

I've got a test build for you to try: https://dl.dropboxusercontent.com/u/3669512/temp/start_alsa.elf

This has a fixed memory split of 128M/128M.

Can you test if it seems okay, and if so I'll make a new official firmware.

popcornmix avatar Feb 01 '14 18:02 popcornmix

The link does not seem to work as I get 404.

giszo avatar Feb 01 '14 18:02 giszo

Sorry, should be there now.

popcornmix avatar Feb 01 '14 19:02 popcornmix

Tested it and the problem is fixed as far as I can tell.

giszo avatar Feb 01 '14 19:02 giszo

rpi-update firmware should now have this fix. @10robinho Does this help your issue?

popcornmix avatar Feb 02 '14 17:02 popcornmix