我想播放从IP Camera收到的音频。压缩音频数据的格式为G726 ADPCM。
我已经在互联网上搜索了几天,但我还没有完成。
我尝试了很多方法来回放它。尝试解码到PCM并构建标题以创建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()时,它抛出了“运行时检查失败#3 - 正在使用变量'hWaveIn'而未被初始化”的消息。
如何初始化hWaveIn?
这是我第一次使用wave音频因此我非常愚蠢^^。
你能告诉我正确的方法吗?
答案 0 :(得分:0)
初始化hEaven open音频设备:
MMRESULT waveInOpen(
LPHWAVEIN phwi,
UINT uDeviceID,
LPCWAVEFORMATEX pwfx,
DWORD dwCallback,
DWORD dwInstance,
DWORD fdwOpen
);
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;
}