我正在尝试使用机械节拍器精确度对鼓机进行编程。理想情况下,它可以以任何合理的速度(低于180 BPM)播放第16(或更好的第32)音符。
对于像这样的东西,AudioKit看起来像一个非常好的库,但是我无法让它以任何足够的时间播放wav样本。下面的代码经常会稍微播放节拍,给hihat样本带来不必要的摇摆感,有时会出现口吃或跳跃。鼓机必须具有坚如磐石的音乐时机。
我在两个独立的iPhone(iPhone 5和iPhone 6),iPad 2以及任何模拟器设备上都有相同的行为。
我做错了吗?或者有没有办法提高AKSequencer计时的准确性?
import AudioKit
class DrumMachine {
var sequencer = AKSequencer()
let sequenceLength = AKDuration(beats: 8.0)
let midi = AKMIDI()
let drums = AKMIDISampler()
init() {
do {
let hiHatFile = try AKAudioFile(readFileName: "Hat_F#1.wav", baseDir: .resources)
try drums.loadAudioFiles([hiHatFile])
} catch {
AKLog("Files or Player Didn't Load")
}
drums.enableMIDI(midi.client, name: "MIDI Sampler")
sequencer.setLength(AKDuration(beats: 8))
_ = sequencer.newTrack()
sequencer.tracks[0].setMIDIOutput(drums.midiIn)
let stepSize = Float(0.25) // 16th notes
let numberOfSteps = Int(Float(sequenceLength.beats) / stepSize)
let duration = Double(stepSize)
for i in 0 ..< numberOfSteps {
let step = Double(i) * stepSize
sequencer.tracks[0].add(
noteNumber: 42 - 12,
velocity: 100,
position: AKDuration(beats: step),
duration: AKDuration(beats: duration)
)
}
AudioKit.output = drums
AudioKit.start()
sequencer.enableLooping()
sequencer.setTempo(100) // 100 BPM
sequencer.play()
}
}