Author Topic: Stuttering / garbled audio on Linux  (Read 1514 times)

Taiko2k

  • Posts: 15
Stuttering / garbled audio on Linux
« on: 9 Mar '19 - 23:53 »
I've received a few reports from users about audio issues, that are:

a. Sometimes audio is garbled for 1-2 secs when starting a track but sometimes remains so
b. Sometimes during a track the audio stutters for a moment

I'm not sure if these are related. Though b possibly may be related to system resource usage in some way causing the channel to stall.

Personally I've not encountered such issues, though I have a rather high end and up to date system.

Would these be issues with bass? Is there anything I can do to mitigate the issues?
« Last Edit: 10 Mar '19 - 01:32 by Taiko2k »

Ian @ un4seen

  • Administrator
  • Posts: 22121
Re: Stuttering / garbled audio on Linux
« Reply #1 on: 11 Mar '19 - 14:55 »
Is there any pattern to the users that are affected by the problem, eg. in Linux/ALSA/PulseAudio versions or the system load at the time?

In the 2nd problem case, is the BASS channel stalling or is the entire output stalling? You can use a BASS_SYNC_STALL sync to check that. If the output is stalling, BASS probably didn't get enough CPU for a moment and the device ran out of buffered data to play. Increasing the device buffer length via the BASS_CONFIG_DEV_BUFFER option (before calling BASS_Init) should help with that. The default setting on Linux is 40ms (60ms on ARM Linux), so you could try going up from that. The 1st problem is stranger but perhaps increasing the buffer length will help with that too?

Taiko2k

  • Posts: 15
Re: Stuttering / garbled audio on Linux
« Reply #2 on: 29 Mar '19 - 22:54 »
In regards to the stuttering, I find I can replicate this by running a disk benchmark application on the same HDD as the audio file that is being played. I can actually stall the stream for many seconds at a time doing this. BASS_ChannelIsActive returns BASS_ACTIVE_STALLED during the stall. I'm sure its not a CPU issue, at least for this particular case, since I have plenty of free CPU resources.

I tired setting the BASS_ASYNCFILE flag and setting BASS_CONFIG_ASYNCFILE_BUFFER to something massive (I see that it fully pre-allocates RAM to value given when creating the channel). Even after letting the channel play for a bit, I can still stall it using the disk benchmark. Shouldn't doing this fully load the file into RAM and thus be unaffected by further IO interruptions?

Ian @ un4seen

  • Administrator
  • Posts: 22121
Re: Stuttering / garbled audio on Linux
« Reply #3 on: 1 Apr '19 - 16:09 »
In that scenario, it would be I/O delays (rather than lack of CPU) that is the issue, ie. the HD benchmark is limiting BASS's file reading.

When BASS_ASYNCFILE is enabled, the file buffer isn't necessarily filled during stream creation but rather just starts filling (asynchronously). You can check how much file data is currently buffered with BASS_StreamGetFilePosition (BASS_FILEPOS_ASYNCBUF). Does that show that the entire file is buffered, yet you still get stalling?

Taiko2k

  • Posts: 15
Re: Stuttering / garbled audio on Linux
« Reply #4 on: 2 Apr '19 - 02:30 »
From what I can tell, the async buffer fills very quickly (within a few ms) then slowly depletes as the track plays eventually to 0 as the end of the file is reached. However at any time during the playing of the track, it can still stall.

I was thinking maybe its poor performance of the NTFS driver or the scheduler in Linux combined with mechanical HDD's (I can't replicate it using SSD's), but I assume the buffer should deplete before stalling in any case. I've had multiple reports of stalling from users, so I assume there is an issue somewhere.

« Last Edit: 2 Apr '19 - 02:57 by Taiko2k »

Ian @ un4seen

  • Administrator
  • Posts: 22121
Re: Stuttering / garbled audio on Linux
« Reply #5 on: 2 Apr '19 - 16:41 »
From what I can tell, the async buffer fills very quickly (within a few ms) then slowly depletes as the track plays eventually to 0 as the end of the file is reached. However at any time during the playing of the track, it can still stall.

In that scenario, is a BASS_SYNC_STALL sync getting triggered, and does BASS_StreamGetFilePosition (BASS_FILEPOS_ASYNCBUF) show that there is buffered data?

Taiko2k

  • Posts: 15
Re: Stuttering / garbled audio on Linux
« Reply #6 on: 2 Apr '19 - 22:18 »
Yes, a BASS_SYNC_STALL does trigger, in the callback function if I get BASS_FILEPOS_ASYNCBUF it shows as having plenty of data.

Ian @ un4seen

  • Administrator
  • Posts: 22121
Re: Stuttering / garbled audio on Linux
« Reply #7 on: 3 Apr '19 - 14:47 »
That's strange. Are you playing any other files at the same time, and are you playing the file directly, eg. not through a mixer? Basically, doing this:

Code: [Select]
BASS_SetConfig(BASS_CONFIG_ASYNCFILE_BUFFER, bignumber);
stream = BASS_StreamCreateFile(false, filename, 0, 0, BASS_ASYNCFILE);
BASS_ChannelSetSync(stream, BASS_SYNC_STALL, 0, StallSyncProc, NULL);
BASS_ChannelPlay(stream, false);

Also, what file format are you testing with? Does the problem happen with WAV files?

stevenmmm

  • Posts: 122
Re: Stuttering / garbled audio on Linux
« Reply #8 on: 3 Apr '19 - 19:58 »
Ian,
I have had a few users (so its quite rare) report the same on a Windows environment where a large async buffer is used but still was reproducably stuttering mid-stream when disc activity is high or network capacity is max'ed (for files accross a network share). In fact we had an email exchange about it around 5 July 2016 with some test results for AAC and FLAC files.
Admittedly I havent had any complaints for several months now so its possible its now solved in my case with a recent bass version.
I just bring this up in case there is any useful information in the test results I sent you back in 2016.
For the record I am using bass mix and the async on the source stream but I have no current users with the issue to be able to assist with further testing.

Taiko2k

  • Posts: 15
Re: Stuttering / garbled audio on Linux
« Reply #9 on: 4 Apr '19 - 00:53 »
Upon further testing, I find that the issue only occurs with FLAC. With other formats, such as MP3 and WAV, the buffer does reach zero before stalling.

I normally use a decoding channel into a mixer, but it still happens with a normal stream and with your code snippet there.

Ian @ un4seen

  • Administrator
  • Posts: 22121
Re: Stuttering / garbled audio on Linux
« Reply #10 on: 4 Apr '19 - 15:09 »
If you use the "iotop" tool to monitor disk activity, do you see any by your BASS test app even after it has buffered the entire file? Also confirm that you are using the latest BASS and BASSFLAC versions. If you are having the problem with a particular FLAC file, please upload that to have a look at here:

   ftp.un4seen.com/incoming/

Taiko2k

  • Posts: 15
Re: Stuttering / garbled audio on Linux
« Reply #11 on: 5 Apr '19 - 00:15 »
I can't seem to get iotop or gnome-system-monitor to show any disk usage for my test app, so I'm not sure whats going on there. I have a basic test C++ app I'm running from terminal for this. (Both this and my main app exhibit this stalling issue with FLAC despite setting a sufficiently large async buffer.)

Ive checked I'm using the latest versions. I've tested with numerous different FLAC files and they all exhibit this behavior, I've tested MP3, WAV, TTA and APE files from the same drive and they work fine.

Ian @ un4seen

  • Administrator
  • Posts: 22121
Re: Stuttering / garbled audio on Linux
« Reply #12 on: 5 Apr '19 - 13:29 »
I can't seem to get iotop or gnome-system-monitor to show any disk usage for my test app, so I'm not sure whats going on there.

Perhaps the file is in the system cache? You can clear that with "echo 3 > /proc/sys/vm/drop_caches". It seems like the problem isn't related to disk I/O, but I'm not sure what else it could be (if it only happens while running an disk benchmark) and why it would only affect FLAC. Please upload one of your FLAC files to have a look at. Also, if you monitor the BASS_GetCPU value in your app, what does it show when the problem is happening?

Taiko2k

  • Posts: 15
Re: Stuttering / garbled audio on Linux
« Reply #13 on: 6 Apr '19 - 02:00 »
BASS_CPU normally stays around 1-2, interestingly during the stall, it stays locked at a fixed number and does not update. After the stall it rises to around 80 then falls back to normal.

I've also noticed that BASS_ChannelGetData blocks does not return during the stall.

As for files, I'm sure its not related to any specific FLAC files, since it does it both for files downloaded from the internet, and ones freshly encoded from WAV using the FLAC encoder.

Using that command to drop the cache does work to make iotop show the activity. It seems to show the test app just initially loading the file with no subsequent access.

Maybe I should note I'm running a benchmark inside a virtual machine which causes the most severe stalling (any application/thread that access's the drive during this pretty much freezes), but I can still replicate intermittent stalling without the VM using native disk benchmark tools.

I also have noticed that audio will stutter when loading a file (like an image file) from the drive when the drive head is parked (I assume, since it happens when the drive have not been accessed in a while). For example in my music player app, if I transition to another track part way though (so the disk has not been accessed in a while but the audio is still playing) and that track was a FLAC, the audio will stutter for a brief moment as the new track is loaded for the crossfade.


« Last Edit: 7 Apr '19 - 14:23 by Taiko2k »

Ian @ un4seen

  • Administrator
  • Posts: 22121
Re: Stuttering / garbled audio on Linux
« Reply #14 on: 8 Apr '19 - 15:07 »
BASS_CPU normally stays around 1-2, interestingly during the stall, it stays locked at a fixed number and does not update. After the stall it rises to around 80 then falls back to normal.

I've also noticed that BASS_ChannelGetData blocks does not return during the stall.

That does sound like the decoder encountered an I/O delay (not sure how though if there was no disk access). BASS_GetCPU actually measures timeliness rather than CPU usage, hence a spike when an I/O delay occurs. The device thread should still be running in the meantime, so I would expect the BASS_GetCPU value to change slightly during the I/O delay. Did it not change at all, to several decimal places? If so, where were you calling BASS_GetCPU from? Perhaps that was delayed too, eg. if you're calling it in the same thread as the blocked BASS_ChannelGetData?

By the way, does the problem still happen if you remove the BASS_ChannelGetData call (and any other BASS calls) during playback? For example, if you just do this:

Code: [Select]
stream = BASS_StreamCreateFile(false, filename, 0, 0, BASS_ASYNCFILE);
BASS_ChannelPlay(stream, false);
getchar();

Taiko2k

  • Posts: 15
Re: Stuttering / garbled audio on Linux
« Reply #15 on: 8 Apr '19 - 17:38 »
BASS_GetCPU does not appear to change at any decimal places during the stall (even for the 20 or so seconds I can get it to stall at a time). I'm calling it form the same thread as everything else. Of course, I would not be able to continuously call BASS_GetCPU during the stall from the same thread as having BASS_ChannelGetData as it does not return.

And indeed, I did another test, it still occurs even with the minimal code with no further calls. (Stalls with FLAC, plays fine with MP3)

Ian @ un4seen

  • Administrator
  • Posts: 22121
Re: Stuttering / garbled audio on Linux
« Reply #16 on: 8 Apr '19 - 17:53 »
OK. I will send you a debug BASSFLAC version to try to locate where the delay is coming from.

Ian @ un4seen

  • Administrator
  • Posts: 22121
Re: Stuttering / garbled audio on Linux
« Reply #17 on: 10 Apr '19 - 17:02 »
For anyone else that has encountered this issue with BASSFLAC, it turns out that the I/O delay was in BASSFLAC checking if the file size had changed (in case the file is still being written). A BASSFLAC update (2.4.4.2) that only rechecks the file size when out of data is up now on the BASS page.

stevenmmm

  • Posts: 122
Re: Stuttering / garbled audio on Linux
« Reply #18 on: 10 Apr '19 - 19:09 »
For anyone else that has encountered this issue with BASSFLAC, it turns out that the I/O delay was in BASSFLAC checking if the file size had changed (in case the file is still being written). A BASSFLAC update (2.4.4.2) that only rechecks the file size when out of data is up now on the BASS page.
would this issue also apply to other formats such as AAC (via media foundation codecs)?

Ian @ un4seen

  • Administrator
  • Posts: 22121
Re: Stuttering / garbled audio on Linux
« Reply #19 on: 11 Apr '19 - 13:18 »
It does look like Media Foundation is doing something similar to what BASSFLAC was. Are you able to reproduce the problem? If so, perhaps we can look into a workaround for it.

stevenmmm

  • Posts: 122
Re: Stuttering / garbled audio on Linux
« Reply #20 on: 11 Apr '19 - 13:42 »
It does look like Media Foundation is doing something similar to what BASSFLAC was. Are you able to reproduce the problem? If so, perhaps we can look into a workaround for it.
My question was based on what was reported and tested by a user back in 2016 - he had AAC and FLAC files and I emailed you some test results early July 2016. Unfortunately I have never been able to reproduce the issue myself which would make things easier to close this out.