Page 1 of 1

maxmod program hangs after pressing a key

Posted: Tue May 07, 2019 8:28 pm
by snoosdoot
read title

im sure what is wrong looks really obvious but im a noob so i dont see it

[mod edit: add code]

Code: Select all

#include <nds.h>
#include <maxmod9.h>
#include <stdio.h>

#include "soundbank.h"
#include "soundbank_bin.h"

int main() {

	consoleDemoInit();
	keyboardDemoInit();
	mmInitDefaultMem((mm_addr)soundbank_bin);
	int loadedsound = -1;
	// load the module
	//mmLoad( MOD_BB );

	// load sound effects

	// Start playing module
	//mmStart( MOD_BB, MM_PLAY_LOOP );
	
	while(1) {
		swiWaitForVBlank();
		scanKeys();
		int pressed = keysDown();
		//if(pressed & KEY_START) break;
		if(pressed & KEY_START){
			loadedsound = MOD_ENIGMA;
			printf("Key pressed");
			mmStop();
			if(loadedsound != -1) mmUnload(loadedsound);
			mmLoad(MOD_ENIGMA);
			mmStart(MOD_ENIGMA, MM_PLAY_LOOP);
		}
		if(pressed & KEY_SELECT){
			loadedsound = MOD_BB;
			printf("Key pressed");
			mmStop();
			if(loadedsound != -1) mmUnload(loadedsound);
			mmLoad(MOD_BB);
			mmStart(MOD_BB, MM_PLAY_LOOP);
		}
	}

}
https://www.dropbox.com/s/ytpemyssacgecdc/arm9.7z?dl=0 here's download

(press start or select to change song, also desmume doesnt detect the key presses so use melonds or real hardware)

Re: maxmod program hangs after pressing a key

Posted: Wed May 08, 2019 6:52 pm
by WinterMute
I've edited your post to add the source code in question - for small chunks like this it can be easier to look at code in place rather than grabbin an archive & extracting it in order to compile locally.

The most immediately obvious problem is this :-

Code: Select all

			loadedsound = MOD_ENIGMA;
			printf("Key pressed");
			mmStop();
			if(loadedsound != -1) mmUnload(loadedsound);
			mmLoad(MOD_ENIGMA);
			mmStart(MOD_ENIGMA, MM_PLAY_LOOP);
You're setting loadedsound before you test it and therefore attempting to unload a module that isn't loaded. Ideally this shouldn't hang but it is clearly an error. You should set loadedsound after mmStart.

Any chunk of code that differs by only one parameter is also a good candidate for a separate function.

Re: maxmod program hangs after pressing a key

Posted: Fri May 10, 2019 3:28 pm
by snoosdoot
WinterMute wrote:
Wed May 08, 2019 6:52 pm
I've edited your post to add the source code in question - for small chunks like this it can be easier to look at code in place rather than grabbin an archive & extracting it in order to compile locally.

The most immediately obvious problem is this :-

Code: Select all

			loadedsound = MOD_ENIGMA;
			printf("Key pressed");
			mmStop();
			if(loadedsound != -1) mmUnload(loadedsound);
			mmLoad(MOD_ENIGMA);
			mmStart(MOD_ENIGMA, MM_PLAY_LOOP);
You're setting loadedsound before you test it and therefore attempting to unload a module that isn't loaded. Ideally this shouldn't hang but it is clearly an error. You should set loadedsound after mmStart.

Any chunk of code that differs by only one parameter is also a good candidate for a separate function.
thanks :D it worked