gpt4 book ai didi

c++ - G726 ADPCM缓冲怎么玩?

转载 作者:太空宇宙 更新时间:2023-11-04 14:18:18 30 4
gpt4 key购买 nike

我想播放从网络摄像机接收到的音频。压缩后的音频数据格式为G726 ADPCM。

在网上找了几天,还没做。

我试过很多方法来播放它。尝试解码为 PCM 并构建 header 以创建 Wave 文件,但都失败了(也许我在执行此操作时犯了一些错误)。

但是今天,我找到了从PC录制和播放波形音频的代码。

代码如下:

// ADPCM.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <MMSystem.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
const int NUMPTS = 8000*10;//44100 * 10;
int sampleRate = 8000;//44100;
short int waveIn[NUMPTS];

HWAVEIN hWaveIn;
WAVEHDR WaveInHdr;
MMRESULT result;
HWAVEOUT hWaveOut;

WAVEFORMATEX pFormat;
pFormat.wFormatTag = WAVE_FORMAT_PCM;
pFormat.nChannels = 1;
pFormat.nSamplesPerSec = sampleRate;
pFormat.nAvgBytesPerSec = 2 * sampleRate;
pFormat.nBlockAlign = 2;
pFormat.wBitsPerSample = 16;
pFormat.cbSize = 0;

result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT);

if(result)
{
char fault[256];
waveInGetErrorTextA(result, fault, 256);
MessageBoxA(NULL, fault, "Failed to open waveform input device.", MB_OK | MB_ICONEXCLAMATION);
return 1;
}

WaveInHdr.lpData = (LPSTR)waveIn;
WaveInHdr.dwBufferLength = 2 * NUMPTS;
WaveInHdr.dwBytesRecorded = 0;
WaveInHdr.dwUser = 0;
WaveInHdr.dwFlags = 0;
WaveInHdr.dwLoops = 0;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
if(result)
{
MessageBoxA(NULL, "Failed to read block from device", NULL, MB_OK | MB_ICONEXCLAMATION);
return 1;
}

result = waveInStart(hWaveIn);
if(result)
{
MessageBoxA(NULL, "Failed to start recording", NULL, MB_OK | MB_ICONEXCLAMATION);
return 1;
}

cout << "Recording..." << endl;
Sleep((NUMPTS/sampleRate) * 1000); //Sleep while recording

cout << "Playing..." << endl;

if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT))
{
MessageBoxA(NULL, "Failed to replay", NULL, MB_OK | MB_ICONEXCLAMATION );
}

waveOutWrite(hWaveOut, &WaveInHdr, sizeof(WaveInHdr));
Sleep((NUMPTS/sampleRate) * 1000); //Sleep for as long as there was recorded

waveOutUnprepareHeader(hWaveOut, &WaveInHdr, sizeof(WAVEHDR));
waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
waveInClose(hWaveIn);
waveOutClose(hWaveOut);

return 0;
}

我的缓冲区中存储了 PCM 数据,我想修改上面的代码来播放它,所以我将其编辑为以下代码:

const int NUMPTS = 4000;//44100 * 10;
int sampleRate = 8000;//44100;
CHAR waveIn[NUMPTS];

HWAVEIN hWaveIn;
WAVEHDR WaveInHdr;
MMRESULT result;
HWAVEOUT hWaveOut;

WAVEFORMATEX pFormat;
pFormat.wFormatTag = WAVE_FORMAT_PCM;
pFormat.nChannels = 1;
pFormat.nSamplesPerSec = sampleRate;
pFormat.nAvgBytesPerSec = sampleRate/2;//2 * sampleRate;
pFormat.nBlockAlign = 1;//2;
pFormat.wBitsPerSample = 4;//16;
pFormat.cbSize = 0;


WaveInHdr.lpData = (LPSTR)waveIn;
WaveInHdr.dwBufferLength = 2 * NUMPTS;
WaveInHdr.dwBytesRecorded = 0;
WaveInHdr.dwUser = 0;
WaveInHdr.dwFlags = 0;
WaveInHdr.dwLoops = 0;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

memcpy(WaveInHdr.lpData, myPCMBuffer, NUMPTS);

waveOutWrite(hWaveOut, &WaveInHdr, sizeof(WaveInHdr));

waveOutUnprepareHeader(hWaveOut, &WaveInHdr, sizeof(WAVEHDR));
waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
waveInClose(hWaveIn);
waveOutClose(hWaveOut);

但是当调用 waveOutUnprepareHeader() 时,它会抛出消息“Run-Time Check Failure #3 - The variable 'hWaveIn' is being used without being initialized”。

如何初始化hWaveIn?

这是我第一次使用波形音频,所以我很笨^^。

你能告诉我正确的做法吗?

最佳答案

初始化 hWaveIn open音频设备:

MMRESULT waveInOpen(
LPHWAVEIN phwi,
UINT uDeviceID,
LPCWAVEFORMATEX pwfx,
DWORD dwCallback,
DWORD dwInstance,
DWORD fdwOpen
);

Example :

result = waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat,
0L, 0L, WAVE_FORMAT_DIRECT);
if (result)
{
char fault[256];
waveInGetErrorText(result, fault, 256);
Application->MessageBox(fault, "Failed to open waveform input device.",
MB_OK | MB_ICONEXCLAMATION);
return;
}

关于c++ - G726 ADPCM缓冲怎么玩?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9499114/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com