录音与播放,边录边放问题(附代码和思路)急!

录音与播放,边录边放问题(附代码和思路)急!

各位好,这个问题弄了几天了,网上搜以好多资料,道理基本明白,可就是程序实现起来老不行,我现在是可以实现单独录音和单独播放,但想边录边放就不行了,现在就是想开两buffer,一个录一个往缓冲区写,写到一定程度,比如40ms就开始播放,播放也用两buffer,一个放一个从缓冲区取数据,就这思路,可不知道为怎么就是不行,请各位说说或者帮忙实现一下,有现成的程序请发xyj8303@yahoo.com.cn谢谢!
下面是我录音和播放的代码:
void CRecordHWndDlg::OnRecStart() 
{
// TODO: Add your control notification handler code here
//allocate buffer memory
pBuffer1=(PBYTE)malloc(INP_BUFFER_SIZE);
pBuffer2=(PBYTE)malloc(INP_BUFFER_SIZE);
if (!pBuffer1 || !pBuffer2) {
if (pBuffer1) free(pBuffer1);
if (pBuffer2) free(pBuffer2);
MessageBeep(MB_ICONEXCLAMATION);
AfxMessageBox("Memory erro!");
return ;
}

//open waveform audo for input

waveform.wFormatTag=WAVE_FORMAT_PCM;
waveform.nChannels=1;
waveform.nSamplesPerSec=11025;
waveform.nAvgBytesPerSec=11025;
waveform.nBlockAlign=1;
waveform.wBitsPerSample=8;
waveform.cbSize=0;


if (waveInOpen(&hWaveIn,WAVE_MAPPER,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) {
free(pBuffer1);
free(pBuffer2);
MessageBeep(MB_ICONEXCLAMATION);
AfxMessageBox("Audio can not be open!");
}
pWaveHdr1->lpData=(LPTSTR)pBuffer1;
pWaveHdr1->dwBufferLength=INP_BUFFER_SIZE;
pWaveHdr1->dwBytesRecorded=0;
pWaveHdr1->dwUser=0;
pWaveHdr1->dwFlags=0;
pWaveHdr1->dwLoops=1;
pWaveHdr1->lpNext=NULL;
pWaveHdr1->reserved=0;

waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));

pWaveHdr2->lpData=(LPTSTR)pBuffer2;
pWaveHdr2->dwBufferLength=INP_BUFFER_SIZE;
pWaveHdr2->dwBytesRecorded=0;
pWaveHdr2->dwUser=0;
pWaveHdr2->dwFlags=0;
pWaveHdr2->dwLoops=1;
pWaveHdr2->lpNext=NULL;
pWaveHdr2->reserved=0;

waveInPrepareHeader(hWaveIn,pWaveHdr2,sizeof(WAVEHDR));

//
pSaveBuffer = (PBYTE)realloc (pSaveBuffer, 1) ;
// Add the buffers

waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;

// Begin sampling

bRecording = TRUE ;
bEnding = FALSE ;
dwDataLength = 0 ;
waveInStart (hWaveIn) ;
}
void CRecordHWndDlg::OnMM_WIM_DATA(UINT wParam, LONG lParam) 
{
// TODO: Add your message handler code here and/or call default
// Reallocate save buffer memory

//

pNewBuffer = (PBYTE)realloc (pSaveBuffer, dwDataLength +
((PWAVEHDR) lParam)->dwBytesRecorded) ;

if (pNewBuffer == NULL)
{
waveInClose (hWaveIn) ;
MessageBeep (MB_ICONEXCLAMATION) ;
AfxMessageBox("erro memory");
return ;
}

pSaveBuffer = pNewBuffer ;
//

CopyMemory (pSaveBuffer + dwDataLength, ((PWAVEHDR) lParam)->lpData,
((PWAVEHDR) lParam)->dwBytesRecorded) ;

dwDataLength += ((PWAVEHDR) lParam)->dwBytesRecorded ;

if (bEnding)
{
waveInClose (hWaveIn) ;
return ;
}
// Send out a new buffer

waveInAddBuffer (hWaveIn, (PWAVEHDR) lParam, sizeof (WAVEHDR)) ;
TRACE("done input data\n");
return ;
}
void CRecordHWndDlg::OnPlayStart() 
{

// TODO: Add your control notification handler code here
if (bPlaying) {
waveOutReset(hWaveOut);
}

//open waveform audio for output
waveform.wFormatTag = WAVE_FORMAT_PCM;
waveform.nChannels = 1;
waveform.nSamplesPerSec =11025;
waveform.nAvgBytesPerSec=11025;
waveform.nBlockAlign =1;
waveform.wBitsPerSample =8;
waveform.cbSize =0;


if (waveOutOpen(&hWaveOut,WAVE_MAPPER,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) {
MessageBeep(MB_ICONEXCLAMATION);
AfxMessageBox("Audio output erro");
}
return ;
}
void CRecordHWndDlg::OnMM_WOM_OPEN(UINT wParam, LONG lParam){
TRACE("open MM_WOM_OPEN\n");
// Set up header

pWaveHdr1->lpData = waveBufferRecord[k];//(LPTSTR)pSaveBuffer ;
pWaveHdr1->dwBufferLength = INP_BUFFER_SIZE;//dwDataLength ;
pWaveHdr1->dwBytesRecorded = 0 ;
pWaveHdr1->dwUser = 0 ;
pWaveHdr1->dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP ;
pWaveHdr1->dwLoops = dwRepetitions ;
pWaveHdr1->lpNext = NULL ;
pWaveHdr1->reserved = 0 ;

// Prepare and write

waveOutPrepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
waveOutWrite (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值