如何安全地将浮点数 *x 变成常量浮点数 *常量 *y
How can I safely make a float *x into a const float *const *y?
我有一个函数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);
(披露:我写的代码让提问者感到困惑)
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 使用提升将数据从 PyObject 复制到浮点数 *
- 使用浮点数和双精度数的非常小数字的数学
- 使用英特尔内联函数将打包的 8 位整数乘以浮点数向量
- 如何在 c++ 中将小数点后两位数的浮点数分配给另一个浮点数
- 返回浮点数的小数位数
- txt 文件中浮点数的最大和最小值
- 为什么 std::cout 打印浮点数、双精度和长双精度到相同的小数精度?
- 将浮点数转换为无符号字符数组并打印出来
- 如何将时间字符串 (M:SS) 转换为浮点数
- 如何将浮点数(*)[6]转换为浮点**类型?
- C++将浮点数乘以分数
- 如何将浮点数转换为uint8_t?
- 将字符串转换为浮点数或整数,而无需使用内置函数(如 atoi 或 atof)
- 如何将XMFLOAT3转换为常量浮点数 *pData
- 将浮点数与常量进行比较时出现奇怪的行为
- 如何安全地将浮点数 *x 变成常量浮点数 *常量 *y
- 获得相同的常量,为浮点数或双精度工作