Skip to content

Instantly share code, notes, and snippets.

@dzonesasaki
Created October 31, 2022 08:25
Show Gist options
  • Save dzonesasaki/8ce56b3fbae87cffadac50dcea5d469e to your computer and use it in GitHub Desktop.
Save dzonesasaki/8ce56b3fbae87cffadac50dcea5d469e to your computer and use it in GitHub Desktop.
NAudio test using c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.Wave;
namespace cnsNAudioTest221031
{
internal class Program
{
static void Main(string[] args)
{
// test for using NAudio
// install NAudio library from NuGet
//ref to : https://www.fast-system.jp/c%E3%81%AEnaudio-%E3%81%A7%E3%83%9E%E3%82%A4%E3%82%AF%E3%81%8B%E3%82%89%E9%8C%B2%E9%9F%B3%E3%82%92%E3%81%99%E3%82%8B/
int deviceIndex = 0;
int fs = 44100;
int NumCh = 1;
string pathDesktop = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string strDateTime = System.DateTime.Now.ToString("yyMMdd_HHMMss");
string fnames = pathDesktop + "\\myTemp_" + strDateTime + ".wav";
WaveInEvent waveIn;
WaveFileWriter waveWriter;
waveIn = new WaveInEvent();
waveIn.DeviceNumber = deviceIndex;
waveIn.WaveFormat = new WaveFormat(fs, NumCh);
waveWriter = new WaveFileWriter(fnames,waveIn.WaveFormat);
waveIn.DataAvailable += (_, ee) =>
{
waveWriter.Write(ee.Buffer, 0, ee.BytesRecorded);
waveWriter.Flush();
};
waveIn.RecordingStopped += (_, __) =>
{
waveWriter.Flush();
};
waveIn.StartRecording();
Console.WriteLine("Recoding now ... ");
Console.WriteLine("to stop hit any key : ");
Console.ReadKey();
//System.Threading.Thread.Sleep(2000);
waveIn.StopRecording();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment