C#實現電腦麥克風錄音

本實例通過Naudio庫完成麥克風錄音並把PCM脈沖信號保存成wav音頻文件。關於音頻的采樣率、比特率、聲道等問題請查閱相關資料,本示例不做解釋。Naudio庫 請從NuGet搜索並下載。

錄音接口類:

public interface ISpeechRecorder
    {
        void SetFileName(string fileName);
        void StartRec();
        void StopRec();
    }

錄音實現類:

using System;
using NAudio.Wave;

namespace Test
{
    class NAudioRecorder : ISpeechRecorder
    {

        public WaveIn waveSource = null;
        public WaveFileWriter waveFile = null;
        private string fileName = string.Empty;

        /// <summary>
        /// 第二步:開始錄音
        /// </summary>
        public void StartRec()
        {
            try
            {
                waveSource = new WaveIn();//保證電腦有麥克接入否則報錯。
                waveSource.WaveFormat = new WaveFormat(16000, 16, 1); // 16KHz,16bit,單聲道的錄音格式

                waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
                waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);

                waveFile = new WaveFileWriter(fileName, waveSource.WaveFormat);

                waveSource.StartRecording();
            }
            catch(Exception e)
            {
                throw new Exception(e.Message);
            }
            
        }

        /// <summary>
        /// 第三步:停止錄音
        /// </summary>
        public void StopRec()
        {
            waveSource.StopRecording();

            // Close Wave(Not needed under synchronous situation)
            if (waveSource != null)
            {
                waveSource.Dispose();
                waveSource = null;
            }

            if (waveFile != null)
            {
                waveFile.Dispose();
                waveFile = null;
            }
        }

        /// <summary>
        /// 第一步:設置錄音結束後保存的文件路徑
        /// </summary>
        /// <param name="fileName">保存wav文件的路徑名</param>
        public void SetFileName(string fileName)
        {
            this.fileName = fileName;
        }

        /// <summary>
        /// 開始錄音回調函數
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
        {
            if (waveFile != null)
            {
                waveFile.Write(e.Buffer, 0, e.BytesRecorded);
                waveFile.Flush();
            }
        }

        /// <summary>
        /// 錄音結束回調函數
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
        {
            if (waveSource != null)
            {
                waveSource.Dispose();
                waveSource = null;
            }

            if (waveFile != null)
            {
                waveFile.Dispose();
                waveFile = null;
            }
        }
    }
}

調用方法:

NAudioRecorder nar=new NAudioRecorder();
nar.SetFileName(@"d:\naudio.wav");
nar.StartRec();

nar.StopRec();

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: