【发布时间】:2011-05-17 08:55:58
【问题描述】:
我有一个函数 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 问题了吗?
到目前为止,感谢大家的帮助,这非常有启发性/澄清/教育/有趣。
【问题讨论】:
-
这没有多大意义。你从一个指向浮点数的指针开始,你想从中得到一个指向浮点数的指针,其中插入了很多常量。嗯?
-
为什么还有
c++标签?不是纯C题吗? -
老实说,当谈到
consts 我问“嗯?”也 - 但这是特征提取库想要的,我试图在开始尝试重写库之前先给它想要的东西。 (无论如何,这可能只会导致更多的混乱;我的工作假设是编写该代码的人比我知道的多一点——尽管可能不比你们中的一些人多。) -
@sbi:它也适用于 C++,如果他用 C++ 编译,那么地板对在 C++ 中有效但在 C 中无效的解决方案开放。
-
@ickydog:你绝对确定它是
const float* const*(最好写成float const* const*)?不只是float const* const?您确定您已经正确解释了库函数的输入内容吗?文档是怎么说的?