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

NAudio - audio visualizer C#

May 24, 2013 at 8:25 PM
Hi. Can anyone explain what is the logic behind an audio visualizer and how can I use NAudio to make a simple visualizer ? I supose I need to make use of NAudio.Wave and analize the waveform created ... but then ?
Coordinator
May 30, 2013 at 9:38 AM
I'd recommend starting by looking at the demo projects that come with NAudio. They show how to draw waveforms and use FFT for spectrum analyser
May 30, 2013 at 7:45 PM
Edited May 30, 2013 at 7:49 PM
Thank you sir !
May 3, 2015 at 7:24 AM
Hi there, I know this is an old thread, but I thought I didn't really think it was necessary to create a new thread. What is meant by FFT?
May 3, 2015 at 8:20 PM
Edited May 3, 2015 at 9:25 PM
An FFT (Fast Fourier Transform) takes a block of raw Audio samples with a power of 2, to transform it into a Frequency and Amplitude representation. Just imagine, any Signal can be displayed as an overlap of many many Sinus curves:

Animation

This is exactly, what FFT does. It splits the block into many Sinus curves and assumes them periodic. This Sinus calculation is done by vectors. To represent any possible vector, this is done with complex numbers. The power of 2 is just for algorithm optimization, to be a "fast" Fourier transform.

So basically in practice your approach is e.g. like this:

Take 1024 raw Audio samples (from a MeteringSampleProvider ) -> FFT (in a SampleAggregator) -> Complex Array with 2048 samples -> Loop through first (or second, as values are mirrored around zero) 1024 of them like this (blue part in Image above, second part is cut off as it is mirrored):
    'NOTE: this Event is fired by the SampleAggregator class.
    Public Sub FftCalculated(ByVal sender As Object, ByVal e As FftEventArgs)
           For i = 0 To e.Result.Length / 2 - 1
                Dim Amplitude As Double = Math.Sqrt(e.Result(i).X ^ 2 + e.Result(i).Y ^ 2)
                Dim Frequency As Double = i * 44100 / e.Result.Length
                'TODO: Paint Frequency and Amplitude here on a control, Bitmap...
                'Some side notes:
                'Max Frequency is half SampleRate. In this case 22050.
                'If you want to get real human reconized values, apply Logarithm on Amplitude and Frequency.
           Next
    End Sub
NOTE: I assumed a SampleRate of 44100 in above Example.

The above Event is fired by the SampleAggregator class, that you can find in a NAudio example. To feed the SampleAggregator I used a MeteringSampleProvider with the StreamVolume Event (where you can also feed a WaveformPainter, etc.):
    'NOTE: This Event is fired by MeteringSampleProvider class.
    Private Sub m_SampleProvider_StreamVolume(ByVal sender As Object, ByVal e As NAudio.Wave.SampleProviders.StreamVolumeEventArgs)

        Dim left As Single = 0
        Dim right As Single = 0

        If e.MaxSampleValues.Length > 0 Then left = e.MaxSampleValues(0)
        'Is not mono Stream?
        If e.MaxSampleValues.Length > 1 Then right = e.MaxSampleValues(1)

        SampleAggregator.Add(0.5 * (left + right))
        'WaveformPainter1.AddMax((left + right) / 2)

    End Sub
Just add the MeteringSampleProvider in your Signal chain to access the raw Audio sample data.

I hope this Points you into the right direction, without confusing you too much with Details.

Kind regards

Freefall


PS: I created a StereoWaveformPainter some time ago based on the WaveformPainter class. Perhaps you want to incorporate it in a future release Mark.
May 3, 2015 at 8:32 PM
Ah thanks, I understand it better now!
May 3, 2015 at 8:57 PM
Please mark this thread as answered so others can see quickly for future questions concerning this.

Thanks.
May 3, 2015 at 9:01 PM

I want OP of the thread, I don't think I can. I was just asking the question in relation to a previous reply to the OP's initial question ah.