这是我的结构
/* wave data block header */
typedef struct wavehdr_tag {
LPSTR lpData; /* pointer to locked data buffer */
DWORD dwBufferLength; /* length of data buffer */
DWORD dwBytesRecorded; /* used for input only */
DWORD_PTR dwUser; /* for client's use */
DWORD dwFlags; /* assorted flags (see defines) */
DWORD dwLoops; /* loop control counter */
struct wavehdr_tag FAR *lpNext; /* reserved for driver */
DWORD_PTR reserved; /* reserved for driver */
} WAVEHDR, *PWAVEHDR, NEAR *NPWAVEHDR, FAR *LPWAVEHDR;
我有这个变量
WAVEHDR waveHeader;
我从麦克风录制了10秒,并且
waveHeader->lpData
具有我的原始记录数据,而waveHeader->dwBytesRecorded
是原始数据的长度现在,我想计算每秒的音量,以说出哪一秒的音量最高,哪一秒的音量最低。
我知道我应该将绝对值相加并除以样本数
我使用
sum += abs(waveHeader->lpData[i]);
从0到1秒的数据长度为我,但是它给我的结果并不理想它总是为我每秒提供相同的结果,但是我在几秒钟内保持沉默,在某些时间里说话...
我读了,我必须添加示例,而不是字节我应如何将
waveHeader->lpData[i]
转换为示例?//len = length of one secs data (waveHeader->dwBytesRecorded/10)
for (int i=0; i<len; i++)
{
sum += abs(waveHeader->lpData[i]);
}
最佳答案
您拥有用于捕获音频的WAVEFORMATEX,对吗?如果是这样,您可以修改以下例程以满足您的需求:
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;
}
}
}
关于c++ - 如何将音频字节转换为样本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55566814/