【问题标题】:how get data from c languagec语言如何获取数据
【发布时间】:2013-02-18 08:48:21
【问题描述】:

下面是我想要在标签上显示它的notetype和notenumber的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);
    }
}

【问题讨论】:

  • 上述方法产生了什么?你能把笔记打印出来吗?
  • 顺便说一下,上面的函数是Objective-C,不是C :)
  • Krumelur,从技术上讲,它是一个包含 Objective C 对象的 C 函数......因此需要由 ObjC++ 编译器编译。但是我们离题了,嗯;)

标签: ios clang midi


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我也在我的代码中使用了这段代码,并对其进行了调整,以使用 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)。

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 2011-10-08
      • 2011-08-12
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 2014-10-03
      • 2016-08-06
      相关资源
      最近更新 更多