Some problems with outputing audio while the host is not playing

Hi everyone,

I am using VST 3 SDK to develop an instrument. It outputs audio both while the host is playing (e. g. convert incoming MIDI events to audio) and while the host is not playing (e. g. play preview audio). So far I have completed the former task, but I met some problems coping with the latter one. Below are some key parts of my code.

First, the program calculates the difference between the current data.processContext->systemTime and the start of time, and converts the value in nanoseconds to samples:

bool isPlaying = (processContext->state & ProcessContext::kPlaying) != 0;
if(!isPlaying) playbackParameters.systemTimeMs = processContext->systemTime / 1000000;
int64_t deltaTimeMs = playbackParameters->projectTimeSamples - startTimeMs;
int64_t currentTimeSamples = playbackParameters->sampleRate / 1000 * deltaTimeMs;

Then, It copies the audio in the buffer to data.outputs:

memcpy(outputChannel, buffer + currentTimeSamples, playbackParameters->numSamples * sizeof(Sample32));

After these procedures, it should output the audio in the buffer to the host, and it indeed does. However, there are some periodic blasting noises in the output audio.
Record 2023-03-20 at 14h19m39s.zip (2.6 MB)

I cannot figure out what causes the problem. Did I make any mistakes?

Problem solved. I should use numSamples to synchronise audio processing…