NAudio icon indicating copy to clipboard operation
NAudio copied to clipboard

Possible to stream encode wave from mic to AAC?

Open jensolsson opened this issue 3 years ago • 0 comments

Hi

I am trying to live encode to AAC from the mic however since APIs seem to take only files as parameters I am trying to overcome the limitation using a named pipe. Is this doable or does the library not support this use case? My code below does not work, I get an error that the pipe is already in use. Any suggestions would be appreciated.

            MediaFoundationApi.Startup();

            var waveIn = new WaveInEvent();
            waveIn.WaveFormat = new WaveFormat(44100, 16, 1);

            WaveInProvider provider = new WaveInProvider(waveIn);

            waveIn.StartRecording();

            new Thread(t => {
                for (int i = 0; i < 100; i++) {
                    Thread.Sleep(100);
                }
                waveIn.StopRecording();
            }).Start();

            var pipe = new NamedPipeServerStream("recordingpipe", PipeDirection.In);
            

            new Thread(t => {
                MediaFoundationEncoder.EncodeToAac(provider, @"\\.\pipe\recordingpipe", 192000);
            }).Start();

            pipe.WaitForConnection();

            while (pipe.CanRead) {
                pipe.ReadByte();
                System.Diagnostics.Debug.Print(".");
            }

            waveIn.Dispose();
            waveIn = null;
            MediaFoundationApi.Shutdown();

jensolsson avatar Mar 07 '22 09:03 jensolsson