关于iOS CoreMIDI库中MIDIServices 相关API解读

关于iOS CoreMIDI库中MIDIServices 相关API解读

一.获取可用MIDI列表

// 获取可用MIDI列表的数量
ItemCount deviceCount = MIDIGetNumberOfDevices();
​
// 遍历MIDI列表,能够获取到设备
for (ItemCount i = 0 ; i < deviceCount ; ++i) {
 MIDIDeviceRef device = MIDIGetDevice(i);
}
​

二.获取设备相关信息

经过1中遍历MIDI列表,能够拿到设备的指针,即可获取到相关的midi信息

1.获ios是什么意思取MIDI设备相关信息

NSString *getPropatyString(MIDIObjectRef object, CFStringRef property )
{
  CFStringRef name = nil;
  if (noErr != MIDIObjectGetStringProperty(object, property, &name))
    return nil;
  return (NSString *)CFBridgingRelease(name);
}

kMIDIPropertyName

设备、实体人头攒动和端点都可能有称号。 显现端点称号的引荐办法指针万用表怎么读数是询问端点称号,假如该称号是仅有的,则仅显人头攒动的近义词现该称号。 假如它不是仅有的,则在设备称号前增加。

其他相关想要获取的能够查阅文档,罗列几个常用的

kMIDIPropertyManufacturer

kMIDIPropertyUniqueID

kMIDIios越狱PropertyDeviceID

kMIDIPropertyDisplayName

调用办法

​
getPropatyString(device, kMIDIPropertyManufacturer ) 
​

2.检查设备是否在线

SInt32 isOffline = 0;
MIDIObjectGetIntegerProperty(device, kMIDIPropertyOffline, &isOffline);

isOffline =指针数组和数组指针的区别= 0为离线

3.获取设备的实体数量人头攒动的近义词

// 获取设备的实体数量
ItemCount entityCount = MIDIDeviceGetNumberOfEntities(device);
​
// 遍历设备的实体数量
for (ItemCount j = 0 ; j < entityCount ; ++j) {
 MIDIEntityRef entity = MIDIDeviceGetEntity(device, j);
 // 同样能够获取实体的称号,能够调这个办法是应该他们都是指针目标
 getPropatyString(entity, kMIDIPropertyName) ;
 
}

3.1rtc是什么意思RTC取设备的源端口指针说漫数量

// 获取源端口的数量
ItemCount sourceCount = MIDIEntityGetNumberOfSources(entity);
for (ItemCount k = 0 ; k < sourceCount ; ++k) {
​
  MIDIEndpointRef source = MIDIEntityGetSource(entity, k);
   // 同样能够获取实体的称号
  getPropatyString(entity, kMIDIPropertyName) ;
}
​

3.2 获取设备人头攒动意图端口的数量

// 获取意图端口的数量
ItemCount destCount = MIDIEntityGetNumberOfDestinations(entity);
for (ItemCount k = 0 ; k < destCount ; ++k) {
   MIDIEndpointRef dest = MIDIEntityGetDestination(entity, k);
   // 同样能够获取实体的称号
  getPropatyString(entity, kMIDIPropertyName) ;   
}

4.直接获取体系源端指针万用表怎么读数

// 获取源端口的数量
ItemCount sourceCount = MIDIGetNumberOfSources();
  
for (NSInteger i = 0; i < sourceCount; i++) {
 MIDIEndpointRef endPointRef = MIDIGetSource(i);
  // 同样能够获取实体的称号
  getPropatyString(endPointRef, kMIDIPropertyName) ; 
}

三、创立MIDI Client 和Port

要使用 CoreMIDI,指针c语言应用程序指针式万用表会创立一个 MIDIClie闰土刺猹ntRef,它能够向让天秤倒追的星座其间增加MIDIPortRef,经过它能够发送和接纳 MIDI。

OSStatus err;
MIDIClientRef clientRef;
MIDIPortRef inputPortRef;
​
NSString *clientName = @"inputClient";
err = MIDIClientCreate((CFStringRef)CFBridgingRetain(clientName), NULL, NULL, &clientRef);
if (err != noErr) {
 NSLog(@"MIDIClientCreate err = %d", err);
 return ;
}
​
NSString *inputPortName = @"inputPort";
err = MIDIInputPortCreate(
 clientRef, (CFStringRef)CFBridgingRetain(inputPortName),
 MIDIInputProc, NULL, &inputPortRef);
if (err != noErr) {
 NSLog(@"MIDIInputPortCreate err = %d", err);
 return ;
}
​
ItemCount sourceCount = MIDIGetNumberOfSources();
​
NSLog( @"errsourceCount = %lu", sourceCount );
for (ItemCount i = 0; i < sourceCount; i++) {
 MIDIEndpointRef sourcePointRef = MIDIGetSource(i);
 err = MIDIPortConnectSource(inputPortRef, sourcePointRef, NULL);
 if (err != noErr) {
  NSLog(@"MIDIPortConnectSource err = %d", err);
  return ;
  }
}
​
static void
MIDIInputProc(const MIDIPacketList *pktlist,
       void *readProcRefCon, void *srcConnRefCon)
{
  MIDIPacket *packet = (MIDIPacket *)&(pktlist->packet[0]);
  UInt32 packetCount = pktlist->numPackets;
  
  for (NSInteger i = 0; i < packetCount; i++) {
    
    Byte mes = packet->data[0] & 0xF0;
    Byte ch = packet->data[0] & 0x0F;
    
    if ((mes == 0x90) && (packet->data[2] != 0)) {
      NSLog(@"note on number = %2.2x / velocity = %2.2x / channel = %2.2x",
         packet->data[1], packet->data[2], ch);
     } else if (mes == 0x80 || mes == 0x90) {
      NSLog(@"note off number = %2.2x / velocity = %2.2x / channel = %2.2x",
         packet->data[1], packet->data[2], ch);
     } else if (mes == 0xB0) {
      NSLog(@"cc number = %2.2x / data = %2.2x / channel = %2.2x",
         packet->data[1], packet->data[2], ch);
     } else {
      NSLog(@"etc");
     }
    
    packet = MIDIPacketNext(packet);
   }
}
​

总结

关于iOS CoreMIDI库中MIDIServices 相关API解读

发表回复

提供最优质的资源集合

立即查看 了解详情