0

我正在开发一个项目,我需要将 PCM 16 位 2 通道声音转换为 IEEE Float 32 位 2 通道。

为此,我使用以下代码:

void CAudioConverter::ConvI16ToF32(BYTE* pcmFrom, BYTE* floatTo, int length)
{
    short* src = reinterpret_cast<short*>(pcmFrom);
    float* dst = reinterpret_cast<float*>(floatTo);
    for (int n = 0; n < length; n++)
    {
        dst[n] = static_cast<float>(src[n]) / 32768.0f;
    }
}

我已经初始化了变量 __pcm32_bytesPerFrame :

WAVEFORMATEX* closestFormat;
ws->default_pb_dev->GetMixFormat(&closestFormat);
__pcm32_bytesPerFrame = closestFormat->nAvgBytesPerSec * (prm->samples_per_frame * 1000 / (prm->clock_rate * closestFormat->nChannels)) / 1000;

strm->pb_max_frame_count 是:

hr = ws->default_pb_dev->GetBufferSize(&ws->pb_max_frame_count);

我在专用线程中有一个 while 循环,它执行以下操作:

hr = strm->default_pb_dev->GetCurrentPadding(&padding);
incoming_frame = __pcm32_bytesPerFrame / 4;
frame_to_render = strm->pb_max_frame_count - padding;
if (frame_to_render >= incoming_frame)
{ 
    frame_to_render = incoming_frame;
} else {
    /* Don't get new frame because there's no space */
    frame_to_render = 0;
}

if (frame_to_render > 0)
{
    pjmedia_frame frame;

hr = strm->pb_client->GetBuffer(frame_to_render, &cur_pb_buf);
if (FAILED(hr)) {
    continue;
}

void* destBuffer = (void*)malloc(strm->bytes_per_frame*frame_to_render*sizeof(pj_uint16_t));

if (strm->fmt_id == PJMEDIA_FORMAT_L16) {



/* PCM mode */
    frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
    frame.size = strm->bytes_per_frame;
    frame.timestamp.u64 = strm->pb_timestamp.u64;
    frame.bit_info = 0;
    frame.buf = destBuffer;
}

status = (*strm->pb_cb)(strm->user_data, &frame);

CAudioConverter* conv = new CAudioConverter();
conv->ConvI16ToF32((BYTE*)destBuffer, cur_pb_buf, frame_to_render);

hr = strm->pb_client->ReleaseBuffer(frame_to_render, 0);
(...)

但是,要将声音发送到 WASAPI 捕获缓冲区,我需要一个 BYTE*。如何填写我的“floatTo”参数?

有任何想法吗?

谢谢

4

1 回答 1

2

那这个呢:

void CAudioConverter::ConvI16ToF32(BYTE* pcmFrom, BYTE* floatTo, int length)
{
    short* src = reinterpret_cast<short*>(pcmFrom);
    float* dst = reinterpret_cast<float*>(floatTo);
    for (int n = 0; n < length; n++)
    {
        dst[n] = static_cast<float>(src[n]) / 32768.0f;
    }   
}

此外,请确保length指示 和 中的元素数pcmFromfloatTo而不是分配的字节数。在您的情况下pcmFrom,应该分配length*2字节并floatTo需要length*4字节空间。

于 2014-03-26T14:14:55.313 回答