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

Different lengths of the same file (in .wav and .mp3)

Mar 13, 2015 at 10:34 PM
Hi,

I need to make an operation on a file and its copy which requires the same lengths of these files.

I spotted, that when i save a file (using WaveFileWriter) to .wav format and then i create a copy of it in .mp3 format (i tried two third party converters) - .wav and .mp3 files have different lengths. I load and compare them using Length field of AudioFileReader.

Why is it different? How can i assure the length is the same (i.e. they are "the same" minus, of course, compression)?

Thanks in advance :)
Coordinator
Mar 17, 2015 at 5:04 PM
how different are they?
They won't be identical, since MP3s can't have an exact length in terms of samples like a WAV file can
Mar 18, 2015 at 8:53 PM
Edited Mar 18, 2015 at 8:55 PM
mp3 will be longer in most cases, right? Maybe it'll be easier if i write what kind of operation i have in mind:

I wanted to create phase/polarity inversion - simply by multiplying every sample by '-1' (the same way as mixing is adding the samples). To check it, i tried to load the same file twice, then invert the phase of one reader and mix it together - the result should be silence. It didn't work that way.
I tried also with loading the files in different format, in mp3 and wav (they should, at least partially, cancel each other out). That's where my initial question came from, but now i see it doesn't work even on the same .wav file.

The sample of the code is below. I used AudioFileReader for both files and WaveFileWriter.
firstBuffer = new float[firstInputReader.WaveFormat.SampleRate * 2];
secondBuffer = new float[secondInputReader.WaveFormat.SampleRate * 2];

int firstBytesRead = 0;
int secondBytesRead = 0;

do
{
    //reading:
    firstBytesRead = firstInputReader.Read(firstBuffer, 0, firstBuffer.Length);
    secondBytesRead = secondInputReader.Read(secondBuffer, 0, secondBuffer.Length);

    //processing:
    for (int i = 0; i < firstBuffer.Length; i++)
    {
          firstBuffer[i] /= 2;  //volume rescaling

          // mixing together with 1st input (which is multiplied by -1, i.e. phase inverted)
          firstBuffer[i] += secondBuffer[i]*-1;
    }

    //writting:
    writer.WriteSamples(firstBuffer, 0, firstBytesRead);
} while (firstBytesRead > 0);

writer.Dispose();
Could you help me with these basic operations? Or maybe there's build in functionality which helps with phase invertion (similar to mixing and MixingSampleProvider)?

Thanks in advance
Apr 19, 2015 at 8:12 PM
ttt, anyone?