This project has moved. For the latest updates, please go here.

Adding some silence to an MP3

Sep 13, 2012 at 2:34 AM

I've just started trying out NAudio and was able to read in my MP3 file and then break it up into some smaller chunks that I saved back out to disk.  I've got a lot to learn about audio data but I was successful in doing this much so far (with some help from some example code).

What I'd like to be able to do is add X seconds of silence to my MP3 - at the start or end of the MP3 file for a project I'm working on with my kids. 

My initial approach was that I'd construct an MP3Frame object and thought that perhaps it would suffice to create a zero-filled data block of the appropriate size and then write that frame out before/after our music clip.  But there's no public constructor for MP3Frame (unless I missed something here) and so I'm assuming that's not the way to go about this.

I need to spend more time looking through the docs and example code on this but if someone's done something like this, I would appreciate some tips.  Not looking for you to write my code for me, just some direction.

I was thinking I might be able to use MP3Frame.LoadFromStream() and then adjust the frame's properties to accomplish this.

Ideas?

 

Thanks,

Matt

Coordinator
Sep 13, 2012 at 6:14 AM

hi Matt,

You have two options. First, you use NAudio to add silence to the beginning (see for example the OffsetSampleProvider I recently added), and then convert the whole thing back to MP3 using LAME.exe.

Second, you could convert a short blank WAV file to MP3, and extract one frame from that to use as your "silence" MP3 frame. Then you could insert multiple instances of that at the start and end of your MP3.

Mark

Apr 22, 2015 at 4:49 AM
Edited Apr 22, 2015 at 4:49 AM
Hi Matt,

I wrote a function to insert the silence in file.

I hope that it can help you.
    private void InsertSilence(WaveFileWriter writer, Double milliseconds)
    {

        Double avgBytesPerMillisecond = (Double)writer.WaveFormat.AverageBytesPerSecond / 1000F;

        Int32 silenceSize = (Int32)(milliseconds * avgBytesPerMillisecond);
        silenceSize = silenceSize - silenceSize % fileIn.WaveFormat.BlockAlign;

        Byte[] silenceArray = new Byte[silenceSize];
        writer.Write(silenceArray, 0, silenceArray.Length);

    }