I have a voice recording program that records sound from microphone, then split it onto WAV single-second fragments and the converts every WAV to MP3.
I getting normal melody when joining all WAV files together. I getting crappy melody when joining all MP3 files together.
What's wrong? i Though wav -> mp3 conversion should not add or remove any chunks from files. This is the code to create single-second fragments in wav and mp3 version:
public void CreateWavAndMp3(string wav_path, string mp3_path, WaveFormat recordingFormat)
{
WaveFileWriter wav_writer = new WaveFileWriter(wav_path, recordingFormat);
List<byte> complete_chunk = new List<byte>(); //to store chunks one after another
for (int i = 0; i < this.Chunks.Count; i++) //here I have raw bytes stored in List<byte[]>. I just do it that way and since WAV files are fine it's no matter
complete_chunk.AddRange(this.Chunks[i]);
long maxFileLength = recordingFormat.AverageBytesPerSecond * 60;
var toWrite = (int)Math.Min(maxFileLength - wav_writer.Length, complete_chunk.Count);
if (toWrite > 0)
{
wav_writer.Write(complete_chunk.ToArray(), 0, complete_chunk.Count); //write wav based on stored chunks
wav_writer.Dispose(); //wav file written
}
//mp3 junk
WaveLib.WaveStream InStr = new WaveLib.WaveStream(wav_path);
Yeti.MMedia.Mp3.Mp3Writer mp3Writer;
Yeti.MMedia.Mp3.Mp3WriterConfig m_Config = new Yeti.MMedia.Mp3.Mp3WriterConfig(InStr.Format);
FileStream Mp3FS = new FileStream(mp3_path, FileMode.Create, FileAccess.Write);
mp3Writer = new Yeti.MMedia.Mp3.Mp3Writer(Mp3FS, m_Config);
byte[] mp3buff = new byte[mp3Writer.OptimalBufferSize];
int read = 0;
long total = InStr.Length;
while ((read = InStr.Read(mp3buff, 0, mp3buff.Length)) > 0)
mp3Writer.Write(mp3buff, 0, read);
InStr.Dispose();
mp3Writer.Dispose();
}
Test sound files: https://www.dropbox.com/s/e43hh4y3oli13f4/livestream.7z?dl=0 so you can hear it too. try joining all files in movie maker or etc.
You're experiencing a problem related to the way MP3 does its encoding. Part of the codec itself adds padding at the start and end of every file. It is not avoidable. You will need to use a different format if you want to join them end to end.
Some music players get around this by calculating how much silence is added. But even this can vary depending on the codec. If you want to dive into the technical details, check out section 2 of this document: http://lame.sourceforge.net/tech-FAQ.txt
(tl;dr: that document says "576 samples", and 16-bit stereo is 4 bytes per sample.)
One other lossy codec that does not exhibit this problem is OGG. "Vorbis" is a NuGet package that is said to support using this format.