自动语音检测和录制

时间:2015-08-20 08:53:53

标签: c#

我是c#编码的新手。我正在构建一个应用程序来检测语音并自动记录它。所有可用的信息都是基于Windows语音识别引擎,但我的要求是,程序应该检测语音并记录它。

记录该文件后,使用hidden markov model(HMM)进行处理。

namespace Application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

   private void button_1_Click(object sender, EventArgs e)
    {

        List<NAudio.Wave.WaveInCapabilities> sources = new List<NAudio.Wave.WaveInCapabilities>();

        for (int i = 0; i < NAudio.Wave.WaveIn.DeviceCount; i++)
        {
            sources.Add(NAudio.Wave.WaveIn.GetCapabilities(i));
        }

        sourceList.Items.Clear();

        foreach (var source in sources)
        {
            ListViewItem item = new ListViewItem(source.ProductName);
            item.SubItems.Add(new ListViewItem.ListViewSubItem(item, source.Channels.ToString()));
            sourceList.Items.Add(item);
        }

    }

    NAudio.Wave.WaveIn sourceStream = null;
    NAudio.Wave.DirectSoundOut waveOut = null;
    NAudio.Wave.WaveFileWriter waveWriter = null;


  private void button_2_click(object sender, MouseEventArgs e)
    {

        if (sourceList.SelectedItems.Count == 0) return;

        int deviceNumber = sourceList.SelectedItems[0].Index;

        sourceStream = new NAudio.Wave.WaveIn();
        sourceStream.DeviceNumber = deviceNumber;
        sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(16000, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);

        sourceStream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailable);
        waveWriter = new NAudio.Wave.WaveFileWriter("test.wav", sourceStream.WaveFormat);
        sourceStream.StartRecording();

    }


    private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
    {

        if (waveWriter == null) return;
        waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
        waveWriter.Flush();

    }


   private void button_3_click(object sender, EventArgs e)
    {

            if (waveOut != null)
            {
                waveOut.Stop();
                waveOut.Dispose();
                waveOut = null;
            }
            if (sourceStream != null)
            {
                sourceStream.StopRecording();
                sourceStream.Dispose();
                sourceStream = null;
            }
            if (waveWriter != null)
            {
                waveWriter.Dispose();
                waveWriter = null;
            }

            speechprocessing.Model_test.speech();
    }


    private void button4_Click(object sender, EventArgs e)
    {

        this.Close();
    }
}
}

这符合我的要求但它有两个按钮来启动和终止录音过程,这不符合我的要求..

程序应该运行,当检测到语音时,必须记录并进一步处理。它应该检测到的单词也不仅仅是我们使用hidden markov model创建模型的几组单词。

所以请帮我解决这个问题。

0 个答案:

没有答案