Author Topic: Starting queued stream after 30 seconds results in delay  (Read 156 times)

dateq

  • Posts: 12
Hi all,

I'm writing a program where you can queue a stream and then start it. Sounds simple enough right? :)

I am noticing some odd behaviour though.
 - When a stream is queued and you start it immediately after que-ing, no delay is noticable.
 - If i stop the playback of that stream and start it again right away, no delay is noticable.
 - But.. If I stop playback of the stream, wait a few seconds, then when I press "play", it takes a second or two to start the stream. It's almost as if the stream gets re-loaded in the background and THEN gets started.

I've been playing around with buffers and such, but nothing seems to remedy the problem.

Here is some of my code:

Code: [Select]

                try {
BASS_SetConfig(BASS_CONFIG_DEV_PERIOD, 1);
} catch (...) {
LogLine("Error setting BASS_CONFIG_DEV_PERIOD to 1", apERROR);
LogLine("BASS_ErrorGetCode: " + IntToStr(BASS_ErrorGetCode()), apAUDIO);
}
try {
BASS_SetConfig(BASS_CONFIG_MIXER_POSEX, 10);
} catch (...) {
LogLine("Error setting BASS_CONFIG_MIXER_POSEX to 10", apERROR);
LogLine("BASS_ErrorGetCode: " + IntToStr(BASS_ErrorGetCode()), apAUDIO);
}
try {
BASS_SetConfig(BASS_CONFIG_BUFFER, 500);
} catch (...) {
LogLine("Error setting BASS_CONFIG_BUFFER to 500", apERROR);
LogLine("BASS_ErrorGetCode: " + IntToStr(BASS_ErrorGetCode()), apAUDIO);
}
if(!BASS_Init(SoundDevice, 44100, BASS_DEVICE_STEREO | BASS_DEVICE_DSOUND, 0, NULL)) {
ShowMessage("MyBASS_Init has not completed!");
LogLine("PlayerMain: SetupClient: BASS_Init failed!", apERROR);
} else {
LogLine("BASS initialized; Errorcode: " + IntToStr(BASS_ErrorGetCode()), apAUDIO);
}
Code: [Select]
Mixer = BASS_Mixer_StreamCreate(44100, chans, BASS_MIXER_NONSTOP);
Code: [Select]
Stream = BASS_StreamCreateFile(false, LoadedFileName.c_str(), 0, 0, BASS_STREAM_DECODE | BASS_UNICODE);
BASS_Mixer_StreamAddChannel(Mixer, Stream, BASS_MIXER_NORAMPIN | BASS_STREAM_AUTOFREE);
        BASS_ChannelPlay(Mixer, false);

Ian @ un4seen

  • Administrator
  • Posts: 26079
Are you sure the delay is as much as 2 seconds? As you're playing through a mixer, there will be a delay introduced by its playback buffer, ie. up to 500ms with your BASS_CONFIG_BUFFER setting. The BASS_MIXER_NONSTOP flag means the mixer's playback buffer will be kept full even if nothing is playing, so the delay will be at/close to the full 500ms. You won't hear a gap when you stop and immediately resume playback if the mixer hasn't generated any new output in the meantime.

If you would like to avoid any extra delay from the mixer, you can disable playback buffering via its BASS_ATTRIB_BUFFER setting:

Code: [Select]
BASS_ChannelSetAttribute(Mixer, BASS_ATTRIB_BUFFER, 0); // disable playback buffering

dateq

  • Posts: 12
Thanks for your reply!

I will try this and will report back on the result ;)

dateq

  • Posts: 12
Well, I tested it and all seemed fine. At least it takes longer for the "timer" to run out before I get a delay on starting a stream.

The turning point seems to be between 32 and 33 seconds.
At 32 seconds, when I start the stream, it starts immediately. At 33 seconds, the delay is back :(
I timed it and the delay is indeed about 500 ms.

dbaxter

  • Posts: 83
This may be totally of the mark, but it affected my programs with BASS - the latest versions of Windows laptops come with the power setting not at "High Performance". The lower setting allows the computer to pause processing. That was causing a delay in startup of a stream for my users. I had to include a small console program that runs at install of the software to change the registry setting back to High Performance.
Here is the procedure:
Code: [Select]
procedure SetHighPerformancePowerPlan;
var
  Command: string;
begin
  // The GUID for the High Performance power plan is usually 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
  Command := 'powercfg /setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c';
  ShellExecute(0, 'open', 'cmd.exe', PChar('/C ' + Command), nil, SW_HIDE);
end;

Perhaps this will help.

Ian @ un4seen

  • Administrator
  • Posts: 26079
At 32 seconds, when I start the stream, it starts immediately. At 33 seconds, the delay is back :(
I timed it and the delay is indeed about 500 ms.

That sounds strange. Please check what BASS_IsStarted and BASS_ChannelIsActive(Mixer) report just before starting playback in each case.

dateq

  • Posts: 12
Well, it seems to have been fixed!!  ;D

At first I thought dbaxter was onto something since I am running on a laptop with windows 11 and the power settings were indeed on "Balanced".
But, after trying it there was no change in the behaviour of my program.

Next, I read up on the BASS_IsStarted documentation and there was something that stood out to me:
Quote
If the device has not been started, then 0 is returned. If the device has been started, then 1 is returned if it is currently active and 2 is returned if it is inactive (nothing is playing and BASS_CONFIG_DEV_NONSTOP is disabled).

So, just for laughs and giggles I implemented a BASS_SetConfig setting "BASS_CONFIG_DEV_NONSTOP" to "TRUE" and that seems to have been it!
I can happily keep a stream standing ready in the queue and as soon as I press "Play" the stream starts without delay! Happy times!

Since I am writing a play-out system for my radiostation, the delay really was a showstopper.
Thanks for all the help!

Ian @ un4seen

  • Administrator
  • Posts: 26079
Good to hear that BASS_CONFIG_DEV_NONSTOP helped, but it is a bit strange, as the mixer's BASS_MIXER_NONSTOP flag should mean that the output device already never stops (because the mixer is always playing). Did you remove that flag or stop the mixer with BASS_ChannelStop/Pause? If not, please try disabling BASS_CONFIG_DEV_NONSTOP again and check what BASS_IsStarted and BASS_ChannelIsActive say.

dateq

  • Posts: 12
Hi again!

Sorry, due to work it took awhile to get back to you Ian.  :-\

Anyways, to answer your question, I use BASS_ChannelStop(Mixer); to stop the playback.
But, even if I queue a stream and wait more than 32 seconds, the delay occurs when starting playback.

With BASS_CONFIG_DEV_NONSTOP there is no delay when starting. Even if I wait 5 minutes to start playback.

BASS_IsStarted and Bass_ChannelActive report the following:

With BASS_CONFIG_DEV_NONSTOP set to FALSE:
Code: [Select]
| AUDIO   | 8:14:45:591 Playback has been stopped

| NOTICE  | 8:19:04:203 BASS_IsStarted: 2, BASS_ChannelIsActive(Mixer): 0
| DEBUG   | 8:19:04:207 BASS_CONFIG_CURVE_VOL set to FALSE; Errorcode: 0
| DEBUG   | 8:19:04:213 Volume (1) set; Errorcode: 0
| AUDIO   | 8:19:04:217 Now playing: SHOTGUN - Fast Fast 2 on StreamOne

With BASS_CONFIG_DEV_NONSTOP set to TRUE:
Code: [Select]
| AUDIO   | 8:21:17:096 Playback has been stopped

| NOTICE  | 8:25:59:812 BASS_IsStarted: 1, BASS_ChannelIsActive(Mixer): 0
| DEBUG   | 8:25:59:817 BASS_CONFIG_CURVE_VOL set to FALSE; Errorcode: 0
| DEBUG   | 8:25:59:820 Volume (1) set; Errorcode: 0
| AUDIO   | 8:25:59:823 Now playing: SHOTGUN - Fast Fast 2 on StreamOne


Ian @ un4seen

  • Administrator
  • Posts: 26079
Anyways, to answer your question, I use BASS_ChannelStop(Mixer); to stop the playback.

OK, that would explain why the mixer's BASS_MIXER_NONSTOP flag didn't have the same effect as BASS_CONFIG_DEV_NONSTOP. The BASS_MIXER_NONSTOP flag means the mixer won't stall when it has no active sources, but it'll still stop when BASS_ChannelStop/Pause is called, and the device output will then stop too shortly after without BASS_CONFIG_DEV_NONSTOP (if nothing else is playing). In your case, the device is apparently going into some deep sleep state after ~30s of inactivity.