Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record audio from microphone and speaker in C#

Tags:

c#

naudio

I'm building an application which will record audio from the microphone and speaker to a file (.wav). I want my application to write data to the file from microphone and speaker to the same merged file. I'm having trouble to save the merged file for both. it saves only for microphone.

This is my code:

using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test_Recorder
{
    public partial class Form2 : Form
    {
        private WaveIn recorder;
        private BufferedWaveProvider bufferedWaveProvider;
        private SavingWaveProvider savingWaveProvider;
        private WaveOut player;
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StartRecording();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StopRecording();
        }
        public void StartRecording()
        {
            // set up the recorder
            recorder = new WaveIn();
            //recorder = new WasapiLoopbackCapture();
            //recorder = new WaveIn();
            recorder.DataAvailable += RecorderOnDataAvailable;

            // set up our signal chain
            bufferedWaveProvider = new BufferedWaveProvider(recorder.WaveFormat);
            savingWaveProvider = new SavingWaveProvider(bufferedWaveProvider, Guid.NewGuid() + ".wav");

            // set up playback
            player = new WaveOut();
            player.Init(savingWaveProvider);
            player.Volume = 1;
            // begin playback & record
            player.Play();
            recorder.StartRecording();
        }
        public void StopRecording()
        {
            // stop recording
            recorder.StopRecording();
            // stop playback
            player.Stop();
            // finalise the WAV file
            savingWaveProvider.Dispose();
        }
        private void RecorderOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
        {
            bufferedWaveProvider.AddSamples(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //rec.BeginRecording();
            Form3 frm = new Form3();
            this.Hide();
            frm.Show();
        }
    }


    class SavingWaveProvider : IWaveProvider, IDisposable
    {
        private readonly IWaveProvider sourceWaveProvider;
        private readonly WaveFileWriter writer;
        private bool isWriterDisposed;

        public SavingWaveProvider(IWaveProvider sourceWaveProvider, string wavFilePath)
        {
            this.sourceWaveProvider = sourceWaveProvider;
            writer = new WaveFileWriter(wavFilePath, sourceWaveProvider.WaveFormat);
        }

        public int Read(byte[] buffer, int offset, int count)
        {
            var read = sourceWaveProvider.Read(buffer, offset, count);
            if (count > 0 && !isWriterDisposed)
            {
                writer.Write(buffer, offset, read);
            }
            if (count == 0)
            {
                Dispose(); // auto-dispose in case users forget
            }
            return read;
        }

        public WaveFormat WaveFormat { get { return sourceWaveProvider.WaveFormat; } }

        public void Dispose()
        {
            if (!isWriterDisposed)
            {
                isWriterDisposed = true;
                writer.Dispose();
            }
        }
    }
}

how i can do for both speaker and microphone to a same file?

like image 690
M Aleem Akbar Avatar asked Nov 21 '25 02:11

M Aleem Akbar


1 Answers

First off all you can NOT save 2 data streams to one file that is already open. You need to create 2 separate Memory Streams and join them with some fancy magic dll :) Then you can save this one memory stream into file with StreamWriter or other file writing methods

To save you googling i found this little piece of code witch might help you solve your problem. http://www.codeproject.com/Articles/31356/C-MP3-Sound-Capturing-Recording-Component, along with this How to record audio from microphone (Line-In) and playback device (speakers Line-Out) to one MP3 file

One more tip. Its better practice to use "using" clause then call Dispose.

like image 128
Tomasz Juszczak Avatar answered Nov 23 '25 14:11

Tomasz Juszczak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!