첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

TestProject.zip
0.00MB

▶ MainForm.cs

using System;
using System.IO;
using System.Windows.Forms;

using NAudio.Wave;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 타겟 디렉토리 경로
        /// </summary>
        private string targetDirectoryPath;

        /// <summary>
        /// 타겟 파일 경로
        /// </summary>
        private string targerFilePath;

        /// <summary>
        /// 웨이브 입력 이벤트
        /// </summary>
        private WaveInEvent waveInEvent;

        /// <summary>
        /// 웨이브 파일 작성자
        /// </summary>
        private WaveFileWriter waveFileWriter = null;

        /// <summary>
        /// 종료 여부
        /// </summary>
        private bool closing = false;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.targetDirectoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "NAudio");

            if(!Directory.Exists(this.targetDirectoryPath))
            {
                Directory.CreateDirectory(this.targetDirectoryPath);
            }

            this.targerFilePath = Path.Combine(this.targetDirectoryPath, "result.wav");

            this.waveInEvent = new WaveInEvent();

            FormClosing                       += Form_FormClosing;
            this.recordButton.Click           += recordButton_Click;
            this.stopButton.Click             += stopButton_Click;
            this.waveInEvent.DataAvailable    += waveInEvent_DataAvailable;
            this.waveInEvent.RecordingStopped += waveInEvent_RecordingStopped;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼을 닫을 경우 처리하기 - Form_FormClosing(sender, e)

        /// <summary>
        /// 폼을 닫을 경우 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.closing = true;
            
            this.waveInEvent.StopRecording();
        }

        #endregion
        #region 레코딩 버튼 클릭시 처리하기 - recordButton_Click(sender, e)

        /// <summary>
        /// 레코딩 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void recordButton_Click(object sender, EventArgs e)
        {
            this.waveFileWriter = new WaveFileWriter(this.targerFilePath, this.waveInEvent.WaveFormat);
            
            this.waveInEvent.StartRecording();

            this.recordButton.Enabled = false; 
            this.stopButton.Enabled   = true;
        }

        #endregion
        #region 중단 버튼 클릭시 처리하기 - stopButton_Click(sender, e)

        /// <summary>
        /// 중단 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void stopButton_Click(object sender, EventArgs e)
        {
            this.waveInEvent.StopRecording();
        }

        #endregion
        #region 웨이브 입력 이벤트 데이터 이용 가능시 처리하기 - waveInEvent_DataAvailable(sender, e)

        /// <summary>
        /// 웨이브 입력 이벤트 데이터 이용 가능시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void waveInEvent_DataAvailable(object sender, WaveInEventArgs e)
        {
            this.waveFileWriter.Write(e.Buffer, 0, e.BytesRecorded);

            if(this.waveFileWriter.Position > this.waveInEvent.WaveFormat.AverageBytesPerSecond * 30)
            {
                this.waveInEvent.StopRecording();
            }
        }

        #endregion
        #region 웨이브 입력 이벤트 레코딩 중단시 처리하기 - waveInEvent_RecordingStopped(sender, e)

        /// <summary>
        /// 웨이브 입력 이벤트 레코딩 중단시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void waveInEvent_RecordingStopped(object sender, StoppedEventArgs e)
        {
            this.waveFileWriter?.Dispose();

            this.waveFileWriter = null;

            this.recordButton.Enabled = true;
            this.stopButton.Enabled   = false;

            if(this.closing) 
            { 
                this.waveInEvent.Dispose();
            }
        }

        #endregion
    }
}
728x90
그리드형(광고전용)
Posted by icodebroker
,