⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 g729编解码dlg.cpp

📁 使用VS6编写的、简单的、G729A编解码程序
💻 CPP
字号:
// G729编解码Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "G729编解码.h"
#include "G729编解码Dlg.h"
#include "EasyG729A.h"
#include "mmsystem.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CG729Dlg dialog

CG729Dlg::CG729Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CG729Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CG729Dlg)
	m_path = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CG729Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CG729Dlg)
	DDX_Text(pDX, IDC_EDIT1, m_path);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CG729Dlg, CDialog)
	//{{AFX_MSG_MAP(CG729Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(IDC_CODE, OnCode)
	ON_BN_CLICKED(IDC_DECODE, OnDecode)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CG729Dlg message handlers

BOOL CG729Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CG729Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CG729Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CG729Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CG729Dlg::OnOpen() 
{
	CString str;
	CFileDialog fileOpen(TRUE, NULL, NULL, 
		OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, "All Files (*.*)|*.*||", this);
	if(fileOpen.DoModal() == IDOK)
	{
		str=fileOpen.GetPathName();
		GetDlgItem(IDC_EDIT1)->SetWindowText(str);
	}
}

void CG729Dlg::OnCode() 
{
	UpdateData(true);
	if(m_path=="")
		return;
	//-------------------------------
	short			speech[L_G729A_FRAME];
	unsigned char	serial[L_G729A_FRAME_COMPRESSED];
	
	CODER_HANDLE hEncoder;
	CFile rf,wf;
	CString s_file,d_file;
	//----------------------------------
	s_file=m_path;
	d_file=m_path+"-Code-To-G729";
	hEncoder = EasyG729A_init_encoder( );
	rf.Open(s_file,CFile::modeRead);
	wf.Open(d_file,CFile::modeCreate|CFile::modeWrite);
	while (rf.Read(speech, L_G729A_FRAME*2) == L_G729A_FRAME*2) 
	{
		EasyG729A_encoder(hEncoder, speech, serial);
		
		wf.Write(serial,  L_G729A_FRAME_COMPRESSED);
	}
	
	EasyG729A_release_encoder( hEncoder );
	rf.Close();
	wf.Close();
}

void CG729Dlg::OnDecode() 
{
	UpdateData(true);
	if(m_path=="")
		return;
	//-------------------------------
	unsigned char	serial[L_G729A_FRAME_COMPRESSED];
	short			synth[L_G729A_FRAME];
	
	CODER_HANDLE hDecoder;
	CFile rf,wf;
	CString s_file,d_file;
	//----------------------------------
	s_file=m_path;
	d_file=m_path+"-Decode-From-G729.pcm";
	hDecoder = EasyG729A_init_decoder( );
	rf.Open(s_file,CFile::modeRead);
	wf.Open(d_file,CFile::modeCreate|CFile::modeWrite);
	while (rf.Read(serial, L_G729A_FRAME_COMPRESSED) == L_G729A_FRAME_COMPRESSED) 
	{
		EasyG729A_decoder(hDecoder, serial, synth );
		sndPlaySound((char * )synth,SND_MEMORY );
		wf.Write(synth,  L_G729A_FRAME*2);
	}
	
	EasyG729A_release_decoder( hDecoder );
	rf.Close();
	wf.Close();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//-----------OnButton2----------
//播放wav,mp3等格式文件:
void CG729Dlg::OnButton2() 
{
	UpdateData(true);
	if(m_path=="")
		return;
	//-------------------------------
	MCI_OPEN_PARMS          open_parms;
	DWORD                   dwReturn;
	char                    buffer[128];
	// Open the wave audio device.
	open_parms.lpstrElementName = m_path;
	
	if (dwReturn = mciSendCommand( 0, MCI_OPEN, 
		  MCI_OPEN_ELEMENT, (DWORD) (LPVOID) &open_parms))
	{
		mciGetErrorString(dwReturn, buffer, sizeof (buffer));
		AfxMessageBox( "打开失败");
	}
	else
	{
		AfxMessageBox( "Open Succeeded");
	}
	
	// Note the wave audio device ID
	wave_device_id = open_parms.wDeviceID;
	MCI_PLAY_PARMS mciPlayParms;

	if (dwReturn = mciSendCommand(wave_device_id,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPVOID) &mciPlayParms))
	{
		AfxMessageBox("播放失败");
    }
}

void CG729Dlg::OnButton3() 
{
	UpdateData(true);
	if(m_path=="")
		return;
	//-------------------------------
	unsigned char*	buf=new unsigned char [268160];
	DWORD f1=0,f2=0;
	CFile rf;
	CString s_file;
	//----------------------------------
	s_file=m_path;
	rf.Open(s_file,CFile::modeRead|CFile::shareDenyNone);

	rf.Read(buf,268160);
	f2=rf.GetLength();

	rf.Close();
	//___________________________________________________
	//___________________________________________________
	MCI_OPEN_PARMS          open_parms;
	DWORD                   dwReturn;
//	UINT                    wave_device_id;
	char                    buffer[128];
	
	// Open the wave audio device.
	open_parms.lpstrDeviceType =  (LPCSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
	open_parms.lpstrElementName = "";
	
	if (dwReturn = mciSendCommand( 0, MCI_OPEN, 
		 MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID|MCI_WAIT | MCI_OPEN_ELEMENT, (DWORD) (LPVOID) &open_parms))
	{
		mciGetErrorString(dwReturn, buffer, sizeof (buffer));
		AfxMessageBox( "打开失败");
	}
	else
	{
		AfxMessageBox( "Open Succeeded");
	}
	
	// Note the wave audio device ID
	wave_device_id = open_parms.wDeviceID;
	
	// Set PCM format of recording.
	MCI_WAVE_SET_PARMS		set_parms;
	set_parms.wFormatTag = WAVE_FORMAT_PCM;
	set_parms.wBitsPerSample = 16;
	set_parms.nChannels = 1;
	set_parms.nSamplesPerSec = 8000;
	set_parms.nAvgBytesPerSec = ((set_parms.wBitsPerSample)/8) *
								  set_parms.nChannels * set_parms.nSamplesPerSec;
	set_parms.nBlockAlign = ((set_parms.wBitsPerSample)/8) * set_parms.nChannels;

	
	if (dwReturn = mciSendCommand( wave_device_id, MCI_SET, MCI_NOTIFY|
		MCI_FORMAT_BYTES|
		MCI_WAVE_SET_FORMATTAG |
		MCI_WAVE_SET_BITSPERSAMPLE |
		MCI_WAVE_SET_CHANNELS|
		MCI_WAVE_SET_SAMPLESPERSEC|
		MCI_WAVE_SET_AVGBYTESPERSEC|
		MCI_WAVE_SET_BLOCKALIGN   ,
		(DWORD)(LPVOID)&set_parms))
	{
		mciGetErrorString(dwReturn, buffer, sizeof(buffer));
		AfxMessageBox(  "设置失败");
	}
	else
	{
		AfxMessageBox("MCI_WAVE_SET Succeeded");
	}
	
	MCI_PLAY_PARMS mciPlayParms;
	
	

    mciPlayParms.dwCallback = (DWORD) this->GetSafeHwnd();
	mciPlayParms.dwFrom=(DWORD) buf;
//	if (dwReturn = mciSendCommand(wave_device_id,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPVOID) &mciPlayParms))
	if (dwReturn = mciSendCommand(wave_device_id,MCI_PLAY,MCI_FROM,(DWORD)(LPVOID) &mciPlayParms))
	{
		AfxMessageBox("播放失败");
    }
}
//////////////////////////////////////////////////////////////////////
//--------------OnButton4---------------------
void CG729Dlg::OnButton4() 
{
	UpdateData(true);
	if(m_path=="")
		return;
	//-------------------------------
	unsigned char*	buf=new unsigned char [268160];
	DWORD f1=0,f2=0;
	CFile rf;
	CString s_file;
	//----------------------------------
	s_file=m_path;
	rf.Open(s_file,CFile::modeRead|CFile::shareDenyNone);
	
	rf.Read(buf,268160);
	f2=rf.GetLength();
	
	rf.Close();
	//___________________________________________________
	MCI_OPEN_PARMS          open_parms;
	DWORD                   dwReturn;
	char                    buffer[128];
	
	// Open the wave audio device.
	open_parms.lpstrDeviceType =  (LPCSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
	open_parms.lpstrElementName = "";
	
	if (dwReturn = mciSendCommand( 0, MCI_OPEN, 
		MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID|MCI_WAIT | MCI_OPEN_ELEMENT, (DWORD) (LPVOID) &open_parms))
	{
		mciGetErrorString(dwReturn, buffer, sizeof (buffer));
		AfxMessageBox( "打开失败");
	}
	else
	{
		AfxMessageBox( "Open Succeeded");
	}
	
	// Note the wave audio device ID
	wave_device_id = open_parms.wDeviceID;
	
	// Set PCM format of recording.
	MCI_WAVE_SET_PARMS		set_parms;
	set_parms.wFormatTag = WAVE_FORMAT_PCM;
	set_parms.wBitsPerSample = 16;
	set_parms.nChannels = 1;
	set_parms.nSamplesPerSec = 8000;
	set_parms.nAvgBytesPerSec = ((set_parms.wBitsPerSample)/8) *
		set_parms.nChannels * set_parms.nSamplesPerSec;
	set_parms.nBlockAlign = ((set_parms.wBitsPerSample)/8) * set_parms.nChannels;
	
	
	if (dwReturn = mciSendCommand( wave_device_id, MCI_SET, 
		MCI_NOTIFY|
		MCI_WAVE_SET_FORMATTAG |
		MCI_WAVE_SET_BITSPERSAMPLE |
		MCI_WAVE_SET_CHANNELS|
		MCI_WAVE_SET_SAMPLESPERSEC|
		MCI_WAVE_SET_AVGBYTESPERSEC|
		MCI_WAVE_SET_BLOCKALIGN   ,
		(DWORD)(LPVOID)&set_parms))
	{
		mciGetErrorString(dwReturn, buffer, sizeof(buffer));
		AfxMessageBox(  "设置失败");
	}
	else
	{
		AfxMessageBox("MCI_WAVE_SET Succeeded");
	}
	
	MCI_PLAY_PARMS mciPlayParms;
	
	
	
    mciPlayParms.dwCallback = (DWORD) this->GetSafeHwnd();
	mciPlayParms.dwFrom=(DWORD) 0;
//	mciPlayParms.dwFrom=(DWORD) buf;
//	if (dwReturn = mciSendCommand(wave_device_id,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPVOID) &mciPlayParms))
	if (dwReturn = mciSendCommand(wave_device_id,MCI_PLAY,MCI_FROM,(DWORD)(LPVOID) &mciPlayParms))
	{
		AfxMessageBox("播放失败");
    }
}

void CG729Dlg::OnButton5() 
{
	//Close the wave device.
	if ( mciSendCommand( wave_device_id, MCI_CLOSE,
		(DWORD)NULL, (DWORD)NULL))
	{
		AfxMessageBox( "关闭失败");
	}
	else
	{
		AfxMessageBox( "MCI_CLOSE Succeeded");
	}
	
}

void CG729Dlg::OnButton6() 
{
	UpdateData(true);
	if(m_path=="")
		return;
	//-------------------------------
	PBYTE	buf=(PBYTE)malloc(16384);// [268160];
	DWORD f1=0,f2=0;
	CFile rf;
	CString s_file;
	//----------------------------------
	s_file=m_path;
	rf.Open(s_file,CFile::modeRead|CFile::shareDenyNone);
	f1=rf.GetLength();
	
	
	//___________________________________________________
	HWAVEOUT hWaveOut;
	PWAVEHDR pwavehd;
	WAVEFORMATEX waveform;
	pwavehd=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	

	waveform.wFormatTag		=	WAVE_FORMAT_PCM;
	waveform.nChannels		=	1;
	waveform.nSamplesPerSec	=8000;
	waveform.nAvgBytesPerSec=16000;
	waveform.nBlockAlign	=1;
	waveform.wBitsPerSample	=16;
	waveform.cbSize			=0;

	if (waveOutOpen( &hWaveOut,WAVE_MAPPER,
		&waveform,(DWORD)this->m_hWnd,
		NULL,CALLBACK_WINDOW) )
	{
		MessageBeep(MB_ICONEXCLAMATION);
		AfxMessageBox("Audio output erro");
	}

	pwavehd->lpData=(LPSTR)buf;
	pwavehd->dwBufferLength=16384;
	pwavehd->dwBytesRecorded=0;
	pwavehd->dwUser=0;
	pwavehd->dwFlags=0;
	pwavehd->dwLoops=1;
	pwavehd->lpNext=NULL;
	pwavehd->reserved=0;

	while(f1!=f2)
	{
		rf.Read(buf,16000);
		f2=rf.GetLength();
		waveOutPrepareHeader(hWaveOut,pwavehd,sizeof(WAVEHDR));
		MMRESULT re;
		re=waveOutWrite(hWaveOut,pwavehd,sizeof(WAVEHDR));
	}
	//------------------------------------------------------
	rf.Close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -