AudioKit ios AKSamplerMetronome [英] AudioKit ios AKSamplerMetronome

查看:311
本文介绍了AudioKit ios AKSamplerMetronome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用AudioKit的AKSamplerMetronome来生成和播放节拍器声音,现在我需要实现一个回调以获得当前节拍假设如果我有5个节拍我需要获得正在播放的当前节拍以便我可以添加一些基于节拍计数的函数,是否有任何回调可用于实现它?

I'm currently using AudioKit's AKSamplerMetronome to generate and play Metronome sounds , now I need implement an callback to get current beat suppose If I'm having 5 beats I need to get current beat that is being played so that I can add some more functions based on the beat count , is there any callback available to achieve It ??

这是我当前的代码

 // using AKSamplerMetronome
var metronome1 = AKSamplerMetronome()
var mixer = AKMixer()
// first sound called
let beatstart = Bundle.main.url(forResource: "mybeat", withExtension: "wav")
 // other sounds based on beat count
let beaten = Bundle.main.url(forResource: "others", withExtension: "wav")
// setting first sound and other beat sounds
metronome1.sound = McountSoundUrl
metronome1.downBeatSound = MoneSoundUrl
metronome1 >>> mixer
AudioKit.output = mixer
AudioKit.start()


推荐答案

你可以用AKSequencer轻松地做到这一点(我做了类似的事情)。我将一个音序器轨道分配给一个AKMIDISampler,产生节拍器声音,另一个轨道分配到一个AKCallbackInstrument。

在发送到AKCallbackInstrument的轨道中,我任意编码了节拍信息。 MIDI数据,例如,第一拍的MIDI数据的MIDINote为1,第二个是MIDINote 2(你可以用速度来做)。然后回调函数可以只查看所有的noteOn消息并从MIDI Note编号中获取当前的节拍,并做出相应的响应。这有点间接,但是有效。

You could do this easily enough with AKSequencer (I did something similar). I assigned one track of the sequencer to an AKMIDISampler, generating the metronome sound, and a second track that went to an AKCallbackInstrument.
In the track being sent to the AKCallbackInstrument, I encoded the beat information arbitrarily in the MIDI data, so for example, the MIDI data for the first beat has a MIDINote of 1, the second, MIDINote 2 (you could do this with the velocity). Then the callback function could would just look at all of the noteOn messages and get the current beat from the MIDI Note number, and respond accordingly. It's a little indirect, but it works.

// create the sequencer before hand (e.g., at init); calling play() immediately after creating it causes some odd behaviour
    let sequencer = AKSequencer()

    // set up the sampler and callbackInst
    let sampler = AKSynthSnare()
    // or for your own sample:
    // let sampler = AKMIDISampler()
    // sampler.loadWav("myMetronomeSound)
    let callbackInst = AKCallbackInstrument()
    AudioKit.output = sampler
    AudioKit.start()

    // create two tracks for the sequencer
    let metronomeTrack = sequencer.newTrack()
    metronomeTrack?.setMIDIOutput(sampler.midiIn)
    let callbackTrack = sequencer.newTrack()
    callbackTrack?.setMIDIOutput(callbackInst.midiIn)

    // create the MIDI data
    for i in 0 ..< 4 {
        // this will trigger the sampler on the four down beats
        metronomeTrack?.add(noteNumber: 60, velocity: 100, position: AKDuration(beats: Double(i)), duration: AKDuration(beats: 0.5))

        // set the midiNote number to the current beat number
        callbackTrack?.add(noteNumber: MIDINoteNumber(i), velocity: 100, position: AKDuration(beats: Double(i)), duration: AKDuration(beats: 0.5))
    }

    // set the callback
    callbackInst.callback = {status, noteNumber, velocity in
        guard status == .noteOn else { return }
        print("beat number: \(noteNumber + 1)")
        // e.g., resondToBeat(beatNum: noteNumber)
    }

    // get the sequencer ready
    sequencer.enableLooping(AKDuration(beats: 4))
    sequencer.setTempo(60)
    sequencer.play()

这篇关于AudioKit ios AKSamplerMetronome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆