Question 1
My first question concerns playback synchronization when using an AVAudioPlayerNode
and an AVAudioSequencer
for MIDI. Basically I'm trying to play something over MIDI, but they need to be perfectly synchronized.
I'm aware there are sync methods for AVAudioPlayerNode
s, but the sequencer does not seem to have something like that.
Currently I've tried using CAMediaTime() + delay
and usleep
on separate threads, but they don't seem to work very well.
Question 2 I'm using the tap on the engine.inputNode
to get the recording, separate from the music playback. However, it seems like the recording starts earlier. When I compare the recorded data with the original playback, the difference is around 300 ms. I could start recording 300 ms later, but even then, that does not guarantee precise sync and is likely to be machine dependent.
So my question is, what would be a good way to ensure that the recording starts precisely at the moment the playback starts?
hostTime(forBeats:error:)
kept causing a crash, but it definitely set me on the correct path. I'll reference the answer in my own answer, and post what I did. – Guido