UIAnimations响应Swift中的MIDI事件

时间:2016-04-30 20:53:58

标签: ios swift audiotoolbox coremidi

我正在尝试使用Apple的AudioToolbox框架编写MIDI序列,以触发UI中的动画。由于Gene De Lisa的博客,我已经成功创建了序列,由于代码的长度,我在这里完全没有复制。基本过程是设置AUGraph,采样器单元(AudioUnit()),musicPlayer和音乐序列:

var processingGraph:AUGraph
var samplerUnit:AudioUnit
var musicPlayer:MusicPlayer
var musicSequence:MusicSequence

然后我做了一个函数来填充音乐序列:

func createMusicSequence(quality: Int, root: UInt8, interval: UInt8) -> MusicSequence {
    // create the sequence
    var musicSequence = MusicSequence()

    var status = NewMusicSequence(&musicSequence)
    if status != OSStatus(noErr) {
        print("\(__LINE__) bad status \(status) creating sequence")
        CheckError(status)
    }

    MusicSequenceSetAUGraph(musicSequence, self.processingGraph)

    status = MusicSequenceNewTrack(musicSequence, &trackOne)
    CheckError(status)

    status = MusicSequenceNewTrack(musicSequence, &trackTwo)
    CheckError(status)

    status = MusicTrackSetDestNode(trackOne, samplerNode)
    CheckError(status)

    status = MusicTrackSetDestNode(trackTwo, samplerNodeTwo)
    CheckError(status)

    var beat = MusicTimeStamp(0)
    var i = 1
    while i <= 4 {
        var mess = MIDINoteMessage(channel: 0,
            note: 60,
            velocity: 64,
            releaseVelocity: 0,
            duration: 1.0 )
        status = MusicTrackNewMIDINoteEvent(trackOne, beat, &mess)
        if status != OSStatus(noErr) {
            CheckError(status)
        }
        beat++
        i++
    }

    switch quality{
        case 0: flexiChord(root, transFunc: interval, chordQuality: maj7)
        case 1: flexiChord(root, transFunc: interval, chordQuality: min7)
        case 2: flexiChord(root, transFunc: interval, chordQuality: dom7sharpNineFive)
        default: print("error creating sequence")
    }

    loopTrack(trackOne)
    loopTrack(trackTwo)

    status = MusicSequenceNewTrack(musicSequence, &trackThree)

    MusicSequenceGetTempoTrack(musicSequence, &trackThree)

    self.musicPlayer = createPlayer(musicSequence)

    return musicSequence
}

所以...我无法理解如何检测音符事件并实时根据它们设置UI动画 - 我明白设置用户回调或midi端点必须要做到这一点,但我使用swift并在那里似乎是我在这个问题上能够理解的小文档。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:2)

使用MIDIDestinationCreateWithBlock创建虚拟目标 在您的readblock中,您将获得一个数据包列表。迭代它们并制作动画。如果您需要声音,则您的readblock必须使用MusicDeviceMIDIEvent

将数据包转发给您的采样器