0
0

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://52.88.2.202/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.

fmod_play_midi.mm
[code:1nays721]

include "../inc/fmod.hpp"

include "../inc/fmod_errors.h"

include <stdio.h>

include <stdlib.h>

include <string.h>

include <Foundation/Foundation.h>

void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(-1);
}
}

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(&amp;system);
ERRCHECK(result);

result = system-&gt;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(&amp;soundExInfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
soundExInfo.cbsize   = sizeof(FMOD_CREATESOUNDEXINFO);

/*Specify the .dls file*/
NSString *dls_path = [[NSBundle mainBundle] pathForResource:@&quot;gs_instruments&quot; ofType:@&quot;dls&quot;];
soundExInfo.dlsname  = [dls_path cStringUsingEncoding:NSASCIIStringEncoding];

result = system-&gt;createSound(filename, FMOD_SOFTWARE | FMOD_CREATESTREAM, &amp;soundExInfo, &amp;sound);
ERRCHECK(result);

result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound, 0, &amp;channel);
ERRCHECK(result);

result = sound-&gt;release();
ERRCHECK(result);
result = system-&gt;release();
ERRCHECK(result);

return 0;

}
[/code:1nays721]

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!

  • You must to post comments
0
0

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;
while(IsPlaying)
{
channel->isPlaying(&IsPlaying);
system->update();
}
[/code:326dc94g]

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!

  • You must to post comments
0
0

It helps indeed :) exactly solving my problem. As you have mentioned, I may have to write something more sophisticated to control the playing and stopping.

Thanks a lot for you help!

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.