2013-02-18 61 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)。

相關問題