A fmod newbie needs help here. Working on an iPhone app, I’m trying to use fmod to play MIDI files on iPhone following the example on wiki (http://184.108.40.206/wiki/index.php5?title=MIDI), and have managed to get the project compiling, but when I run my app, there is no sound and the app exits normally in no time. By stepping in the debugger, I see that the system->playSound() call returns instantly.
Here is my code in objective-c++, pretty much the piece available on wiki.
void ERRCHECK(FMOD_RESULT result)
if (result != FMOD_OK)
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
int fmod_play_midi(const char * filename)
FMOD_RESULT result = FMOD_OK;
FMOD::System *system = NULL;
FMOD::Sound *sound = NULL;
FMOD::Channel *channel = NULL;
FMOD_CREATESOUNDEXINFO soundExInfo; /* Global Settings */ result = FMOD::System_Create(&system); ERRCHECK(result); result = system->init(32, FMOD_INIT_NORMAL, NULL); ERRCHECK(result); /*Allocate memory for an instance of FMOD_CREATESOUNDEXINFO*/ /*This must be done to initialize the FMOD_CREATESOUNDEXINFO before midi playback*/ memset(&soundExInfo, 0, sizeof(FMOD_CREATESOUNDEXINFO)); soundExInfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); /*Specify the .dls file*/ NSString *dls_path = [[NSBundle mainBundle] pathForResource:@"gs_instruments" ofType:@"dls"]; soundExInfo.dlsname = [dls_path cStringUsingEncoding:NSASCIIStringEncoding]; result = system->createSound(filename, FMOD_SOFTWARE | FMOD_CREATESTREAM, &soundExInfo, &sound); ERRCHECK(result); result = system->playSound(FMOD_CHANNEL_FREE, sound, 0, &channel); ERRCHECK(result); result = sound->release(); ERRCHECK(result); result = system->release(); ERRCHECK(result); return 0;
The dls file "gs_instruments.dls" is the system default of MacOSX (/System/Library/Components/CoreAudio.component/Contents/Resources/gs_instruments.dls).
Am I doing anything wrong? I’ve read quite some posts on the forum matching the search string "MIDI" but with no luck. Any advice is appreciated, thanks!
- oxcsnicho asked 9 years ago
You’re playing the sound and then exiting immediately. System::playSound() starts the sound playing, but doesn’t wait for it to finish. It’s playing in the background (on another thread) while your program continues to run.
If you want to wait for the sound to finish in your program, you can try code like this:
[code:326dc94g]bool IsPlaying = true;
You have to be careful with that, though, because some music files (MODs and their family for sure, not sure about MIDI) have loops "baked" in (for lack of a better word), which means that they’ll never finish, which means that that loop won’t end.
You also ought to put in some sort of delay or sleep into the loop (I’m not sure what the system call is on iPhone).
But really, if this is going into a larger application, you don’t want your whole app to hang while your midi is playing. I would create audio init and shutdown functions which you can call on startup and shutdown respectively which create the system object. Then your fmod_play_midi() function can do the playSound() call, and the rest of your program can continue unhindered.
Hope that helps!
- Adiss answered 9 years ago
Please login first to submit.