Sync plugin playback with host

Hi,

I’m trying to figure out how I can play an audio file from a plugin in sync with the host playback. So far I can load an audio file into a transport source, press play in my host and the plugin plays the audio file in sync with the host, but if I move the playback position in the host, the transport source continues to play from its original position. I tried adding some code like this to my processBlock() function.

if (m_HostPlayerInfo.isPlaying)
{	
	transportSource.setNextReadPosition(m_HostPlayerInfo.timeInSamples);
	//transportSource.setPosition(m_HostPlayerInfo.timeInSeconds);
	if (!transportSource.isPlaying())
		transportSource.start();
}
else if (transportSource.isPlaying())
	transportSource.stop();
}
This however resulted in severe crackling during playback, so I tried moving it to a timerCallback I have for the plugin. That works much better and the crackling is very minimal but I still hear it sometimes and I'm hoping there is a better solution for achieving this. Does anyone have any suggestions?

To me it sounds like you want to trigger a sound when the host starts playing. It would make more sense to then just simply start playback of the transportSource when the host started playing, and stop ih the host stops. I imagine the use case for setNextReadPosition is for repositioning the playhead within the transportSource every once in a while and not every sample.

Another issue might be that this code is running once every block and not every sample or vice versa.
You could also try low pass filtering the playhead position from the host so jumps aren’t so sudden. So basically get the host’s new playheadposition every block and than linear interpolate the transport sources’ playhead.

There is no sensible place for an AudioTransportSource in a plugin:

  1. loading audio files is the responsibility of the host. You will get the audio delivered free to the door via processBlock (AudioBuffer& buffer, [...])
  2. the features the TransportAudioSource adds is play/stop, which is controlled from the gui thread. So by design it can’t be in sync
  3. your plugin should work correctly without any contact to the message thread, just imagine what happens in a non realtime bounce

If you need to supply audio other than what the host delivers:

  • For instruments have a look at Synthesiser and the classes around that
  • For injecting audio from file use a PositionableAudioSource, e.g. AudioFormatReaderSource and call setNextAudioPosition with the information retrieved from AudioPlayHead in processBlock
    (I used that for an adaptive audio library, but it is actually an edge case, normally leave the audio reading to the host, to allow the full potential of the DAW to the user)