开发者

Getting data from a microphone in C#

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?


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);
    }
}


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."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜