Objective C中的简单midi文件编写器

时间:2014-01-07 01:56:43

标签: ios objective-c hex midi

我正在用Objective C编写一个程序来生成一个MIDI文件。作为一个测试,我要求它写一个播放一个音符的文件,并在之后停止三角形音符。

但是我试图用Logic和Sibelius打开它,他们都说文件已损坏。

这是文件的十六进制读数..

4D 54 68 64   00 00 00 06   00 01 00 01   00 40   - MThd header

4D 54 72 6B   00 00 00 0D      - MTrk - with length of 13 as 32bit hex [00 00 00 0D]


81 00   90 48 64   82 00    80 48 64  - the track
delta    noteOn    delta    noteOff 


FF 2F 00                       - end of file

这是我编写增量时间的例程,并写下注释 -

- (void) appendNote:(int)note state:(BOOL)on isMelody:(BOOL)melodyNote{           // generate a MIDI note and add it to the 'track' NSData object
char c[3];

if( on ){
    c[0] = 0x90;
    c[2] = volume;
} else {
    c[0] = 0x80;
    c[2] = lastVolume;
}
c[1] = note;

[track appendBytes:&c length:3];

}

- (void) writeVarTime:(int)value{                       // generate a MIDI delta time and add it to the 'track' NSData object
char c[2];
if( value < 128 ){
    c[0] = value;
    [track appendBytes:&c length:1];
} else {
    c[0] = value/128 | 0x80;
    c[1] = value % 128;
    [track appendBytes:&c length:2];
}

}

有没有聪明的MIDI专家知道这个MIDI文件有什么问题?

1 个答案:

答案 0 :(得分:1)

缺少EOF事件的增量时间。