How To

Interrupt

3

McPepic 2022-11-06 20:02

So I was working on my recreation of Megalovania and I’m wondering if anyone knew a good way to run the music and game logic at the same time.

I’ve tried running the game in the music loop, but that looks really choppy.
I’ve also tried converting the music and running the logic during vertical blanking, but for some reason, it causes problems with the music.

Also, while I’m asking for help, does anyone know how I could get the music to loop?

Any ideas would be helpful. :)


nathanielbabiak 2022-11-06 21:42 (Edited)

On your first subject, music handling... Check out the LowRes NX manual for "=MUSIC".

Start your music with:

MUSIC [p]

Then, to keep your music going, use:

ON VBL CALL MUSIC_HANDLE
SUB MUSIC_HANDLE
IF MUSIC(P)=0 THEN MUSIC [p]
END SUB

On your second subject, getting the music to loop... Check out manual for "The Pattern Editor". It mentions a toggle button for the "loop" and "stop" flags for the current pattern.


McPepic 2022-11-06 22:47 (Edited)

@nathanielbabiak

I probably should have clarified. The program uses a MIDI file and plays using SP4CEBAR's MIDI tool in play mode.

The program reads straight from the hex data and plays the notes in real-time. Because this all happens in a separate do-loop, I tried putting the game logic inside of that loop.

Because of the MIDI instructions though, it appeared really choppy. I then tried running the game logic during VBL, but then the MIDI tool started having problems managing the music.

I was wondering if there was some way to run both the game logic and music without these weird problems.


was8bit 2022-11-07 03:26

Run with debug mode on and see if you are pegging your processor.... if so then this will cause your graphics to act oddly as if the code processing exceeds the 1/60th of a second it forces a screen update whether your code is wanting it or not...


was8bit 2022-11-07 03:27 (Edited)

Also, not sure if wait vbl is in the player code but however it is monitoring the timing maybe trim that down, but that might mess up the music...


nathanielbabiak 2022-11-07 04:03 (Edited)

The MIDI tools code is tough to incorporate into another program. It'd be easier if SP4CEBAR modified the code in the following ways:

And, as was8bit mentioned, you'd need to check for exceeding the 1/60th of a second. After SP4CEBAR's modifications, you could do this by checking the debug CPU in this code:

DO
  CALL MAIN_GAME
  CALL MAIN_MIDI
  WAIT VBL
LOOP

You'd just comment-out one CALL instruction, and you can see what amount of CPU is left over for the other CALL instruction (when it's not commented-out).


McPepic 2022-11-07 18:32

I tried putting the midi instructions into a subroutine (to access variables outside) and put both the game logic and music logic into a single do-loop. This showed that the way it was set up was just too slow.

Because of this, I went into the music logic and found out that the way it times the sound is by using a wait command. So instead, I had it return immediately from the subroutine so it could continue running the logic and it worked!

Thanks guys.


nathanielbabiak 2022-11-07 19:18 (Edited)

Yeah I would’ve suggested you resolve it without help from SP4CEBAR, but I saw all those WAIT VBL instructions and that WAIT 4 instruction, and figured it just wouldn’t work so easy. Glad it did though! Good work!


was8bit 2022-11-07 19:42

Coolio :)


McPepic 2022-11-07 19:45

There were some issues with the music and I realized it was the WAIT VBL instruction that I needed for the game logic. I figured out a way to only run game logic once per frame without holding up the music though. :)

All that's left on that front is getting the music to loop now.


nathanielbabiak 2022-11-07 19:57 (Edited)

Hey since you know the music won't be changing, couldn't you just hard-code the TIMER when it needs to loop?

Run this and make note of the TRACE value when the music stops.

TIME_START = TIMER
CALL START_MIDI
DO
  CALL MAIN_GAME
  CALL MAIN_MIDI
  WAIT VBL
  TRACE TIME_START - TIMER
LOOP

Then, set the ALARM value here, replace 1234:

ALARM = TIMER
DO
  IF TIMER >= ALARM THEN
    CALL START_MIDI
    ALARM = TIMER + 1234
  END IF
  CALL MAIN_GAME
  CALL MAIN_MIDI
  WAIT VBL
LOOP


SP4CEBAR 2022-11-09 15:36 (Edited)

@mcpepic
When the midi tool reaches the end of a file the program will exit its loop, to start it again simply put the entire program in an infinite loop, or add a label at the start of the program and a goto to that label at the end of the program

The converter converts the tracks one by one, if you want them to play at the same time, then you'd need to use Sound Composer or any other music editor to arrange the tracks so that they play at the same time, but that may not even work because the quantization may have caused an offset between the tracks, the Sound Composer doesn't can't shift notes, so currently the best way to do it would be to manually remove or add some bytes to the tracks until it's shifted just right


McPepic 2022-11-09 16:43

@SP4CEBAR
Wait, are you saying I’ve been using the converter wrong the whole time?


SP4CEBAR 2022-11-09 20:56

The converter is very primitive, it doesn't have any code to make multiple tracks play together in the NX music file
if you only wanted a single track to play then the converted music file would be good enough
Also, the MIDI file could contain more notes than what would fit in an NX music file


SP4CEBAR 2023-10-04 20:40 (Edited)

@mcPepic if you are still working on this project, then you could call your game function from the MIDI_VISUALIZERS subprogram of the new midi API

EDIT: you should use the GRAPHICS subprogram instead, this subprogram was added in a newer version and it is called every frame (as long as the CPU isn't at 100%)


Log in to reply.