简体   繁体   English

如何在 swift 中使用 AudioKit 生成重复声音

[英]How to generate repeating sound with AudioKit in swift

I'm trying to make metronome app that plays sound on repeat (BPM) and with/without each tack volume.我正在尝试制作可以重复播放声音(BPM)和有/没有每个大头钉音量的节拍器应用程序。 I tried to achieve that with Timer and GCD Timer like that:我试图用TimerGCD Timer来实现这一点:

let interval = TimeInterval(Double(60) / Double(bpm))
timer = Timer.scheduledTimer(timeInterval: interval,
                             target: self,
                             selector: #selector(playTackt),
                             userInfo: nil,
                             repeats: true
@objc func playTackt(){
    if shouldPlaySoundTackts{
        audioPlayer.volume = 1
        playedSoundTackts += 1
        if playedSoundTackts == soundTackts{
            shouldPlaySoundTackts = false
            playedSoundTackts = 0
        }
    }else{
        audioPlayer.volume = 0
        playedMuteTackts += 1
        if playedMuteTackts == muteTackts{
            shouldPlaySoundTackts = true
            playedMuteTackts = 0
        }
    }
  audioPlayer.play()
}

But the problem is that Timer has some lags in miliseconds.但问题是 Timer 有一些以毫秒为单位的滞后。 Then i tried to replay sound with audioPlayerDidFinishPlaying method but it call's after ~0.5 seconds after sound finish's playing.然后我尝试使用audioPlayerDidFinishPlaying方法重播声音,但它会在声音完成播放后约 0.5 秒后调用。 So is there another way to generate or repeat sound with bpm and with/without each tack volume?那么是否有另一种方法可以使用 bpm 和使用/不使用每个大头钉音量来生成或重复声音?

You could try using either the Sequencer or AppleSequencer and assigning the notes as Midi.您可以尝试使用 Sequencer 或 AppleSequencer 并将音符分配为 Midi。 One known drawback with AppleSampler is a delay in the first note playing. AppleSampler 的一个已知缺点是播放第一个音符时会有延迟。 You can see an example of AppleSequencer in MusicToy and Sequencer in the Shaker example of AudioKit/Cookbook on Github.您可以在 Github 上的 AudioKit/Cookbook 的 Shaker 示例中看到 MusicToy 中的 AppleSequencer 示例和 Sequencer 示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM