bool readSamples()

in pedalboard/juce_overrides/juce_PatchedMP3AudioFormat.cpp [3318:3387]


  bool readSamples(int **destSamples, int numDestChannels,
                   int startOffsetInDestBuffer, int64 startSampleInFile,
                   int numSamples) override {
    if (destSamples == nullptr) {
      jassertfalse;
      return false;
    }

    if (numDestChannels == 0) {
      return true;
    }

    if (currentPosition != startSampleInFile) {
      if (!stream.seek((int)(startSampleInFile / samplesPerFrame - 1))) {
        currentPosition = -1;
        createEmptyDecodedData();
      } else {
        decodedStart = decodedEnd = 0;
        const int64 streamPos = stream.currentFrameIndex * samplesPerFrame;
        int toSkip = (int)(startSampleInFile - streamPos);
        jassert(toSkip >= 0);

        while (toSkip > 0) {
          if (!readNextBlock()) {
            createEmptyDecodedData();
            break;
          }

          const int numReady = decodedEnd - decodedStart;

          if (numReady > toSkip) {
            decodedStart += toSkip;
            break;
          }

          toSkip -= numReady;
        }

        currentPosition = startSampleInFile;
      }
    }

    while (numSamples > 0) {
      if (decodedEnd <= decodedStart && !readNextBlock()) {
        for (int i = numDestChannels; --i >= 0;)
          if (destSamples[i] != nullptr)
            zeromem(destSamples[i] + startOffsetInDestBuffer,
                    (size_t)numSamples * sizeof(float));

        return false;
      }

      const int numToCopy = jmin(decodedEnd - decodedStart, numSamples);
      float *const *const dst = reinterpret_cast<float **>(destSamples);
      memcpy(dst[0] + startOffsetInDestBuffer, decoded0 + decodedStart,
             (size_t)numToCopy * sizeof(float));

      if (numDestChannels > 1 && dst[1] != nullptr)
        memcpy(dst[1] + startOffsetInDestBuffer,
               (numChannels < 2 ? decoded0 : decoded1) + decodedStart,
               (size_t)numToCopy * sizeof(float));

      startOffsetInDestBuffer += numToCopy;
      decodedStart += numToCopy;
      currentPosition += numToCopy;
      numSamples -= numToCopy;
    }

    return true;
  }