How to convert audio byte to samples

微笑、不失礼 提交于 2019-12-02 00:05:17

You have the WAVEFORMATEX used for capturing the audio, right? If so, you can modify the following routine to meet your needs:

void ProcessSamples(WAVEHDR* header, WAVEFORMATEX* format)
{
    BYTE* pData = (BYTE*)(header->data);
    DWORD dwNumSamples = header->dwBytesRecorded / format->nBlockAlign;

    // 16-bit stereo, the most common format
    if ((format->wBitsPerSample == 16) && (format->nChannels == 2))
    {
        for (DWORD index = 0; index < dwNumSamples; index++)
        {
            short left = *(short*)pData; pData+=2;
            short right = *(short*)pData; pData+=2;
        }
    }
    else if ((format->wBitsPerSample == 16) && (format->nChannels == 1))
    {
        for (DWORD index = 0; index < dwNumSamples; index++)
        {
            short monoSample = *(short*)pData; pData+=2;
        }
    }
    else if ((format->wBitsPerSample == 8) && (format->nChannels == 2))
    {
        // 8-bit samples are unsigned.
        // "128" is the median silent value
        // normalize to a "signed" value
        for (DWORD index = 0; index < dwNumSamples; index++)
        {
            signed char left = (*(signed char*)pData) - 128; pData += 1;
            signed char right = (*(signed char*)pData) - 128; pData += 1;
        }
    }
    else if ((format->wBitsPerSample == 8) && (format->nChannels == 1))
    {
        for (DWORD index = 0; index < dwNumSamples; index++)
        {
            signed char monosample = (*(signed char*)pData) - 128; pData += 1;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!