I’m performing sample-accurate scheduling using both getDSPClock
, getLength
and setPosition
.
I have two sounds – sound1 has already started playing, and I want to synchronise sound2 with it by starting it at the right point in the sound.
When the sound sample is different from the system sample rate (i.e. say system running at 48000 vs. sound loaded at 44100) I cannot use getLength
directly to offset the position. I have to convert to the system sample rate first:
result = sound1->sound->getLength(&sound1->len_pcm, FMOD_TIMEUNIT_PCM);
ERRCHECK(result);
result = sound1->sound->getLength(&sound1->len_ms, FMOD_TIMEUNIT_MS);
ERRCHECK(result);
// work out the actual sample rate of the sound
sound1->sr = sound1->len_pcm / (sound1->len_ms / 1000);
// work out the length of the sound in terms of the system sample rate
// so we can call accurately using a combination of getDSPClock and setPosition
sound1->len_pcm_systemsr = sound1->len_ms * (system_samplerate / 1000);
…and then when I want to set the offset position correctly I have to convert back to the sound’s sample rate to use setPosition
correctly. Assume I’ve already worked out what the correct position
unsigned long long channelgroup_dsp = 0;
channelgroup->getDSPClock(&channelgroup_dsp, 0);
unsigned long long channel_start_pos = 0;
unsigned long long start_point_offset = (channelgroup_dsp - startpoint);
if (start_point_offset < sound2->len_pcm_systemsr)
{
// the current clock position of the channel group is less than the length of the sound
// this means we're in the first loop
// set the start position of the sound to the position
channel_start_pos = start_point_offset;
}
else
{
// we must be in a subsequent loop
// work out where in the loop we are
channel_start_pos = start_point_offset % sound2->len_pcm_systemsr;
}
// now we have to convert the dsp clock (which is at system sample rate)
// to the sound's sample rate
float soundoffset_ms = channel_start_pos / (system_samplerate / 1000);
unsigned int soundoffset_samples = soundoffset_ms * (sound1->sr / 1000);
sound2->channel->setPosition(soundoffset_samples, FMOD_TIMEUNIT_PCM);
What I’d like to know is:
- Is there a simpler way to get the sound’s sample rate?
- Is there a more efficient way to do this?