2013-02-18 68 views
0

下面是c方法我得到notetype和notenumber我想要显示它在标签上。 实际上,我正在播放midi文件,下面的方法会返回midi文件数据,但是在clang方法中,但我想在标签上显示它。如何从c语言获取数据

static void MyMIDIReadProc(const MIDIPacketList *pktlist, 
          void *refCon, 
          void *connRefCon) { 


    AudioUnit *player = (AudioUnit*) refCon; 

    MIDIPacket *packet = (MIDIPacket *)pktlist->packet; 
    for (int i=0; i < pktlist->numPackets; i++) { 
     Byte midiStatus = packet->data[0]; 
     Byte midiCommand = midiStatus >> 4; 


     if (midiCommand == 0x09) { 
      Byte note = packet->data[1] & 0x7F; 
      Byte velocity = packet->data[2] & 0x7F; 

      int noteNumber = ((int) note) % 12; 

      NSString *noteType; 
      switch (noteNumber) { 

       case 0: 
        noteType = @"C"; 
        break; 
       case 1: 
        noteType = @"C#"; 
        break; 
       case 2: 
        noteType = @"D"; 
        break; 
       case 3: 
        noteType = @"D#"; 
        break; 
       case 4: 
        noteType = @"E"; 
        break; 
       case 5: 
        noteType = @"F"; 
        break; 
       case 6: 
        noteType = @"F#"; 
        break; 
       case 7: 
        noteType = @"G"; 
        break; 
       case 8: 
        noteType = @"G#"; 
        break; 
       case 9: 
        noteType = @"A"; 
        break; 
       case 10: 
        noteType = @"Bb"; 
        break; 
       case 11: 
        noteType = @"B"; 
        break; 
       default: 
        break; 
      } 

      NSLog(@"noteType : noteNumber %@",[noteType stringByAppendingFormat:[NSString stringWithFormat:@": %i", noteNumber]]); 
      ViewController* audio = (__bridge ViewController*)refCon; 
      [audio.self.noteDisplayLabel setText:@"sdasd"]; 
      audio.test_messages = @"sdsadsa"; 
      [audio labelText:@"asdasdas"]; 
      NSLog(@"%@", audio.test_messages); 
      OSStatus result = noErr; 
      // result = MusicDeviceMIDIEvent (player, midiStatus, note, velocity, 0); 
     } 
     packet = MIDIPacketNext(packet); 
    } 
} 
+0

上述方法产生了什么?你能打印票据吗? – Krumelur 2013-02-18 08:51:11

+0

而上面的函数是Objective-C,而不是C :) – Krumelur 2013-02-18 08:51:37

+0

Krumelur,从技术上讲,它是一个C函数,Objective C对象在...中,因此需要由ObjC++编译器编译。但我们离题了,呃;) – MOK9 2013-03-07 08:02:06

回答

0

您的NSLog邮件是否正常工作?看起来他们应该是。

从MIDI Read Proc设置视图并不是一个好习惯(甚至可能有问题),因为这是实时回调,并且您不想花时间在此处写入UI。

如果你把事件推到某个地方(比如数组)并且在结束函数中发送一个通知给你的View Controller(带有数组对象)会更好。你想从这个功能快速返回。

0

我还在我的这段代码中使用了这个代码,并使用NSNotification将它修改为在标签上显示注释。

static NSString* kNAMIDINoteOnNotification = @"kNAMIDINoteOnNotification"; 
static NSString* kNAMIDI_Note = @"kNAMIDI_Note"; 

...

case语句

NSMutableDictionary* info = [[NSMutableDictionary alloc] init]; 
[info setObject:[NSNumber numberWithInteger:note] forKey:kNAMIDI_Note]; 
NSNotification* notification = [NSNotification notificationWithName:kNAMIDINoteOnNotification object:nil userInfo:info]; 
[[NSNotificationCenter defaultCenter] postNotification:notification]; 

中显示的音符来监视通知

// notification to monitor for incoming midi note events 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(log:) name:@"kNAMIDINoteOnNotification" object:nil]; 


- (void)log:(NSNotification *)notification 
{ 

    [NSThread isMainThread]; 

    NSDictionary* info = notification.userInfo; 


    NSNumber *note; 
    note = [info objectForKey:kNAMIDI_Note]; 

    BRMidiNoteName *noteConverter = [[BRMidiNoteName alloc] init]; 

    NSString *noteName; 
    noteName = [noteConverter nameFromNumber:[note intValue] withNotation:[defaults valueForKey:kSettingsNotation]]; 

    [self.currentNoteLabel performSelectorOnMainThread: @selector(setText:) withObject: noteName waitUntilDone: NO]; 

} 

我的代码视图控制器添加此之后添加此实际上是使用字典将MIDI音符编号转换为音符名称(EG音符编号60 = C) 4)。

相关问题