473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Immediate audio playback from Mic

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

Expand|Select|Wrap|Line Numbers
  1. #define InBlocks 4 //input buffer numbers
  2. #define OutBlocks 4  //output buffer numbers
  3. #define  INP_BUFFER_SIZE 160
  4.  
  5. pWaveHdr1=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
  6. pWaveHdr2=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
  7. pWaveHdrOut=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
  8.     //***reinterpret_cast ****  operation is used to convert any type of
  9.     //pointer to other type
  10.     //allocate memory for save buffer
  11.     for(int i=0;i<InBlocks;i++)
  12.     {
  13.         m_AudioDataIn[i].dwLength = 0;
  14.         m_AudioDataIn[i].lpdata =reinterpret_cast<PBYTE>              (malloc(1));
  15.  
  16.     }
  17.  
  18.     nAudioIn = 0;
  19.     nAudioOut = 0;
  20.     nSend = 0;
  21.     nReceive = 0;
  22.  
  23. .cpp file
  24. void CRecTestDlg::OnBegin()//it's a button 
  25. {
  26.     // TODO: Add your control notification handler code here
  27.     GetDlgItem(IDC_BEGIN)->EnableWindow(false);
  28.  
  29.     //allocate buffer memory
  30.     pBuffer1=(PBYTE)malloc(INP_BUFFER_SIZE);
  31.     pBuffer2=(PBYTE)malloc(INP_BUFFER_SIZE);
  32.     if (!pBuffer1 || !pBuffer2) 
  33.     {
  34.         if (pBuffer1) free(pBuffer1);
  35.         if (pBuffer2) free(pBuffer2);
  36.         MessageBeep(MB_ICONEXCLAMATION);
  37.         AfxMessageBox("Memory erro!");
  38.         return ;
  39.     }
  40.  
  41.  
  42.     //open waveform audio for input
  43.  
  44.     m_waveformin.wFormatTag=WAVE_FORMAT_PCM;
  45.     m_waveformin.nChannels=1;
  46.     m_waveformin.nSamplesPerSec=8000;//采样频率
  47.     m_waveformin.nAvgBytesPerSec=16000;
  48.     m_waveformin.nBlockAlign=2;
  49.     m_waveformin.wBitsPerSample=16;
  50.     m_waveformin.cbSize=0;
  51.  
  52.  
  53.     if (waveInOpen(&hWaveIn,WAVE_MAPPER,&m_waveformin,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW))
  54.     {   //打开录音设备函数 
  55.         free(pBuffer1);
  56.         free(pBuffer2);
  57.         MessageBeep(MB_ICONEXCLAMATION);
  58.         AfxMessageBox("Audio can not be open!");
  59.     }
  60.     pWaveHdr1->lpData=(LPTSTR)pBuffer1;
  61.     pWaveHdr1->dwBufferLength=INP_BUFFER_SIZE;
  62.     pWaveHdr1->dwBytesRecorded=0;
  63.     pWaveHdr1->dwUser=0;
  64.     pWaveHdr1->dwFlags=0;
  65.     pWaveHdr1->dwLoops=1;
  66.     pWaveHdr1->lpNext=NULL;
  67.     pWaveHdr1->reserved=0;
  68.  
  69.     waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));
  70.  
  71.     pWaveHdr2->lpData=(LPTSTR)pBuffer2;
  72.     pWaveHdr2->dwBufferLength=INP_BUFFER_SIZE;
  73.     pWaveHdr2->dwBytesRecorded=0;
  74.     pWaveHdr2->dwUser=0;
  75.     pWaveHdr2->dwFlags=0;
  76.     pWaveHdr2->dwLoops=1;
  77.     pWaveHdr2->lpNext=NULL;
  78.     pWaveHdr2->reserved=0;
  79.  
  80.     waveInPrepareHeader(hWaveIn,pWaveHdr2,sizeof(WAVEHDR));
  81.  
  82.     // Add the buffers
  83.  
  84.     waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
  85.     waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
  86.  
  87.     // Begin sampling
  88.     waveInStart (hWaveIn) ;
  89.  
  90.     //::AfxBeginThread(Audio_Listen_Thread,this);
  91.     //::AfxBeginThread(Audio_Send_Thread,this);
  92.     m_waveformout.wFormatTag        =    WAVE_FORMAT_PCM;
  93.     m_waveformout.nChannels            =1;
  94.     m_waveformout.nSamplesPerSec    =8000;
  95.     m_waveformout.nAvgBytesPerSec   =16000;
  96.     m_waveformout.nBlockAlign        =2;
  97.     m_waveformout.wBitsPerSample    =16;
  98.     m_waveformout.cbSize            =0;
  99.  
  100.  
  101.     if (waveOutOpen(&hWaveOut,WAVE_MAPPER,&m_waveformout,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) {
  102.         MessageBeep(MB_ICONEXCLAMATION);
  103.         AfxMessageBox("Audio output erro");
  104.     }
  105.  
  106.     return ;
  107.  
  108. }
  109.  
  110. LRESULT CRecTestDlg::OnMM_WIM_DATA(UINT wParam,LONG lParam)
  111. {
  112.  
  113.     int nextBlock = (nAudioIn+1)% InBlocks;    
  114.     if(m_AudioDataIn[nextBlock].dwLength!=0)//下一“块”没发走
  115.     {  //把PWAVEHDR(即pBUfferi)里的数据拷贝到当前“块”中
  116.  
  117.          m_AudioDataIn[nAudioIn].lpdata  
  118.         = (PBYTE)realloc (m_AudioDataIn[nAudioIn].lpdata , (((PWAVEHDR) lParam)->dwBytesRecorded+m_AudioDataIn[nAudioIn].dwLength)) ;
  119.         if (m_AudioDataIn[nAudioIn].lpdata == NULL)
  120.         {
  121.             waveInClose (hWaveIn) ;
  122.             MessageBeep (MB_ICONEXCLAMATION) ;
  123.             AfxMessageBox("erro memory OnMM_WIM_DATA");
  124.             return -1;
  125.         }
  126.         CopyMemory ((m_AudioDataIn[nAudioIn].lpdata+m_AudioDataIn[nAudioIn].dwLength), 
  127.                    ((PWAVEHDR) lParam)->lpData,
  128.                    ((PWAVEHDR) lParam)->dwBytesRecorded) ;//(*destination,*resource,nLen);
  129.  
  130.         m_AudioDataIn[nAudioIn].dwLength +=((PWAVEHDR) lParam)->dwBytesRecorded;
  131.  
  132.     }
  133.     else //把PWAVEHDR(即pBUfferi)里的数据拷贝到下一“块”中
  134.     {
  135.         nAudioIn = (nAudioIn+1)% InBlocks;
  136.         m_AudioDataIn[nAudioIn].lpdata = (PBYTE)realloc
  137.             (0,((PWAVEHDR) lParam)->dwBytesRecorded);
  138.         CopyMemory(m_AudioDataIn[nAudioIn].lpdata,
  139.                 ((PWAVEHDR) lParam)->lpData,
  140.                 ((PWAVEHDR) lParam)->dwBytesRecorded) ;
  141.        m_AudioDataIn[nAudioIn].dwLength =((PWAVEHDR) lParam)->dwBytesRecorded;
  142.  
  143.     }
  144.       // Send out a new buffer    
  145.     waveInAddBuffer (hWaveIn, (PWAVEHDR) lParam, sizeof (WAVEHDR)) ;
  146.     return 0;
  147.  
  148.  
  149. }
  150.  
  151. LRESULT CRecTestDlg::OnMM_WIM_CLOSE(UINT wParam,LONG lParam)
  152. {
  153.  
  154.     waveInUnprepareHeader (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
  155.     waveInUnprepareHeader (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
  156.  
  157.     free (pBuffer1) ;
  158.     free (pBuffer2) ;
  159.  
  160.     return 0;
  161. }
  162. LRESULT CRecTestDlg::OnMM_WOM_OPEN(UINT wParam,LONG lParam)
  163. {   
  164.     // Set up header    
  165.     pWaveHdrOut->lpData          = (LPTSTR)m_AudioDataIn[nAudioOut].lpdata ;
  166.     pWaveHdrOut->dwBufferLength  = m_AudioDataIn[nAudioOut].dwLength ;
  167.     pWaveHdrOut->dwBytesRecorded = 0 ;
  168.     pWaveHdrOut->dwUser          = 0 ;
  169.     pWaveHdrOut->dwFlags         = WHDR_BEGINLOOP ;
  170.     pWaveHdrOut->dwLoops         = 1 ;
  171.     pWaveHdrOut->lpNext          = NULL ;
  172.     pWaveHdrOut->reserved        = 0 ;
  173.  
  174.     // Prepare and write
  175.     waveOutPrepareHeader (hWaveOut, pWaveHdrOut, sizeof (WAVEHDR)) ;
  176.     waveOutWrite (hWaveOut, pWaveHdrOut, sizeof (WAVEHDR)) ;
  177.  
  178.   return 0;
  179.  
  180. }
  181. LRESULT CRecTestDlg::OnMM_WOM_DONE(UINT wParam,LONG lParam)
  182. {  
  183.     free(m_AudioDataIn[nAudioOut].lpdata);
  184.     m_AudioDataIn[nAudioOut].lpdata = reinterpret_cast<PBYTE>(malloc(1));
  185.     m_AudioDataIn[nAudioOut].dwLength = 0;
  186.  
  187.     nAudioOut= (nAudioOut+1)%OutBlocks;
  188.     ((PWAVEHDR)lParam)->lpData          = (LPTSTR)m_AudioDataIn[nAudioOut].lpdata ;
  189.     ((PWAVEHDR)lParam)->dwBufferLength  = m_AudioDataIn[nAudioOut].dwLength ;
  190.     TRACE("the next length %d\n",((PWAVEHDR)lParam)->dwBufferLength);
  191.     waveOutPrepareHeader (hWaveOut,(PWAVEHDR)lParam,sizeof(WAVEHDR));
  192.     waveOutWrite(hWaveOut,(PWAVEHDR)lParam,sizeof(WAVEHDR));//cut
  193.    return 0;
  194.  
  195. }
  196. LRESULT CRecTestDlg::OnMM_WOM_CLOSE(UINT wParam,LONG lParam)
  197. {
  198.     waveOutUnprepareHeader (hWaveOut, pWaveHdrOut, sizeof (WAVEHDR)) ;
  199.  
  200.     //release all the memory of the AudioData
  201.     for(int i=0;i<InBlocks;i++)
  202.     {
  203.         if(m_AudioDataIn[i].dwLength != 0)
  204.             free(m_AudioDataIn[i].lpdata);
  205.  
  206.     }
  207.     /*for(int i=0;i<OutBlocks;i++)
  208.     {
  209.         if(m_AudioDataOut[i].dwLength != 0)
  210.             free(m_AudioDataOut[i].lpdata);
  211.     }*/
  212.  
  213.     return 0 ;
  214. }
Sep 25 '12 #1
5 2782
weaknessforcats
9,208 Expert Mod 8TB
What is your question? I only see a lot of code.
Sep 25 '12 #2
xusen
5
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?
Sep 25 '12 #3
xusen
5
maybe I should provide source code to you ?
Sep 25 '12 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 25 '12 #5
xusen
5
this a mfc project,C++;i want to know how to modify ? others ,we don't discuss now
Sep 26 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: OysterCracker | last post by:
Hi - My js knowledge is pretty much cut-'n-paste, and I'd appreciate a little guidance.I have a simple splash webpage that includes an embedded ..wav file of about 60kb. Currently, some dial-up...
0
by: julio | last post by:
I have an application developed in .net, that is essentially an audio player, it can perform searches on a db on a server, and then play audio tracks (the audio tracks too are on a server, another...
1
by: mmc | last post by:
3 methods needs doing; wave1: This static method takes an AudioInputStream and gradually lowers and then raises the volume so that the new AudioInputStream appears to be constantly alerting in...
2
by: taaz | last post by:
I'm developing the Delay Audiometry feedback. My Step are... Recording -> Playing... these are on different events. I've used the mcisendstring()for manipulation of audio & made the functions ...
0
by: Juanma1 | last post by:
My program is supposed to play a wav file of a ticking clock using the media player object so that only after the file is over (this is to build tension), some textboxes are populated with data...
6
by: ikkeugh | last post by:
Hello , i've got a question : How do you make those mouse over buttons in Visual Basic 6.0 with a delay ? I want them to look like this . The buttons come a few (small)seconds later so they have...
0
by: harryben | last post by:
Hi Everyone, I tried to post this thread in microsoft public forum with no success. However, heres the situation, I ve been developping a WinForm Application in C#. This Application does 2 things...
1
msewing
by: msewing | last post by:
I'm running 2.5.1 on Fedora 8 (2.6.23.9-85.fc8) with the following soundcards: Ensoniq ES1370 (rev 01) VIA Technologies, Inc. VT8233/A/8235/8237 AC97 Audio Controller (rev 50) I'm working on...
1
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= | last post by:
I am using Windows Media Player to play my half second audio files one after the other using the code below... My problem comes when I play a second audio file immediately after the first one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.