Immediate audio playback from Mic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xusen
    New Member
    • Sep 2012
    • 5

    Immediate audio playback from Mic

    I want to develop a soft like Microphone,Othe r words for that
    I speak some words for mic ,play from earphone immediately!!!
    .h file

    Code:
    #define InBlocks 4 //input buffer numbers
    #define OutBlocks 4  //output buffer numbers
    #define  INP_BUFFER_SIZE 160
    
    pWaveHdr1=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
    pWaveHdr2=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
    pWaveHdrOut=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
    	//***reinterpret_cast ****  operation is used to convert any type of
    	//pointer to other type
    	//allocate memory for save buffer
    	for(int i=0;i<InBlocks;i++)
    	{
    		m_AudioDataIn[i].dwLength = 0;
    		m_AudioDataIn[i].lpdata =reinterpret_cast<PBYTE>              (malloc(1));
    
    	}
    
    	nAudioIn = 0;
    	nAudioOut = 0;
    	nSend = 0;
    	nReceive = 0;
    
    .cpp file
    void CRecTestDlg::OnBegin()//it's a button 
    {
    	// TODO: Add your control notification handler code here
    	GetDlgItem(IDC_BEGIN)->EnableWindow(false);
    
    	//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 audio for input
    	
    	m_waveformin.wFormatTag=WAVE_FORMAT_PCM;
    	m_waveformin.nChannels=1;
    	m_waveformin.nSamplesPerSec=8000;//采样频率
    	m_waveformin.nAvgBytesPerSec=16000;
    	m_waveformin.nBlockAlign=2;
    	m_waveformin.wBitsPerSample=16;
    	m_waveformin.cbSize=0;
    
    	
    	if (waveInOpen(&hWaveIn,WAVE_MAPPER,&m_waveformin,(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));
    			
    	// Add the buffers
    	
    	waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
    	waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
    		
    	// Begin sampling
    	waveInStart (hWaveIn) ;
    
    	//::AfxBeginThread(Audio_Listen_Thread,this);
    	//::AfxBeginThread(Audio_Send_Thread,this);
    	m_waveformout.wFormatTag		=	WAVE_FORMAT_PCM;
    	m_waveformout.nChannels		    =1;
    	m_waveformout.nSamplesPerSec	=8000;
    	m_waveformout.nAvgBytesPerSec   =16000;
    	m_waveformout.nBlockAlign	    =2;
    	m_waveformout.wBitsPerSample	=16;
    	m_waveformout.cbSize			=0;
    	
    	
    	if (waveOutOpen(&hWaveOut,WAVE_MAPPER,&m_waveformout,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) {
    		MessageBeep(MB_ICONEXCLAMATION);
    		AfxMessageBox("Audio output erro");
    	}
    	
    	return ;
    
    }
    
    LRESULT CRecTestDlg::OnMM_WIM_DATA(UINT wParam,LONG lParam)
    {
       
        int nextBlock = (nAudioIn+1)% InBlocks;	
    	if(m_AudioDataIn[nextBlock].dwLength!=0)//下一“块”没发走
    	{  //把PWAVEHDR(即pBUfferi)里的数据拷贝到当前“块”中
            
    	 	m_AudioDataIn[nAudioIn].lpdata  
    		= (PBYTE)realloc (m_AudioDataIn[nAudioIn].lpdata , (((PWAVEHDR) lParam)->dwBytesRecorded+m_AudioDataIn[nAudioIn].dwLength)) ;
    		if (m_AudioDataIn[nAudioIn].lpdata == NULL)
    		{
    			waveInClose (hWaveIn) ;
    			MessageBeep (MB_ICONEXCLAMATION) ;
    			AfxMessageBox("erro memory OnMM_WIM_DATA");
    			return -1;
    		}
    	    CopyMemory ((m_AudioDataIn[nAudioIn].lpdata+m_AudioDataIn[nAudioIn].dwLength), 
    				   ((PWAVEHDR) lParam)->lpData,
    				   ((PWAVEHDR) lParam)->dwBytesRecorded) ;//(*destination,*resource,nLen);
    		
    		m_AudioDataIn[nAudioIn].dwLength +=((PWAVEHDR) lParam)->dwBytesRecorded;
            
    	}
    	else //把PWAVEHDR(即pBUfferi)里的数据拷贝到下一“块”中
    	{
    		nAudioIn = (nAudioIn+1)% InBlocks;
    		m_AudioDataIn[nAudioIn].lpdata = (PBYTE)realloc
    			(0,((PWAVEHDR) lParam)->dwBytesRecorded);
    		CopyMemory(m_AudioDataIn[nAudioIn].lpdata,
    			    ((PWAVEHDR) lParam)->lpData,
    				((PWAVEHDR) lParam)->dwBytesRecorded) ;
    	   m_AudioDataIn[nAudioIn].dwLength =((PWAVEHDR) lParam)->dwBytesRecorded;
    
    	}
      	// Send out a new buffer	
    	waveInAddBuffer (hWaveIn, (PWAVEHDR) lParam, sizeof (WAVEHDR)) ;
    	return 0;
    
    	
    }
    
    LRESULT CRecTestDlg::OnMM_WIM_CLOSE(UINT wParam,LONG lParam)
    {
    
    	waveInUnprepareHeader (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
    	waveInUnprepareHeader (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
    	
    	free (pBuffer1) ;
    	free (pBuffer2) ;
    
    	return 0;
    }
    LRESULT CRecTestDlg::OnMM_WOM_OPEN(UINT wParam,LONG lParam)
    {   
    	// Set up header    
    	pWaveHdrOut->lpData          = (LPTSTR)m_AudioDataIn[nAudioOut].lpdata ;
    	pWaveHdrOut->dwBufferLength  = m_AudioDataIn[nAudioOut].dwLength ;
        pWaveHdrOut->dwBytesRecorded = 0 ;
    	pWaveHdrOut->dwUser          = 0 ;
    	pWaveHdrOut->dwFlags         = WHDR_BEGINLOOP ;
    	pWaveHdrOut->dwLoops         = 1 ;
    	pWaveHdrOut->lpNext          = NULL ;
    	pWaveHdrOut->reserved        = 0 ;
    
        // Prepare and write
    	waveOutPrepareHeader (hWaveOut, pWaveHdrOut, sizeof (WAVEHDR)) ;
    	waveOutWrite (hWaveOut, pWaveHdrOut, sizeof (WAVEHDR)) ;
    
      return 0;
    	
    }
    LRESULT CRecTestDlg::OnMM_WOM_DONE(UINT wParam,LONG lParam)
    {  
    	free(m_AudioDataIn[nAudioOut].lpdata);
    	m_AudioDataIn[nAudioOut].lpdata = reinterpret_cast<PBYTE>(malloc(1));
    	m_AudioDataIn[nAudioOut].dwLength = 0;
     
        nAudioOut= (nAudioOut+1)%OutBlocks;
    	((PWAVEHDR)lParam)->lpData          = (LPTSTR)m_AudioDataIn[nAudioOut].lpdata ;
    	((PWAVEHDR)lParam)->dwBufferLength  = m_AudioDataIn[nAudioOut].dwLength ;
        TRACE("the next length %d\n",((PWAVEHDR)lParam)->dwBufferLength);
    	waveOutPrepareHeader (hWaveOut,(PWAVEHDR)lParam,sizeof(WAVEHDR));
        waveOutWrite(hWaveOut,(PWAVEHDR)lParam,sizeof(WAVEHDR));//cut
       return 0;
    
    }
    LRESULT CRecTestDlg::OnMM_WOM_CLOSE(UINT wParam,LONG lParam)
    {
    	waveOutUnprepareHeader (hWaveOut, pWaveHdrOut, sizeof (WAVEHDR)) ;
    		
    	//release all the memory of the AudioData
    	for(int i=0;i<InBlocks;i++)
    	{
    		if(m_AudioDataIn[i].dwLength != 0)
    			free(m_AudioDataIn[i].lpdata);
    		
    	}
    	/*for(int i=0;i<OutBlocks;i++)
    	{
    		if(m_AudioDataOut[i].dwLength != 0)
    			free(m_AudioDataOut[i].lpdata);
    	}*/
    
    	return 0 ;
    }
    Last edited by Niheel; Sep 25 '12, 04:06 AM. Reason: please use code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What is your question? I only see a lot of code.

    Comment

    • xusen
      New Member
      • Sep 2012
      • 5

      #3
      I've made ​​some improvements, (delete lines from 113 to 133)my idea is: delay control at 10-20ms, but now the delay is too big, there are about 150ms;how to modify?

      Comment

      • xusen
        New Member
        • Sep 2012
        • 5

        #4
        maybe I should provide source code to you ?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          I don't have time to debug your application but if there is a question about programming or language syntax, I can help with that.

          It does appear you are mixing C and C++. I would pick one language or the other and stick with that one. C and C++ don't work the same.

          Also, it looks like you have code in a header file. That's not a good idea. Code is supposed to be in .cpp files while declarations are in header files.

          Comment

          • xusen
            New Member
            • Sep 2012
            • 5

            #6
            this a mfc project,C++;i want to know how to modify ? others ,we don't discuss now

            Comment

            Working...