⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audioapp.cpp

📁 windows embedded ce (wince6.0)上录音事例程序源码。可编成.exe在系统上运行。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                     return -1;
                }

              // See how many waveOut devices are registered in the system.
             n=waveInGetNumDevs();

             if(0 == n)
             {
             	  RETAILMSG(1, (TEXT( "ERROR: waveInGetNumDevs reported zero devices, we need at least one.")));
                MessageBox(hwnd, TEXT("ERROR: waveInGetNumDevs reported zero devices, we need at least one."), TEXT("NotesApp"), MB_OK | MB_ICONSTOP);
                return -1;
             }


              // Create our WaveIn headers for the bucket brigade
        	ZeroMemory(&whr1,sizeof(whr1));
        	ZeroMemory(&whr2,sizeof(whr2));

             // set up pwfxRec to the format we want to record in: 11kHz, 8bit,  Mono
             ZeroMemory(&wfxRec, sizeof(wfxRec));
             wfxRec.wFormatTag = WAVE_FORMAT_PCM;  // PCM FORMAT
             wfxRec.nChannels = 1; // MONO Sound
             wfxRec.nSamplesPerSec = 11025; // 11 kHz
             wfxRec.wBitsPerSample = 8; // 8 bits per sample
             wfxRec.nBlockAlign = wfxRec.nChannels*wfxRec.wBitsPerSample/8;
             wfxRec.nAvgBytesPerSec = wfxRec.nSamplesPerSec*wfxRec.nBlockAlign;
             wfxRec.cbSize = 0;
             pwfxRec = &wfxRec;

              // Create/Initialize the new file
               hResult=RecordFile.Create(pszWAVFilename,GENERIC_WRITE,
             					   CREATE_ALWAYS,0,(LPVOID*)&pwfxRec,NULL);
               if(ERROR_SUCCESS != hResult) {
                   	RETAILMSG(1, (TEXT("ERROR: Could not Open %s"),pszWAVFilename));
                       return -1;
               }

              // prepare the input buffers which will hold 20msec  worth of audio data at a time
              nBufSize = ((DWORD)(5*wfxRec.nSamplesPerSec)) * wfxRec.nBlockAlign; // ~20 ms buffer
              if(nBufSize < wfxRec.nBlockAlign)
                nBufSize = wfxRec.nBlockAlign; // make sure we don't go too small, or else we'll get exceptions
              data1 = new char[nBufSize];
              data2 = new char[nBufSize];
              if(!data1 || !data2)
              {
                    RETAILMSG(1, (TEXT("ERROR: OOM. Could not allocate capture buffers.")));
                    return -1;
              }

              ZeroMemory(data1,nBufSize);
              ZeroMemory(data2,nBufSize);

              // Open the Wave Intput Stream
              mmRtn = waveInOpen(&hwi,dwDeviceID,&wfxRec, (DWORD)hwnd,0,CALLBACK_WINDOW);
              if(MMSYSERR_NOERROR != mmRtn) {
              	RETAILMSG(1, (TEXT("ERROR: waveInOpen failed with return code %d"), mmRtn));
                     return mmRtn;
              }

               // Prepare first header
              whr1.lpData = data1;
              whr1.dwBufferLength = nBufSize;
              whr1.dwLoops = 0; // Do not set looping flags and dwLoops anymore
              whr1.dwFlags = 0;  // Just Set them to 0

              mmRtn = waveInPrepareHeader(hwi,&whr1,sizeof(whr1));
              if(MMSYSERR_NOERROR != mmRtn) {
              	RETAILMSG(1, (TEXT("ERROR: waveInPrepareHeader failed with return code %d"), mmRtn));
                     return mmRtn;
              }


              // Prepare second header
              whr2.lpData = data2;
              whr2.dwBufferLength = nBufSize;
              whr2.dwLoops = 0; // Do not set looping flags and dwLoops anymore
              whr2.dwFlags = 0; // Just Set them to 0

              mmRtn = waveInPrepareHeader(hwi,&whr2,sizeof(whr2));
              if(MMSYSERR_NOERROR != mmRtn) {
              	RETAILMSG(1, (TEXT("ERROR: waveInPrepareHeader failed with return code %d"), mmRtn));
                     return mmRtn;
              }

              // Pass both buffers to the driver to be queued up
              mmRtn = waveInAddBuffer(hwi, &whr1, sizeof(WAVEHDR));
              if(mmRtn != MMSYSERR_NOERROR)
              {
                    RETAILMSG(1, (TEXT("ERROR: waveInAddBuffer failed with return code %d"), mmRtn));
                    return mmRtn;
              }
              dwNumCaptureBuffers = 1;

              mmRtn = waveInAddBuffer(hwi, &whr2, sizeof(WAVEHDR));
              if(mmRtn != MMSYSERR_NOERROR)
              {
                    RETAILMSG(1, (TEXT("ERROR: waveInAddBuffer failed with return code %d"), mmRtn));
                    return mmRtn;
              }
              dwNumCaptureBuffers++;

              RETAILMSG(1, (TEXT("Capture Buffers Queued up")));

              // start the recording
              mmRtn = waveInStart(hwi);
              if(mmRtn != MMSYSERR_NOERROR)
              {
                    RETAILMSG(1, (TEXT("ERROR: waveInStart failed with return code %d"), mmRtn));
                    return mmRtn;
              }

              bIsRecordingPaused = false;

              RETAILMSG(1, (TEXT("Capture Started")));

              return 0;
              break;
        case WM_PLAY_WAVEFILE:
              // Play the given wavefile
             pszWAVFilename = (LPTSTR)lParam;

             if(NULL == pszWAVFilename)
                {
                     // no/invalid file name given
                     MessageBox(hwnd, TEXT("WAVE File Name was NULL"), TEXT("NotesApp"), MB_OK | MB_ICONSTOP);
                     return -1;
                }

              // See how many waveOut devices are registered in the system.
             n=waveOutGetNumDevs();

             if(0 == n)
             {
             	  RETAILMSG(1, (TEXT( "ERROR: waveOutGetNumDevs reported zero devices, we need at least one.")));
                MessageBox(hwnd, TEXT("ERROR: waveOutGetNumDevs reported zero devices, we need at least one."), TEXT("NotesApp"), MB_OK | MB_ICONSTOP);
                return -1;
             }

             hResult=WaveFile.Create(pszWAVFilename,GENERIC_READ,
             					   OPEN_EXISTING,0,(LPVOID*)&pwfx,NULL);
             if(ERROR_SUCCESS != hResult) {
                 	RETAILMSG(1, (TEXT("ERROR: Could not Open %s"),pszWAVFilename));
                     return -1;
             }


              // Create our WaveOut headers for the bucket brigade
        	ZeroMemory(&wh1,sizeof(wh1));
        	ZeroMemory(&wh2,sizeof(wh2));

              dwNumPlaybackBuffers = 0;  // in case something bad happened in a previous playback loop make sure we start at a good value.

              // allocate 2 fixed sized buffers for the bucket brigade
              // buffer size needs to be a multiple of the block alignment (tight loop in driver makes some assumptions)
              wh1.dwBufferLength = ((DWORD)(( pwfx->nSamplesPerSec)+1)) * pwfx->nBlockAlign; // ~20 ms buffer
              if(wh1.dwBufferLength < pwfx->nBlockAlign)
                wh1.dwBufferLength = pwfx->nBlockAlign; // make sure we don't go too small, or else we'll get exceptions

              wh2.dwBufferLength = wh1.dwBufferLength;

              RETAILMSG(1, (TEXT("NOTE: dwBufferLength is %d"), wh1.dwBufferLength));

              // Allocate Space for the playback Buffers
              playData1 = new char[wh1.dwBufferLength];
              playData2 = new char[wh2.dwBufferLength];
              if(!playData1 || !playData2)
              {
                    RETAILMSG(1, (TEXT("ERROR: OOM. Could not allocate playback buffers.")));
                    return -1;
              }

              ZeroMemory(playData1,wh1.dwBufferLength);
              ZeroMemory(playData2,wh2.dwBufferLength);

              wh1.lpData = playData1;
              wh2.lpData = playData2;

              // Fill the buffers
              WaveFile.ReadData(wh1.lpData,wh1.dwBufferLength,&nBytes);
              if(nBytes != wh1.dwBufferLength) {
              	RETAILMSG(1, (TEXT("ERROR: Could not read a buffer's worth of data from the file.")));
                     return -1;
              }

              WaveFile.ReadData(wh2.lpData,wh2.dwBufferLength,&nBytes);
              if(nBytes != wh2.dwBufferLength) {
              	RETAILMSG(1, (TEXT("ERROR: Could not read a buffer's worth of data from the file.")));
                     return -1;
              }

              // Open the Wave Output Stream
              mmRtn = waveOutOpen(&hwo,dwDeviceID,pwfx, (DWORD)hwnd,0,CALLBACK_WINDOW);
              if(MMSYSERR_NOERROR != mmRtn) {
              	RETAILMSG(1, (TEXT("ERROR: waveOutOpen failed with return code %d"), mmRtn));
                     return mmRtn;
              }

               // Prepare first header
              mmRtn = waveOutPrepareHeader(hwo,&wh1,sizeof(wh1));
              if(MMSYSERR_NOERROR != mmRtn) {
              	RETAILMSG(1, (TEXT("ERROR: waveOutPrepareHeader failed with return code %d"), mmRtn));
                     return mmRtn;
              }

              // Prepare second header
              mmRtn = waveOutPrepareHeader(hwo,&wh2,sizeof(wh2));
              if(MMSYSERR_NOERROR != mmRtn) {
              	RETAILMSG(1, (TEXT("ERROR: waveOutPrepareHeader failed with return code %d"), mmRtn));
                     return mmRtn;
              }

              // Pass the headers to the waveAPI so that they can be queued up to play

              // Write out the header to the waveform audio output stream
              mmRtn=waveOutWrite(hwo,&wh1,sizeof(wh1));
              if(MMSYSERR_NOERROR != mmRtn) {
              	RETAILMSG(1, (TEXT("ERROR: waveOutWrite failed with return code %d"), mmRtn));
                     return mmRtn;
              }

              dwNumPlaybackBuffers++; // increment number of Playback Buffers we are tracking

              mmRtn=waveOutWrite(hwo,&wh2,sizeof(wh2));
              if(MMSYSERR_NOERROR != mmRtn) {
              	RETAILMSG(1, (TEXT("ERROR: waveOutWrite failed with return code %d"), mmRtn));
                     return mmRtn;
              }

              dwNumPlaybackBuffers++; // increment number of Playback Buffers we are tracking

              return 0;
              break;
       case WM_RECORD_STOP:
              // Stop recording the current WAVFILE
              // Stop the capture stream
             if(hwi)
             {
                    mmRtn = waveInStop(hwi);
                    if(mmRtn != MMSYSERR_NOERROR)
                        {
                            RETAILMSG(1, (TEXT("ERROR: waveInStop failed with return code %d"), mmRtn));
                            return mmRtn;
                        }

                    mmRtn = waveInReset(hwi);
                    if(mmRtn != MMSYSERR_NOERROR)
                        {
                            RETAILMSG(1, (TEXT("ERROR: waveInReset failed with return code %d"), mmRtn));
                            return mmRtn;
                        }


                    // at this point we need to wait for all capture buffers to be returned from the driver
                    // waveInReset should speed this along.
                    // once all the buffers are returned and all data is written out to file, the WM_RECORD_FINISHED
                    // message will be sent and we'll take care of writing out that data and closing the file and streams
                    bStopRecording = true; // this tells the MM_WIM_DATA handler to not pass any finished buffers back to the driver
              }
              else
                  RETAILMSG(1, (TEXT("hwi was NULL in STOP")));

              return 0;
              break;
        case WM_RECORD_FINISHED:
              // If we are processing this message, then we assume that all capture buffers have been returned
              // from the driver and written to the file. We will now close out the file
              if(hwi)
              {
                    mmRtn = waveInClose(hwi);
                    if(mmRtn != MMSYSERR_NOERROR)
                        {
                            RETAILMSG(1, (TEXT("ERROR: waveInClose failed with return code %d"), mmRtn));
                            return mmRtn;
                        }
                    hwi = NULL;
              }
              else
                  RETAILMSG(1, (TEXT("hwi was NULL")));

              // Close the file
              RecordFile.Close();

              RETAILMSG(1, (TEXT("Captured file should be closed now.")));

              // reset for next use
              bStopRecording = false;

              // Enable the Record Button
              EnableWindow(hwndRecordButton, true);

              return 0;
              break;
        case WM_PLAYBACK_FINISHED:
              // If we are processing this message, then we assume that all playback buffers have been returned
              // from the driver. We will now close out the file(s)
              if(hwo)
              {
                    mmRtn = waveOutClose(hwo);
                    if(mmRtn != MMSYSERR_NOERROR)
                        {
                            RETAILMSG(1, (TEXT("ERROR: waveOutClose failed with return code %d"), mmRtn));
                            return mmRtn;
                        }
                    hwo = NULL;
              }
              else
                  RETAILMSG(1, (TEXT("hwo was NULL")));

              // Close the file
              WaveFile.Close();

              RETAILMSG(1, (TEXT("Playback file should be closed now.")));

              // reset this for next use
              bStopPlaying = false;

              // Enable the Play button
              EnableWindow(hwndPlayButton, true);

              return 0;
              break;
        case WM_PLAY_STOP:
              // stop playing the current WAVEFILE

              // stop the playback stream
              if(hwo)
              {
                    mmRtn = waveOutReset(hwo);
                    if(mmRtn != MMSYSERR_NOERROR)
                    {
                        RETAILMSG(1, (TEXT("ERROR: waveOutReset failed with return code %d"), mmRtn));
                        return mmRtn;
                    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -