Author Topic: Sizzling noise with push stream on macOS  (Read 331 times)

saga

  • Posts: 2504
Sizzling noise with push stream on macOS
« on: 14 Apr '21 - 13:01 »
I'm experiencing a strange issue on macOS with a BASS push stream. Due to the architecture of some underlying libraries we use, the general design of our audio implementation is as follows:
- A BASS recording callback delivers data to another library (in blocks of 480 frames, so BASS_ATTRIB_GRANULE of the recording stream has been chosen accordingly)
- We get back 480 frames from that library and put them into a push stream

This works fine on all platforms, but on macOS you can hear some sizzling noise which kinda sounds like a single frame is dropped multiple times per second. I've tried increasing the buffer size and update period, but it doesn't help. I know that in general there can be clock discrepancies between difference audio devices, which could gradually lead to an effect like this, but this happens with the built-in microphone and speakers / headphone output of the MacBook (so they should be using the same base clock from my understanding), and it happens pretty much instantly.

I have attached a minimal example demonstrating the problem (compile with clang main.cpp -Ipath/to/include -Lpath/to/lib -lbass -lc++). It outputs a constant sine wave, and works as expected on all systems but macOS (tested on 10.15 but we have observed the problem on other versions as well). It doesn't seem to happen every time the program is run, but it should be apparent after trying 2-3 times. I have also attached a capture from the MacBook's built-in output (as it's analog it might not very well show what is actually happening, but at least it gets across what the issue sounds like).

Is this a bug in BASS? If not, is there a way we can avoid the issue without changing the whole callback + push stream architecture (which would be somewhat complex due to the underlying libraries we have no control over)?

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Sizzling noise with push stream on macOS
« Reply #1 on: 14 Apr '21 - 17:14 »
I don't see any obvious signs of frames being dropped in your recording, but I do see some imperfections in the waveform. They appear to be on granule boundaries. I don't seem to be able to reproduce that here, but I think one possible cause might be if there was insufficient data to fill the resampling buffer. What is your mac's native sample rate set at? You can use Apple's "Audio MIDI Setup" app to check and change that. If it isn't 48000 Hz (like your recording and stream), does changing it to that prevent the problem? Another thing you could try is feeding some silent samples (maybe around 8 of them with the default BASS_CONFIG_SRC setting) to the push stream before starting, and see if that prevents it.

Btw, you don't really need to reduce the BASS_CONFIG_UPDATEPERIOD setting in this case because BASS_StreamPutData will already put the data in the stream's playback buffer if there's space, so there's nothing for the buffer update cycles to do.

saga

  • Posts: 2504
Re: Sizzling noise with push stream on macOS
« Reply #2 on: 15 Apr '21 - 10:46 »
The native sampling rate was set to 44100 Hz (I don't think I ever manually changed that. Strange, I would have assumed all systems use 48000 these days), and setting it to 48000 Hz prevents the issue.

Sadly, pushing some silent samples when the stream starts doesn't seem to prevent the issue. I tried pushing varying amounts (8 to 48 samples) of silence, also removed the BASS_CONFIG_UPDATEPERIOD. The problem seems to be slightly less pronounced than before, but is still present.
Setting BASS_ATTRIB_SRC to 0 avoids the issue but obviously reduces the sound quality. It might work as a temporary solution (as the stream will most typically not contain high-frequency data that could cause a lot of aliasing), but if you have any better suggestions, please let me know.

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Sizzling noise with push stream on macOS
« Reply #3 on: 15 Apr '21 - 17:38 »
That seems to confirm that the issue is resampling-related. BASS will currently play out the data in the resampling buffer before stalling. Doing that should generally sound better (small imperfection rather than a gap) but it may also mean that the issue continues for longer in cases like yours, ie. BASS keeps dipping into the resampling buffer instead of stalling briefly to wait for more data. I'll try to think of a solution.

In the meantime, regarding stuffing some silence at the start to fill the resampling buffer, that needs to be just before the audio data (ideally in the same BASS_StreamPutData call), otherwise it might be played by itself before the audio. If you aren't already stuffing the silence and first audio data in the same BASS_StreamPutData call, please give that a try and see whether it helps.

saga

  • Posts: 2504
Re: Sizzling noise with push stream on macOS
« Reply #4 on: 15 Apr '21 - 17:48 »
In the meantime, regarding stuffing some silence at the start to fill the resampling buffer, that needs to be just before the audio data (ideally in the same BASS_StreamPutData call), otherwise it might be played by itself before the audio. If you aren't already stuffing the silence and first audio data in the same BASS_StreamPutData call, please give that a try and see whether it helps.
I did indeed put the BASS_StreamPutData call in the record proc (something along the lines of if(first){first=false;BASS_StreamPutData(...);}), so the padding data should have been put in the buffer pretty much at the same time as the first 10ms of sound data.

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Sizzling noise with push stream on macOS
« Reply #5 on: 16 Apr '21 - 18:18 »
Here's an update for you to try:

   www.un4seen.com/stuff/bass-osx.zip

It will delay the start of playback slightly (including after a stall) when required to avoid dipping into the resampling buffer, but will still drain the resampling buffer (before stalling) at other times. Pretty much equivalent to the silence stuffing idea but handled automatically. It hasn't been tested much yet, so please report how you get on with it.

saga

  • Posts: 2504
Re: Sizzling noise with push stream on macOS
« Reply #6 on: 19 Apr '21 - 08:47 »
Thanks for the update. I gave it a try with my test program, and initially it worked just as expected. But after leaving the machine running for a few minutes (about five minutes I'd say), the little clicks returned. Not as regularly as before, i.e. not at the end of every granule, but still several times per second. As they effectively stopped again when I started interacting with the system (just wiggling the mouse cursor was enough), I guess it might have something to do with power management changing the CPU frequency, or something similar?
« Last Edit: 19 Apr '21 - 09:13 by saga »

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Sizzling noise with push stream on macOS
« Reply #7 on: 19 Apr '21 - 17:15 »
The BASS update is only delaying the start just enough to avoid dipping into the resampling buffer, so there isn't any margin for fluctuations (or if the recording is running slightly slower than the playback in your case). I guess that's what you're seeing there.

Here's another update for you to try, which will only drain the resampling buffer at the end of the stream:

   www.un4seen.com/stuff/bass-osx.zip

Let me know how it goes. Instead of the imperfections from dipping into the resampling buffer, you should hear a single little "click" from a very short stall.

saga

  • Posts: 2504
Re: Sizzling noise with push stream on macOS
« Reply #8 on: 20 Apr '21 - 11:49 »
The new update definitely improves things. I left the test application running for about three hours now and I only heard a short click a couple of times, maybe every 20-30 minutes or so.

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Sizzling noise with push stream on macOS
« Reply #9 on: 20 Apr '21 - 16:38 »
That seems to confirm that dipping into the resampling buffer was the issue, but I'm not really sure about only doing that at the end of the stream (like in the last update). The problem I see with that is if a stream ends while it's stalled (eg. perhaps a timed-out connection) then there will be a tiny burst of sound as the resampling buffer is drained (because it wasn't drained before stalling). If the resampling buffer is never drained then that would mean the stream's final samples aren't fully played, which wouldn't be good either.

Here's another update for you to try:

   www.un4seen.com/stuff/bass-osx.zip

It goes back to draining the resampling buffer before stalling, but that has been tweaked slightly to (hopefully) lessen any imperfections. Let me know how it compares in your tests.

saga

  • Posts: 2504
Re: Sizzling noise with push stream on macOS
« Reply #10 on: 21 Apr '21 - 12:14 »
I'd say the new update is comparable to the previous version; maybe even producing less clicks though I didn't keep count.

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Sizzling noise with push stream on macOS
« Reply #11 on: 22 Apr '21 - 17:34 »
OK, that's good. I will leave it like this for now then.

saga

  • Posts: 2504
Re: Sizzling noise with push stream on macOS
« Reply #12 on: 22 Apr '21 - 18:02 »
Okay, thanks. Does this change also apply to other platforms? I tried setting the sample rate of my output device to 44.1 kHz on Windows and didn't seem to encounter the same bug.

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Sizzling noise with push stream on macOS
« Reply #13 on: 23 Apr '21 - 17:44 »
That piece of code is the same on all platforms. Perhaps you didn't encounter the issue on Windows because there was more buffered data, so it never dipped into the resampling buffer. You could try enabling WASAPI recording (set BASS_CONFIG_REC_WASAPI to 1) and see if the problem happens then. The playback and recording data flow should be more in sync when using WASAPI for both, so more likely to maintain a low buffer level.

saga

  • Posts: 2504
Re: Sizzling noise with push stream on macOS
« Reply #14 on: 26 Apr '21 - 12:45 »
Hmm, I tried enabling that flag to see if there's a difference. I think I could observe a few more pops within the first second after the stream started, but all in all the output stays stable even without the fixed BASS version. Which is good, I guess. :) Thanks again.

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Sizzling noise with push stream on macOS
« Reply #15 on: 27 Apr '21 - 17:05 »
The issue would only appear when the stream's buffer has only just enough data, ie. almost but not quite stalling. It sounds like that isn't happening with your test on Windows. Perhaps the GRANULE setting is preventing it, ie. if that doesn't match the devices' own block size.

Here's a Windows version of the update anyway :)

   www.un4seen.com/stuff/bass.zip

saga

  • Posts: 2504
Re: Sizzling noise with push stream on macOS
« Reply #16 on: 29 Apr '21 - 09:26 »
Thanks, I'll test-drive it as soon as possible. Is the update also in the latest "stuff" versions for Linux and Android, in case it's applicable there? We didn't have anyone reporting issues on those systems, but better safe than sorry. :)

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Sizzling noise with push stream on macOS
« Reply #17 on: 29 Apr '21 - 17:02 »
The changes weren't in the Linux/Android builds yet, but here are updates that do include it:

   www.un4seen.com/stuff/bass-linux.zip
   www.un4seen.com/stuff/bass-android.zip

saga

  • Posts: 2504
Re: Sizzling noise with push stream on macOS
« Reply #18 on: 30 Apr '21 - 09:34 »
Cheers, I will report back if anything strange happens due to the updates. :)