Sound, an app-global "Pause button"

Continuing from here.

Below is what i almost posted, then had a second though, as usual…

Now i think that the functionality i wanted just does not exist in CGE ATM


Doc: “This is useful if you simply want to disable any sound output”

This sounds misleadingly encouraging. Consider this:

D:\DelphiProjects\Libs\Games\castle-engine\src\audio\castlesoundengine_engine.inc
Line 400

    property Device: string read FDevice write SetDevice;

    { Enable sound.

      If set @false before your fist call to @TCastlePlayingSound.Play, then @ContextOpen will not initialize any audio device, provided by the platform(operating system). 

      This is useful if you simply want to disable any sound output
      (or backend, like OpenAL, usage), even when sound library (like OpenAL)
      is available.

      If the sound context was already initialized when setting this property to False,
      we will eventually close it and might also release the audio device for other applications to use (this depends upon the used back-end implementation details). More precisely, we will
      call @ContextClose here, and then call @ContextOpen again if you set the property back to @True. 

    This might also cause an audible "click" in headphones, depending upon used back-end and platform.     This would also reset all the memory data buffers of all the playing streams and make CGE forget the current positions in sound loops. 

    In other words,  `SoundEngine.Enabled := false` is not a way to temporarily mute the application, it is similar to pressing "Stop" button on a music player, rather than "Pause" button. For a short-term pausing consider setting @Volume to zero.
 }
    property Enabled: boolean read FEnabled write SetEnabled default DefaultEnabled;

Line 388

    { Sound volume, affects all sounds (effects and music).
      This must always be within 0..1 range.
      
      This property (unlike @Enabled) might be used for temporarily muting your application: setting it to   0.0 means that there would be no audible effects, and the sound backends (OpenAL, FMOD etc.) should be optimized for this specific case. }
    property Volume: Single read FVolume write SetVolume


NOW the question: would it really be an equivalent of “pause button” ? My gut feeling is that all the looped sound sources would still be doing their loops, and all non-looped ones would run to theend and expire.

To my specific need it does not matter. To a usual game it would not matter too - the scene changed and so should be sound.

However, is there really a “pause” functionality in CGE, that “stops the time” in audio domain?
Probably not, and the wording above, i concocted i patched together from your explanations, are probably wrong.

No, i am on a fence if a global “audio pause” functionality is really needed for CGE.
On one hand, having an option is alway more freedom than not.
On another - it seems few would practically need it.

Then, what about fading? may CGE (on both global Sound Engine and local Sound Sources) levels have an out of the box “slow stop” and “slow resume” ? Again, it is not core functionality but off a nice-to-have shelf.

Oookay… Sadly, setting SoundEngine.Volume := 0 also might cause an audible click. Not always, it seem somehow related to the loop positions in a “pulsing” sound sources.

Perhaps, OpenAL optimization hjere is a bit too aggressive and even “fading” in CGE level would not preclude it completely. Not a big issue for me, so not sure if i would explore it.

Just to register, that my above rant about clicks being exclusive to “Enabled” property was wrong…

This warning about clicks possibility might be put into docs though. They seems to be not 100% repro, so a developer might happen being totally oblivious to it, wil app users experience it. Just speculating, though. I DO hear it, but this does not mean every Win7 dev would, less so Linux/Android devs. “here be dragons”.


And one more update, after i removed the forced re-initializing all the sound sources after “un-pausing” (was required for .Enabled approach) the clicks becane erratic and rare. Probably they do more this bad spund samples with non-smooth edges, or with interference of several of those.

Not sure what the wording there in doc should be then…

Sorry for the delay in answering! I got buried by tasks (mostly CGE tasks, so I suppose in a way that’s good :slight_smile: ). I’m getting back now to answering everything pending on our forum.

  • As for audible clicks:

    Admittedly I do not experience them. And we allowed to set sound volume to 0 in all CAG games ( Escape from the Universe for Nintendo Switch - Nintendo Official Site , https://unholy-society.com/ ) on various platforms, we didn’t get any report about clicks being a problem from our internal QA or players.

    But I suppose it can happen and it depends on OS, and particular sound possibly played at this time, and even hardware like headphones. It is not something we can deal with at CGE level. I’m not even sure if OpenAL can deal with this, it may be something at lower level.

  • We don’t want to implement “changing volume smoothly with some delay” in CGE – although you could do it easily yourself, using e.g. just lerp to control SoundEngine.Volume.

  • There is indeed no way to “pause time of sound playback” now.

    As you found, 1. “SoundEngine.Volume := 0” mutes sounds, but they still proceed forward. 2. toggling “SoundEngine.Enabled” between false and true stops all non-looping sounds, and loses tracking of sounds – because we close and reinitialize our sound backend (like OpenAL). So neither 1 or 2 “just pause time”.

    I didn’t really need such “sound pausing” so far in my games.

    Internally looking at OpenAL API it doesn’t have any global way to pause processing (alcSuspendContext and alcProcessContext, why they sound tempting, are not about this). We’d have to do this by actually pausing all OpenAL sounds, calling alSourcePause on all of them, to later resume. Possible, but not trivial, and I admittedly don’t think now that we need this feature that much.

    We plan to add more sound backends and support sound middleware like FMOD Studio and Wwise (see Roadmap | Manual | Castle Game Engine ) – at which point, such pausing and much more will be possible to achieve in a specialized sound application, and from the engine you would just send a generic named event like SoundEngine.Event('pause').

  • I updated various comments in CastleSoundEngine to address some things mentioned here. Thanks!

No problem, free is free.

not even sure if OpenAL can deal with this, it may be something at lower level.

Agree.

although you could do it easily yourself, using e.g. just lerp to control SoundEngine.Volume .

I understand you would not want to implement settings for differenr “laws” of fading - linear, logarythmic, timespan and what not, OTOH i wonder if changing this property from another thread would work. And main thread might well be busy with something else… At least source-wise if not execution-wise,

I didn’t really need such “sound pausing” so far in my games.

Well, yes. It is definitely of “nice to have” kind, not something essential.