wince录音实现

http://blog.csdn.net/zjh824/article/details/4017820

 

修改BSP里声卡驱动WaveMain.cpp里HandleWaveMessage里的消息响应:

case WIDM_OPEN:g_pHWContext->BspOpenRecChanel(TRUE);

case WIDM_CLOSE:  g_pHWContext->BspOpenRecChanel(FALSE);

在应用层使用声音输入设备里打开录音通道,

 

void HardwareContext::BspOpenRecChanel(BOOL bOpen)
{
 if(bOpen)
 {
  IOW_REG_OR(ULONG,  &(v_pGpioRegs->gpio[4].paden), GPIO22); //设置GPIO

        PIO_OUTPUT_ONE (GPIO, GPIO_GROUP(4), GPIO_INDEX(22));     
        PIO_OUTPUT_ENABLE (GPIO, GPIO_GROUP(4), GPIO_INDEX(22));
  
  //EnterCriticalSection( &csMutex );

  g_oalSysInfo.audioInfo[0].bRecordMic2 = FALSE;
  g_oalSysInfo.audioInfo[0].bRecordMic1 = FALSE;
  g_oalSysInfo.audioInfo[0].bRecordLineIn = TRUE;

    // LeaveCriticalSection( &csMutex );
       msWait(20);

 }
 else
 {
  IOW_REG_OR(ULONG,  &(v_pGpioRegs->gpio[4].paden), GPIO22);

  
        PIO_OUTPUT_ZERO (GPIO, GPIO_GROUP(4), GPIO_INDEX(22));     
       PIO_OUTPUT_ENABLE (GPIO, GPIO_GROUP(4), GPIO_INDEX(22));
  
  g_oalSysInfo.audioInfo[0].bRecordMic2 = TRUE;
  g_oalSysInfo.audioInfo[0].bRecordMic1 = TRUE;
  g_oalSysInfo.audioInfo[0].bRecordLineIn = FALSE;
        msWait(20);


 }

}

 

//修改声卡寄存器并调节输入增益

 

void HardwareContext::AudioKickInputDMA()
{
    USHORT usRegVal = 0;
 AudioPowerInput(TRUE);

 if(g_oalSysInfo.audioInfo[0].bRecordMic2)
 {

 }
 else if(g_oalSysInfo.audioInfo[0].bRecordMic1)
 {

 }
 else if(g_oalSysInfo.audioInfo[0].bRecordLineIn)
 {
   AudioInMute(FALSE);
 }

}

 

BOOL HardwareContext::AudioInMute(BOOL mute)
{
   
    if (mute)
    { // set mute on
  WriteAC97Codec( RT_LINE_IN_VOL, (unsigned short) (ac97_regs[RT_LINE_IN_VOL/2]  & 0xE808 )); //Disable LineIn to all output Mixer

  m_RTCodec5610->Enable_ADC_Input_Source(R_LINE_IN | L_LINE_IN,FALSE);      //Disable LineIn to ADC Mixer
    }
    else
    {
 // WriteAC97Codec( RT_LINE_IN_VOL, (unsigned short) (ac97_regs[RT_LINE_IN_VOL/2]  & 0x6808 ));//Enable LineIn to HP Mixer
  WriteAC97Codec( RT_LINE_IN_VOL, (unsigned short) (ac97_regs[RT_LINE_IN_VOL/2]  & 0xA000 ));//Enable LineIn to HP Mixer zjhdebug
  m_RTCodec5610->Enable_ADC_Input_Source(R_LINE_IN | L_LINE_IN,TRUE);        //Enable LineIn to ADC Mixer 
    }

 return TRUE;
}

 

应用层代码,使用CallBaCK_WINDOW方式。.h文件

  1. // RecorderDlg.h : header file   
  2. //   
  3.   
  4. #if !defined(AFX_RECORDERDLG_H__9E12B8F2_2F24_46CE_84C8_6E787E0FA8AB__INCLUDED_)   
  5. #define AFX_RECORDERDLG_H__9E12B8F2_2F24_46CE_84C8_6E787E0FA8AB__INCLUDED_   
  6.   
  7. #if _MSC_VER >= 1000   
  8. #pragma once   
  9. #endif // _MSC_VER >= 1000   
  10.   
  11. /   
  12. // CRecorderDlg dialog   
  13.   
  14. #define MAX_SIZE_INPUT_BUFFER   1024   // 数据块单元大小   
  15.   
  16. #define RECORDTIME 3000  // 录音时间   
  17.   
  18. #define MAX_NAMELENGTH 16   
  19.   
  20. #define MAX_NBEST   8   
  21.   
  22. #define WM_TIMEUP WM_USER+105  // 自定义消息   
  23.   
  24. extern "C" __declspec(dllimportshort SR_INITCE(char *RamAddress,long RamSize,char *RomAddress);  
  25. extern "C" __declspec(dllimport)  short SR_interfaceCE(char *RamAddress, char *RomAddress,   
  26.                                                        short *SpeechBuffer, long SampleAmount,   
  27.                                                        char **NameList, short NameAmount,  
  28.                                                        short *BestLemma);  
  29.   
  30. class CRecorderDlg : public CDialog  
  31. {  
  32. // Construction   
  33. public:  
  34.     CRecorderDlg(CWnd* pParent = NULL); // standard constructor   
  35.   
  36.     BOOL CheckMic();                 // 检查Mic状态   
  37.     void SetWaveFormat( WORD    wCh = 1 ,                 // 声道数量(单、双)   
  38.                         DWORD   dwSampleRate = 8000,     // 采样率   
  39.                         WORD    wBitsPerSample = 16 );    // 采样位数(比特)   
  40.     void WriteDatatoFile();          // 把数据写入文件   
  41.     void Recognize();  
  42.   
  43. // Dialog Data   
  44.     //{{AFX_DATA(CRecorderDlg)   
  45.     enum { IDD = IDD_RECORDER_DIALOG };  
  46.     CListCtrl   m_cNameList;  
  47.     CButton m_bWriteFile;  
  48.     //}}AFX_DATA   
  49.   
  50.     // ClassWizard generated virtual function overrides   
  51.     //{{AFX_VIRTUAL(CRecorderDlg)   
  52.     protected:  
  53.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support   
  54.     //}}AFX_VIRTUAL   
  55.   
  56. // Implementation   
  57. protected:  
  58.     HICON m_hIcon;  
  59.   
  60.     SHORT  m_nInputBuffer[MAX_SIZE_INPUT_BUFFER];  // 系统输入数据缓冲   
  61.     DWORD  m_dwSavedLength;      // 录音数据长度   
  62.     PBYTE  m_pCurrentData,      // 当前系统缓冲中的数据   
  63.            m_pSavedData;        // 当前自定义缓冲数据       
  64.   
  65.     HWAVEIN m_hWaveIn;         // 录音句柄   
  66.     HWAVEOUT m_hWaveOut;       // 放音句柄   
  67.     WAVEHDR m_WaveHeader;      // 音频数据头   
  68.     WAVEFORMATEX m_WaveFormat; // 音频工作格式   
  69.   
  70.     BOOL m_bEnding, m_bRecording, m_bPlaying;      // 当前工作状态   
  71.   
  72.     // Generated message map functions   
  73.     //{{AFX_MSG(CRecorderDlg)   
  74.     virtual BOOL OnInitDialog();  
  75.     afx_msg void OnRecord();  
  76.     afx_msg void OnStop();  
  77.     afx_msg void OnPlay();  
  78.     afx_msg void OnTimer(UINT nIDEvent);  
  79.     virtual void OnCancel();  
  80.     afx_msg void OnDestroy();  
  81.     afx_msg LRESULT OnMM_WIM_OPEN(WPARAM wParam,LPARAM lParam);  
  82.     afx_msg LRESULT OnMM_WIM_DATA(WPARAM wParam,LPARAM lParam);  
  83.     afx_msg LRESULT OnMM_WIM_CLOSE(WPARAM wParam,LPARAM lParam);  
  84.     afx_msg LRESULT OnMM_WOM_OPEN(WPARAM wParam,LPARAM lParam);  
  85.     afx_msg LRESULT OnMM_WOM_DONE(WPARAM wParam,LPARAM lParam);  
  86.     afx_msg LRESULT OnMM_WOM_CLOSE(WPARAM wParam,LPARAM lParam);  
  87.     afx_msg void OnLoad();  
  88.     //}}AFX_MSG   
  89.     afx_msg LRESULT OnTimeUp(UINT wParam,LONG lParam);  // 到达时间限制(响应自定义消息WM_TIMEUP)   
  90.   
  91.     DECLARE_MESSAGE_MAP()  
  92.   
  93. public:  
  94.     void Search();  
  95.     char* RomAddress;  
  96.     char* RamAddress;  
  97.       
  98.     short   Rtn_SR_Init ;  
  99.     char    **NameList;                                             //输入文件:一句语音、电话本   
  100.     short   NameAmount;  
  101.     unsigned short   NEW_NameList[MAX_NAMELENGTH];  
  102.       
  103.     short   iBest , RtnFlag , iName ;  
  104.     short   *BestLemma;                                             //在接口中没有具体定义,实际上就是RamAddress的最前面给出   
  105.       
  106.     long    RamSize, RomSize;  
  107.       
  108. };  
  109.   
  110. //{{AFX_INSERT_LOCATION}}   
  111. // Microsoft eMbedded Visual C++ will insert additional declarations immediately before the previous line.   
  112.   
  113. #endif // !defined(AFX_RECORDERDLG_H__9E12B8F2_2F24_46CE_84C8_6E787E0FA8AB__INCLUDED_)  

 

 .cpp文件:

[c-sharp] view plain copy print ?
  1. #include "stdafx.h"  
  2. #include "Recorder.h"  
  3. #include "RecorderDlg.h"  
  4.  
  5. #ifdef _DEBUG  
  6. #define new DEBUG_NEW  
  7. #undef THIS_FILE   
  8. static char THIS_FILE[] = __FILE__;  
  9. #endif   
  10.   
  11. /   
  12. // CRecorderDlg dialog   
  13.   
  14. CRecorderDlg::CRecorderDlg(CWnd* pParent /*=NULL*/)  
  15.     : CDialog(CRecorderDlg::IDD, pParent)  
  16. {  
  17.     //{{AFX_DATA_INIT(CRecorderDlg)   
  18.     //}}AFX_DATA_INIT   
  19.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32   
  20.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);  
  21.   
  22.     //设定成员初始变量   
  23.     m_pSavedData     = NULL;  
  24.     m_bRecording     = FALSE;  
  25.     m_bPlaying       = FALSE;  
  26.     m_bEnding        = FALSE;  
  27.     m_dwSavedLength  = 0;  
  28.   
  29.     Rtn_SR_Init = -1;  
  30. }  
  31.   
  32. void CRecorderDlg::DoDataExchange(CDataExchange* pDX)  
  33. {  
  34.     CDialog::DoDataExchange(pDX);  
  35.     //{{AFX_DATA_MAP(CRecorderDlg)   
  36.     DDX_Control(pDX, IDC_NAMELIST, m_cNameList);  
  37.     DDX_Control(pDX, IDC_WRITE, m_bWriteFile);  
  38.     //}}AFX_DATA_MAP   
  39. }  
  40.   
  41. BEGIN_MESSAGE_MAP(CRecorderDlg, CDialog)  
  42.     //{{AFX_MSG_MAP(CRecorderDlg)   
  43.     ON_BN_CLICKED(IDC_RECORD, OnRecord)  
  44.     ON_BN_CLICKED(IDC_STOP, OnStop)  
  45.     ON_BN_CLICKED(IDC_PLAY, OnPlay)  
  46.     ON_WM_TIMER()  
  47.     ON_WM_DESTROY()  
  48.     ON_MESSAGE(MM_WIM_OPEN,OnMM_WIM_OPEN)  
  49.     ON_MESSAGE(MM_WIM_DATA,OnMM_WIM_DATA)  
  50.     ON_MESSAGE(MM_WIM_CLOSE,OnMM_WIM_CLOSE)  
  51.     ON_MESSAGE(MM_WOM_OPEN,OnMM_WOM_OPEN)  
  52.     ON_MESSAGE(MM_WOM_DONE,OnMM_WOM_DONE)  
  53.     ON_MESSAGE(MM_WOM_CLOSE,OnMM_WOM_CLOSE)  
  54.     ON_BN_CLICKED(IDC_LOAD, OnLoad)  
  55.     //}}AFX_MSG_MAP   
  56.     ON_MESSAGE(WM_TIMEUP,OnTimeUp)  
  57. END_MESSAGE_MAP()  
  58.   
  59. /   
  60. // CRecorderDlg message handlers   
  61.   
  62. BOOL CRecorderDlg::OnInitDialog()  
  63. {  
  64.     CDialog::OnInitDialog();  
  65.   
  66.     // Set the icon for this dialog.  The framework does this automatically   
  67.     //  when the application's main window is not a dialog   
  68.     SetIcon(m_hIcon, TRUE);         // Set big icon   
  69.     SetIcon(m_hIcon, FALSE);        // Set small icon   
  70.       
  71.     CenterWindow(GetDesktopWindow());   // center to the hpc screen   
  72.   
  73.     m_bWriteFile.SetCheck(FALSE);  
  74.       
  75.     m_cNameList.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);// | LVS_EX_ONECLICKACTIVATE);   
  76.     m_cNameList.InsertColumn(0, _T("Index"), LVCFMT_LEFT, 80);  
  77.     m_cNameList.InsertColumn(1, _T("BestLemma"),LVCFMT_LEFT, 120);  
  78.   
  79.     m_cNameList.InsertItem(0, _T("1"));  
  80.     m_cNameList.InsertItem(1, _T("2"));  
  81.     m_cNameList.InsertItem(2, _T("3"));  
  82.     m_cNameList.InsertItem(3, _T("4"));  
  83.     m_cNameList.InsertItem(4, _T("5"));  
  84.     m_cNameList.InsertItem(5, _T("6"));  
  85.     m_cNameList.InsertItem(6, _T("7"));  
  86.     m_cNameList.InsertItem(7, _T("8"));  
  87.   
  88.     m_cNameList.InsertItem(8, _T("Confidence"));  
  89.     m_cNameList.InsertItem(9, _T("State"));  
  90.   
  91.     m_cNameList.SetItemText(9,1, _T("Ready"));  
  92.     // TODO: Add extra initialization here   
  93.       
  94.     return TRUE;  // return TRUE  unless you set the focus to a control   
  95. }  
  96.   
  97.   
  98.   
  99. void CRecorderDlg::OnRecord()   
  100. {  
  101.     // TODO: Add your control notification handler code here   
  102.     m_bEnding = FALSE;  
  103.   
  104.     if (m_pSavedData)  
  105.     {  
  106.         free(m_pSavedData);  
  107.         m_pSavedData = NULL;  
  108.     }  
  109.   
  110.     m_pSavedData =  reinterpret_cast<PBYTE>(malloc(1));  
  111.   
  112.     if ( !CheckMic() )  
  113.         return;  
  114.       
  115.     SetWaveFormat();  
  116.   
  117.     MMRESULT mmrResult = waveInOpen(&m_hWaveIn,WAVE_MAPPER,  
  118.                                     &m_WaveFormat,(DWORD)this->m_hWnd,  
  119.                                     NULL,CALLBACK_WINDOW);  
  120.   
  121.     if (mmrResult != MMSYSERR_NOERROR)   
  122.     {  
  123.         MessageBeep(MB_ICONEXCLAMATION);  
  124.         AfxMessageBox(CString("不能打开声音设备!"));  
  125.         return;  
  126.     }  
  127.   
  128.     m_WaveHeader.lpData  = (CHAR *)m_nInputBuffer;  
  129.     m_WaveHeader.dwBufferLength  = MAX_SIZE_INPUT_BUFFER;  
  130.     m_WaveHeader.dwBytesRecorded = 0;  
  131.     m_WaveHeader.dwUser  = 0;  
  132.     m_WaveHeader.dwLoops = 1;  
  133.     m_WaveHeader.dwFlags = 0;  
  134.     m_WaveHeader.lpNext  = NULL;  
  135.     m_WaveHeader.reserved = 0;  
  136.   
  137.     mmrResult = waveInPrepareHeader(m_hWaveIn, &m_WaveHeader, sizeof(WAVEHDR));  
  138.   
  139.     if( (mmrResult!= MMSYSERR_NOERROR) || ( m_WaveHeader.dwFlags != WHDR_PREPARED) )  
  140.     {  
  141.         AfxMessageBox(CString("waveInPrepareHeader操作失败!"));  
  142.         return;  
  143.     }  
  144.       
  145.     mmrResult = waveInAddBuffer(m_hWaveIn, &m_WaveHeader, sizeof(WAVEHDR) );  
  146.   
  147.     if(mmrResult != MMSYSERR_NOERROR)   
  148.     {  
  149.         AfxMessageBox((CString("waveInAddBuffer操作失败!")));  
  150.         return;  
  151.     }  
  152.           
  153.     m_bRecording   = TRUE ;  
  154.     m_bEnding      = FALSE ;  
  155.     m_dwSavedLength = 0 ;  
  156.   
  157.     mmrResult = waveInStart(m_hWaveIn );  
  158.   
  159.     if (mmrResult != MMSYSERR_NOERROR)   
  160.     {  
  161.         AfxMessageBox(CString("Cannot Start Wave In !"));  
  162.         return;  
  163.     }  
  164. }  
  165.   
  166. void CRecorderDlg::OnStop()   
  167. {  
  168.     // TODO: Add your control notification handler code here   
  169.     OnTimeUp(NULL, NULL);  
  170. }  
  171.   
  172. void CRecorderDlg::OnPlay()   
  173. {  
  174.     // TODO: Add your control notification handler code here   
  175.     if (!m_pSavedData)  
  176.     {  
  177.         AfxMessageBox(CString("没有录音数据!"));  
  178.         return;  
  179.     }  
  180.   
  181.   
  182.     m_bEnding = FALSE;  
  183.   
  184.     if ( m_bPlaying )   
  185.         waveOutReset(m_hWaveOut);  
  186.   
  187.     SetWaveFormat();  
  188.       
  189.     MMRESULT mmrResult = waveOutOpen(&m_hWaveOut,WAVE_MAPPER,  
  190.                                      &m_WaveFormat,(DWORD)this->m_hWnd,  
  191.                                      NULL,CALLBACK_WINDOW);  
  192.   
  193.   
  194.     if ( mmrResult != MMSYSERR_NOERROR )   
  195.     {  
  196.         MessageBeep(MB_ICONEXCLAMATION);  
  197.         AfxMessageBox(CString("音频输出错误!"));  
  198.         return;  
  199.     }  
  200. }  
  201.   
  202. void CRecorderDlg::OnTimer(UINT nIDEvent)   
  203. {  
  204.     // TODO: Add your message handler code here and/or call default   
  205.     PostMessage(WM_TIMEUP);  
  206.   
  207.     CDialog::OnTimer(nIDEvent);  
  208. }  
  209.   
  210. void CRecorderDlg::OnCancel()   
  211. {  
  212.     // TODO: Add extra cleanup here   
  213.       
  214.     CDialog::OnCancel();  
  215. }  
  216.   
  217.   
  218. LRESULT CRecorderDlg::OnTimeUp(WPARAM wParam,LPARAM lParam)  
  219. {  
  220.     waveInReset(m_hWaveIn);  
  221.   
  222.     m_bRecording = FALSE;  
  223.   
  224.     m_bEnding = TRUE;  
  225.   
  226.     KillTimer(1);  
  227.   
  228.     TRACE(CString("MSG_TIMEUP/n"));  
  229.   
  230.     return 0L;  
  231. }  
  232.   
  233. void CRecorderDlg::WriteDatatoFile()  
  234. {  
  235.     CTime tTime = CTime::GetCurrentTime();  
  236.     CString strFileName;  
  237.     CString strTime;  
  238.   
  239.     strTime.Format(_T("%d"), tTime.GetMonth());  
  240.     strFileName = strTime;  
  241.   
  242.     strTime.Format(_T("%d"), tTime.GetDay());  
  243.     strFileName += strTime;  
  244.   
  245.     strTime.Format(_T("%d"), tTime.GetHour());  
  246.     strFileName += strTime;  
  247.   
  248.     strTime.Format(_T("%d"), tTime.GetMinute());  
  249.     strFileName += strTime;  
  250.   
  251.         //tTime.Format( "%A, %B %d, %Y" );   
  252.   
  253.     strFileName += ".dat";  
  254.   
  255.     strFileName = "..//Recorder//" + strFileName;  
  256.   
  257.     CFile* fSoundFile = NULL;  
  258.   
  259.     TRY  
  260.     {  
  261.         fSoundFile = new CFile(strFileName, CFile::modeCreate | CFile::modeWrite);  
  262.           
  263.         SHORT nWrittenData;  
  264.   
  265.         PSHORT pShort = (SHORT *)m_pSavedData;   // 将系统默认的BYTE变量转为SHORT以便图形输出   
  266.         DWORD dwLength = m_dwSavedLength/2;      // 由BYTE型长度得到SHORT型长度   
  267.   
  268.         for (DWORD I=0; I<dwLength; I++)  
  269.         {  
  270.             nWrittenData = pShort[I];  
  271.             fSoundFile->Write(&nWrittenData, sizeof(SHORT));  
  272.         }  
  273.     }  
  274.     CATCH(CFileException, pEx)  
  275.         pEx->ReportError();  
  276.   
  277.     AND_CATCH(CMemoryException, pEx)  
  278.         AfxAbort();  
  279.   
  280.     END_CATCH  
  281.   
  282.     if (fSoundFile)  
  283.     {  
  284.         fSoundFile->Flush();  
  285.         fSoundFile->Close();  
  286.         delete fSoundFile;  
  287.         fSoundFile = NULL;  
  288.     }  
  289. }  
  290.   
  291.   
  292.   
  293. void CRecorderDlg::OnDestroy()   
  294. {  
  295.     CDialog::OnDestroy();  
  296.       
  297.     // TODO: Add your message handler code here   
  298.     free(m_pSavedData);  
  299.   
  300.     m_pSavedData = NULL;  
  301. }  
  302.   
  303.   
  304.   
  305. /*===================================================== 
  306. 函数名称:   ReadNameFile 
  307. 函数功能:    
  308. 输入参数:    
  309. 输出参数:    
  310. ====================================================*/  
  311. void ReadNameFile(FILE *fp_Name, char **NameList, short NameAmount)  
  312. {  
  313.     short iName;  
  314.     short Temp;  
  315.   
  316.     fscanf(fp_Name,"%d/n", &Temp);  
  317.   
  318.     for(iName=0; iName<NameAmount; iName++)  
  319.     {  
  320.         NameList[iName] = (char*)malloc(sizeof(char)*(2*MAX_NAMELENGTH+1));  
  321.     }  
  322.   
  323.     for(iName=0; iName<NameAmount; iName++)  
  324.     {  
  325.         fscanf(fp_Name,"%s/n",NameList[iName]);  
  326.     }  
  327.   
  328.     return;  
  329. }  
  330.   
  331.   
  332. void FileMatchRom(char *RomAddress, FILE *fp_Rom)  
  333. {  
  334.     long    FileLength_BYTE;  
  335.     short   *RomFile;  
  336.     RomFile = (short*)(RomAddress);  
  337.   
  338.   
  339.     fseek(fp_Rom, 0, SEEK_END);  
  340.     FileLength_BYTE = ftell(fp_Rom);  
  341.     fseek(fp_Rom, 0, SEEK_SET);  
  342.     fread(RomFile, 2, (FileLength_BYTE/2), fp_Rom);  
  343.   
  344.     return;  
  345. }  
  346.   
  347.   
  348.   
  349.   
  350. void CRecorderDlg::Recognize()  
  351. {  
  352.     char* RomAddress;  
  353.     char* RamAddress;  
  354.   
  355.     short   Rtn_SR_Init = -1;  
  356.     char    **NameList;                                             //输入文件:一句语音、电话本   
  357.     short   NameAmount;  
  358.     unsigned short   NEW_NameList[MAX_NAMELENGTH];  
  359.   
  360.     short   iBest = 0, RtnFlag = 0, iName = 0;  
  361.     short   *BestLemma;                                             //在接口中没有具体定义,实际上就是RamAddress的最前面给出   
  362.   
  363.     long    RamSize, RomSize;  
  364.   
  365.     FILE    *fp_Name;                                 
  366.     FILE    *fp_Rom;  
  367.   
  368. //  char    NameFile[100] = "//Recorder//FileIO//NameList.txt";   
  369.     char    NameFile[100] = "//Datastorage//Recorder//FileIO//NameList.txt";  
  370.     char    RomFile[100] = "//Datastorage//Recorder//FileIO//Rom_B03.bin";  //Table、Model、NetStuff合并成一个文件  
  371.   
  372.     //0418194332 0418194336 0418194341 0418194350 0418194355 0418194359 0418194403 0418194408   
  373.     //0418194412 0418194416 0418194454 0418194458 0418194503 0418194507 0418194512 0418194517 0418194521   
  374.   
  375.   
  376.     fp_Name   = fopen(NameFile,"r");  
  377.     fscanf(fp_Name, "%d", &NameAmount);  
  378.     NameList  = (char**)malloc(sizeof(char*)*NameAmount);  
  379.     ReadNameFile(fp_Name, NameList, NameAmount);  
  380.     fclose(fp_Name);  
  381.   
  382.     RamSize = 500*1024;                                             //系统供给的地址空间,保证RAM和ROM的首地址和连续的空间长度   
  383.     RomSize = 300*1024;  
  384.     RamAddress = (char*)malloc(sizeof(char)*RamSize);  
  385.     RomAddress = (char*)malloc(sizeof(char)*RomSize);  
  386.     BestLemma = (short*)(RamAddress);  
  387.   
  388.     long TickCount = 0 ;   
  389.   
  390. //==========================================================================================   
  391.   
  392. //  MyPrintf(_T("File Match Rom&Ram..."));   
  393.     m_cNameList.SetItemText(9,1, _T("File Match Rom&Ram..."));  
  394.     fp_Rom = fopen(RomFile,"rb");  
  395.     if(!fp_Rom)  
  396.     {  
  397.         OutputDebugString(_T("/r/nfailed open the rom file/r/n"));  
  398.         return;  
  399.     }  
  400.     FileMatchRom(RomAddress, fp_Rom);  
  401.     fclose(fp_Rom);  
  402.   
  403.   
  404. //  MyPrintf(_T("Rom&Ram Checking..."));   
  405.     m_cNameList.SetItemText(9,1, _T("Rom&Ram Checking..."));  
  406.     Rtn_SR_Init = SR_INITCE(RamAddress, RamSize, RomAddress);  
  407.     if(Rtn_SR_Init == 1)  
  408.     {  
  409. //      MyPrintf(_T("ROM中的数据错误"));   
  410.         AfxMessageBox(_T("ROM中的数据错误"));  
  411.         return ;  
  412.     }  
  413.     else if(Rtn_SR_Init == 2)  
  414.     {  
  415. //      MyPrintf(_T("RAM分配资源不足"));   
  416.         AfxMessageBox(_T("RAM分配资源不足"));  
  417.         return ;  
  418.     }  
  419. //  else   
  420. //      AfxMessageBox(_T("MESSAGE:Rom&Ram Checked OK!"));   
  421.           
  422.   
  423. //  MyPrintf(_T("Utterance Processing..."));   
  424.     m_cNameList.SetItemText(9,1, _T("Utterance Processing..."));  
  425.     long dwStartTime = GetTickCount();   
  426.     //测试该函数的运行时间   
  427.     RtnFlag = SR_interfaceCE(RamAddress, RomAddress, (SHORT *)m_pSavedData, m_dwSavedLength/2, NameList, NameAmount, BestLemma);  
  428.     long dwFinishTime = GetTickCount();   
  429.     TickCount = TickCount + dwFinishTime - dwStartTime;  
  430.   
  431. //  MyPrintf(_T("BestLemma List"));   
  432.     if(RtnFlag == 0)  
  433.         AfxMessageBox(_T("Speech Data Lacked!"));  
  434.     else  
  435.     {  
  436.         for(iBest=0; iBest<MAX_NBEST; iBest++)  
  437.         {  
  438.             CString str;  
  439.             MultiByteToWideChar(CP_ACP, 0, NameList[BestLemma[iBest]], -1, NEW_NameList,  MAX_NAMELENGTH);  
  440.             str.Format(_T("%d: %s"), BestLemma[iBest], NEW_NameList);  
  441.             m_cNameList.SetItemText(iBest, 1, str.GetBuffer(100));  
  442.         }  
  443.         char temp[50];  
  444.         wsprintf((LPTSTR)temp,_T("CONFIDENCE:%d!"), RtnFlag);  
  445. //      MyPrintf((LPTSTR)temp);   
  446.         m_cNameList.SetItemText(8, 1, (LPTSTR)temp);  
  447. //      if(RtnFlag>0 && RtnFlag<25)   
  448. //          MyPrintf(_T("SYSTEM SUGGESTION: Trustless Result!"));   
  449. //      else    
  450. //          if(RtnFlag>50)   
  451. //              MyPrintf(_T("SYSTEM SUGGESTION: Trusty Result!"));   
  452. //      else    
  453. //          MyPrintf(_T("SYSTEM SUGGESTION: Try Again Suggested!"));   
  454.     }  
  455.   
  456. //  free(SpeechBuf);   
  457.   
  458.     CString str;   
  459.     str.Format(_T("Done.Cost %d ms."), (int)(TickCount));   
  460.     m_cNameList.SetItemText(9,1, str);  
  461. //  AfxMessageBox(str);   
  462.   
  463.   
  464. //===========================================================================================   
  465.   
  466.     free(RamAddress);  
  467.     free(RomAddress);  
  468.     return ;  
  469. }  
  470.   
  471.   
  472. BOOL CRecorderDlg::CheckMic()  
  473. {  
  474.     MMRESULT mmrResult;  
  475.   
  476.     mmrResult = waveInGetNumDevs();   
  477.   
  478.     if (mmrResult == 0)  
  479.     {  
  480.         AfxMessageBox(CString("未检测到声音设备!"));  
  481.         return FALSE;  
  482.     }  
  483.   
  484.     WAVEINCAPS  m_WaveInDevCaps;  
  485.    
  486.     mmrResult = waveInGetDevCaps (0, &m_WaveInDevCaps, sizeof(WAVEINCAPS));  
  487.      
  488.     if ( mmrResult != MMSYSERR_NOERROR)  
  489.     {  
  490.         AfxMessageBox(CString("声卡类型不可知!"));  
  491.         return FALSE;  
  492.     }  
  493.   
  494.     return TRUE;  
  495. }  
  496.   
  497. /   
  498. //                                                                            
  499. //  函数名:      SetWaveFormat()       
  500. //  返回值:      无       
  501. //  功能:        设定数据采集时的编码方法、采样率等格式。   
  502. //  注释:        无   
  503. //                                                                            
  504. /   
  505.   
  506. void CRecorderDlg::SetWaveFormat(WORD wCh,  DWORD dwSampleRate, WORD wBitsPerSample)   
  507. {  
  508.     m_WaveFormat.wFormatTag      = WAVE_FORMAT_PCM;         // PCM编码格式   
  509.     m_WaveFormat.nChannels       = wCh;                     // 默认为1   
  510.     m_WaveFormat.nSamplesPerSec  = dwSampleRate;            // 采样率   
  511.     m_WaveFormat.nAvgBytesPerSec = dwSampleRate * wCh * wBitsPerSample/8;  // 标准计算公式   
  512.     m_WaveFormat.nBlockAlign     = wCh * wBitsPerSample/8;  // 标准计算公式   
  513.     m_WaveFormat.wBitsPerSample  = wBitsPerSample;          // 默认为16比特   
  514.     m_WaveFormat.cbSize          = 0;                       // 保留   
  515. }  
  516.   
  517. /   
  518. //                                                                            
  519. //  函数名:      OnMM_WIM_OPEN()       
  520. //  返回值:      无       
  521. //  功能:        重载对系统消息MM_WIM_OPEN的响应。   
  522. //  注释:        当声音设备被打开时发送此消息。   
  523. //                                                                            
  524. /   
  525.   
  526. LRESULT CRecorderDlg::OnMM_WIM_OPEN(WPARAM wParam, LPARAM lParam)   
  527. {  
  528.     m_bRecording=TRUE;  
  529.   
  530.     GetDlgItem(IDC_RECORD)->EnableWindow(FALSE);  
  531.     GetDlgItem(IDC_STOP)->EnableWindow(TRUE);  
  532.   
  533.     SetTimer(1, RECORDTIME, NULL);  
  534.   
  535.     TRACE(CString("MM_WIM_OPEN/n"));  
  536.   
  537.     return 0L;  
  538. }  
  539.   
  540. /   
  541. //                                                                            
  542. //  函数名:      OnMM_WIM_DATA()       
  543. //  返回值:      无       
  544. //  功能:        重载对系统消息MM_WIM_DATA的响应。   
  545. //  注释:        当系统缓冲已满,或者waveInReset函数被调用时,发送此消息。   
  546. //                                                                            
  547. /   
  548.   
  549. LRESULT CRecorderDlg::OnMM_WIM_DATA(WPARAM wParam, LPARAM lParam)   
  550. {  
  551.     PBYTE m_pCurrentData = (PBYTE)realloc(m_pSavedData,   
  552.                                           m_dwSavedLength + ((PWAVEHDR) lParam)->dwBytesRecorded) ;  
  553.       
  554.     if (!m_pCurrentData)  
  555.     {  
  556.         waveInClose (m_hWaveIn) ;  
  557.         MessageBeep (MB_ICONEXCLAMATION) ;  
  558.         AfxMessageBox(CString("内存分配失败。请调低采样率,或检查可用的系统内存。"));  
  559.         CDialog::OnCancel();  
  560.     }  
  561.       
  562.     m_pSavedData = m_pCurrentData ;  
  563.       
  564.     CopyMemory(m_pSavedData + m_dwSavedLength, ((PWAVEHDR) lParam)->lpData,  
  565.         ((PWAVEHDR) lParam)->dwBytesRecorded) ;  
  566.       
  567.     m_dwSavedLength += ((PWAVEHDR) lParam)->dwBytesRecorded;  
  568.       
  569.     if (m_bEnding)  
  570.     {  
  571.         waveInClose (m_hWaveIn);  
  572.         return 1L;  
  573.     }  
  574.       
  575.     waveInAddBuffer (m_hWaveIn, (PWAVEHDR) lParam, sizeof (WAVEHDR)) ;  
  576.   
  577.     return 0L;  
  578. }  
  579.   
  580. /   
  581. //                                                                            
  582. //  函数名:      OnMM_WIM_CLOSE()       
  583. //  返回值:      无       
  584. //  功能:        重载对系统消息MM_WIM_CLOSE的响应。   
  585. //  注释:        音频设备被关闭时,发送此消息。发送此消息后,音频设备句柄   
  586. //               将不再有效。   
  587. //                                                                            
  588. /   
  589.   
  590. LRESULT CRecorderDlg::OnMM_WIM_CLOSE(WPARAM wParam, LPARAM lParam)   
  591. {  
  592.     TRACE(CString("MM_WIM_CLOSE/n"));  
  593.   
  594.     if ( m_dwSavedLength==0 )   
  595.         return 1L;  
  596.       
  597.     waveInUnprepareHeader(m_hWaveIn, &m_WaveHeader, sizeof (WAVEHDR)) ;  
  598.   
  599.     m_bRecording = FALSE ;  
  600.   
  601.     // 在这里做识别   
  602. //  Recognize();   
  603.     Search();  
  604.   
  605.     if (m_bWriteFile.GetCheck())  
  606.         WriteDatatoFile();  
  607.   
  608.     GetDlgItem(IDC_RECORD)->EnableWindow(TRUE);  
  609.     GetDlgItem(IDC_STOP)->EnableWindow(FALSE);  
  610.   
  611. //  AfxMessageBox(CString("录音完毕"));   
  612.   
  613.     return 0L;  
  614. }  
  615.   
  616. /   
  617. //                                                                            
  618. //  函数名:      OnMM_WOM_OPEN()       
  619. //  返回值:      无       
  620. //  功能:        重载对系统消息MM_WOM_OPEN的响应。   
  621. //  注释:        音频设备被打开时,发送此消息。   
  622. //                                                                            
  623. /   
  624.   
  625. LRESULT CRecorderDlg::OnMM_WOM_OPEN(WPARAM wParam, LPARAM lParam)  
  626. {  
  627.     TRACE(CString("MM_WOM_OPEN /n"));  
  628.   
  629.     GetDlgItem(IDC_PLAY)->EnableWindow(FALSE);  
  630.   
  631.     m_WaveHeader.lpData          = (LPSTR)m_pSavedData ;  
  632.     m_WaveHeader.dwBufferLength  = m_dwSavedLength ;  
  633.     m_WaveHeader.dwBytesRecorded = 0 ;  
  634.     m_WaveHeader.dwUser          = 0 ;  
  635.     m_WaveHeader.dwFlags         = WHDR_BEGINLOOP | WHDR_ENDLOOP ;  
  636.     m_WaveHeader.dwLoops         = 1;  
  637.     m_WaveHeader.lpNext          = NULL ;  
  638.     m_WaveHeader.reserved        = 0 ;  
  639.       
  640.     waveOutPrepareHeader (m_hWaveOut, &m_WaveHeader, sizeof (WAVEHDR)) ;  
  641.     waveOutWrite (m_hWaveOut, &m_WaveHeader, sizeof (WAVEHDR)) ;  
  642.       
  643.     m_bEnding = FALSE ;  
  644.     m_bPlaying = TRUE ;  
  645.   
  646.     return 0L;  
  647. }  
  648.   
  649. /   
  650. //                                                                            
  651. //  函数名:      OnMM_WOM_DONE()       
  652. //  返回值:      无       
  653. //  功能:        重载对系统消息MM_WOM_DONE的响应。   
  654. //  注释:        当放音结束,或者waveOutReset函数被调用时,发送此消息   
  655. //                                                                            
  656. /   
  657.   
  658. LRESULT CRecorderDlg::OnMM_WOM_DONE(WPARAM wParam, LPARAM lParam)  
  659. {  
  660.   
  661.     TRACE(CString("MM_WOM_DONE/n"));  
  662.   
  663.     waveOutUnprepareHeader (m_hWaveOut, &m_WaveHeader, sizeof (WAVEHDR)) ;  
  664.     waveOutClose (m_hWaveOut) ;  
  665.       
  666.     m_bPlaying = FALSE ;  
  667.   
  668.     return 0L;  
  669. }  
  670.   
  671. /   
  672. //                                                                            
  673. //  函数名:      OnMM_WOM_CLOSE()       
  674. //  返回值:      无       
  675. //  功能:        重载对系统消息MM_WOM_CLOSE的响应。   
  676. //  注释:        音频设备被关闭时,发送此消息。发送此消息后,音频设备句柄   
  677. //               将不再有效。   
  678. //                                                                            
  679. /   
  680.   
  681. LRESULT CRecorderDlg::OnMM_WOM_CLOSE(WPARAM wParam, LPARAM lParam)  
  682. {  
  683.     TRACE(CString("MM_WOM_CLOSE/n"));  
  684.   
  685.     m_bEnding = TRUE ;  
  686.     m_bPlaying = FALSE ;  
  687.   
  688.     GetDlgItem(IDC_PLAY)->EnableWindow(TRUE);  
  689.   
  690.     AfxMessageBox(CString("放音完毕"));  
  691.   
  692.     return 0L;  
  693. }  
  694.   
  695.   
  696.   
  697.   
  698.   
  699. void CRecorderDlg::OnLoad()   
  700. {  
  701.     // TODO: Add your control notification handler code here   
  702.     Rtn_SR_Init = -1;  
  703.     iBest = 0, RtnFlag = 0, iName = 0;  
  704.   
  705.     HANDLE hDev;  
  706.       
  707.     hDev = CreateFile(TEXT("FME1:"), GENERIC_READ|GENERIC_WRITE,  
  708.                                 FILE_SHARE_READ|FILE_SHARE_WRITE,  
  709.                                 NULL, OPEN_ALWAYS, 0, 0);  
  710.       
  711.     CloseHandle(hDev);  
  712.   
  713.     FILE    *fp_Name;                                 
  714.     FILE    *fp_Rom;  
  715.       
  716.     //  char    NameFile[100] = "//Recorder//FileIO//NameList.txt";   
  717.     char    NameFile[100] = "//Datastorage//Recorder//FileIO//NameList.txt";  
  718.     char    RomFile[100] = "//Datastorage//Recorder//FileIO//Rom_B03.bin";  //Table、Model、NetStuff合并成一个文件  
  719.       
  720.     //0418194332 0418194336 0418194341 0418194350 0418194355 0418194359 0418194403 0418194408   
  721.     //0418194412 0418194416 0418194454 0418194458 0418194503 0418194507 0418194512 0418194517 0418194521   
  722.       
  723.       
  724.     fp_Name   = fopen(NameFile,"r");  
  725.     fscanf(fp_Name, "%d", &NameAmount);  
  726.     NameList  = (char**)malloc(sizeof(char*)*NameAmount);  
  727.     ReadNameFile(fp_Name, NameList, NameAmount);  
  728.     fclose(fp_Name);  
  729.       
  730.     RamSize = 500*1024;                                             //系统供给的地址空间,保证RAM和ROM的首地址和连续的空间长度   
  731.     RomSize = 300*1024;  
  732.     RamAddress = (char*)malloc(sizeof(char)*RamSize);  
  733.     RomAddress = (char*)malloc(sizeof(char)*RomSize);  
  734.     BestLemma = (short*)(RamAddress);  
  735.       
  736.     long TickCount = 0 ;   
  737.       
  738.     //==========================================================================================   
  739.       
  740.     //  MyPrintf(_T("File Match Rom&Ram..."));   
  741.     m_cNameList.SetItemText(9,1, _T("File Match Rom&Ram..."));  
  742.     fp_Rom = fopen(RomFile,"rb");  
  743.     if(!fp_Rom)  
  744.     {  
  745.         OutputDebugString(_T("/r/nfailed open the rom file/r/n"));  
  746.         return;  
  747.     }  
  748.     FileMatchRom(RomAddress, fp_Rom);  
  749.     fclose(fp_Rom);  
  750.       
  751.       
  752.     //  MyPrintf(_T("Rom&Ram Checking..."));   
  753.     m_cNameList.SetItemText(9,1, _T("Rom&Ram Checking..."));  
  754.     Rtn_SR_Init = SR_INITCE(RamAddress, RamSize, RomAddress);  
  755.     if(Rtn_SR_Init == 1)  
  756.     {  
  757.         //      MyPrintf(_T("ROM中的数据错误"));   
  758.         AfxMessageBox(_T("ROM中的数据错误"));  
  759.         return ;  
  760.     }  
  761.     else if(Rtn_SR_Init == 2)  
  762.     {  
  763.         //      MyPrintf(_T("RAM分配资源不足"));   
  764.         AfxMessageBox(_T("RAM分配资源不足"));  
  765.         return ;  
  766.     }  
  767.     m_cNameList.SetItemText(9,1, _T("Load OK..."));  
  768.       
  769. }  
  770.   
  771. void CRecorderDlg::Search()  
  772. {  
  773.     m_cNameList.SetItemText(9,1, _T("Utterance Processing..."));  
  774.     long TickCount = 0 ;   
  775.   
  776.     long dwStartTime = GetTickCount();   
  777.     //测试该函数的运行时间   
  778.     RtnFlag = SR_interfaceCE(RamAddress, RomAddress, (SHORT *)m_pSavedData, m_dwSavedLength/2, NameList, NameAmount, BestLemma);  
  779.     long dwFinishTime = GetTickCount();   
  780.     TickCount = TickCount + dwFinishTime - dwStartTime;  
  781.       
  782.     //  MyPrintf(_T("BestLemma List"));   
  783.     if(RtnFlag == 0)  
  784.         AfxMessageBox(_T("Speech Data Lacked!"));  
  785.     else  
  786.     {  
  787.         for(iBest=0; iBest<MAX_NBEST; iBest++)  
  788.         {  
  789.             CString str;  
  790.             MultiByteToWideChar(CP_ACP, 0, NameList[BestLemma[iBest]], -1, NEW_NameList,  MAX_NAMELENGTH);  
  791.             str.Format(_T("%d: %s"), BestLemma[iBest], NEW_NameList);  
  792.             m_cNameList.SetItemText(iBest, 1, str.GetBuffer(100));  
  793.         }  
  794.         char temp[50];  
  795.         wsprintf((LPTSTR)temp,_T("CONFIDENCE:%d!"), RtnFlag);  
  796.         //      MyPrintf((LPTSTR)temp);   
  797.         m_cNameList.SetItemText(8, 1, (LPTSTR)temp);  
  798.         //      if(RtnFlag>0 && RtnFlag<25)   
  799.         //          MyPrintf(_T("SYSTEM SUGGESTION: Trustless Result!"));   
  800.         //      else    
  801.         //          if(RtnFlag>50)   
  802.         //              MyPrintf(_T("SYSTEM SUGGESTION: Trusty Result!"));   
  803.         //      else    
  804.         //          MyPrintf(_T("SYSTEM SUGGESTION: Try Again Suggested!"));   
  805.     }  
  806.       
  807.     //  free(SpeechBuf);   
  808.       
  809.     CString str;   
  810.     str.Format(_T("Done.Cost %d ms."), (int)(TickCount));   
  811.     m_cNameList.SetItemText(9,1, str);  
  812. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值