我正在尝试使用sendMIDISysExEvent:(NSData *)midiData
作为在音乐应用中重新调整少量MIDI音符的方法。我希望ViewController
向Synth
类发送MIDI消息,以更改10个音符的调音。用于重新调整一个音符的标准MIDI系统专用消息格式如下所示
F0 7F <device ID> 08 02 tt ll [kk xx yy zz] F7.
legend: tt ll [kk xx yy zz]
(NB - 与MIDI 1.0详细规范4.2,第49页略有不同)
使用typedef struct
,可以使用单个float
代替uint8_t
来表示调整频率,例如
PlayViewController.h
typedef struct
{
uint8_t SYSEX_SysexHeader; // oxF0h; // System Exclusive Header
uint8_t SYSEX_UniversalRealTimeHeader; // ox7Fh; // Universal RealTime Header
uint8_t SYSEX_myPhone; // ox00h; // ID of target device (e.g. iPhone)
uint8_t SYSEX_subID1; // ox08h; // sub-ID #1 (MIDI Tuning Standard)
uint8_t SYSEX_subID2; // ox02h; // sub-ID #2 (note change)
uint8_t SYSEX_tuningProgramNumber; // ox0h; // tuning program number (0 -127)
uint8_t SYSEX_numberOfKeys; // ox10h; // number of changes
uint8_t SYSEX_key0;
float TUNING_pitch_0;
uint8_t SYSEX_key1;
float TUNING_pitch_1;
uint8_t SYSEX_key2;
float TUNING_pitch_2;
uint8_t SYSEX_key3;
float TUNING_pitch_3;
uint8_t SYSEX_key4;
float TUNING_pitch_4;
uint8_t SYSEX_key5;
float TUNING_pitch_5;
uint8_t SYSEX_key6;
float TUNING_pitch_6;
uint8_t SYSEX_key7;
float TUNING_pitch_7;
uint8_t SYSEX_key8;
float TUNING_pitch_8;
uint8_t SYSEX_key9;
float TUNING_pitch_9;
uint8_t eox; // OxF7h; //
}
TuneEvent;
仍然在.h
typedef NS_ENUM(NSInteger, SYSEX)
{
SYSEX_SysexHeader = 240,
SYSEX_UniversalRealTimeHeader = 127,
SYSEX_myPhone = 0,
SYSEX_subID1 = 8,
SYSEX_subID2 = 2,
SYSEX_tuningProgramNumber = 0,
SYSEX_numberOfKeysToBeChanged = 1,
SYSEX_key0 = 61,
SYSEX_key1 = 62,
SYSEX_key2 = 63,
SYSEX_key3 = 64,
SYSEX_key4 = 65,
SYSEX_key5 = 66,
SYSEX_key6 = 67,
SYSEX_key7 = 68,
SYSEX_key8 = 69,
SYSEX_key9 = 70,
SYSEX_eox = 247
};
typedef NS_ENUM(NSInteger, TUNING)
{
TUNING_pitch0,
TUNING_pitch1,
TUNING_pitch2,
TUNING_pitch3,
TUNING_pitch4,
TUNING_pitch5,
TUNING_pitch6,
TUNING_pitch7,
TUNING_pitch8,
TUNING_pitch9
};
float TUNING_float(TUNING micro);
最后是浮点值... (thanks to this answer)
PlayViewController.m
float TUNING_float(TUNING micro)
{
switch (micro)
{
case TUNING_pitch0:
return 579.4618f;
case TUNING_pitch1:
return 607.0552f;
case TUNING_pitch2:
return 662.2421f;
case TUNING_pitch3:
return 708.2311f;
case TUNING_pitch4:
return 772.6157f;
case TUNING_pitch5:
return 809.4070f;
case TUNING_pitch6:
return 882.9894f;
case TUNING_pitch7:
return 910.5828f;
case TUNING_pitch8:
return 993.3631f;
case TUNING_pitch9:
return 1030.1540f;
default:
return 0.0f;
}
}
然而,当我阅读this answer时,我开始问我如何根据NSData
将发送的sendMIDISysExEvent:(NSData *)midiData
来准备MIDI数据包。并且this answer建议NSValue
作为封装C结构的更好方法,就像我正在尝试创建的那样,所以我很困惑。如果确实如此,我很困惑为什么Apple会推出像sendMIDISysExEvent:(NSData *)midiData
这样的方法。
我的问题是:基于消息格式(在我上面的代码中概述),我如何准备midiData
以便使用此方法发送它?
结论
为了回应接受的答案,“原始二进制数据”的长度,以字节数而不是数据类型定义,解释了它们之间的显着差异NSData
和NSValue
。这也表明sendMIDISysExEvent:(NSData *)midiData
仅用于处理数据字节,即uint8_t
而不是float.
换句话说,明智的选择是使用字节表示频率值,如下所示MIDI调音标准
yy = MSB of fractional part (1/128 semitone = 100/128 cents = .78125 cent units) zz = LSB of fractional part (1/16384 semitone = 100/16384 cents = .0061 cent units)
答案 0 :(得分:2)
NSData
和NSValue
都是&#34;原始二进制数据&#34;的包装,但具有不同的意图,并且在定义二进制数据的长度方面有不同的方式。
NSValue
目标,例如int
,float
(还有struct
),NSValue
的界面旨在通过&#34;数据的长度&#34;通过类型。因此,不可能在NSValue中封装可变长度的对象(如VLA或C字符串),因为数据的大小不能从类型信息(cf. NSValue)中派生出来:
您指定的类型必须是恒定长度。你不能存储C. 字符串,可变长度数组和结构,以及其他数据类型 在NSValue中的不确定长度 - 你应该使用NSString或 这些类型的NSData对象。
NSData
表示任意字节缓冲区的框。因此它有一个接口,您可以通过该接口定义&#34;原始二进制数据的长度&#34;就字节而言(不是类型)。
关于你的问题,由于sendMIDISysExEvent:(NSData *)midiData
中的MIDI接口需要NSData
- 对象,唯一(有意义的)方法是将对象作为NSData
- 对象传递。< / p>
希望它有所帮助。