Author Topic: BASS indefinetely changes clock resolution on init  (Read 1116 times)

IL

  • Guest
It seems that on init bass.dll calls timeBeginPeriod(1) of winmm.dll to set 1ms clockres.
High resolution timer runs until program using bass.dll stops. Bass.dll itself never calls timeEndPeriod, which it should.
This is especially not good for notebook on battery, due to power wastage arises.
Second thought, why not call timeBeginPeriod when it actually needed and timeEndPeriod afterwards?

Ian @ un4seen

  • Administrator
  • Posts: 20393
Re: BASS indefinetely changes clock resolution on init
« Reply #1 on: 15 Mar '16 - 13:39 »
"timeBeginPeriod(1)" is called when the first device is initialized via BASS_Init, and then "timeEndPeriod(1)" is called when there are no devices initialized, ie. after BASS_Free is called for all initialized devices. Are you calling BASS_Free and still seeing the 1ms period in effect?

IL

  • Guest
Re: BASS indefinetely changes clock resolution on init
« Reply #2 on: 15 Mar '16 - 14:27 »
First, this is about Noisy player and Really Quick Player plugins for FAR manager.
For instance, in https://github.com/bgarrels/far-plugins/blob/master/Noisy/Player/PlayerMain.pas
Bass_Init is called right from the app start (TBassPalyer.Create) and Bass_Free is not called until the Exit from the app (TBassPalyer.Destroy).
What do you recommend to change?

Ian @ un4seen

  • Administrator
  • Posts: 20393
Re: BASS indefinetely changes clock resolution on init
« Reply #3 on: 15 Mar '16 - 14:56 »
The main reason that BASS sets the timer resolution to 1ms is for syncs, eg. so that a BASS_SYNC_POS sync function gets called as close as possible to when the wanted position is heard. If you don't care about that (the code in your link doesn't appear to be using any non-mixtime syncs), it is actually possible to disable the 1ms timer resolution via an undocumented BASS_CONFIG_NOTIMERES config option. Here's a thread on the subject:

   www.un4seen.com/forum/?topic=10278

IL

  • Guest
Re: BASS indefinetely changes clock resolution on init
« Reply #4 on: 15 Mar '16 - 16:39 »
I'm sorry, I don't understand why is it really needed to keep sync with 1ms precission instead of default 15.6ms. Does 1ms add a real benefit to most or many applications using BASS.DLL?

The thing that BASS_CONFIG_NOTIMERES config option is undocumented and a pre-compile define setting makes almost impossible to alleviate the issue.
What I undestood after reading the link you provided is that in the code using BASS I can call timeEndPeriod myself:
BASS_Init(...);
timeEndPeriod(15);
Is this correct?

Unfortunately using timeEndPeriod is pretty undocumented, except that excerpt on MSDN
Quote
You must match each call to timeBeginPeriod with a call to timeEndPeriod, specifying the same minimum resolution in both calls. An application can make multiple timeBeginPeriod calls as long as each call is matched with a call to timeEndPeriod.
it seems that almost nobody uses it leaving restoring timer resolution to the OS upon process exit.

Anyway thank you for explanation.

Ian @ un4seen

  • Administrator
  • Posts: 20393
Re: BASS indefinetely changes clock resolution on init
« Reply #5 on: 15 Mar '16 - 17:16 »
What I undestood after reading the link you provided is that in the code using BASS I can call timeEndPeriod myself:
BASS_Init(...);
timeEndPeriod(15);
Is this correct?

No, the parameter used in the timeEndPeriod call must match the parameter used in the timeBeginPeriod call, so it would need to be 1 in this case, as that is what BASS uses in its timeBeginPeriod call. I don't think it's a good idea to call timeEndPeriod yourself though, as that will unbalance the timeBeginPeriod/timeEndPeriod calls when BASS_Free calls timeEndPeriod itself. Better to add a BASS_SetConfig call instead:

Code: [Select]
#define BASS_CONFIG_NOTIMERES 29
BASS_SetConfig(BASS_CONFIG_NOTIMERES, 1); // disable 1ms timer resolution

Note that should be done before calling BASS_Init (not after).

SoundMike

  • Posts: 331
Re: BASS indefinetely changes clock resolution on init
« Reply #6 on: 16 Mar '16 - 00:20 »
"timeBeginPeriod(1)" is called when the first device is initialized via BASS_Init, and then "timeEndPeriod(1)" is called when there are no devices initialized, ie. after BASS_Free is called for all initialized devices.
Interesting - I didn't know that. I'm calling timeBeginPeriod() and timeEndPeriod() in my own program, so that's probably not necessary if BASS is doing this anyway. In my code I set the period to the minimum period returned by timeGetDevCaps(). On a test on my development machine, that was 1ms.

Edit: I also only call timeBeginPeriod() etc if running under Vista or later and if running on mains power.
« Last Edit: 16 Mar '16 - 00:36 by SoundMike »

IL

  • Guest
Re: BASS indefinetely changes clock resolution on init
« Reply #7 on: 16 Mar '16 - 05:47 »
Code: [Select]
#define BASS_CONFIG_NOTIMERES 29
BASS_SetConfig(BASS_CONFIG_NOTIMERES, 1); // disable 1ms timer resolution
Note that should be done before calling BASS_Init (not after).
Thank you, now it makes sense to me how to turn timeBeginPeriod call off. I wrongly assumed that #define BASS_CONFIG_NOTIMERES is BASS.DLL compile time define.
I'll use your post for reference.

Edit: I also only call timeBeginPeriod() etc if running under Vista or later and if running on mains power.
Which is very reasonable, IMO.

Thanks guys.

rv

  • Posts: 191
Re: BASS indefinetely changes clock resolution on init
« Reply #8 on: 16 Mar '16 - 12:00 »
Why not Windows XP?

SoundMike

  • Posts: 331
Re: BASS indefinetely changes clock resolution on init
« Reply #9 on: 16 Mar '16 - 12:55 »
Not sure why XP was excluded - I added this code ages ago, and it was probably due to my assumption that one of the functions was not available in XP. But I could be wrong. I've just looked up those functions again and they all appear to have been available since Windows 2000 (which was pre-XP). So I should probably remove that particular test and just test for AC power.

Ray890

  • Posts: 31
Re: BASS indefinetely changes clock resolution on init
« Reply #10 on: 25 Mar '16 - 16:12 »
As quoted by Coolsoft in regards to VMS 2.x,
VMS does not call TimeBeginPeriod, but I suppose BASS does. VMS-synth component initialize BASS as soon as it starts (actually started by the VMS-driver component), and leave it initialized to have it ready if the driver is stopped/restarted lately. Think about VanBasco being closed and/or restarted by double clicking on a KAR file in Windows Explorer.

VirtualMIDISynth 2.x is structured in a way where it keeps BASS Initialized at all times in order to keep soundfonts loaded in memory, there's a feature that VMS 2.x has is that it changes the "BASS Update Rate" to 10ms whenever there's no or very minimal activity.  My idea is that as the BASS_Update can be part of what control the clock resolution, where 1ms update will trigger 1ms timer, and 10ms update will trigger a 10ms/5ms timer.etc.  If the timer's required for additional things that can idle for a lot of the time, the timer could be summoned during the point of time then go back to it's previous point after.

Would something like this work?
« Last Edit: 25 Mar '16 - 16:16 by Ray890 »

Ian @ un4seen

  • Administrator
  • Posts: 20393
Re: BASS indefinetely changes clock resolution on init
« Reply #11 on: 25 Mar '16 - 17:45 »
I will look into the possibility of BASS automatically enabling and disabling the 1ms timer resolution.

Is the "BASS Update Rate" that you mention referring to the BASS_CONFIG_UPDATEPERIOD setting? If so, that controls how frequent playback buffer update cycles are, but BASS will actually disable buffer update cycles when nothing is playing, so raising the BASS_CONFIG_UPDATEPERIOD setting at that point won't really have any effect, ie. it's probably unnecessary.