I'm trying to draw a waveform from a raw audio file. I demuxed/decoded an audio file using FFmpeg and I have those informations: samples buffer, the size of the samples buffer, the duration of the audio file (in seconds), sample rate (44100, 48000, etc), sample size, sample format (uint8, int16, int32, float, double), and the raw audio data itself.
Digging on the Internet I found this algorithm (more here):
White Noise:
The Algorithm
All you need to do is randomize every sample from –amplitude to amplitude. We don’t care about the number of channels in most cases so we just fill every sample with a new random number.
Random rnd = new Random();
short randomValue = 0;
for (int i = 0; i < numSamples; i++)
{
randomValue = Convert.ToInt16(rnd.Next(-amplitude, amplitude));
data.shortArray[i] = randomValue;
}
It's really good but I don't want to draw that way, but this way:
Is there any algorithm or idea of how I can be drawing using the informations that I have?
First, you need to determine where on the screen each sample will end up.
int x = x0 + sample_number * (xn - x0) / number_of_samples;
Now, for all samples with the same x
, determine the min and the max separately for positive and negative values. Draw a vertical line, a dark one from negative max to positive max, then a light one from negative min to positive min over the top of it.
Edit: thinking about this a little more, you probably want to use an average instead of the min for the inner lines.