Search code examples
androidc++audioratesuperpowered

How to use superpowered time stretching class on audio process


I'm having a problem changing the rate of my audio with superpowered time stretching. I know that I need to process the time stretching rate times the number of frames due to output the number of frames.

Ex: If I set the time-stretching rate to 1.5 (increase 50% of original speed) and the number of frames is 144, my number of frames needed is 1.5144=216. The problem happens when I set the time-stretching rate to 1.3 (increase 30% of original speed): 1.5144=187.2 - a decimal amount.

My audio processing method is the following:

bool MultiTracksPlayer::process(
  short int *output,
  unsigned int numberOfFrames,
  unsigned int samplerate
) {
  int i = 0;
  for (i = 0; i < tracksAmount; i++) {
    tracks[i]->outputSamplerate = samplerate;
    tracks[i]->syncMode = Superpowered::SyncMode_TempoAndBeat;
  }

  auto numberOfInputFramesNeeded = (int) (timeStretching->rate * (float) numberOfFrames);

  float buffer[3][numberOfInputFramesNeeded * 8 + 64];
  bool silence[3];

  silence[0] = silence[1] = silence[2] = true;

  for (i = tracksAmount - 1; i >= 0; i--) {
    if (tracks[i]->leftVolume > 0) {
      if (tracks[i]->rightVolume > 0) {
        if (tracks[i]->processStereo(buffer[2], !silence[2], numberOfInputFramesNeeded))
          silence[2] = false;
      } else if (tracks[i]->processStereo(buffer[0], !silence[0], numberOfInputFramesNeeded))
        silence[0] = false;
    } else if (tracks[i]->processStereo(buffer[1], !silence[1], numberOfInputFramesNeeded, true))
      silence[1] = false;
  }

  audioMixer.process(
    silence[0] ? nullptr : buffer[0],
    silence[1] ? nullptr : buffer[1],
    silence[2] ? nullptr : buffer[2],
    nullptr,
    buffer[0],
    numberOfInputFramesNeeded
  );

  silence[0] &= silence[1] && silence[2];

  timeStretching->addInput(buffer[0], (int) numberOfInputFramesNeeded);
  timeStretching->getOutput(buffer[0], (int) numberOfFrames);

  if (!silence[0])
    Superpowered::FloatToShortInt(buffer[0], output, numberOfFrames);
  return !silence[0];
}

Solution

  • I changed my approach of time stretching to the advanced audio player playback rate attribute.