Created
April 7, 2022 10:02
-
-
Save McTopaz/b3b30a7bead10c44767ee8a4318b1339 to your computer and use it in GitHub Desktop.
NAudio example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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