Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:15:38+00:00 2026-06-17T12:15:38+00:00

I’m building an application which records audio from the microphone to a file (.mp3).

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:15:39+00:00Added an answer on June 17, 2026 at 12:15 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I
i want to parse a xhtml file and display in UITableView. what is the
I am using jsonparser to parse data and images obtained from json response. When
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.