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

📄 voicectrldlg.cpp

📁 声音识别系统的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		//计算极值数量
		if(tt[count]>130)
			maxc1++;
		}

    //判断数据是否对称
	if(tk<3 &&(duichen1<-10 || duichen1>10)){
		duichen1=0;
		file.Close();
		DeleteFile(_T("current.wav"));
		delete cmb;
		delete tt;
        m_Result_Show.SetWindowText(_T("Say Again!"));
		MessageBox(_T("Say Again!"));
		tk++;
		return;
	}
	if(tk>=3)
		tk=0;

    //cmb数据复制到tt中,tt最终存储当前声音
    for(count=0;count<i;count++){
	  tt[i]=cmb[i];
	}
    mqz=i;
	delete cmb;
	file.Close();
	//DeleteFile(_T("current.wav"));

pp.Format(_T("jing=%d,i=%d,duichen=%d,max=%d,maxc=%d"),jing,mqz,duichen1,max1,maxc1);
MessageBox(pp);

    //释放空间,关闭声音文件,删除声音文件

	//////2 对比cmb(文件)与tt(当前),得出结果 ///////////////////////////////////////////////////// 
    /////////////////从文件读取每一个声音文件的名字////////////////////////////////////
	TCHAR pbuf[2000];
	if(!filedatabase.Open(_T("database.txt"),CFile::modeRead)){
//		MessageBox(_T("Date Null~!"));    //数据不存在时应先报错再返回,但是现在这里有错误。。。
	    return ;            
	}
	if(filedatabase.GetLength()==0){
//		MessageBox(_T("Date Wrong~!"));		//数据不存在时应先报错再返回,但是现在这里有错误。。。
		return ;            
	}
	 
    int a=filedatabase.GetLength();  
	filedatabase.Read(pbuf,a);     
    alltext=CString(pbuf);   	
	result=_T("result"); 
	
    while(1)
	{	             
         i=0;
		 nBeg=alltext.Find('=',nEnd);   
         nEnd=alltext.Find('\n',nBeg+1);   
         command=alltext.Mid(nBeg+1,nEnd-nBeg-2);
        
		 if(nBeg<1 && 1==mark){
		     break;
		 }
         mark=1;
		  
         nBeg=alltext.Find('=',nEnd);   
         nEnd=alltext.Find('\n',nBeg+1);          
		 filename=alltext.Mid(nBeg+1,nEnd-nBeg-2);
           
		 nBeg=alltext.Find('=',nEnd);   
         nEnd=alltext.Find('\n',nBeg+1);          
		 meaning=alltext.Mid(nBeg+1,nEnd-nBeg-2);          

		 ////////////读当前声音文件进行比较/////////////////
		 if(!file.Open(filename,CFile::modeRead)){     //创建一个标准格式文件
             MessageBox(_T("Wave file was bad!"));     //打不开文件说明用户的文件不存在	    
		     return;                    
		 }
	     cmb=(unsigned short *)malloc((file.GetLength()+1) * sizeof(char));
	     ff=(unsigned short *)malloc((file.GetLength()+1) * 2 * sizeof(char));
         file.Read(cmb,file.GetLength());

         //拆分cmb数据到ff,cmb从3000开始,读取8000个
	     ff[0]=cmb[3000]%256;
	     ff[1]=cmb[3000]/256;
  	     for(count=1;count<8000;count++){
	         ff[count*2]=cmb[count+3000]%256;
	         ff[count*2+1]=cmb[count+3000]/256;
		 }

         //提取ff数据到cmb,精度2,静音值122
	     for(count=0;count<15998;count++){
		     if((i<100||i>600)){
				 if(abs(ff[count]-jing)>2 && abs(ff[count+1]-jing)>2 && abs(ff[count+2]-jing)>2){
                     cmb[i]=ff[count];
		             i++;
					}
			 }
			 else{
			     cmb[i]=ff[count];
		         i++;
			 }
		  //计算对称度(当前)
		  duichen2=duichen2+(ff[count+1]-ff[count]);
		  //计算最高幅度值
		  if(ff[count]>max2)
		  	  max2=ff[count];
		  //计算极值数量
		  if(ff[count]>130)
			  maxc2++;
		 }

	//显示i的个数
pp.Format(_T("jing=%d,i=%d,duichen=%d,max=%d,maxc=%d"),jing,i,duichen2,max2,maxc2);
MessageBox(pp);

		 //用MessageBox显示结果
//         for(count=0;count<i;count++){
//		    pp.Format(_T("%x,"),cmb[count]);
//		    strTemp=strTemp+pp;
//		 }
//	     MessageBox(strTemp);

         //释放空间,关闭声音文件
	     delete cmb;
	     delete ff;
	     file.Close(); 

		 if(abs(mqz-i)<=re){			//有更接近的值
			 re=abs(mqz-i);
		     output=command;
		 }
		 //pp.Format(_T("result===%d,"),re);//输出返回结果
		 //MessageBox(pp);
		 //pp.Format(_T("result===%d,"),re);//输出返回结果
		 //MessageBox(pp);


	 }   
	 delete tt;
	 filedatabase.Close(); 

	//3 得出结果,显示结果 

	if(re<=800)
	{
		 pp.Format(output);
		 MessageBox(pp,_T("Result"));
	}
//	DeleteFile(_T("current.wav"));
//    output=_T("RESULT:")+output;
//    m_Result_Show.SetWindowText(output);
    ////4 执行,待做
    //这部分等前面都基本正确再做

}





void CVoiceCtrlDlg::OnMM_WIM_OPEN(UINT wParam, LONG lParam) 
{   //一开始录音,就自动调用这个
	// TODO: Add your message handler code here and/or call default
    //MessageBox(_T("luyin"));
	//SetTimer(1,100,NULL);   //设置定时器1 为100ms  
	bRecording=TRUE;  
	bEnding=FALSE;
    
 //   MessageBox(_T("OnMM_WIM_OPEN"));
	//TRACE( L"dddd\n" );
	
}


void CVoiceCtrlDlg::OnMM_WIM_DATA(UINT wParam, LONG lParam) 
{
    //缓冲区用完时,自动调用这个回调函数
//	MessageBox(_T("OnMM_WIM_DATA"));
	// TODO: Add your message handler code here and/or call default
	// Reallocate save buffer memory
	if(bEnding){
	  waveInClose(hWaveIn);  //关闭录音
      //MessageBox(_T("OnMM_WIM_DATA   wrong 11111"));
	  return;
	}
	//////////////////////////////////////////////////////////////////////////
	
	pNewBuffer =(PBYTE)realloc(pSaveBuffer, dwDataLength +((PWAVEHDR) lParam)->dwBytesRecorded) ;
	if (pNewBuffer == NULL)
	{   
		waveInClose (hWaveIn) ;
		MessageBeep (MB_ICONEXCLAMATION) ;
		AfxMessageBox(_T("erro memory"));
		return ;
	}	
	pSaveBuffer = pNewBuffer ;
    //TRACE( L"This is pNewBuffer: %s\n",pNewBuffer );
 

	CopyMemory (pSaveBuffer + dwDataLength,  //目的 //????????why to add dwDataLength??
		       ((PWAVEHDR) lParam)->lpData,   //源
	           ((PWAVEHDR) lParam)->dwBytesRecorded) ;   //长度
	
	dwDataLength += ((PWAVEHDR) lParam)->dwBytesRecorded ;

    //写入文件
    outWaveFile.SeekToEnd();
	outWaveFile.Write(pSaveBuffer,((PWAVEHDR) lParam)->dwBytesRecorded);
 
	//LPMMIOINFO lpmmioinfo;
	//mmioOpen( _T("temp.wav"), lpmmioinfo,MMIO_CREATE);

	// Send out a new buffer    //添加新的缓冲区
    //(PWAVEHDR)lParam->lpData;
    ((PWAVEHDR) lParam)->lpData=NULL;
	waveInAddBuffer (hWaveIn, (PWAVEHDR) lParam, sizeof (WAVEHDR)) ;
//	TRACE(_T("done input data\n"));
	return ;

	
}


void CVoiceCtrlDlg::OnMM_WIM_CLOSE(UINT wParam, LONG lParam) 
{
	 //MessageBox(_T("OnMM_WIM_CLOSE"));
	// TODO: Add your message handler code here and/or call default
//	KillTimer(1);       //关闭定时器
	//TRACE("MM_WIM_CLOSE\n");
	if (0==dwDataLength) { 
		return;
	}
	
    //在停止录音之后,调用调用下面的函数,断开wavehdr数据结构和音频输入设备的联系
	waveInUnprepareHeader (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveInUnprepareHeader (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
	
	//释放缓冲区
	free(pBuffer1) ;
	free(pBuffer2) ;
	//free(pSaveBuffer);
	//free(pNewBuffer);

	

	if (dwDataLength > 0)    //如果声音文件有数据(可以播放)
	{
	 // MessageBox(_T("Yes"));

	}
	else{
	   MessageBox(_T("wave file was bad!"));
	}
   
	//bRecording = FALSE ;   	
	return ;
	
}


//播放 
/*void CVoiceCtrlDlg::OnBUTTONPlay() 
{
	// TODO: Add your control notification handler code here
    //MessageBox(_T("OnBUTTONPlay() "));
	//sndPlaySound(_T("temp.wav"),SND_ASYNC);   //!!!!!!!!!!
	if (bPlaying) 
	{
    	waveOutReset(hWaveOut);
	}

	//open waveform audio for output
	waveform.wFormatTag		=	WAVE_FORMAT_PCM;
	waveform.nChannels		=	1;
	waveform.nSamplesPerSec	=11025;
	waveform.nAvgBytesPerSec=11025;
	waveform.nBlockAlign	=1;
	waveform.wBitsPerSample	=8;
	waveform.cbSize			=0;
		
	if (waveOutOpen(&hWaveOut,WAVE_MAPPER,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) 
	{
		MessageBeep(MB_ICONEXCLAMATION);
		AfxMessageBox(_T("Audio output erro"));
	}
	
	return ;
}

*/
//打开声音,播放
void CVoiceCtrlDlg::OnMM_WOM_OPEN(UINT wParam, LONG lParam){
    

	pWaveHdr1->lpData          = (LPSTR)pSaveBuffer ;
	pWaveHdr1->dwBufferLength  = dwDataLength ;
	pWaveHdr1->dwBytesRecorded = 0 ;
	pWaveHdr1->dwUser          = 0 ;
	pWaveHdr1->dwFlags         = WHDR_BEGINLOOP | WHDR_ENDLOOP ;
	pWaveHdr1->dwLoops         = 1;//dwRepetitions ;
	pWaveHdr1->lpNext          = NULL ;
	pWaveHdr1->reserved        = 0 ;
	
	// Prepare and write
	//打开音频输出设备
	waveOutPrepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveOutWrite (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	
	bEnding = FALSE ;
	bPlaying = TRUE ;
	
	
}


//打开完成
void CVoiceCtrlDlg::OnMM_WOM_DONE(UINT wParam, LONG lParam){
 
//	MessageBox(_T("OnMM_WOM_DONE"));

	//TRACE("open MM_WOM_DONE\n");
	waveOutUnprepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveOutClose (hWaveOut) ;
	
	bPaused = FALSE ;
	dwRepetitions = 1 ;
	bPlaying = FALSE ;	
	
	return  ;
	
}


void CVoiceCtrlDlg::OnMM_WOM_CLOSE(UINT wParam, LONG lParam){
//	TRACE("open MM_WOM_CLOSE\n");
    //	MessageBox(_T("OnMM_WOM_CLOSE"));

	bPaused = FALSE ;
	dwRepetitions = 1 ;
	bPlaying = FALSE ;	
    
//	MessageBox(_T("5"));
 
	return ;
	
}





 

⌨️ 快捷键说明

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