使用MusicSequenceFileCreate()将MusicSequence保存到.mid文件

时间:2012-07-16 03:06:14

标签: ios midi audiotoolbox

我目前正在开发一款需要根据用户输入创建MusicSequence的应用,而我目前在MusicSequenceFileCreate()上收到-1错误。 -1并未在MusicSequence参考中列为错误 - 我使用错误的格式打印OSStatus吗?

非常感谢任何帮助!我没有能够在网上找到很多关于将MusicSequence保存到.mid文件的参考资料......

我在最后收到错误。

OSStatus status = 0;

MusicSequence newSeq;
status = NewMusicSequence(&newSeq);
if(status){
    printf("Error new sequence: %ld\n", status);
    status = 0;
} else {
    MusicSequenceSetSequenceType(newSeq, kMusicSequenceType_Seconds);
}

MusicTrack tempoTrack;
status = MusicSequenceGetTempoTrack(newSeq, &tempoTrack);
if(status){
    printf("Error getting tempo track: %ld\n", status);
    status = 0;
}
status = MusicTrackNewExtendedTempoEvent(tempoTrack, 0, 120);
if(status){
    printf("Error adding tempo to track: %ld\n", status);
    status = 0;
}

MusicTrack thisTrack;
status = MusicSequenceNewTrack(newSeq, &thisTrack);
if(status){
    printf("Error adding main track: %ld\n", status);
    status = 0;
}

for(int i = 0; i<convertThis.melodyPoints.count; i++) {
    MIDINoteMessage thisMessage;
    thisMessage.note = thisNote.midiNoteNumber;
    thisMessage.duration = thisNote.duration;
    thisMessage.velocity = 120;
    thisMessage.releaseVelocity = 0;
    thisMessage.channel = 1;
    status = MusicTrackNewMIDINoteEvent(thisTrack, thisNoteBegin, &thisMessage);
    if(status){
        printf("Error adding midi note: %ld\n", status);
        status = 0;
    }
}

NSURL *thisurl = [NSURL URLWithString:[@"~/Documents" stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mid", convertThis.title]]];
status = MusicSequenceFileCreate(newSeq, (__bridge CFURLRef) thisurl, kMusicSequenceFile_MIDIType, kMusicSequenceFileFlags_EraseFile, 0);
if(status != noErr){
    printf("Error on create: %ld\n", status);
    status = 0;
}

1 个答案:

答案 0 :(得分:3)

访问您的文档目录

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

如果您想在其中创建一个用于存储MIDI的文件夹,您需要首先检查它是否有效,或者像Create a folder inside documents folder in iOS apps

那样创建它

将字符串添加到CFURLRef以直接在MusicSequenceFileCreate中使用

NSString *midiPath = [documentsDirectory
                               stringByAppendingPathComponent:[NSString stringWithFormat:@"/MIDIs/%@.mid",convertThis.title]];
CFURLRef midiURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:midiPath];

然后只是

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