Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Recording audio and save as Raw data (A-Law)

$
0
0

Hmm,

I'm having some troubles with the recorded audio. Basically the above function do the job pretty well.

But, there is some noise at the beginning of the file and this noise is a consequence of the fact that I'm encoding the wav header.

So, you told me to use wavefileREader and all the rest.

But...

I get this error when trying to read a memorystream containing the recorded audio

Invalid WAV file - No fmt chunk found

Looks like the fmt chucnk will be added when closing the writer, but since my writer is connected to a memorystream, the result is that my memorystream is empty.

To keep it simple, I post the code I'm using, I guess there is something wrong, but have no idea o'what.

 

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;
using NAudio;
using NAudio.Wave;
using System.IO;

namespace SoundsCS
{
    public partial class frmRecord : Form
    {
        public frmRecord()
        {
            InitializeComponent();
        }

        IWaveIn waveIn;
        WaveFileWriter writer;
        MemoryStream memStream;

        private void cmdStartRec_Click(object sender, EventArgs e)
        {
            if (waveIn == null)
            {
                memStream = new MemoryStream();
                
                waveIn = new WaveIn();
                waveIn.WaveFormat = new WaveFormat(8000, 1);

                writer = new WaveFileWriter(memStream, waveIn.WaveFormat);

                waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
                waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
                waveIn.StartRecording();
                cmdStartRec.Enabled = false;
            }
        }

        void waveIn_RecordingStopped(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new EventHandler(waveIn_RecordingStopped), sender, e);
            }
            else
            {
                waveIn.Dispose();
                waveIn = null;

                ByteArrayToFile(@"C:\temp\miofile.raw", GetALawBytes(ref memStream));

                memStream.Position = 0;
                ByteArrayToFile(@"C:\temp\miofilewav.wav", memStream.ToArray());

                writer.Close();
                writer = null;

                cmdStartRec.Enabled = true;
                //progressBar1.Value = 0;
            }
        }

        void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            if (this.InvokeRequired)
            {
                //Debug.WriteLine("Data Available");
                this.BeginInvoke(new EventHandler<WaveInEventArgs>(waveIn_DataAvailable), sender, e);
            }
            else
            {
                //Debug.WriteLine("Flushing Data Available");
                writer.Write(e.Buffer, 0, e.BytesRecorded);
                int secondsRecorded = (int)(writer.Length / writer.WaveFormat.AverageBytesPerSecond);
                if (secondsRecorded >= 30)
                {
                    StopRecording();
                }
                else
                {
                    //progressBar1.Value = secondsRecorded;
                }
            }
        }

        void StopRecording()
        {
            //Debug.WriteLine("StopRecording");
            waveIn.StopRecording();
        }

        private void btnStopRec_Click(object sender, EventArgs e)
        {
            if (waveIn != null)
            {
                StopRecording();
            }
        }

        byte[] GetALawBytes(ref MemoryStream ms)
        {
            //Comment from here
            ms.Position = 0;
            WaveFileReader wfr = new WaveFileReader(ms);
            byte[] arr = new byte[0];
            int a = wfr.Read(arr, 0, (int)ms.Length);
            //to here to make it to work

            byte[] data = ms.ToArray();
            int length = ms.ToArray().Length;
            byte[] encoded = new byte[length / 2];
            int outIndex = 0;

            for (int n = 0; n < length; n += 2)
            {
                encoded[outIndex++] = NAudio.Codecs.ALawEncoder.LinearToALawSample(BitConverter.ToInt16(data, n));
            }

            return encoded;
        }

        public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
        {
            try
            {
                // Open file for reading

                System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);

                // Writes a block of bytes to this stream using data from a byte array.
                _FileStream.Write(_ByteArray, 0, _ByteArray.Length);
                // close file stream
                _FileStream.Close();
                return true;
            }
            catch (Exception _Exception)
            {
                // Error
                MessageBox.Show("Exception caught in process: {0}", _Exception.ToString());
            }
            // error occured, return false
            return false;
        }
    }
}

The project only needs two commandButton, cmdStartRecord and btnStopRecord.

In the GetALawBytes function, commenting the first 4 lines of code will make the entire project to work. 

The final result is a .raw file playing as expected (but the noise).

The wav will not play at all.

Any suggestion? What I want to achieve is to extract the acutal audio data from the memorystream and save it as pure aLaw data.


Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>