佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1579|回复: 3

如何用C++写一个Program可以持续录音, high pass filter后再储存? 高手请帮帮忙

[复制链接]
发表于 8-5-2009 09:46 PM | 显示全部楼层 |阅读模式
我是C++的初学者,需要做一个Speech Recognition System, 想请问下怎样写一个C++ Program 可以 Record Sound 的, 小弟想让Speech Recognition可以持续录音, 当有声音读进透过麦克风时, 他会自动储存有声音的一段, 好像High pass filter这样. 小弟找了很多有关C++的书, 都没教怎样录音, 请各位高手帮帮忙, 感激不尽!
回复

使用道具 举报


ADVERTISEMENT

发表于 10-5-2009 08:10 AM | 显示全部楼层
如果你用VC++的話,可以看看 waveInOpen

WAV檔也只有兩個 Channel,沒有 encoding,很容易使用。
回复

使用道具 举报

 楼主| 发表于 10-5-2009 09:42 PM | 显示全部楼层
原帖由 馬拉棧 于 10-5-2009 08:10 AM 发表 如果你用VC++的話,可以看看 waveInOpenWAV檔也只有兩個 Channel,沒有 encoding,很容易使用。


CSRTestDlg::CSRTestDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CSRTestDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CSRTestDlg)
    m_TemplateFolder = _T("c:\\test\\template");
    m_TestingFolder = _T("c:\\test\\test");
    m_Result = _T("");
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    //initialize recording variables
    m_Second=1;   
    WaveIn=new HWAVEIN;
    WaveOut=new HWAVEOUT;
    Format=new WAVEFORMATEX;
    Format->nChannels=1;    // default 1 channel, 16 bits, PCM, 8000 hz
    Format->wBitsPerSample=16;
    Format->wFormatTag=1;    // 1 for PCM_FORMAT
    Format->nSamplesPerSec=16000;        
    Format->nBlockAlign=(Format->nChannels)*(Format->wBitsPerSample)/8;
    Format->nAvgBytesPerSec=(Format->nSamplesPerSec)*(Format->nBlockAlign);
    m_TotalBytes=m_Second*Format->nAvgBytesPerSec;   
    Sample=new unsigned char[m_TotalBytes];
    unsigned int i;
    for(i=0;i<m_TotalBytes;i++)
    {    Sample=0;    }

}                    

void CSRTestDlg::OnRecord()
{
    WAVEHDR Header;
        
    if(waveInOpen(WaveIn,WAVE_MAPPER,Format,(DWORD)m_hWnd,0,CALLBACK_WINDOW))
    {    AfxMessageBox("Error opening input device!");
        return;
    }
   
    Header.dwBufferLength=m_TotalBytes;   
    Header.lpData=(char *)Sample;
    Header.dwFlags=0;
    Header.dwLoops=0;
   
    if(waveInPrepareHeader(*WaveIn,&Header,sizeof(WAVEHDR)))
    {    AfxMessageBox("Error preparing header!");
        return;
    }

    if(waveInAddBuffer(*WaveIn,&Header,sizeof(WAVEHDR)))
    {    AfxMessageBox("Error adding buffer!");
        return;
    }

    if(waveInStart(*WaveIn))
    {    AfxMessageBox("Error start recording!");
        return;
    }        
}

void CSRTestDlg::OnSave()
{
    CFileDialog fDialog(FALSE);
    fDialog.m_ofn.lpstrFilter="Speech Files(*.snd)\0*.snd\0\0";
    fDialog.m_ofn.lpstrTitle="Choose a File Name to Save";
   
    if (fDialog.DoModal()==IDCANCEL){
        return;
    }
        
    CString sphName=fDialog.GetPathName();
    if (sphName.GetLength()==0)
    {
        AfxMessageBox("Please select a speech file.");
        return;
    }
    int i;
    i=sphName.ReverseFind('.');
    if(i!=-1)
    {sphName=sphName.Left(i);}
    sphName=sphName+".snd";

    CFile sndFile;
    if(sndFile.Open(sphName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary)==FALSE)
        {    AfxMessageBox("Error Writing Speech File..");
            return;
        }

    for(i=0;i<m_TotalBytes;i++)
    {   
        sndFile.Write(&Sample,sizeof(unsigned char));
    }
               
    sndFile.Close();

}

请问下在以上的Code,在OnRecord() function 过后,wav file 是 save在那里的? 看不出在OnSave() function里面是怎样Save的,难道OnRecord后是直接link去Sample和m_TotalBytes? 然后再Write进SndFile?对不起,我对C++声音这方面不清楚,请帮帮忙

[ 本帖最后由 rufus1230 于 10-5-2009 09:46 PM 编辑 ]
回复

使用道具 举报

发表于 11-5-2009 03:40 PM | 显示全部楼层
上面個程序你從別的地方抄下來的吧?才那幾十行也懶得去研究?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 11-12-2025 11:44 PM , Processed in 0.107358 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表