0
votes

I'm using an NAudio wrapper for LAME to convert my wav file to mp3. However, when I use NAudio to convert my mp3 back to wav, the wav file is a bit larger than the original wav file. Is there a way I can get it so that the original and decoded wav files are exactly the same size? Aside from this everything else works and I'm able to play the wav and mp3 files without issue. Thanks and I have included my conversion functions below:

private void ConvertWavMP3(string wavFile)
    {
        try
        {
            using (var wavRdr = new WaveFileReader(wavFile))
            using (var mp3Writer = new LameMP3FileWriter(wavFile.Replace(".wav", ".mp3"), wavRdr.WaveFormat, 128))
            {
                wavRdr.CopyTo(mp3Writer);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error converting wav to mp3.", "CONVERSION ERROR");
        }
    }

    private static void ConvertMp3ToWav(string mp3File)
    {
        try
        {
            using (Mp3FileReader reader = new Mp3FileReader(mp3File))
            {
                //using (WaveStream pcmStream = new WaveFormatConversionStream(new WaveFormat(8000, 16, 1), reader))
                using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
                {
                    WaveFileWriter.CreateWaveFile(mp3File.Replace(".mp3", ".wav"), pcmStream);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error converting mp3 to wav.", "CONVERSION ERROR");
        }
    }
1
WAV is a raw format so it may not be exactly the same, but should be close. Have you compared the original and output bitrates/channels/etc to ensure that something is not changing during conversion?Troy Mac1ure
Yes, all waveformat settings are the same between the 2. When I perform a File.ReadAllBytes, the output wav file byte array is much larger than the original. So the mp3 conversion added extra samples to the original wav recording.H.Tran
The overal filesize must be equal (barring slight time length changes) if the input & output settings are equal (bitrate = bitsPerSample * samplesPerSecond * channels) unless the original is in a compressed format already.Troy Mac1ure
You could try manual reading the header (if WaveFileReader doesn't give you the info) to see if it is being stored the same way. soundfile.sapp.org/doc/WaveFormatTroy Mac1ure
When I compared the byte array returned from ReadAllBytes, the array values were the same, however, the decoded wav file had extra bytes at the end. Thanks for this info though! I'm new to audio compression and coding so this is very valuable information for me.H.Tran

1 Answers

0
votes

MP3 is a lossy codec so it doesn't decompress to the exact same audio that you encoded. It also works by creating a series of encoded "frames", each of which has a fixed length, so the decoded MP3 will usually not be exactly the same duration as the input, but a few milliseconds longer.