12

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?

Alconja
  • 14,834
  • 3
  • 60
  • 61
Hashim Al-Arab
  • 121
  • 1
  • 1
  • 3
  • possible duplicate of [Managed access to microphone input and system volume](http://stackoverflow.com/questions/1191613/managed-access-to-microphone-input-and-system-volume) – Andy White May 17 '10 at 22:53
  • 8
    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? – Conrad Albrecht May 18 '10 at 04:29

2 Answers2

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);
    }
}
Aleks
  • 492
  • 5
  • 5
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."

Caleb Laws
  • 71
  • 5