code microphone

Download Code Microphone

If you can't read please download the document

Upload: minh-phan

Post on 11-Jan-2016

222 views

Category:

Documents


6 download

DESCRIPTION

Microphone

TRANSCRIPT

01#include 02#include 03#include 04using namespace std;05 06int main()07{08 const int NUMPTS = 44100 * 10;09 int sampleRate = 44100;10 short int waveIn[NUMPTS];11 12 HWAVEIN hWaveIn;13 WAVEHDR WaveInHdr;14 MMRESULT result;15 16 WAVEFORMATEX pFormat;17 pFormat.wFormatTag = WAVE_FORMAT_PCM;18 pFormat.nChannels = 1;19 pFormat.nSamplesPerSec = sampleRate;20 pFormat.nAvgBytesPerSec = 2 * sampleRate;21 pFormat.nBlockAlign = 2;22 pFormat.wBitsPerSample = 16;23 pFormat.cbSize = 0;24 25 result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT);26 27 if(result)28 {29 char fault[256];30 waveInGetErrorTextA(result, fault, 256);31 MessageBoxA(NULL, fault, "Failed to open waveform input device.", MB_OK | MB_ICONEXCLAMATION);32 return 1;33 }34 35 WaveInHdr.lpData = (LPSTR)waveIn;36 WaveInHdr.dwBufferLength = 2 * NUMPTS;37 WaveInHdr.dwBytesRecorded = 0;38 WaveInHdr.dwUser = 0;39 WaveInHdr.dwFlags = 0;40 WaveInHdr.dwLoops = 0;41 waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));42 43 result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));44 if(result)45 {46 MessageBoxA(NULL, "Failed to read block from device", NULL, MB_OK | MB_ICONEXCLAMATION);47 return 1;48 }49 50 result = waveInStart(hWaveIn);51 if(result)52 {53 MessageBoxA(NULL, "Failed to start recording", NULL, MB_OK | MB_ICONEXCLAMATION);54 return 1;55 }56 57 cout