我目前正在使用AudioKit的AKSamplerMetronome来生成和播放Metronome的声音,现在我需要实现一个回调来获得当前的节拍,假设我有5个节拍,我需要播放当前的节拍,这样我就可以在节拍计数的基础上添加更多的功能,有什么可以实现的回调吗?
这是我目前的代码
// 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()发布于 2017-12-04 10:04:00
您可以很轻松地使用AKSequencer (我做了类似的事情)。我给AKMIDISampler分配了一个音轨,产生节拍器的声音,第二个音轨则指向一个AKCallbackInstrument。
在发送给AKCallbackInstrument的磁道中,我在MIDI数据中任意编码了拍频信息,因此,例如,MIDI数据的第一个节拍的MIDINote为1,第二个为MIDINote 2(您可以用速度来实现这一点)。然后,回调函数只需查看所有的noteOn消息,并从MIDI编号中获取当前的节拍,并相应地做出响应。这有点间接,但有效。
// 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()https://stackoverflow.com/questions/47629637
复制相似问题