超级强大:实时音高转换,时间拉伸器不工作

Superpowered: real time pitch shift with timestretcher not working

我正在为各种实时 FX 使用 Superpowered,它们都非常简单。然而,音高变换是另一回事,我认为事实上是因为它基于时间拉伸算法,当然必须处理随时间变化的输出,这比应用 EQ 或混响等 FX 复杂得多。但是我只对改变麦克风输入的音调感兴趣。

我查看了 the only example I could find on GitHub 并略微对其进行了调整以适合我的工作:

static bool audioProcessing(void *clientdata,
                            float **buffers,
                            unsigned int inputChannels,
                            unsigned int outputChannels,
                            unsigned int numberOfSamples,
                            unsigned int samplerate,
                            uint64_t hostTime) {
    __unsafe_unretained Superpowered *self = (__bridge Superpowered *)clientdata;

    SuperpoweredAudiobufferlistElement inputBuffer;

    inputBuffer.startSample = 0;
    inputBuffer.samplesUsed = 0;
    inputBuffer.endSample = self->timeStretcher->numberOfInputSamplesNeeded;
    inputBuffer.buffers[0] = SuperpoweredAudiobufferPool::getBuffer(self->timeStretcher->numberOfInputSamplesNeeded * 8 + 64);
    inputBuffer.buffers[1] = inputBuffer.buffers[2] = inputBuffer.buffers[3] = NULL;

    self->outputBuffers->clear();
    self->timeStretcher->process(&inputBuffer, self->outputBuffers);
    int samples = self->timeStretcher->numberOfInputSamplesNeeded;
    float *timeStretchedAudio = (float *)self->outputBuffers->nextSliceItem(&samples);
    if (timeStretchedAudio != 0) {
        SuperpoweredDeInterleave(timeStretchedAudio, buffers[0], buffers[1], numberOfSamples);
    }

    //self->outputBuffers->rewindSlice();

    return true;
}

我删除了大部分我认为不必要的代码。例如,有一个 while 循环似乎可以处理时间延长的情况,我只是在输入的同时输出。

一些观察:

实现标有TODO的部分。这就是您需要为 timeStretcher 提供输入的地方。还要注意将输出与输入分开。可以在输入被消耗之前写入输出。

我终于成功了:

static bool audioProcessing(void *clientdata,
                                float **buffers,
                                unsigned int inputChannels,
                                unsigned int outputChannels,
                                unsigned int numberOfSamples,
                                unsigned int samplerate,
                                uint64_t hostTime) {
    __unsafe_unretained Superpowered *self = (__bridge Superpowered *)clientdata;

    //timeStretching->setRateAndPitchShift(realTimeRate, realTimePitch);
    SuperpoweredAudiobufferlistElement inputBuffer;
    inputBuffer.startSample = 0;
    inputBuffer.samplesUsed = 0;
    inputBuffer.endSample = numberOfSamples;
    inputBuffer.buffers[0] = SuperpoweredAudiobufferPool::getBuffer((unsigned int) (numberOfSamples * 8 + 64));
    inputBuffer.buffers[1] = inputBuffer.buffers[2] = inputBuffer.buffers[3] = NULL;

    // Converting the 16-bit integer samples to 32-bit floating point.
    SuperpoweredInterleave(buffers[0], buffers[1], (float *)inputBuffer.buffers[0], numberOfSamples);
    //SuperpoweredShortIntToFloat(audioInputOutput, (float *)inputBuffer.buffers[0], (unsigned int) numberOfSamples);

    self->timeStretcher->process(&inputBuffer, self->outputBuffers);

    // Do we have some output?
    if (self->outputBuffers->makeSlice(0, self->outputBuffers->sampleLength)) {
        while (true) { // Iterate on every output slice.
            // Get pointer to the output samples.
            int numSamples = 0;
            float *timeStretchedAudio = (float *)self->outputBuffers->nextSliceItem(&numSamples);
            if (!timeStretchedAudio || *timeStretchedAudio == 0) {
                break;
            }

            // Convert the time stretched PCM samples from 32-bit floating point to 16-bit integer.
            //SuperpoweredFloatToShortInt(timeStretchedAudio, audioInputOutput,
            //                            (unsigned int) numSamples);
            SuperpoweredDeInterleave(timeStretchedAudio, buffers[0], buffers[1], numSamples);
            self->recorder->process(timeStretchedAudio, numSamples);

            // Write the audio to disk.
            //fwrite(audioInputOutput, 1, numSamples * 4, fd);
        }
        // Clear the output buffer list.
        self->outputBuffers->clear();
        // If we have enough samples in the fifo output buffer, pass them to the audio output.
        //SuperpoweredFloatToShortInt((float *)inputBuffer.buffers[0], audioInputOutput, (unsigned int) numberOfSamples);
    }

    return true;
}

我不确定更改费率是否也有效,但我不喜欢这个应用程序。 YMMV.