How to use Superpowered TimeStretching in realtime in AUInternalRenderBlock

344 Views Asked by At

I try to use SuperpoweredTimeStretching in AU render block. For example on one channel for simple code.

I don't change speed of audio at this moment (so, I don't need use "circular buffer" or something like this - count of in and out samples in buffers is fixed). But I have very strange situation. This my code is works good, but if I don't change pitch!

If I don't change pitch - I have one slice (1024 samples). But if I change pitch parameter - I will have two slices (512 samples each) It seems absolutely normal (I implemented iterator). But, when slices will more than one (two 512, not one 1024) - it sounds with artefacts.

I don't understand what I do wrong.

- (AUInternalRenderBlock)internalRenderBlock {

AudioBufferList *renderABLCapture = &renderABL;

SuperpoweredTimeStretching *timeStretch = new SuperpoweredTimeStretching(48000);

//If I change pitch it sounds very "ugly" with artefacts. If nothig to change - everything is ok.
timeStretch->setRateAndPitchShift(1.0f, 1); // Speed is fixed. Only pitch changed.

// This buffer list will receive the time-stretched samples.
SuperpoweredAudiopointerList *outputBuffers = new SuperpoweredAudiopointerList(8, 16);


return ^AUAudioUnitStatus(AudioUnitRenderActionFlags    *actionFlags,
                          const AudioTimeStamp        *timestamp,
                          AVAudioFrameCount            frameCount,
                          NSInteger                outputBusNumber,
                          AudioBufferList            *outputBufferListPtr,
                          const AURenderEvent        *realtimeEventListHead,
                          AURenderPullInputBlock        pullInputBlock ) {

    int numBuffers = outputBufferListPtr->mNumberBuffers;

    pullInputBlock(actionFlags, timestamp, frameCount, 0, renderABLCapture);

    Float32 *sampleDataOutLeft  = (Float32*)outputBufferListPtr->mBuffers[0].mData;
    Float32 *sampleDataOutRight = (Float32*)outputBufferListPtr->mBuffers[1].mData;


    size_t sampleSize = sizeof(Float32);
    //***********

    SuperpoweredAudiobufferlistElement inputBuffer;
    inputBuffer.samplePosition = 0;
    inputBuffer.startSample = 0;
    inputBuffer.samplesUsed = 0;
    inputBuffer.endSample = frameCount; //
    inputBuffer.buffers[0] = SuperpoweredAudiobufferPool::getBuffer(frameCount * 18 + 64);
    inputBuffer.buffers[1] = inputBuffer.buffers[2] = inputBuffer.buffers[3] = NULL;


    //Input sample data to inputBuffer for timeStretch

    memcpy((float*)inputBuffer.buffers[0], renderABLCapture->mBuffers[0].mData, sampleSize * frameCount);

    timeStretch->process(&inputBuffer, outputBuffers); //Process

    if (outputBuffers->makeSlice(0, outputBuffers->sampleLength)) {

        int count = 0;
        int numSamples = 0;

        while (true) { // Iterate on every output slice.

            //If I have more than one slice - it sounds very "ugly" with artefacts.

            // Get pointer to the output samples.
            float *timeStretchedAudio = (float *)outputBuffers->nextSliceItem(&numSamples);
            if (!timeStretchedAudio) break;

            for (int i = 0; i < numSamples; i++) {

                Float32 *sample = &timeStretchedAudio[i];
                sampleDataOutLeft[i + count] = *sample;

            }

            count += numSamples;

        };

        outputBuffers->clear();

    }

    return noErr;
};

}

1

There are 1 best solutions below

0
On BEST ANSWER

My problem was error with interleaved channels.

This is correct code

return ^AUAudioUnitStatus(AudioUnitRenderActionFlags    *actionFlags,
                              const AudioTimeStamp      *timestamp,
                              AVAudioFrameCount         frameCount,
                              NSInteger             outputBusNumber,
                              AudioBufferList           *outputBufferListPtr,
                              const AURenderEvent       *realtimeEventListHead,
                              AURenderPullInputBlock        pullInputBlock ) {

        pullInputBlock(actionFlags, timestamp, frameCount, 0, renderABLCapture);

        Float32 *sampleDataInLeft = (Float32*) renderABLCapture->mBuffers[0].mData;
        Float32 *sampleDataInRight = (Float32*) renderABLCapture->mBuffers[1].mData;

        Float32 *sampleDataOutLeft  = (Float32*)outputBufferListPtr->mBuffers[0].mData;
        Float32 *sampleDataOutRight = (Float32*)outputBufferListPtr->mBuffers[1].mData;


        SuperpoweredAudiobufferlistElement inputBuffer;
        inputBuffer.samplePosition = 0;
        inputBuffer.startSample = 0;
        inputBuffer.samplesUsed = 0;
        inputBuffer.endSample = frameCount;
        inputBuffer.buffers[0] = SuperpoweredAudiobufferPool::getBuffer(frameCount * 8 + 64);
        inputBuffer.buffers[1] = inputBuffer.buffers[2] = inputBuffer.buffers[3] = NULL;

        SuperpoweredInterleave(sampleDataInLeft, sampleDataInRight, (Float32*)inputBuffer.buffers[0], frameCount);

        timeStretch->setRateAndPitchShift(1.0f, -2);
        timeStretch->setSampleRate(48000);
        timeStretch->process(&inputBuffer, outputBuffers);

        if (outputBuffers->makeSlice(0, outputBuffers->sampleLength)) {

            int numSamples = 0;
            int samplesOffset =0;

            while (true) {

                Float32 *timeStretchedAudio = (Float32 *)outputBuffers->nextSliceItem(&numSamples);
                if (!timeStretchedAudio) break;

                  SuperpoweredDeInterleave(timeStretchedAudio, sampleDataOutLeft + samplesOffset, sampleDataOutRight + samplesOffset, numSamples);

                samplesOffset += numSamples;

            };

            outputBuffers->clear();

        }

        return noErr;
    };