无法从MusicSequence生成MIDI文件

时间:2018-07-17 05:47:07

标签: ios objective-c core-audio audiotoolbox coremidi

我正在尝试从简单的MusicSequence生成MIDI文件。这是我的代码:

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;
}

我的代码在MusicSequenceFileCreate处失败。

这是我得到的错误:

  

线程1:EXC_BAD_ACCESS(代码= EXC_I386_GPFLT)

我该如何克服?

1 个答案:

答案 0 :(得分:0)

这似乎是MIDI URL定义的错误。我将该行更改为此,现在可以使用。

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