Search code examples
c#audiobitmapnaudiowaveform

What is the relationship between AudioFileReader.Length and AudioFileReader.TotalTime in N Audio?


I am writing a small app to create a waveform from an MP3 file. I create a bitmap, but at the moment just make it wildly too big (12800 pixels) so I can ensure the whole wavelength fits on.

var samples = reader.Length / (reader.WaveFormat.Channels * reader.WaveFormat.BitsPerSample / 8);
Bitmap bim = new Bitmap(12800, 200);
Graphics g = Graphics.FromImage(bim);

This works, but I end up with a bunch of white space at the end of the waveform image:

enter image description here

I want to do this more intelligently, and use a variable calculated by the number of samples to ensure the bitmap width matches the length of the waveform (i.e. no white space).

What I really want to do is to tie the width to time - so 1 second is (for example) 15 pixels.

I can get the duration using AudioFileReader.TotalTime, but I can't work out how this correlates to AudioFileReader.Length.


Solution

  • Length is in bytes, TotalTime converts that into a TimeSpan for you using the AverageBytesPerSecond of the WaveFormat