Getting data from a microphone in C#
Asked Answered
V

2

13

I'm trying to record audio data from a microphone (or line-in), and then replay it again, using C#.

Any suggestions on how I can achieve this?

Vamp answered 17/5, 2010 at 22:44 Comment(2)
possible duplicate of Managed access to microphone input and system volumeCampbellbannerman
Could you tell us why you feel compelled to be sarcastic in showing that you found a search that works, especially since "C#" is a useless term in many search engines?Boito
B
3

See Console and multithreaded recording and playback

class Program
{

    static void Main(string[] args)
    {
        rex.Data += new RecorderEx.DataEventHandler(rex_Data);
        rex.Open += new EventHandler(rex_Open);
        rex.Close += new EventHandler(rex_Close);
        rex.Format = pcmFormat;
        rex.StartRecord();
        Console.WriteLine("Please press enter to exit!");
        Console.ReadLine();
        rex.StopRecord();
    }

    static RecorderEx rex = new RecorderEx(true);
    static PlayerEx play = new PlayerEx(true);
    static IntPtr pcmFormat = AudioCompressionManager.GetPcmFormat(1, 16, 44100);

    static void rex_Open(object sender, EventArgs e)
    {
        play.OpenPlayer(pcmFormat);
        play.StartPlay();
    }

    static void rex_Close(object sender, EventArgs e)
    {
        play.ClosePlayer();
    }

    static void rex_Data(object sender, DataEventArgs e)
    {
        byte[] data = e.Data;
        play.AddData(data);
    }
}
Bruns answered 2/2, 2014 at 12:32 Comment(0)
D
1

A live link of NAudio.

https://github.com/naudio/NAudio

It's available as a NuGet Package

Information about Output devices:

https://github.com/naudio/NAudio/blob/master/Docs/OutputDeviceTypes.md

Keep in mind for performance the following from the FAQ:

"Is .NET Performance Good Enough for Audio?

While .NET cannot compete with unmanaged languages for very low latency audio work, it still performs better than many people would expect. On a fairly modest PC, you can quite easily mix multiple WAV files together, including pass them through various effects and codecs, play back glitch free with a latency of around 50ms."

Disproportionation answered 3/2, 2022 at 22:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.