Esempio n. 1
0
BOOL CWaveRecord::Record(WORD nChs, DWORD nSamplesPerSec, WORD wBitsPerSample)
{
	MMRESULT mmResult = 0;

	if(m_bRecording)
	{
		MyMessageBox(_T("Recording now"), eERR_ERROR);
		return FALSE;
	}

	if((wBitsPerSample % 8))
	{
		MyMessageBox(_T("BPS must be multiple of 8"), eERR_ERROR);
		return FALSE;
	}

	if(wBitsPerSample > 16)
	{
		wBitsPerSample = 16;
	}
	
	SaveWaveFormat(nChs, nSamplesPerSec, wBitsPerSample);

	if(!AllocateMemory(m_dwQueBufferSize))
	{
		MyMessageBox(_T("Allocate memory failed"), eERR_ERROR);
		return FALSE;
	}

	if(m_hParentWnd)
	{
		// Open the given waveform-audio input device for recording
		// Using window callback mechanism
		mmResult = waveInOpen(&m_hWaveIn, 
							  m_nDeviceID, 
							  &m_format, 
						      (DWORD)GetSafeHwnd(), 
						      NULL, 
						      CALLBACK_WINDOW);
	}
	else
	{
		// Using callback function callback mechanism
		mmResult = waveInOpen(&m_hWaveIn, 
							  m_nDeviceID, 
							  &m_format, 
							  (DWORD)waveInProc,
							  (DWORD_PTR)this,
							  CALLBACK_FUNCTION
							  );
	}

	if(mmResult)
	{
		MyMessageBox(_T("waveInOpen failed"), eERR_ERROR);
		Release();
		return FALSE;
	}
	else
	{
		// Add input buffers to the input queue
		for(int i = 0; i< m_wInQueue; i++)
		{
			AddInputBufferToQueue(i);
		}
		
		// Start recording
		mmResult = waveInStart(m_hWaveIn);
		if(mmResult)
		{
			MyMessageBox(_T("waveInStart failed"), eERR_ERROR);
			Release();
			return FALSE;
		}
	}

	m_bRecording = TRUE;
	return TRUE;
}
Esempio n. 2
0
// 
// 录制
//
BOOL CAudioCtrl::Record(UINT uiRecChannel,   /*录制信道*/
						DWORD dwSamplingRate,/*采样率*/ 
						WORD  wSamplingBit  /*采样位*/
						) 
{
	// 录音设备正在录音
	if ( m_eStatus == ENUM_STATUS_RECORDING )
	{
		TRACE("recording...\r\n");
		return FALSE;
	}

	if (m_eStatus != ENUM_STATUS_READY)
	{
		TRACE("AudioCtrl state failed...\r\n");
		return FALSE;
	}

	m_bRecording = TRUE;
	MMRESULT mmReturn = 0;
	ASSERT((wSamplingBit%8) == 0);
	if (wSamplingBit > 16)
	{
		wSamplingBit = 16;
	}
	
	SetWaveFormat(uiRecChannel, dwSamplingRate, wSamplingBit);
	if (!SetRelateParaAfterGetWaveFormat())
	{
		return TRUE;
	}

	mmReturn = ::waveInOpen(&m_hRecord, m_uiDeviceID, &m_Format, (DWORD)GetSafeHwnd(), NULL, CALLBACK_WINDOW);
	if (mmReturn)
	{
		TRACE("waveInOpen is failed!\r\n");
		goto failed;
	}
	else
	{
		// make several input buffers and add them to the input queue
		for(int i=0; i<m_wInQueu; i++)
		{
			AddInputBufferToQueue ( i );
		}
		
		// start recording
		mmReturn = ::waveInStart ( m_hRecord );
		if ( mmReturn )
		{
			TRACE("waveInStart is failed!\r\n");
			goto failed;
		}
	}
	m_eStatus = ENUM_STATUS_RECORDING;

	return TRUE;
failed:
	FreeBuffer();
	return FALSE;
}