filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.14k stars 450 forks source link

How write Wav PCM file? #459

Open LordKmon opened 2 years ago

LordKmon commented 2 years ago

I am trying to record audio stream from a microphone to file (wav format). By default, the file is recorded in stereo, the IEEE Float codec, but I need to record audio in PCM-format (16 kHz, Mono)

Where should this format be used in this program code? (variable need_wave_format)

The project uses NuGet CSCore.

Link to this sample project: https://github.com/LordKmon/CsCoreRecordProblem

Code from winform:

    public partial class Form1 : Form
        {
            private WasapiCapture m_SoundKeeper;
            private IWriteable m_Writer;
            private IWaveSource m_FinalSource;

            public Form1()
            {
                InitializeComponent();
            }

            private void btn_StartRecord_Click(object sender, EventArgs event_args)
            {
                //Find and set Device (microphone)
                MMDevice selected_device = null;
                using (var deviceEnumerator = new MMDeviceEnumerator())
                using (var deviceCollection = deviceEnumerator.EnumAudioEndpoints(DataFlow.Capture, DeviceState.Active))
                {
                    selected_device = deviceCollection[0];
                }

                // Format that I needed
                WaveFormat need_wave_format = new WaveFormat(16000, 16, 1, AudioEncoding.Pcm);

                // Start record
                m_SoundKeeper = new WasapiCapture();
                m_SoundKeeper.Device = selected_device;
                m_SoundKeeper.Initialize();
                var soundInSource = new SoundInSource(m_SoundKeeper);

                var singleBlockNotificationStream = new SingleBlockNotificationStream(soundInSource.ToSampleSource());
                m_FinalSource = singleBlockNotificationStream.ToWaveSource();

                m_Writer = new WaveWriter("output.wav", m_FinalSource.WaveFormat);

                byte[] buffer = new byte[m_FinalSource.WaveFormat.BytesPerSecond / 2];
                soundInSource.DataAvailable += (s, e) =>
                {
                    int read;
                    while ((read = m_FinalSource.Read(buffer, 0, buffer.Length)) > 0)
                        m_Writer.Write(buffer, 0, read);
                };

                l_Status.Text = "RECORD !!!";
                m_SoundKeeper.Start();
            }

            private void btn_Stop_Click(object sender, EventArgs e)
            {
                if (m_SoundKeeper == null)
                    return;

                m_SoundKeeper.Stop();
                m_SoundKeeper.Dispose();
                m_SoundKeeper = null;
                m_FinalSource.Dispose();

                if (m_Writer is IDisposable)
                    ((IDisposable)m_Writer).Dispose();

                l_Status.Text = "...";
            }
        }
    }

Where do I should to use variable "need_wave_format" in this code so that the output is a file of the Wav-PCM format?