0 votes
by (290 points)

Hey everyone, you all have been extremely helpful as I put everything together, but I have one more issue I was hoping you might help with.

My game has a mechanic where as you complete certain passages, different layers of background music fade in. Some of these require fairly precise timing, and I've encountered an issue where occasionally the loop would start slightly late and the beat of the music would not line up, causing the music to sound sort of messy and incorrect.

I'm achieving the effect by having all of the loops started at the top of the game, just with the audio silenced like:

<<audio "Drums" play loop volume 0>>

And then when the player completes a section, the music fades in:

<<audio "Drums" fadeoverto 5 0.75>>

However this is setup is fairly finicky and will occasionally not work as intended. Within the Twine program itself, I was able to solve the issue with the <<waitforaudio>> macro, but whenever I export as an .html file or upload the game to a host online, the timing problem returns.

Any ideas on how I might be able to get this to work?

Currently using Twine Version 2.2.1 and Sugarcube 2.21.0.

Really appreciate you guys' fantastic insight as I figure all this out. Thank you!

2 Answers

+1 vote
by (63.1k points)
selected by
Best answer
To my knowledge, there's no way to sync up tracks in a way that will produce perfect results without either a whole lot of code or a library. The web audio API can probably handle this, but it will never produce consistent results without you driving into the code yourself.
+2 votes
by (1.1k points)

Actually (no offense Chapel) I have got one that TME coded for me like a year or two ago that might be perfect. It is for a fight sequence and it plays battle music until someone gets beat down. Then that one fades away as the loser requiem begins.


Is this what you mean? They fade together flawlessly. I'm no audiophile, but it seems flawless at any rate. I'm not saying CDs==records... but I mean... close is close.

If you think this will help I can give you the entire code set but it is playable as he wrote it here

Today i use it as part of a larger construct.

by (290 points)
Ah interesting, thanks to both you and Chapel!

Taking a look at your game's code and I'm not seeing any special audio commands--looks like one theme fades out and then another plays when you win the fight. They fade together super well, but I think that may be because the "win" theme is not as rhymthic as the "fight" theme so it doesn't sound off during the time where one is playing on top of the other regardless of where in the track the clip is triggered.

If I were starting over from scratch it would be smart to do something similar to this, but unfortunately I came up with this whole thing back when I didn't know what Twine was/wasn't quite capable of. My needs are a little more complicated, where I'm hoping to have looped layers of the soundtrack playing on top of each other where the rhythm lines up--basically building a track as you play the game, like as you complete a room a drum track will fade in, then you beat another and a bassline will fade in, etc.

I think Chapel may be right that to do what I'm specifically looking to do, it'd take some fairly complicated code to get the audio playing exactly right every time.

After a little playing around with different hosting possibilities, it seems like itch.io works most consistently, though it varies from browser to browser (Chrome works best for me so far). I think this may be the best I'm able to do at the moment with my limited coding knowledge.

Thanks again for your help! Definitely got me thinking about how to put together my next one...
by (2.3k points)
I don't suppose you still have the code laying around? I'm having difficulties getting my SC 2.2.1 music working properly....
by (1.1k points)
Yes. I sure do. Just click on the link above in my answer (the word *here*).

If that does not work I can send you it some other way.