0

下面是我要在标签上显示的注释类型和注释编号的 c 方法。实际上我正在播放一个midi文件,方法是返回midi文件数据,但是在clang方法中,但我想在标签上显示它。

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);
    }
}
4

2 回答 2

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 = C4)。

于 2014-05-16T13:16:38.370 回答
0

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

从 MIDI Read Proc 设置您的视图不是一个好的做法(甚至可能有问题),因为这是一个实时回调,您不想花时间在这个地方写入 UI。

如果您将事件推送到某个地方(如数组)并在结束函数处向您的视图控制器(使用数组对象)发送通知,那就更好了。你想从这个函数中快速返回。

于 2013-03-07T08:09:40.413 回答