Help with music speeding up with level timer

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
AlphaBlue1011
Bob-Omb
Bob-Omb
Posts: 24
Joined: Sun Feb 02, 2025 3:00 pm
Flair: never underestimate hyperfixations :3
Pronouns: she/her or they/them

Help with music speeding up with level timer

Postby AlphaBlue1011 » Sun Feb 02, 2025 3:11 pm

I'm quite new to SMBX2, and am currently trying to figure out how to make music speed up once 100 seconds are left on the timer, similarly to basically any Mario game with a level timer. I plan on doing this for music in ogg format, and game music emulator formats like spc and vgm. Some code examples for doing that with both would be appreciated! ^v^

deice
Volcano Lotus
Volcano Lotus
Posts: 590
Joined: Fri Jul 23, 2021 7:35 am

Re: Help with music speeding up with level timer

Postby deice » Tue Feb 04, 2025 6:37 am

create a file in your level folder (the one where all the custom graphics go) called luna.lua, open it in a text editor, and paste the following inside:

Code: Select all

local speedUpValue = 1.1 -- change this number to set how fast the song becomes

function onTick()
	if(Timer.isActive() and Timer.getValue() == 100) then
		if(Audio.MusicGetSpeed() <= 1 and Audio.MusicGetSpeed() ~= -1) then
			Audio.MusicSetSpeed(Audio.MusicGetSpeed() * speedUpValue)
		elseif(Audio.MusicGetTempo() <= 1 and Audio.MusicGetTempo() ~= -1) then
			Audio.MusicSetTempo(Audio.MusicGetTempo() * speedUpValue)
		end
	end
end
note that this might not work for absolutely every possible format, but it should work for at least every format which has a "tempo" argument listed here, as well as ogg files.

if you want this to apply to every level in an episode, create the luna.lua file in your episode folder (the one where your world file is) instead

AlphaBlue1011
Bob-Omb
Bob-Omb
Posts: 24
Joined: Sun Feb 02, 2025 3:00 pm
Flair: never underestimate hyperfixations :3
Pronouns: she/her or they/them

Re: Help with music speeding up with level timer

Postby AlphaBlue1011 » Sat Feb 08, 2025 8:19 pm

tysm! i'm very much a lunalua noob, so i appreciate the help. i'll be sure to credit you for it!
i'd like to ask though, would it be possible to change the speed without changing the pitch? normally, mario games don't change the pitch.
EDIT: after a bit of research, i found out that changing just the tempo for ogg is not possible. a bit disappointing, but it's just a limitation. i'm fine with it though, it's not a deal breaker. ¯\_(ツ)_/¯

deice
Volcano Lotus
Volcano Lotus
Posts: 590
Joined: Fri Jul 23, 2021 7:35 am

Re: Help with music speeding up with level timer

Postby deice » Sun Feb 09, 2025 5:22 am

AlphaBlue1011 wrote:
Sat Feb 08, 2025 8:19 pm
EDIT: after a bit of research, i found out that changing just the tempo for ogg is not possible. a bit disappointing, but it's just a limitation. i'm fine with it though, it's not a deal breaker. ¯\_(ツ)_/¯
keep in mind that it might be possible to emulate it by calling Audio.MusicSetPitch() and setting the song to a lower pitch after speeding it up, but i'm not exactly sure how all the math works out in regards to it as i haven't experimented with it myself, so i didn't include it in the code. i encourage trying to test it by yourself if you have the time

AlphaBlue1011
Bob-Omb
Bob-Omb
Posts: 24
Joined: Sun Feb 02, 2025 3:00 pm
Flair: never underestimate hyperfixations :3
Pronouns: she/her or they/them

Re: Help with music speeding up with level timer

Postby AlphaBlue1011 » Wed Feb 12, 2025 1:30 pm

I was experimenting, but it didn't work out. Essentially, the music is being sped up by an additional +0.15 (MusicGetSpeed() + 0.15), which works fine. but when I try to lower the pitch by 0.15 (MusicGetPitch() - 0.15), it doesn't do anything. What I did instead was speed it up by +0.15, but also switch the song to an identical version but with a -15% pitch change that I did with Audacity, so it'll basically be the same thing I was trying to do but done in a different way.
also, I changed the "Timer.getValue() == 100" to "Timer.getValue() <=100" so if the level starts with less than 100 seconds, which is what I'm doing for a remake of NSMB2's Coin Rush since some levels start with less than 100 seconds, it'll still do the adjustments. The == only does its job if it's AT 100 seconds, not lower. I'm just using my very minimal knowledge from a few google searches.


Return to “LunaLua Help”

Who is online

Users browsing this forum: Petal [Bot] and 0 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari