Skip to content

Instantly share code, notes, and snippets.

@McTopaz
Created April 7, 2022 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McTopaz/b3b30a7bead10c44767ee8a4318b1339 to your computer and use it in GitHub Desktop.
Save McTopaz/b3b30a7bead10c44767ee8a4318b1339 to your computer and use it in GitHub Desktop.
NAudio example
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Xml.Linq;
using NAudio.CoreAudioApi;
using NAudio.Wave;
namespace ConsoleApplication
{
class Program
{
public static void Main()
{
NAudioInfo();
Record(0);
}
public static void NAudioInfo()
{
Console.WriteLine();
Console.WriteLine(" === WaveIn / WaveInCapabilities ===");
for (var i = 0; i < WaveIn.DeviceCount; i++)
{
var cap = WaveIn.GetCapabilities(i);
Console.WriteLine($"ProductName:\t{cap.ProductName}");
Console.WriteLine($"ProductGuid:\t{cap.ProductGuid}");
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine(" === MMDeviceEnumerator / MMDevice ===");
foreach (var device in new MMDeviceEnumerator().EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active))
{
Console.WriteLine($"FriendlyName:\t{device.FriendlyName}");
Console.WriteLine($"ID:\t\t{device.ID}");
Console.WriteLine();
}
Console.WriteLine();
}
public static void Record(int deviceId)
{
var wie = new WaveInEvent();
wie.DataAvailable += Wie_DataAvailable;
wie.DeviceNumber = deviceId;
wie.BufferMilliseconds = 20;
wie.StartRecording();
Console.ReadLine();
}
private static void Wie_DataAvailable(object sender, WaveInEventArgs e)
{
if (e.Buffer.Length < 1)
{
return;
}
var provider = new RawSourceWaveStream(new MemoryStream(e.Buffer), new WaveFormat(16000, 16, 1));
var player = new WaveOut();
player.DeviceNumber = 0;
player.Init(provider);
player.Play();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment