Castle Paradox Forum Index Castle Paradox

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Gamelist   Review List   Song List   All Journals   Site Stats   Search Gamelist   IRC Chat Room

Music - game synch

 
Post new topic   Reply to topic    Castle Paradox Forum Index -> Creative Corner
View previous topic :: View next topic  
Author Message
Bagne
ALL YOUR NUDIBRANCH ARE BELONG TO GASTROPODA




Joined: 19 Feb 2003
Posts: 518
Location: Halifax

PostPosted: Thu Oct 22, 2009 2:33 pm    Post subject: Music - game synch Reply with quote

How does the timing for midi music work w.r.t. ticks in a plotscript?

Can they be synchronized? I remember somehow concluding that this was not possible using BAM ... I forget why.

What about MIDI?
Does the timing always depend on processor speed, stuff on the map etc. etc. etc?
Or are our machines nowadays fast enough to be reasonably consistent?
_________________
Working on rain and cloud formation
Back to top
View user's profile Send private message
Bagne
ALL YOUR NUDIBRANCH ARE BELONG TO GASTROPODA




Joined: 19 Feb 2003
Posts: 518
Location: Halifax

PostPosted: Thu Oct 22, 2009 2:35 pm    Post subject: Reply with quote

Oh, and what about ticks in a tile animation?
Those would be the same as ticks in a plotscript, neh?


I always thought it would be cool to get some tiles moving in time to the music.
_________________
Working on rain and cloud formation
Back to top
View user's profile Send private message
Bob the Hamster
OHRRPGCE Developer




Joined: 22 Feb 2003
Posts: 2526
Location: Hamster Republic (Southern California Enclave)

PostPosted: Thu Oct 22, 2009 2:51 pm    Post subject: Re: Music - game synch Reply with quote

Accurate music timing is not possible in the OHRRPGCE. You can get your timing reasonably close, but it is not possible to know exaclty which note will play on which game tick. This affects all music formats, BAM, MIDI, OGG, MP3, MOD, whatever. The reason is that music is played in a separate thread from the rest of the engine, so things that slow down the engine do not slow down the game. There are possibly some things we could do to improve the situation for future versions, but certainly nothing that will be ready for the next release.

As for making your timing match more closely with the music, your best bet is probably to rely on the "milliseconds" command instead of wait commands. "milliseconds" reads the system timer, which will remain accurate even if some other factors slow down the game engine.

Maybe something like

Code:

script, wait milliseconds, ms delay, begin
  variable(start ms)
  start ms := milliseconds
  while(milliseconds <= start ms + ms delay) do(
    wait(1)
  )
end


The above example will still not be perfectly accurate because it will be limited to the resolution of a game tick, but it should at least be more accurate than a regular wait, so it will probably do a better job of lining up with the music.

Bagne wrote:
Oh, and what about ticks in a tile animation?
Those would be the same as ticks in a plotscript, neh?

I always thought it would be cool to get some tiles moving in time to the music.


Yes, ticks in a tile animation are exactly the same as ticks in a plotscript.

I guess you could leave the animation pattern blank, and then impelment your own tile animation pattern using the "set tile animation offset" command together with the "wait milliseconds" script I posted above, and try to make it match the time signature of your music. I am not sure how well it would work in practice...
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Pepsi Ranger
Reality TV Host




Joined: 05 Feb 2003
Posts: 493
Location: South Florida

PostPosted: Thu Oct 22, 2009 6:33 pm    Post subject: Reply with quote

I think it's time to reveal one of the The Adventures of Powerstick Man: Extended Edition secrets.

I have three sequences that require and utilize musically synchronized scripts. TMC wrote these scripts for me to make it work:

Code:
global variable (1,waitsecs counter)

script, waitsecs start, begin
waitsecs counter := milliseconds
end

script, waitsecs, ticks, begin
#waitsecs counter += ticks * 56 + 12
waitsecs counter += ticks * 57
while (milliseconds <= waitsecs counter) do (wait)
end

script, waitsecs2, ticks, begin
waitsecs counter += ticks * 56 + 12
#waitsecs counter += ticks * 57
while (milliseconds <= waitsecs counter) do (wait)
end


It works like this. At the top of your script, you'll want to call "waitsecs start" so that it knows you're gonna use the waitsecs command as ticks. If you leave it out, the script won't know what you're talking about. You need to do this for every instance you use waitsecs.

Example (from the infamous "Serenade"):

Code:
script,Serenade,begin
wait (30)
show text box (2353)
wait for text box
stop sound (112)
stop sound (113)
stop sound (116)
stop sound (122)
play song (55)

waitsecs start
waitsecs (50)

set hero picture (0,255)
set hero direction (0,north)
set hero frame (0,0)
wait (2)
set hero frame (0,1)
wait (2)
set hero direction (0,east)
set hero frame (0,0)
waitsecs (181)
suspend box advance
show text box (2354)
waitsecs (51)
advance text box
set hero frame (0,1)
show text box (2355)
waitsecs (51)
advance text box
.
.
.
end


The thing to take away from this example is that waitsecs relies on milliseconds, not ticks, which are prone to timing problems depending on the speed of the computer. So, every time you run the script, the timing will be identical as the last time you tested it (or close enough to identical that you won't notice if it's off).

I still use "wait" wherever the timing isn't critical. But whenever you see "waitsecs," that means something is changing in tune with the verse that's onscreen. The people who have playtested this script can attest to its accuracy.

You'll notice that I have two versions of "waitsecs." TMC wasn't sure which one would suit my needs more, so he gave me two equations. It turned out that both were needed.

I use "waitsecs" for dialogue triggers and "waitsecs2" for animations (which I didn't paste into this example, but I used it primarily for the air guitar sequence halfway through the song).

TMC can explain the timing if you need him to. But the main thing is that these scripts have worked well for me. The hard part is figuring out how many doctored ticks you need for each sequence. I believe the equations he made equal exactly 18 ticks to a second.

Let me know (and TMC know) if this works for you.
_________________
Progress Report:

The Adventures of Powerstick Man: Extended Edition

Currently Updating: General sweep of the game world and dialogue boxes. Adding extended maps.

Tightfloss Maiden

Currently Updating: Chapter 2
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
TMC
On the Verge of Insanity




Joined: 05 Apr 2003
Posts: 3240
Location: Matakana

PostPosted: Fri Oct 23, 2009 3:58 am    Post subject: Reply with quote

The improvement of waitsecs over James' script is that small errors do not build up over time: the timing is always as good as possible (to within one game tick).

To anyone wondering why there are two scripts, it is because Pepsi had already synchronised some music when I realised that my script was flawed. So I added and tweaked some constant to it so that I could fix the flaw but still stay close enough to the old version to not break the synchronisation. Just ignore waitsecs2 and use waitsecs.

I can not remember why waitsecs uses 57ms a 'tick', which is 17.5 ticks a second when the OHR uses 18.2 ticks a second. Probably the same reason as above. Anyway you can easily modify the script to count in any units you want.
_________________
"It is so great it is insanely great."
Back to top
View user's profile Send private message Send e-mail
Bagne
ALL YOUR NUDIBRANCH ARE BELONG TO GASTROPODA




Joined: 19 Feb 2003
Posts: 518
Location: Halifax

PostPosted: Fri Oct 23, 2009 5:09 am    Post subject: musical timing Reply with quote

Thanks -
I'll give those a shot.

So that's your visual timing ...
How did you find the landmarks in the music?
Did you just jump into a midi player and find that ...
say ...
There's an important chord at 13.54 seconds - so, that's when I trigger X ?
Or did you manage to assign a length of time to a BAM 1/16 note?
_________________
Working on rain and cloud formation
Back to top
View user's profile Send private message
TMC
On the Verge of Insanity




Joined: 05 Apr 2003
Posts: 3240
Location: Matakana

PostPosted: Fri Oct 23, 2009 5:40 am    Post subject: Reply with quote

There's a few alternatives. First is trial and error (and probably most important).

I guess you could work out times like you described. However there might be some slight unaccounted lag time when actually playing the music ingame.

Another, you can write a script that starts the music and listens for keypresses, triggering the next event and writing waitsecs calls to g_debug.txt (using trace) when you press a key; these will be rough but you can then clean them up (using trial and error)

You could maybe even insert SYSEX events into a MIDI which set global variables when run in music_native (not sure if these actually work), and run a script which waits for the global to change and then again write to g_debug.txt as above.
_________________
"It is so great it is insanely great."
Back to top
View user's profile Send private message Send e-mail
Camdog




Joined: 08 Aug 2003
Posts: 606

PostPosted: Fri Oct 23, 2009 6:23 am    Post subject: Reply with quote

This thread got me thinking...

Would it be possible to include a plotscript command in later versions that returned the number of milliseconds into the current song being played? That way, while it will still be a bit of hack to get the music to sync with the game, perhaps it would be possible to get the game to sync with the music?
Back to top
View user's profile Send private message
TMC
On the Verge of Insanity




Joined: 05 Apr 2003
Posts: 3240
Location: Matakana

PostPosted: Fri Oct 23, 2009 6:46 am    Post subject: Reply with quote

Our music libraries/backends are very inconsistent feature-wise. I don't like SDL_mixer.
I guess we could manually track the number of milliseconds, but that would actually be nearly identical to doing it via script.
Hey, wow, SDL_Mixer has a SetMusicPosition function, great (but no way to actually GET the music position). I think someone requested that feature but we thought it was impossible. But it's a nuisance that most of the API is not documented!
_________________
"It is so great it is insanely great."
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Castle Paradox Forum Index -> Creative Corner All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group