登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

面包会有的

... ...

 
 
 

日志

 
 

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

2011-11-10 14:37:36|  分类: 声音播放 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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

各位好,这个问题弄了几天了,网上搜以好多资料,道理基本明白,可就是程序实现起来老不行,我现在是可以实现单独录音和单独播放,但想边录边放就不行了,现在就是想开两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)) ;
}

  评论这张
 
阅读(1838)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018