我正在用麦克风录制我的声音。为此,我使用了waveIn()和waveOut()函数。当我使用waveInStart()函数开始录制时,它首先完全录制我的声音达指定时间并将其存储在()缓冲区中,然后从该缓冲区播放我的声音。在录制期间和播放期间,它什么都不做。我想同时录制和播放。 为此,我想在录制期间访问缓冲区。这怎么可能 ?或者任何其他建议都会有所帮助。
Here is the code :
#include <iostream>
#include <Windows.h>
using namespace std;
#pragma comment(lib, "winmm.lib")
short int waveIn[8000 * 3];
void PlayRecord();
void writedataTofile(LPSTR lpData,DWORD dwBufferLength);
void StartRecord()
{
const int NUMPTS = 8000 * 3; // 3 seconds
int sampleRate = 8000;
// 'short int' is a 16-bit type; I request 16-bit samples below
// for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types
HWAVEIN hWaveIn;
MMRESULT result;
WAVEFORMATEX pFormat;
pFormat.wFormatTag=WAVE_FORMAT_PCM; // simple, uncompressed format
pFormat.nChannels=1; // 1=mono, 2=stereo
pFormat.nSamplesPerSec=sampleRate; // 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz
pFormat.nAvgBytesPerSec=sampleRate*2; // = nSamplesPerSec × nBlockAlign
pFormat.nBlockAlign=2; // = (nChannels × wBitsPerSample) / 8
pFormat.wBitsPerSample=16; // 16 for high quality, 8 for telephone-grade
pFormat.cbSize=0;
// Specify recording parameters
result = waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat,
0L, 0L, WAVE_FORMAT_DIRECT);
WAVEHDR WaveInHdr;
// Set up and prepare header for input
WaveInHdr.lpData = (LPSTR)waveIn;
WaveInHdr.dwBufferLength = NUMPTS*2;
WaveInHdr.dwBytesRecorded=0;
WaveInHdr.dwUser = 0L;
WaveInHdr.dwFlags = 0L;
WaveInHdr.dwLoops = 0L;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
// Insert a wave input buffer
result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
// Commence sampling input
result = waveInStart(hWaveIn);
cout << "recording..." << endl;
Sleep(3 * 1000);
// Wait until finished recording
waveInClose(hWaveIn);
PlayRecord();
}
void PlayRecord()
{
const int NUMPTS = 8000 * 3; // 3 seconds
int sampleRate = 8000;
// 'short int' is a 16-bit type; I request 16-bit samples below
// for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types
HWAVEIN hWaveIn;
WAVEFORMATEX pFormat;
pFormat.wFormatTag=WAVE_FORMAT_PCM; // simple, uncompressed format
pFormat.nChannels=1; // 1=mono, 2=stereo
pFormat.nSamplesPerSec=sampleRate; // 44100
pFormat.nAvgBytesPerSec=sampleRate*2; // = nSamplesPerSec * n.Channels * wBitsPerSample/8
pFormat.nBlockAlign=2; // = n.Channels * wBitsPerSample/8
pFormat.wBitsPerSample=16; // 16 for high quality, 8 for telephone-grade
pFormat.cbSize=0;
// Specify recording parameters
waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat, 0L, 0L, WAVE_FORMAT_DIRECT);
WAVEHDR WaveInHdr;
// Set up and prepare header for input
WaveInHdr.lpData = (LPSTR)waveIn;
WaveInHdr.dwBufferLength = NUMPTS*2;
WaveInHdr.dwBytesRecorded=0;
WaveInHdr.dwUser = 0L;
WaveInHdr.dwFlags = 0L;
WaveInHdr.dwLoops = 0L;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
HWAVEOUT hWaveOut;
cout << "playing..." << endl;
waveOutOpen(&hWaveOut, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT);
waveOutWrite(hWaveOut, &WaveInHdr, sizeof(WaveInHdr)); // Playing the data
Sleep(3 * 1000); //Sleep for as long as there was recorded
waveInClose(hWaveIn);
waveOutClose(hWaveOut);
}
int main()
{
StartRecord();
return 0;
}
答案 0 :(得分:0)
从技术上讲,如果你分配缓冲区,你可以将in和output分配给同一个缓冲区,并运行一个线程来进行回放,并运行一个线程来进行记录。但是,我希望你需要更多的东西。
问题在于缓冲区内容将通过某种机制从内存加载到硬件中,并且“预读取”数据以便在小块中播放,以及“缓冲”记录端。驱动程序和硬件都将具有一些“缓存”机制。这意味着播放将在录制内存之前读取数据,这当然无法正常工作。
大多数音频处理系统的工作方式是将输出延迟一点,所以你输入一点,处理它,输出它。当然,这会导致一个小的延迟,这可能会很烦人。
答案 1 :(得分:0)
在调用waveInStart之前,您可以为音频驱动程序准备并添加几个缓冲区。驱动程序将对它们进行排队,并从缓冲区到缓冲区进行排序而不会丢失任您必须在waveInOpen中使用fdwOpen标志,以便每次填充缓冲区时都会收到通知。
waveOut具有相同的排队功能:您可以在播放前一个缓冲区时输出缓冲区,并且它将在缓冲区之间平滑排序。
因此,您可以使用十个0.3秒的缓冲区而不是一个大的3秒缓冲区,并编写代码来处理这些缓冲区从记录到播放时的填充。结果将是没有暂停的音频,但延迟时间为0.3秒。