■ WaveInEvent 클래스를 사용해 WAV 파일을 레코딩하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
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 } } |