简体   繁体   English

在C ++中处理音频缓冲区时,如何从float-> double-> float执行转换

[英]How can I perform conversion from float -> double -> float when processing an audio buffer in c++

I am currently developing an application where frames of audio samples are processed in the following callback. 我目前正在开发一个应用程序,其中在以下回调中处理音频样本的帧。

void Eav07AudioProcessor::processBlock (AudioSampleBuffer& buffer)
{

    for (int channel = 0; channel < getNumInputChannels(); ++channel)
    {
        float* channelData = buffer.getSampleData (channel);        

        // ..do something to the data...
        DoSomeHeavyweightDSPon(&channelData[0]); // in the header, DoSomeHeavyweightDSPon(double data[somebignumber])
    }
}

The function that will sit in place of the gain process above expects channelData to be double precision. 将代替上面的增益过程的函数期望channelData是双精度的。 The channelData parameter is float precision. channelData参数为浮点精度。 Do I need to somehow cast channelData from float to double and then from double to float after the processing or am I missing a trick. 我是否需要在处理后以某种方式将channelData从float转换为double,然后从double转换为float,还是我错过了一个窍门。

If an explicit conversion is necessary, what is the most efficient way to do it considering that this method is called 100s of times per second. 如果必须进行显式转换,考虑到此方法每秒被调用100次,最有效的方法是什么。

Does the mentioned function take double or double* ? 提到的功能是double还是double*吗? The latter case will always require copying all the channelData into a double buffer, and vice versa after the function ran. 后一种情况总是需要将所有channelData复制到一个double缓冲区中,反之亦然。 If you really want an efficient way, you will need to (copy and) change the problematic function to take float* intead of double* 如果您确实想要一种有效的方法,则需要(复制并)更改有问题的函数以将float*double*

if the function simply takes double , no explicit conversion is needed. 如果函数仅使用double ,则不需要显式转换。

No, casting the float to double and back will not do what the gain()-function expects. 不可以,将float转换为double并返回将不会达到gain()函数所期望的。 The gain-function expects a pointer to an array of doubles and no amount of casting will do that. 增益函数需要一个指向double数组的指针,并且没有大量的强制转换可以做到这一点。

The best thing you can do is replace the gain()-function. 您可以做的最好的事情是替换gain()函数。 I would just use the "demo-code" you included (the inner for-loop). 我只会使用您包含的“演示代码”(内部for循环)。

If you have a requirement to call the gain(double*)-function you can do either: 如果需要调用gain(double *)函数,则可以执行以下任一操作:

  • create a second buffer with size: malloc(buffer.getNumSamples()*sizeof(double)) and copy them over by hand (memcpy doesnt work because it doesnt generate the four zero-bytes you need in between each float). 创建一个大小为malloc(buffer.getNumSamples()*sizeof(double))的第二个缓冲区,然后手动复制它们(memcpy无法正常工作,因为它不会在每个float之间生成您需要的四个零字节)。

  • [hack]: just use the buffer you have but set every second float to 0, then sign extend the other float into this one. [hack]:仅使用您拥有的缓冲区,但是将第二个浮点数设置为0,然后用符号将另一个浮点数扩展到该浮点数中。 This effectively halves the number of samples you have but has very little overhead. 这样可以有效地将您拥有的样本数量减半,但开销却很小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM