Set audio fadeout times on Sugarcube

Sugarcube 2.31

Just looking at the audio documentation. Can I adjust the fadeout
time from the preset 5s? I can see how I could adjust fadein time using fadeover to. But that starts playing a track. What if I want it to end - but speed up or slow down the fadeout? Would it be 2 separate commands, eg Fadeover to 10 0 and then Stop?

Thanks, Ben

You can just use fadeoverto 10 0 in the <<audio>> macro for that, except for the stop part (also, be careful of the capitalization, since it probably matters).

If you want to trigger the stop after 10 seconds as well, then you can do this:

<<audio "track_id" fadeoverto 10 0>>
<<run setTimeout(function () { $.wiki('<<audio "track_id" stop>>'); }, 10000)>>

(10,000 ms = 10 seconds) See the setTimeout() method and the .wiki() method for details.

Hope that helps! :grinning:

EDIT: Fixed changing fadeto to fadeoverto.

That’s super helpful. You are using fadeto instead of fadeoverto? Which raises a question: what is the practical difference between:

  • fadein
  • fadeout
  • fadeoverto
  • fadeto

?
Thanks! Ben

You want the fadeoverto action. For example:

<<audio "trackId" fadeoverto 10 0>>

I.e., fade over 10 seconds to volume level 0.

Any track faded to volume 0 automatically ends playback.


The differences are:

  • fadein: Start playback of the selected tracks and fade them from their current volume level to 1 (loudest) over 5 seconds.
  • fadeout: Start playback of the selected tracks and fade them from their current volume level to 0 (silent) over 5 seconds.
  • fadeoverto seconds level: Start playback of the selected tracks and fade them from their current volume level to the specified level over the specified number of seconds.
  • fadeto level: Start playback of the selected tracks and fade them from their current volume level to the specified level over 5 seconds.

Whoops. Yeah, that was supposed to be fadeoverto. I looked at the wrong name.

I fixed my post above.

Good to know. You should probably mention that in the documentation.

That basically means that you don’t need the setTimeout() line in my earlier code.

This is all great to know. Thank you! ben

Hey, can I use a variable function in the setTimeout? Using the back-quotes doesn’t seem to work. Is there a different formatting style? This is what I have at the moment.




<<link>><<run setTimeout(function () { $.wiki('<<audio "berlin" play>>'); }, `(52/$wps)`*1000)>><</link>>

Which just plays the track without a delay. Whereas this works:

<<link>><<run setTimeout(function () { $.wiki('<<audio "berlin" play>>'); }, 7*1000)>><</link>>

Simply remove the back-quotes. You don’t need them there.

Amazing! Thank you! You wouldn’t have any insight into why the setTimout function is temperamental when it comes to triggering audio tracks on mobile devices? (It’s a subject of another thread I have going here. Any help would be super appreciated—am I going to need to make a desktop version of this project, and then a mobile one…?

I already replied there.