// CaptureWaveDialog.cpp : implementation file // #include "stdafx.h" #include "dicwav.h" #include "CaptureWaveDialog.h" #define DEFAULT_CREATE_WAVE_TEXT "Press the Browse button to select an output wave file name..." #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CCaptureWaveDialog dialog CCaptureWaveDialog::CCaptureWaveDialog(CWnd* pParent /*=NULL*/) : CDialog(CCaptureWaveDialog::IDD, pParent) { //{{AFX_DATA_INIT(CCaptureWaveDialog) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_pCapture = NULL; m_bIsCapturing = FALSE; } CCaptureWaveDialog::~CCaptureWaveDialog() { if(m_pCapture) { m_pCapture->Release(); } } void CCaptureWaveDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCaptureWaveDialog) DDX_Control(pDX, IDC_COMBO_AUDIO_DEVICES, m_AudioDevices); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCaptureWaveDialog, CDialog) //{{AFX_MSG_MAP(CCaptureWaveDialog) ON_BN_CLICKED(IDC_DO_CAPTURE_WAVE, OnDoCaptureWave) ON_BN_CLICKED(IDC_BROWSEOUTPUTWAVEFILE, OnBrowseoutputwavefile) ON_WM_CLOSE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCaptureWaveDialog message handlers BOOL CCaptureWaveDialog::IsMMCapabilities() { if(m_pCapture) { return TRUE; } HRESULT hr = CoCreateInstance(CLSID_ltmmCapture, NULL, CLSCTX_INPROC_SERVER, IID_IltmmCapture, (void**) &m_pCapture); if(FAILED(hr) || (m_pCapture == NULL)) { return FALSE; } return TRUE; } L_INT32 CCaptureWaveDialog::GetAudioDeviceCount() { L_INT32 nCount= 0; if(!IsMMCapabilities()) { return 0; } IltmmDevices* pDevices; m_pCapture->get_AudioDevices(&pDevices); if(pDevices == NULL) { return 0; } pDevices->get_Count(&nCount); pDevices->Release(); return nCount; } BOOL CCaptureWaveDialog::OnInitDialog() { CDialog::OnInitDialog(); m_AudioDevices.AddString("None"); if(m_strFileName.GetLength()) { SetDlgItemText (IDC_EDIT_OUTPUT_CAPTURED_WAVE, m_strFileName); } else { SetDlgItemText (IDC_EDIT_OUTPUT_CAPTURED_WAVE, DEFAULT_CREATE_WAVE_TEXT); } L_INT32 nAudioDeviceCount = GetAudioDeviceCount(); if(nAudioDeviceCount) { CString *pAudioDeviceNameArray = new CString[nAudioDeviceCount]; if(pAudioDeviceNameArray) { if(GetAudioDevices(pAudioDeviceNameArray, nAudioDeviceCount)) { for(int nDeviceIndex = 0; nDeviceIndex < nAudioDeviceCount; nDeviceIndex++) { m_AudioDevices.AddString((LPCSTR)pAudioDeviceNameArray[nDeviceIndex]); } } delete[]pAudioDeviceNameArray; } } m_AudioDevices.SetCurSel(0); return TRUE; } BOOL CCaptureWaveDialog::GetAudioDevices(CString *pAudioDeviceNameArray, L_INT32 nArraySize) { L_INT32 nAudioDeviceCount= GetAudioDeviceCount(); if( (nAudioDeviceCount== 0) || (pAudioDeviceNameArray== NULL) || (nArraySize < nAudioDeviceCount)) { //Sorry, I can't help you ! return FALSE; } IltmmDevices* pDevices = NULL; m_pCapture->get_AudioDevices(&pDevices); if(pDevices == NULL) { pDevices->Release(); return FALSE; } HRESULT hr = pDevices->put_Selection(0); if(FAILED(hr)) { return FALSE; } for(int nDeviceIndex = 0; nDeviceIndex < nAudioDeviceCount; nDeviceIndex++) { USES_CONVERSION; IltmmDevice* pDevice = NULL; BSTR DeviceName; pDevices->Item(nDeviceIndex, &pDevice); hr = pDevice->get_FriendlyName(&DeviceName); if(FAILED(hr)) { pDevice->Release(); pDevices->Release(); return FALSE; } pAudioDeviceNameArray[nDeviceIndex] = OLE2T(DeviceName); SysFreeString(DeviceName); pDevice->Release(); } pDevices->Release(); return TRUE; } void CCaptureWaveDialog::OnDoCaptureWave() { CWnd * pWnd = NULL; L_INT nDICOMAudioFormatIndex = -1; if(m_bIsCapturing) { pWnd = GetDlgItem(IDC_DO_CAPTURE_WAVE); if(pWnd) { pWnd->SetWindowText("Start Capture"); } StopCapture(); m_bIsCapturing = FALSE; pWnd = GetDlgItem(IDC_COMBO_AUDIO_DEVICES); if(pWnd) { pWnd->EnableWindow(TRUE); } pWnd = GetDlgItem(IDC_BROWSEOUTPUTWAVEFILE); if(pWnd) { pWnd->EnableWindow(TRUE); } return; } CString strOutputWaveFileName; if(m_AudioDevices.GetCurSel()==0) { AfxMessageBox("Please select an Audio Capture Device first!"); return; } GetDlgItemText (IDC_EDIT_OUTPUT_CAPTURED_WAVE, strOutputWaveFileName); if((strOutputWaveFileName.GetLength() == 0) || (strOutputWaveFileName== DEFAULT_CREATE_WAVE_TEXT)) { AfxMessageBox("Please enter a valid output file name "); return ; } if(!SelectAudioDevice(m_AudioDevices.GetCurSel() -1)) { AfxMessageBox("Couldn't Select Audio Device!"); return ; } nDICOMAudioFormatIndex = GetDICOMAudioFormatIndex(); if(nDICOMAudioFormatIndex==-1) { AfxMessageBox("This capture device doesn't support any audio format that meets the DICOM Basic Voice Audio IOD constraints."); return ; } if(!SetAudioCaptureFormat(nDICOMAudioFormatIndex)) { AfxMessageBox("Couldn't set the audio capture format"); return ; } if(!SetTargetWaveFileName(strOutputWaveFileName)) { AfxMessageBox("Couldn't set the target file name"); return ; } if(!SetTargetFormatAudio()) { AfxMessageBox("Couldn't set the target format"); return ; } if(!StartCapture()) { AfxMessageBox("Couldn't start capturing"); return ; } pWnd = GetDlgItem(IDC_DO_CAPTURE_WAVE); if(pWnd) { pWnd->SetWindowText("Stop Capture"); } pWnd = GetDlgItem(IDC_COMBO_AUDIO_DEVICES); if(pWnd) { pWnd->EnableWindow(FALSE); } pWnd = GetDlgItem(IDC_BROWSEOUTPUTWAVEFILE); if(pWnd) { pWnd->EnableWindow(FALSE); } m_bIsCapturing = TRUE; } void CCaptureWaveDialog::OnBrowseoutputwavefile() { CString strOutFileName; strOutFileName.Empty(); GetDlgItemText (IDC_EDIT_OUTPUT_CAPTURED_WAVE, strOutFileName); if(strOutFileName == DEFAULT_CREATE_WAVE_TEXT) { strOutFileName.Empty(); } TCHAR szFilters[] =_T ("Wave Files (*.wav)|*.wav|All files (*.*)|*.*||"); CFileDialog dlg (FALSE, NULL, strOutFileName,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, szFilters); if (dlg.DoModal () == IDOK) { SetDlgItemText (IDC_EDIT_OUTPUT_CAPTURED_WAVE, dlg.GetPathName ()); UpdateData(); } } BOOL CCaptureWaveDialog::SelectAudioDevice(L_INT32 nIndex) { if(!IsMMCapabilities()) { return FALSE; } IltmmDevices* pDevices; m_pCapture->get_AudioDevices (&pDevices); if(pDevices == NULL) { return FALSE; } HRESULT hr =pDevices->put_Selection(nIndex); if(FAILED(hr)) { pDevices->Release(); return FALSE; } pDevices->Release(); return TRUE; } BOOL CCaptureWaveDialog::SetTargetWaveFileName(const CString& strFileName) { if(!IsMMCapabilities()) { return FALSE; } BSTR bstr; CStringToBSTR(strFileName,&bstr); HRESULT hr =m_pCapture->put_TargetFile (bstr); if(FAILED(hr)) { SysFreeString(bstr); return FALSE; } SysFreeString(bstr); return TRUE; } BOOL CCaptureWaveDialog::SetTargetFormatAudio() { if(!IsMMCapabilities()) { return FALSE; } HRESULT hr =m_pCapture->put_TargetFormat (ltmmCapture_TargetFormat_WAVE); if(FAILED(hr)) { return FALSE; } return TRUE; } BOOL CCaptureWaveDialog::StartCapture() { if(!IsMMCapabilities()) { return FALSE; } HRESULT hr =m_pCapture->StartCapture (ltmmCapture_Mode_Audio); if(FAILED(hr)) { return FALSE; } return TRUE; } BOOL CCaptureWaveDialog::StopCapture() { if(!IsMMCapabilities()) { return FALSE; } HRESULT hr =m_pCapture->StopCapture (); if(FAILED(hr)) { return FALSE; } return TRUE; } //You have to call SelectAudioDevice first L_INT CCaptureWaveDialog::GetDICOMAudioFormatIndex() { L_INT32 nCount; L_INT nAudioFormatIndex; if(!IsMMCapabilities()) { return -1; } IltmmAudioFormats* AudioFormats = NULL; HRESULT hr =m_pCapture->get_AudioCaptureFormats(&AudioFormats); if(FAILED(hr)) { return -1; } hr =AudioFormats->get_Count(&nCount); if(FAILED(hr)) { AudioFormats->Release(); return -1; } if(nCount) { for(nAudioFormatIndex = 0; nAudioFormatIndex < nCount; nAudioFormatIndex++) { IltmmAudioFormat* AudioFormat = NULL; L_INT32 nFrequency = 0; L_INT32 nBits=0; L_INT32 nChannels=0; AudioFormats->Item(nAudioFormatIndex, &AudioFormat); AudioFormat->get_SampleFrequency(&nFrequency); AudioFormat->get_BitsPerSample(&nBits); AudioFormat->get_Channels(&nChannels); if ( (nFrequency == DICOM_WAVE_SUITABLE_FREQUENCY ) && (nBits == DICOM_WAVE_SUITABLE_BITS ) && (nChannels == DICOM_WAVE_SUITABLE_CHANNELS )) { AudioFormat->Release(); AudioFormats->Release(); return nAudioFormatIndex; } AudioFormat->Release(); } } AudioFormats->Release(); return -1; } BOOL CCaptureWaveDialog::SetAudioCaptureFormat(L_INT nIndex) { IltmmAudioFormats* AudioFormats = NULL; if(!IsMMCapabilities()) { return FALSE; } HRESULT hr =m_pCapture->get_AudioCaptureFormats(&AudioFormats); if(FAILED(hr)) { return FALSE; } hr =AudioFormats->put_Selection(nIndex); if(FAILED(hr)) { AudioFormats->Release(); return FALSE; } AudioFormats->Release(); return TRUE; } void CCaptureWaveDialog::CStringToBSTR(const CString& str,BSTR * bstr) { int size=MultiByteToWideChar(CP_ACP,0,str,-1,0,0); OLECHAR* olestr=new OLECHAR[size+1]; olestr[0]=0; if (size) { MultiByteToWideChar(CP_ACP,0,str,size,olestr,size); } *bstr=SysAllocString(olestr); delete [] olestr; } void CCaptureWaveDialog::OnClose() { if(m_bIsCapturing) { AfxMessageBox("Please stop capturing first!"); return; } CDialog::OnClose(); } void CCaptureWaveDialog::OnOK() { GetDlgItemText(IDC_EDIT_OUTPUT_CAPTURED_WAVE,m_strFileName); if(m_strFileName == DEFAULT_CREATE_WAVE_TEXT) { m_strFileName.Empty(); } CDialog::OnOK(); }