www.pudn.com > tapi3.zip > WAVEDLG.CPP


// wavedlg.cpp : implementation file 
// (c) Dialogic corp 1995, 1996 
 
#include "stdafx.h" 
//#include "talker32.h" 
#include "resource.h" 
#include "wavedlg.h" 
#include  
#include  
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char BASED_CODE THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CWaveDlg dialog 
 
 
CWaveDlg::CWaveDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CWaveDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CWaveDlg) 
	m_csFormat = _T(""); 
	m_nPlayFile = -1; 
	m_csCustom = _T(""); 
	m_nCustom = 0; 
	//}}AFX_DATA_INIT 
} 
 
 
void CWaveDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CWaveDlg) 
	DDX_Control(pDX, IDC_PLAYFORMAT, m_btnPlayFormat); 
	DDX_Control(pDX, IDC_FORMAT, m_ctlFormat); 
	DDX_CBString(pDX, IDC_FORMAT, m_csFormat); 
	DDX_Radio(pDX, IDC_PLAYFORMAT, m_nPlayFile); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CWaveDlg, CDialog) 
	//{{AFX_MSG_MAP(CWaveDlg) 
	ON_BN_CLICKED(IDC_PLAYFORMAT, OnPlayformat) 
	ON_CBN_SELCHANGE(IDC_FORMAT, OnSelchangeFormat) 
	ON_BN_CLICKED(IDC_RECFORMAT, OnRecformat) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CWaveDlg message handlers 
 
BOOL CWaveDlg::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	 
	m_ctlFormat.AddString("1. ADPCM 4 bit 6 kHz");	 
	m_ctlFormat.AddString("2. ADPCM 4 bit 8 kHz");	 
	m_ctlFormat.AddString("3. MULAW 8 bit 6 kHz");	 
	m_ctlFormat.AddString("4. MULAW 8 bit 8 kHz");	 
	m_ctlFormat.AddString("5. MULAW 8 bit 11 kHz");	 
	m_ctlFormat.AddString("6.  ALAW 8 bit 6 kHz");	 
	m_ctlFormat.AddString("7.  ALAW 8 bit 8 kHz");	 
	m_ctlFormat.AddString("8.  ALAW 8 bit 11 kHz");	 
	m_ctlFormat.AddString("9.   PCM 8 bit 6 kHz");	 
	m_ctlFormat.AddString("10.  PCM 8 bit 8 kHz");	 
	m_ctlFormat.AddString("11.  PCM 8 bit 11 kHz");	 
	GetPlayFormatID();	// get the format ID for play file 
	m_btnPlayFormat.SetCheck(1); 
	OnPlayformat(); 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
void CWaveDlg::OnPlayformat()  
{ 
	int nPlay = m_btnPlayFormat.GetCheck(); 
	if(nPlay)  
	{ 
		if(!m_csCustom.IsEmpty())  
			m_nCustom = m_nPlayFileFormatID = m_ctlFormat.AddString(m_csCustom); 
	} 
	else if(m_nCustom)m_ctlFormat.DeleteString(m_nCustom);	// do not show 
	m_ctlFormat.SetCurSel(nPlay ? m_nPlayFileFormatID : m_nFormatID);	 
	m_ctlFormat.EnableWindow(!nPlay); 
	 
} 
 
void CWaveDlg::OnRecformat()  
{ 
	OnPlayformat(); 
} 
 
void CWaveDlg::OnSelchangeFormat()  
{ 
	m_nFormatID = m_ctlFormat.GetCurSel();	 
} 
 
// find out the format of the file selected to play 
void CWaveDlg::GetPlayFormatID() 
{ 
	WAVEFORMATEX WF; 
	HMMIO hmmH;       
	MMCKINFO mmParent, mmSubchunk; 
	DWORD dwFmtSize; 
	int nBase=0, nMax=0, nOffset = -1; 
	BOOL bWrong = FALSE; 
	 
	m_nPlayFileFormatID = 11; 
    // Open wave file  
	if(m_csFileName.IsEmpty()) hmmH = 0; 
    else hmmH = mmioOpen((LPSTR)(LPCTSTR)m_csFileName, NULL, MMIO_READ); //| MMIO_ALLOCBUF); 
    if(!hmmH)  
	{ 
    	m_csCustom = "File not found"; 
    	return; 
	} 
     
    // Locate a ‘RIFF’ chunk with a ‘WAVE’ form type  
    mmParent.fccType = mmioFOURCC('W', 'A', 'V', 'E'); 
    if (mmioDescend(hmmH, (LPMMCKINFO) &mmParent, NULL, MMIO_FINDRIFF)) 
		goto FinishGetPFID; 
    // Find the format chunk  
    mmSubchunk.ckid = mmioFOURCC('f', 'm', 't', ' '); 
    if (mmioDescend(hmmH, &mmSubchunk, &mmParent, MMIO_FINDCHUNK)) 
		goto FinishGetPFID; 
 
    // Get the size of the format chunk, allocate memory for it  
    dwFmtSize = mmSubchunk.cksize; 
	if(dwFmtSize > sizeof(WAVEFORMATEX)) goto FinishGetPFID; 
 
    // Read the format chunk  
    if (mmioRead(hmmH, (HPSTR) &WF, dwFmtSize) != (LONG) dwFmtSize) 
		goto FinishGetPFID; 
    mmioClose(hmmH, 0); 
 
	if(WF.nChannels != 1) bWrong = TRUE; 
	switch(WF.wFormatTag) 
	{ 
    	case WAVE_FORMAT_DIALOGIC_OKI_ADPCM: // 1,2 
			if(WF.wBitsPerSample != 4)  
			{ 
				bWrong = TRUE; 
				break; 
			} 
        	nBase = 0; 
		 	nMax = 1; 
		break;  
     	case WAVE_FORMAT_MULAW:				 // 3 to 5 
        	nBase = 2; 
		 	nMax = 4; 
		break;  
      	case WAVE_FORMAT_ALAW:				 // 6 to 8 
        	nBase = 5; 
		 	nMax = 7; 
		break;  
    	case WAVE_FORMAT_PCM:				 // 9 to 11 
        	nBase = 8; 
		 	nMax = 10; 
		break;  
          
      default:       
	  	m_csCustom = "Unsupported format"; 
        return;      
	} 
	if(WF.nSamplesPerSec > 5800 && 	WF.nSamplesPerSec < 6200)	// 6000 
		nOffset = 0; 
	else if(WF.nSamplesPerSec > 7800 && 	WF.nSamplesPerSec < 8200) 
		nOffset = 1; 
	else if(WF.nSamplesPerSec > 10800 && 	WF.nSamplesPerSec < 11250) 
		nOffset = 2; 
	else bWrong = TRUE; 
	if(nOffset == -1 || ((nBase + nOffset) > nMax)) bWrong = TRUE; 
	 
	if(bWrong) m_csCustom = "Unsupported format"; 
	else m_nPlayFileFormatID = nBase + nOffset; 
	return; 
FinishGetPFID: 
	m_csCustom = "Invalid file format"; 
    mmioClose(hmmH, 0); 
}