Syncing AppleSequencer and audio players in audiokit

41 Views Asked by At

I am trying to use the sequencer to coordinate playback of audio files as well as playback a metronome loaded in the sampler. It seems sequencer doesn't have a play(at:) method that would allow us to pass in an avaudio time or similar. my sequence isn't playing in sync with the methods I have attempted.

I have tried starting the sequence then taking the host time of the sequence to trigger audio players playback using the play(at:) method. However this doesnt seem to get the results I am expecting.

    func startPlayback(_ loopPlayback: Bool, startTime: AVAudioTime = .now())
    {
        sequencerTwo.rewind()
        sequencerTwo.preroll()
        sequencerTwo.play()
        let hostTIme = try! sequencerTwo.hostTime(forBeats: 0)
        let referenceTime = AVAudioTime(hostTime: hostTIme)
        startAudioPlayers(loopPlayback, startTime: .now())
        
        
    }
    func startAudioPlayers(_ loopPlayback: Bool, startTime: AVAudioTime = .now())
    {
        isPlaying = true
        do {
            try startAudioEngine()

        }
        catch {
            print(error)
        }
        self.playbackChannels.forEach{ $0.getAudioPlayer().isLooping = loopPlayback}
        print("start time: \(startTime.hostTime)")
        self.playbackChannels.forEach{
            $0.getAudioPlayer().play(at: startTime,completionCallbackType: .dataPlayedBack)
        }
    }
0

There are 0 best solutions below