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

📄 recordhwnddlg.cpp

📁 声音的线性预测编码和解码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		return;
	}
	if (!bPaused) {
		waveOutPause(hWaveOut);
		bPaused = TRUE;
	}
	else
	{
		waveOutRestart(hWaveOut);
		bPaused=FALSE;
	}	
	return ;
	
}

void CRecordHWndDlg::OnPlayStop() 
{
	// TODO: Add your control notification handler code here
	if (!bPlaying) {
		return ;
	}
	bEnding=TRUE;
	waveOutReset(hWaveOut);
	return ;

	
}


LRESULT CRecordHWndDlg::OnMM_WOM_OPEN(WPARAM wParam, LPARAM lParam){
	TRACE("open MM_WOM_OPEN\n");
	// Set up header
	
	pWaveHdr1->lpData          = (LPTSTR)pSaveBuffer ;
	pWaveHdr1->dwBufferLength  = dwDataLength ;
	pWaveHdr1->dwBytesRecorded = 0 ;
	pWaveHdr1->dwUser          = 0 ;
	pWaveHdr1->dwFlags         = WHDR_BEGINLOOP | WHDR_ENDLOOP ;
	pWaveHdr1->dwLoops         = dwRepetitions ;
	pWaveHdr1->lpNext          = NULL ;
	pWaveHdr1->reserved        = 0 ;
	
	// Prepare and write
	
	waveOutPrepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveOutWrite (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	
	bEnding = FALSE ;
	bPlaying = TRUE ;
	
	((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_SAVEFILE)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_BUTTON1)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_OPENLPC)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_SAVELPC)))->EnableWindow(FALSE);
	
	return  0L;
	
	
}

LRESULT CRecordHWndDlg::OnMM_WOM_DONE(WPARAM wParam, LPARAM lParam){

	TRACE("open MM_WOM_DONE\n");
	waveOutUnprepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveOutClose (hWaveOut) ;
	
	bPaused = FALSE ;
	dwRepetitions = 1 ;
	bPlaying = FALSE ;	
	
	return   0L;
	
}
LRESULT CRecordHWndDlg::OnMM_WOM_CLOSE(WPARAM wParam, LPARAM lParam){
	TRACE("open MM_WOM_CLOSE\n");
	bPaused = FALSE ;
	dwRepetitions = 1 ;
	bPlaying = FALSE ;	

	((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_SAVEFILE)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_BUTTON1)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_OPENLPC)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_SAVELPC)))->EnableWindow(TRUE);
	return  0L;
	
}

void CRecordHWndDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnTimer(nIDEvent);
	MMTIME mmTime;
	mmTime.wType=TIME_MS;
	TCHAR time[20];
	switch(nIDEvent) {
	case 1:
	waveInGetPosition(hWaveIn,&mmTime,sizeof(MMTIME));
	itoa(mmTime.u.ms/10000,time,10);
	((CWnd *)GetDlgItem(IDC_STATIC))->SetWindowText(time);
	}
	return;
}

void CRecordHWndDlg::OnBnClickedSavefile()
{
	// TODO: 在此添加控件通知处理程序代码
	CString FileSavedName;
	CFileDialog SaveFileDialog(FALSE,"wav");
	if(SaveFileDialog.DoModal() == IDOK)
	{
		GetCurrentDirectory(30,FileSavedName.GetBuffer(30));
		FileSavedName.ReleaseBuffer();
		FileSavedName += "\\"+SaveFileDialog.GetFileName();//+"+ABC";
		
		AfxMessageBox("Save File as " + FileSavedName,0,0);

		//	文件结构:
		//+--------------------------+
		//|标志符(RIFF) 			4|
		//+--------------------------+
		//|数据大小					4|
		//+--------------------------+
		//|格式类型("WAVE")		4|
		//+--------------------------+
		//|"fmt"					4|
		//+--------------------------+
		//|Sizeof(PCMWAVEFORMAT)	4|
		//+--------------------------+
		//|PCMWAVEFORMAT			16|
		//+--------------------------+
		//|"data"					4|
		//+--------------------------+
		//|声音数据大小				4|
		//+-------------------------+
		//|声音数据					|
		//+-------------------------+
		//
		CFile WaveFile;
		CFileException fileException;
		if(!WaveFile.Open((LPSTR)FileSavedName.GetBuffer(),//"d:\\test.wav",
			CFile::modeCreate|CFile::modeWrite,&fileException)){
			CString errMsg;
			errMsg.Format("Can't Create File %s, Err = %u",FileSavedName,fileException.m_cause);
			AfxMessageBox(errMsg,0,0);
			return;
		}
		
		DWORD dwTempNum = sizeof(PCMWAVEFORMAT) + dwDataLength + 20;
		WaveFile.Write("RIFF",4);
		WaveFile.Write((void *)&dwTempNum,4);
		WaveFile.Write("WAVE",4);
		WaveFile.Write("fmt ",4);
		dwTempNum = sizeof(PCMWAVEFORMAT);
		WaveFile.Write((void *)&dwTempNum,4);

		
		PCMWAVEFORMAT format; //定义PCMWAVEFORMAT结构对象,用来判断WAVE文件格式;
		format.wBitsPerSample = this->waveform.wBitsPerSample;

		format.wf.nAvgBytesPerSec = this->waveform.nAvgBytesPerSec;
		format.wf.nBlockAlign = this->waveform.nBlockAlign;
		format.wf.nChannels = this->waveform.nChannels;
		format.wf.nSamplesPerSec = this->waveform.nSamplesPerSec;
		format.wf.wFormatTag = this->waveform.wFormatTag;

		WaveFile.Write((void *)&format,sizeof(PCMWAVEFORMAT));
		WaveFile.Write("data",4);
		dwTempNum = this->dwDataLength;
		WaveFile.Write((void *)&dwTempNum,4);
		WaveFile.Write((void *)this->pSaveBuffer,dwTempNum);
		WaveFile.Close();
		return;
	}	

}

void CRecordHWndDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strFileOpenedName;
	CFileDialog dlgOpenFile(TRUE,"wav");
	if(dlgOpenFile.DoModal() == IDOK)
	{
		GetCurrentDirectory(30,strFileOpenedName.GetBuffer(30));
		strFileOpenedName.ReleaseBuffer();
		strFileOpenedName += "\\"+dlgOpenFile.GetFileName();//+"+ABC";
		
		AfxMessageBox("Open File " + strFileOpenedName,0,0);

		//	文件结构:
		//+--------------------------+
		//|标志符(RIFF) 			4|
		//+--------------------------+
		//|数据大小					4|
		//+--------------------------+
		//|格式类型("WAVE")		4|
		//+--------------------------+
		//|"fmt"					4|
		//+--------------------------+
		//|Sizeof(PCMWAVEFORMAT)	4|
		//+--------------------------+
		//|PCMWAVEFORMAT			16|
		//+--------------------------+
		//|"data"					4|
		//+--------------------------+
		//|声音数据大小				4|
		//+-------------------------+
		//|声音数据					|
		//+-------------------------+
		//
		CFile WaveFile;
		CFileException fileException;
		if(!WaveFile.Open((LPSTR)strFileOpenedName.GetBuffer(),//"d:\\test.wav",
			CFile::modeRead,&fileException)){
			CString errMsg;
			errMsg.Format("Can't Open File %s, Err = %u",strFileOpenedName,fileException.m_cause);
			AfxMessageBox(errMsg,0,0);
			return;
		}
		
		DWORD dwTempNum ;

		WaveFile.Seek(20,CFile::begin);
		
		PCMWAVEFORMAT format; //定义PCMWAVEFORMAT结构对象,用来判断WAVE文件格式;
		
		WaveFile.Read((void *)&format,sizeof(PCMWAVEFORMAT));

		this->waveform.wBitsPerSample = format.wBitsPerSample;

		this->waveform.nAvgBytesPerSec = format.wf.nAvgBytesPerSec ;
		this->waveform.nBlockAlign = format.wf.nBlockAlign ;
		this->waveform.nChannels = format.wf.nChannels ;
		this->waveform.nSamplesPerSec = format.wf.nSamplesPerSec;
		this->waveform.wFormatTag = format.wf.wFormatTag;
		
		this->waveform.cbSize = 0;

		WaveFile.Seek(40,CFile::begin);
		WaveFile.Read((void*)&dwTempNum,4);
		
		this->dwDataLength = dwTempNum;
		this->pSaveBuffer = (PBYTE)realloc(pSaveBuffer,this->dwDataLength) ;
		WaveFile.Read((void*)this->pSaveBuffer,this->dwDataLength);
		WaveFile.Close();


		bPaused = FALSE ;
		dwRepetitions = 1 ;
		bPlaying = FALSE ;	
	
        ((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(TRUE);		
		((CWnd *)(this->GetDlgItem(IDC_BUTTON1)))->EnableWindow(TRUE);
		((CWnd *)(this->GetDlgItem(IDC_OPENLPC)))->EnableWindow(TRUE);
		((CWnd *)(this->GetDlgItem(IDC_SAVELPC)))->EnableWindow(TRUE);
		return;
	}
}

void CRecordHWndDlg::OnBnClickedSavelpc()
{
	// TODO: 在此添加控件通知处理程序代码
	//Select File Name to save compressed data
	CFileDialog dlgSaveDialog(FALSE,"DAT");
	if(dlgSaveDialog.DoModal()!=IDOK)return;
	CString strFileSavedName ;
	GetCurrentDirectory(30,strFileSavedName.GetBuffer(30));
	strFileSavedName.ReleaseBuffer();
	strFileSavedName += "\\"+dlgSaveDialog.GetFileName();
	AfxMessageBox("File Saved as" + strFileSavedName,0,0);

	CFile WaveCompressedFile;
	CFileException fileException;
	if(!WaveCompressedFile.Open((LPSTR)strFileSavedName.GetBuffer(),//"d:\\test.wav",
		CFile::modeCreate|CFile::modeWrite,&fileException))
	{
		CString errMsg;
		errMsg.Format("Can't Create File %s, Err = %u",strFileSavedName,fileException.m_cause);
		AfxMessageBox(errMsg,0,0);
		return;
	}

	CFile DataAnalysis;
	DataAnalysis.Open(_T("c:\data.txt"),CFile::modeCreate|CFile::modeWrite);
	CString strData;

	    //Compute compressed data and write it to file
	DWORD i,j,k,l,x,y,z,m,frame = 256,N = 2,dwTemp;
	dwTemp = dwDataLength - dwDataLength%frame;
	//Write essential data
	WaveCompressedFile.Write((void*)&dwTemp,sizeof(DWORD));
	WaveCompressedFile.Write((void*)&frame,sizeof(DWORD));
	WaveCompressedFile.Write((void*)&N,sizeof(DWORD));
	//Write WAVEFORMATEX
	WaveCompressedFile.Write((void*)&this->waveform,sizeof(WAVEFORMATEX));

	//begin computing and writing each segment data into file;
	float *relation	= new float[N+1];	
	float *modulus	= new float[N*N];
	float *forecast	= new float[N];
	float *compressed	= new float[frame - N];

	//this layer loop stands for each segment for line prediction
	for(i = 0;i + frame < dwDataLength; i+=frame)
	{
		//this layer loop stands for computing each segment's prediction's relation function(0-N)
		for(j=0;j<=N;j++)
		{
			relation[j]=0;
			for(l=0;l<j;l++)
				relation[j] +=  pSaveBuffer[i+l];
			for(k=j;k<frame;k++)
			{
				relation[j] += pSaveBuffer[i+k-j]*pSaveBuffer[i+k];
			}//end for k
			relation[j] /= frame;
		}//end for j

		//this layer loop stands for building up matrix based on relation function
		for(x=0;x<N;x++)
			for(y=0;y<N;y++)		
				modulus[x*N + y] = relation[abs(x-y)];		

		//get the result of equation set by computing.
		EqualtionSet myEqualtionSet1(N,modulus,relation+1,forecast);
		myEqualtionSet1.Compute();

		//write intial value for prediction
		WaveCompressedFile.Write((void*)(pSaveBuffer+i),sizeof(BYTE)*N);
		//write prediction modulus
		WaveCompressedFile.Write((void*)forecast,sizeof(float)*N);

		//this layer stands for computing difference between prediction and actual value		
		//and write the differrences into file
		for(z=0;z<frame-N;z++)
		{
			compressed[z] = 0;
			for(m=0;m<N;m++)
				compressed[z] += pSaveBuffer[z+i+m] * forecast[m];
			compressed[z] -= pSaveBuffer[z+i+N];	
			strData.Format("%f\n",compressed[z]);
			DataAnalysis.Write(strData,strData.GetLength());
			//if(abs(compressed[z]*10)>128)AfxMessageBox("Notics",0,0);
		}		
		//and write the differrences into file
		WaveCompressedFile.Write((void*)compressed,sizeof(float)*(frame-N));	
	}//end for i
	WaveCompressedFile.Close();

	DataAnalysis.Close();

}

void CRecordHWndDlg::OnBnClickedEqualtionset()
{
	// TODO: 在此添加控件通知处理程序代码
	float a[9]={2,3,1,
			  1,2,3,
			  1,1,0};
	float b[3] ={55,
			   70,
			   15};
	float x[3];
	EqualtionSet myEqualtionSet(3,a,b,x);
	myEqualtionSet.Compute();
	CString result;
	result.Format("1:%f,2:%f,3:%f",x[0],x[1],x[2]);
	AfxMessageBox(result,0,0);

}

void CRecordHWndDlg::OnBnClickedOpenlpc()
{
		// TODO: 在此添加控件通知处理程序代码
	//Select File Name to open compressed data
	CFileDialog dlgSaveDialog(TRUE,"DAT");
	if(dlgSaveDialog.DoModal()!=IDOK)return;
	CString strFileSavedName ;
	GetCurrentDirectory(30,strFileSavedName.GetBuffer(30));
	strFileSavedName.ReleaseBuffer();
	strFileSavedName += "\\"+dlgSaveDialog.GetFileName();
	AfxMessageBox("Open File Named:" + strFileSavedName,0,0);

	CFile WaveCompressedFile;
	CFileException fileException;
	if(!WaveCompressedFile.Open((LPSTR)strFileSavedName.GetBuffer(),//"d:\\test.wav",
		CFile::modeRead,&fileException))
	{
		CString errMsg;
		errMsg.Format("Can't Open File %s, Err = %u",strFileSavedName,fileException.m_cause);
		AfxMessageBox(errMsg,0,0);
		return;
	}
	
    //read compressed data and decompress it back to user
	DWORD i,j,k,l,x,y,z,m,frame = 256,N = 2,dwTemp;
	
	//read essential data
	WaveCompressedFile.Read((void*)&dwTemp,sizeof(DWORD));
	this->dwDataLength = dwTemp;
	WaveCompressedFile.Read((void*)&frame,sizeof(DWORD));
	WaveCompressedFile.Read((void*)&N,sizeof(DWORD));
	
	this->pSaveBuffer = (PBYTE)realloc(pSaveBuffer,this->dwDataLength) ;
	//read WAVEFORMATEX
	WaveCompressedFile.Read((void*)&this->waveform,sizeof(WAVEFORMATEX));

	//begin computing and reading each segment data from file;
	float *relation	= new float[N+1];	
	float *modulus	= new float[N*N];
	float *forecast	= new float[N];
	float *difference	= new float[frame - N];
	float *temp = new float[frame - N];

	//this layer loop stands for each segment for line prediction
	for(i = 0;i < dwDataLength; i+=frame)
	{		
		//write intial value for prediction
		WaveCompressedFile.Read((void*)(pSaveBuffer+i),sizeof(BYTE)*N);
		//write prediction modulus
		WaveCompressedFile.Read((void*)forecast,sizeof(float)*N);

		// read the differrences from file			
		WaveCompressedFile.Read((void*)difference,sizeof(float)*(frame - N));
		//this layer stands for computing difference between prediction and actual value	
		for(z=0;z<frame-N;z++)
		{
			temp[z] = 0;
			for(m=0;m<N;m++)
				temp[z] += pSaveBuffer[z+i+m] * forecast[m];
			pSaveBuffer[z+i+N]=(unsigned char)(temp[z] - difference[z]);
		}		
	}//end for i
	WaveCompressedFile.Close();
	
	delete []relation;
	delete []modulus;
	delete []forecast;
	delete []difference;
	delete []temp;

	bPaused = FALSE ;
    dwRepetitions = 1 ;
	bPlaying = FALSE ;

	((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(TRUE);		
	((CWnd *)(this->GetDlgItem(IDC_BUTTON1)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_OPENLPC)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_SAVELPC)))->EnableWindow(TRUE);
}

⌨️ 快捷键说明

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