Question

I'm building an application which records audio from the microphone to a file (.mp3). I want my application to write data to the file only when a high enough amplitude is detected. I'm having trouble trying to save the selected data. All I get is a fast distortion, not even close from what was recorded.

This is my code:

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

namespace NAudio_Testing
{
    public partial class Form1 : Form
    {
        int counter = 0;
        int passed = 0;
        private BufferedWaveProvider bufferedWaveProvider;
        WaveIn waveIn;
        WaveFileWriter writer;
        string output = "C:/Users/klein/AppData/Roaming/Microsoft/Windows/gravacao.mp3";
        public Form1()
        {
            InitializeComponent();
            waveIn = new WaveIn();
            writer = new WaveFileWriter(output, waveIn.WaveFormat);

            waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(new_dataAvailable);

            bufferedWaveProvider = new BufferedWaveProvider(waveIn.WaveFormat);
            bufferedWaveProvider.DiscardOnBufferOverflow = true;
            waveIn.StartRecording();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Start();
        }

        void new_dataAvailable(object sender, WaveInEventArgs e)
        {
            for (int i = 0; i < e.BytesRecorded; i+=2)
            {
                short sample = (short)((e.Buffer[i + 1] << 8) | e.Buffer[i + 0]);
                float sample32 = sample / 32768f;
                if (sample32 > 0.02) //0.02~0.03
                {
                    writer.WriteSample(sample);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            waveIn.StopRecording();
            waveIn.Dispose();
            waveIn = null;
            writer.Close();
            writer = null;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox1.Text = counter++.ToString();
        }

    }
}

EDIT: I am able to record the audio successfully if I remove the selective if and just add the lines, the problem, of course is that then I'm unable to record or not according to the amplitude of the audio:

writer.WriteByte(e.Buffer[i + 0]);
writer.WriteByte(e.Buffer[i + 1]);

Like so:

void new_dataAvailable(object sender, WaveInEventArgs e)
    {
        for (int i = 0; i < e.BytesRecorded; i+=2)
        {
            short sample = (short)((e.Buffer[i + 1] << 8) | e.Buffer[i + 0]);
            float sample32 = sample / 32768f;

            writer.WriteByte(e.Buffer[i + 0]);
            writer.WriteByte(e.Buffer[i + 1]);

        }
    }

Thank you.

Was it helpful?

Solution

Your approach is essentially throwing away all samples near a zero crossing, irrespective of whether the overall audio at that time is loud or not. It will distort the signal, and speed it up, since audio signals typically have lots of zero crossings every second.

Instead, you need to measure the maximum volume of all the samples in a short time period (how long is up to you, but you could do 1 second at a time). If during that time any sample is louder than the specified minimum value then you record the whole block.

The second problem with your code is that you are not actually creating an MP3 file. The WaveFileWriter makes a WAV file. Change your filename to end in WAV and listen to that to hear the results of your recording. Then you would need to convert to MP3 as a second step after recording. (I have an article on how to convert between formats here).

OTHER TIPS

You should compare absolute value of sample, since samples are signed.

 if (Abs(sample32) > 0.02) //0.02~0.03
   {
      writer.WriteSample(sample);
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top