Algorithm to draw waveform from audio

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.


有一个来自BBC研发部门的很好的节目音频波形,可以做你想做的,你可以咨询他们的消息来源。


I think you are referring to a waveform described here.

http://manual.audacityteam.org/o/man/audacity_waveform.html

I have not read the whole page. But each vertical bar represents a window of waveform samples. The dark blue are the maximum positive and and minimum negative values in that window (I think). And the light blue is the RMS which is root mean squared. http://www.mathwords.com/r/root_mean_square.htm. (basically you square the values within each window, take an average, and then square root.

Hope this helps.

链接地址: http://www.djcxy.com/p/5800.html

上一篇: OpenGL ES呈现到纹理

下一篇: 从音频中绘制波形的算法