Using AVAudioEngine to schedule sounds for low-latency metronome

后端 未结 3 2108
我寻月下人不归
我寻月下人不归 2021-02-05 19:49

I am creating a metronome as part of a larger app and I have a few very short wav files to use as the individual sounds. I would like to use AVAudioEngine because NSTimer has si

3条回答
  •  我寻月下人不归
    2021-02-05 20:30

    To expand upon 5hrp's answer:

    Take the simple case where you have two beats, an upbeat (tone1) and a downbeat (tone2), and you want them out of phase with each other so the audio will be (up, down, up, down) to a certain bpm.

    You will need two instances of AVAudioPlayerNode (one for each beat), let's call them audioNode1 and audioNode2

    The first beat you will want to be in phase, so setup as normal:

    let buffer = tickBuffer(forBpm: bpm)
    audioNode1player.scheduleBuffer(buffer, atTime: nil, options: .loops, completionHandler: nil)
    

    then for the second beat you want it to be exactly out of phase, or to start at t=bpm/2. for this you can use an AVAudioTime variable:

    audioTime2 = AVAudioTime(sampleTime: AVAudioFramePosition(AVAudioFrameCount(audioFile2.processingFormat.sampleRate * 60 / Double(bpm) * 0.5)), atRate: Double(1))
    

    you can use this variable in the buffer like so:

    audioNode2player.scheduleBuffer(buffer, atTime: audioTime2, options: .loops, completionHandler: nil)
    

    This will play on loop your two beats, bpm/2 out of phase from each other!

    It's easy to see how to generalise this to more beats, to create a whole bar. It's not the most elegant solution though, because if you want to say do 16th notes you'd have to create 16 nodes.

提交回复
热议问题