Best way to fade in/out audio in Sugarcube?

Twine 2, Sugarcube 2.31.1

Hey awesome people.

So I’m trying to figure out the best way to fade out sounds from previous passages and start playing the sounds in a new passage. Currently, I have this code which works fine for individual audio tracks.

:: PassageReady 
	
		<<if def $music>> 
			/*remember what WAS just playing */
			<<set _oldmusic to $music>> 
	
			/*fade out and then stop what WAS playing */
			<<audio _oldmusic fadeoverto 2 0>>
			<<timed 2s>>  
				<<audio _oldmusic stop>>
			<</timed>>
			
		<</if>>

:: passage 1
	<<set $music to "test1">>
	<<audio $music volume 1 play>>

:: passage 2
	<<set $music to "test2">>
	<<audio $music volume 1 play>>

Is there a way to make this kind of thing work with playlists too?
I essentially need a way to say “everything that IS playing fade out, THEN stop” and meanwhile start playing the new passages track. I tried using

<<audio ":playing:not($music)" stop>>

but couldn’t get that to work.

Any ideas? :stuck_out_tongue:

Did you mean actual playlists, controlled via <<playlist>> or simply multiple individual tracks?

If you did mean actual playlists, then you could do it similarly to the example you showed above for individual tracks. For example:

:: PassageReady 
<<if def $music>> 
	/* Fade out and then stop the currently playing track. */
	<<capture $music>>
		<<audio $music fadeoverto 2 0>>
		<<timed 2s>>
			<<audio $music stop>>
		<</timed>>
	<</capture>>
<</if>>

<<if def $playlist>> 
	/* Fade out and then stop the currently playing playlist. */
	<<capture $playlist>>
		<<playlist $playlist fadeoverto 2 0>>
		<<timed 2s>>
			<<playlist $playlist stop>>
		<</timed>>
	<</capture>>
<</if>>


:: passage 1
<<set $music to "test1">>
<<audio $music volume 1 play>>
<<set $playlist to "listtest1">>
<<playlist $playlist volume 1 play>>


:: passage 2
<<set $music to "test2">>
<<audio $music volume 1 play>>
<<set $playlist to "listtest1">>
<<playlist $playlist volume 1 play>>

NOTE: If you use <<capture>> here, you don’t need the temporary variable.

 

That will depend on what “everything” happens to be.

For individual tracks, the former is easy and the latter can be accomplished either with a manual <<timed>> kludge, similar to what you’re doing now, or automatically via a little JavaScript.

For actual playlists, you’ll have to do pretty much what you’re doing now (as seen in my example above).

 

Selectors are not interpolated, so that’s the issue here. You’d want something like:

<<audio `":playing:not(" + $music + ")"` stop>>

That said, that may not be what you end up wanting/needing to do—depending on if you’re actually using playlists and/or multiple individual tracks.

1 Like

Ah that’s perfect and means I can use any mix of audio or playlists! It’s incredible how satisfying it is now navigating between passages and hearing the smooth audio transitions!

Capture - of course! I’m using it in loops now, but I didn’t realise how helpful it could be for this kind of use case too.

Thank you so much!!