iosobjective-ccore-audioaudiotoolboxcoremidi

Having trouble generating MIDI file from MusicSequence


I'm trying to generate a MIDI file from a simple MusicSequence. Here's my code:

MusicSequence mySequence;
MusicTrack myTrack;
NewMusicSequence(&mySequence);
MusicSequenceNewTrack(mySequence, &myTrack);

MIDINoteMessage noteMessage;
MusicTimeStamp timestamp = 0;
noteMessage.channel = 0;
noteMessage.note = 60;
noteMessage.velocity = 90;
noteMessage.releaseVelocity = 0;
noteMessage.duration = 4;

if (MusicTrackNewMIDINoteEvent(myTrack, timestamp, &noteMessage) != noErr) NSLog(@"ERROR creating the note");
else NSLog(@"Note added");

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                    NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *midiPath = [documentsDirectory
                    stringByAppendingPathComponent:@"testfile.mid"];
CFURLRef midiURL = (__bridge CFURLRef)([[NSURL alloc] initFileURLWithPath:midiPath]);

OSStatus status = 0;

status = MusicSequenceFileCreate(mySequence, midiURL, kMusicSequenceFile_MIDIType, kMusicSequenceFileFlags_EraseFile, 0);

if(status != noErr){
    printf("Error on create: %d\n", (int)status);
    status = 0;
}

My code fails at MusicSequenceFileCreate.

This is the error I get:

Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

How can I get past this?


Solution

  • This seemed to be an error with the definition of the MIDI URL. I changed that line to this and it works now.

    CFURLRef midiURL = (__bridge CFURLRef)([NSURL fileURLWithPath:midiPath]);