728x90
728x170
▶ 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
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 누겟 설치 : jacobslusser.ScintillaNET (0) | 2022.01.07 |
---|---|
[C#/WINFORM] MethodInvoker 대리자 : 크로스 스레드(Cross Thread) 처리하기 (0) | 2021.12.31 |
[C#/WINFORM/.NET5] 웹 페이지에서 프로그램 실행하기 (0) | 2021.12.31 |
[C#/WINFORM/.NET5] 웨이브 폼 렌더링 사용하기 (0) | 2021.12.29 |
[C#/WINFORM/.NET5] WasapiLoopbackCapture 클래스 : 사운드 카드 출력을 WAV 파일로 레코딩하기 (0) | 2021.12.28 |
[C#/WINFORM/.NET5] WaveOutEvent 클래스 : Volume 속성을 사용해 볼륨 설정하기 (0) | 2021.12.26 |
[C#/WINFORM/.NET5] WaveOutEvent 클래스 : 오디오 파일 재생하기 (기능 개선) (0) | 2021.12.26 |
[C#/WINFORM/.NET5] WaveOutEvent 클래스 : 오디오 파일 재생하기 (0) | 2021.12.26 |
[C#/WINFORM/.NET5] 마이크 입력 스펙트로그램 표시하기 (0) | 2021.12.18 |
[C#/WINFORM] 사운드 필터링 사용하기 (0) | 2021.12.09 |