如何安全地将浮点数 *x 变成常量浮点数 *常量 *y

How can I safely make a float *x into a const float *const *y?

本文关键字:浮点数 常量 何安全 安全      更新时间:2023-10-16

我有一个函数audioReceived (float * input, int bufferSize, int nChannels)我想在其中调用一个需要const float *const *inputBuffers的函数库。

显然,投射const float *const *inputBuffers = (const float* const*)input;编译,但这是一个糟糕的主意,使程序崩溃,杀死小猫等。 没有人需要修改原始float* input,而是正在处理的传入音频数据。

我如何以正确的方式做到这一点?

编辑:这里有更多的代码。 audioReceived是:

void testApp::audioReceived (float * input, int bufferSize, int nChannels){ 
     Vamp::RealTime rt = Vamp::RealTime::fromMilliseconds(ofGetSystemTime());
     float const *const tmp[] = { input, 0 };    
     Vamp::Plugin::FeatureSet fs = myPlugin->process(tmp, rt);
 }

库函数process实际上是在基类中定义的:

 /**
 * Process a single block of input data.
 * 
 * If the plugin's inputDomain is TimeDomain, inputBuffers will
 * point to one array of floats per input channel, and each of
 * these arrays will contain blockSize consecutive audio samples
 * (the host will zero-pad as necessary).  The timestamp in this
 * case will be the real time in seconds of the start of the
 * supplied block of samples.
 *
 * If the plugin's inputDomain is FrequencyDomain, inputBuffers
 * will point to one array of floats per input channel, and each
 * of these arrays will contain blockSize/2+1 consecutive pairs of
 * real and imaginary component floats corresponding to bins
 * 0..(blockSize/2) of the FFT output.  That is, bin 0 (the first
 * pair of floats) contains the DC output, up to bin blockSize/2
 * which contains the Nyquist-frequency output.  There will
 * therefore be blockSize+2 floats per channel in total.  The
 * timestamp will be the real time in seconds of the centre of the
 * FFT input window (i.e. the very first block passed to process
 * might contain the FFT of half a block of zero samples and the
 * first half-block of the actual data, with a timestamp of zero).
 *
 * Return any features that have become available after this
 * process call.  (These do not necessarily have to fall within
 * the process block, except for OneSamplePerStep outputs.)
 */
virtual FeatureSet process(const float *const *inputBuffers,
               RealTime timestamp) = 0;

在实际标题中:

FeatureSet process(const float *const *inputBuffers, Vamp::RealTime timestamp);

我认为EXC_BAD_ACCESS可能源于库函数想要一个零填充数组,而我没有给它一个。(a) 这听起来是否合理,(b) 如果是,是否该提出一个不同的 SO 问题?

感谢大家到目前为止的帮助,它非常有启发性/澄清/教育/有趣。

语义

在这里很重要。从参数名称中,我可以猜到您要调用的函数接受多个缓冲区,因此它希望一个指针数组浮点(即数组数组)。由于您只有一个数组,因此您需要创建一个包含原始指针的数组,并将其传递给函数。

如果函数对它正在传递的数组的长度(即缓冲区的数量)有一个单独的参数,那么使用一元&运算符获取地址并传递 1 的长度就足够了,否则你需要创建一个临时的 null 终止数组:

float const *const tmp[] = { input, 0 };

并将其传递给函数。

&input对于演员本身来说应该足够了。请注意,内部函数的参数是指向指针的指针。

编辑:

要按照原始问题注释中的要求获取以空结尾的输入缓冲区列表,您可以使用:

float const * const buffers[] = {input, 0};
float *

float **

因此,将input投射到inputBuffers是行不通的。

通常,从非 const 转换为 const 是隐含的,您不需要做任何特殊的事情。您不能轻易地从 const 转换为非 const。当你考虑它时,这是合乎逻辑的。

你需要

给它传递一个float **,即指向浮点数指针的指针。const只是告诉您该函数不会修改指向的任何值。

如果你有nChannels通道数量可能可变,每个通道bufferSize个浮点数,那么你可以设置必要的存储

,例如
float **data = new float *[nChannels];
for (int c = 0; c < nChannels; ++c) {
    data[c] = new float[bufferSize];
}

之后,data 以正确的格式传递给此函数。(该函数知道这些东西有多大,因为您之前已经向插件构造函数提供了通道计数和块大小。

您的示例代码显示您有一个单指针float *,这表明您可能从交错格式的数据开始(即单个数组中的交替通道值)。如果是这种情况,您需要将反交错到单独的通道中,以便传递给 process 函数:

for (int i = 0; i < bufferSize; ++i) {
    for (int c = 0; c < nChannels; ++c) {
        data[c][i] = input[i * nChannels + c];
    }
}
myPlugin->process(data, rt);

最后,不要忘记在之后delete[]成员指针和双指针。

for (int c = 0; c < nChannels; ++c) {
    delete[] data[c];
}
delete[] data;

另一方面,如果你知道你只会有一个数据通道——并且你在插件构造函数中提供了 1 作为通道计数——那么你可以按照其他人的建议做一个额外的间接寻址:

myPlugin->process(&input, rt);

(披露:我写的代码让提问者感到困惑)