void CPlaySound::OnWriteSoundData(WPARAM wParam, LPARAM lParam) { LPWAVEHDR lpHdr = (LPWAVEHDR) lParam; MMRESULT err = 0; if (lpHdr) { //char debugbuffer[256]; //sprintf(debugbuffer, "SOUND BUFFER written: %d, %d\n",lpHdr->dwBufferLength,m_nOutputBuffers); //TRACE(debugbuffer); if (m_bPlay) { short * lpInt = (short *) lpHdr->lpData; DWORD dwSamples = lpHdr->dwBufferLength/sizeof(short); ProcessSoundData(lpInt, dwSamples); err = ::waveOutPrepareHeader(m_hPlay, lpHdr, sizeof(WAVEHDR)); if (err) TRACE("error from waveoutprepareheader\n"); err = ::waveOutWrite(m_hPlay, lpHdr, sizeof(WAVEHDR)); if (err) TRACE("error from waveoutwrite\n"); m_nOutputBuffers++; } } }
LRESULT CRecordSound::OnSoundData(WPARAM wParam, LPARAM lParam) { LPWAVEHDR lpHdr = (LPWAVEHDR) lParam; if(lpHdr) { short int * lpInt = (short int*) lpHdr->lpData; DWORD cbRecorded = lpHdr->dwBytesRecorded; ::waveInUnprepareHeader(m_hRecord, lpHdr, sizeof(WAVEHDR)); ProcessSoundData(lpInt, cbRecorded/sizeof(short int)); // if ((cbRecorded==0)&&(m_bRecording == FALSE))//无声音缓冲进入 // { // TRACE("firsttime =TRUE\n"); // firsttime =TRUE; // } if(m_Writer) { WAVEHDR* pWriteHdr = new WAVEHDR; if(!pWriteHdr) return FALSE; memcpy(pWriteHdr,lpHdr,sizeof(WAVEHDR)); BYTE * pSound = new BYTE[lpHdr->dwBufferLength]; if(!pSound) { if (pWriteHdr) { delete pWriteHdr; } return FALSE; } memcpy(pSound,lpHdr->lpData,lpHdr->dwBufferLength); pWriteHdr->lpData = (char*)pSound; m_Writer->PostThreadMessage(WM_WRITESOUNDFILE_WRITEBLOCK,GetCurrentThreadId(),(LPARAM) pWriteHdr); } if(m_Player) { m_Player->PostThreadMessage(WM_PLAYSOUND_PLAYBLOCK, GetCurrentThreadId(),(LPARAM) lpHdr); } else { if (lpHdr->lpData) { delete lpHdr->lpData; lpHdr->lpData = NULL; } if (lpHdr) { delete lpHdr; lpHdr = NULL; } } char debugbuffer[256]; sprintf(debugbuffer, "SOUND BUFFER returned: %d\n",cbRecorded); TRACE(debugbuffer); if(m_bRecording) { LPWAVEHDR lpHdr = CreateWaveHeader(); ::waveInPrepareHeader(m_hRecord,lpHdr, sizeof(WAVEHDR)); ::waveInAddBuffer(m_hRecord, lpHdr, sizeof(WAVEHDR)); m_nInputBuffers++; } } return ERROR_SUCCESS; }