■ WasapiLoopbackCapture 클래스를 사용해 사운드 카드 출력을 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> /// WASAPI 루프백 캡처 /// </summary> private WasapiLoopbackCapture wasapiLoopbackCapture; /// <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.wasapiLoopbackCapture = new WasapiLoopbackCapture(); FormClosing += Form_FormClosing; this.recordButton.Click += recordButton_Click; this.stopButton.Click += stopButton_Click; this.wasapiLoopbackCapture.DataAvailable += wasapiLoopbackCapture_DataAvailable; this.wasapiLoopbackCapture.RecordingStopped += wasapiLoopbackCapture_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.wasapiLoopbackCapture.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.wasapiLoopbackCapture.WaveFormat); this.wasapiLoopbackCapture.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.wasapiLoopbackCapture.StopRecording(); } #endregion #region WASAPI 루프백 캡처 데이터 이용 가능시 처리하기 - wasapiLoopbackCapture_DataAvailable(sender, e) /// <summary> /// WASAPI 루프백 캡처 데이터 이용 가능시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void wasapiLoopbackCapture_DataAvailable(object sender, WaveInEventArgs e) { this.waveFileWriter.Write(e.Buffer, 0, e.BytesRecorded); if(this.waveFileWriter.Position > this.wasapiLoopbackCapture.WaveFormat.AverageBytesPerSecond * 30) { this.wasapiLoopbackCapture.StopRecording(); } } #endregion #region WASAPI 루프백 캡처 레코딩 중단시 처리하기 - wasapiLoopbackCapture_RecordingStopped(sender, e) /// <summary> /// WASAPI 루프백 캡처 레코딩 중단시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void wasapiLoopbackCapture_RecordingStopped(object sender, StoppedEventArgs e) { this.waveFileWriter?.Dispose(); this.waveFileWriter = null; this.recordButton.Enabled = true; this.stopButton.Enabled = false; if(this.closing) { this.wasapiLoopbackCapture.Dispose(); } } #endregion } } |
※ Windows는 기록할 특정 프로그램의 출력을 대상으로 하는 API를 제공하지 않는다.
※ WASAPI 루프백 캡처를 사용하면 특정 출력 장치에서 재생 중인 모든 오디오를 녹음할 수 있다.
※ WasapiLoopbackCapture 클래스는 기본 시스템 장치를 사용하지만 MMDeviceEnumerator로 찾을 수 있는 원하는 렌더링 MMDevice를 전달할 수 있다.