Click here to Skip to main content
11,431,011 members (69,939 online)
Click here to Skip to main content

Sound visualizer in C#

, 15 Aug 2007 GPL3
Rate this:
Please Sign up or sign in to vote.
Simple sound sampler with visualization
Screenshot - SoundViewer.jpg

Introduction

This project demonstrates the use of the Fast Fourier Transform and Windows GDI to produce near real-time visualizations of the time and frequency domains of sound.

Background

While looking for information on signal processing for a robotics project, it became apparent that examples written in C# were hard to find. This led me to create some of my own classes, which I have provided here for this demonstration.

Using the Code

Audio input for this demonstration is provided by the Wave classes developed by Ianier Munoz. Wave samples are further processed using the AudioFrame class.

class AudioFrame
{
    private Bitmap _canvasTimeDomain;
    private Bitmap _canvasFrequencyDomain;
    private double[] _waveLeft;
    private double[] _waveRight;
    private double[] _fftLeft;
    private double[] _fftRight;
    private SignalGenerator _signalGenerator;
    private bool _isTest = false;
    public AudioFrame(bool isTest)
    {
        _isTest = isTest;
    }

    /// <span class="code-SummaryComment"><summary>
</span>
    /// Process 16 bit sample
    /// <span class="code-SummaryComment"></summary>
</span>
    /// <span class="code-SummaryComment"><param name="wave"></param>
</span>
    public void Process(ref byte[] wave)
    {
        _waveLeft = new double[wave.Length / 4];
        _waveRight = new double[wave.Length / 4];
        if (_isTest == false)
        {
            // Split out channels from sample
            int h = 0;
            for (int i = 0; i < wave.Length; i += 4)
            {
                _waveLeft[h] = (double)BitConverter.ToInt16(wave, i);
                _waveRight[h] = (double)BitConverter.ToInt16(wave, i + 2);
                h++;
            }
        }
        else
        {
            // Generate artificial sample for testing
            _signalGenerator = new SignalGenerator();
            _signalGenerator.SetWaveform("Sine");
            _signalGenerator.SetSamplingRate(44100);
            _signalGenerator.SetSamples(16384);
            _signalGenerator.SetFrequency(5000);
            _signalGenerator.SetAmplitude(32768);
            _waveLeft = _signalGenerator.GenerateSignal();
            _waveRight = _signalGenerator.GenerateSignal();
        }
        // Generate frequency domain data in decibels
        _fftLeft = FourierTransform.FFTDb(ref _waveLeft);
        _fftRight = FourierTransform.FFTDb(ref _waveRight);
    }

    /// <span class="code-SummaryComment"><summary>
</span>
    /// Render time domain to PictureBox
    /// <span class="code-SummaryComment"></summary>
</span>
    /// <span class="code-SummaryComment"><param name="pictureBox"></param>
</span>
    public void RenderTimeDomain(ref PictureBox pictureBox)
    {
        // Set up for drawing
        _canvasTimeDomain = new Bitmap(pictureBox.Width, pictureBox.Height);
        Graphics offScreenDC = Graphics.FromImage(_canvasTimeDomain);
        SolidBrush brush = new System.Drawing.SolidBrush
                    (Color.FromArgb(0, 0, 0));
        Pen pen = new System.Drawing.Pen(Color.WhiteSmoke);
        // Determine channel boundaries
        int width = _canvasTimeDomain.Width;
        int center = _canvasTimeDomain.Height / 2;
        int height = _canvasTimeDomain.Height;
        offScreenDC.DrawLine(pen, 0, center, width, center);
        int leftLeft = 0;
        int leftTop = 0;
        int leftRight = width;
        int leftBottom = center - 1;
        int rightLeft = 0;
        int rightTop = center + 1;
        int rightRight = width;
        int rightBottom = height;
        // Draw left channel
        double yCenterLeft = (leftBottom - leftTop) / 2;
        double yScaleLeft = 0.5 * (leftBottom - leftTop) / 32768;  
            // a 16 bit sample has values from -32768 to 32767
        int xPrevLeft = 0, yPrevLeft = 0;
        for (int xAxis = leftLeft; xAxis < leftRight; xAxis++)
        {
            int yAxis = (int)(yCenterLeft + (_waveLeft[_waveLeft.Length / 
                (leftRight - leftLeft) * xAxis] * yScaleLeft));
            if (xAxis == 0)
            {
                xPrevLeft = 0;
                yPrevLeft = yAxis;
            }
            else
            {
                pen.Color = Color.LimeGreen;
                offScreenDC.DrawLine(pen, xPrevLeft, yPrevLeft, xAxis,yAxis);
                xPrevLeft = xAxis;
                yPrevLeft = yAxis;
            }
        }
        // Draw right channel
        int xCenterRight = rightTop + ((rightBottom - rightTop) / 2);
        double yScaleRight = 0.5 * (rightBottom - rightTop) / 32768;  
            // a 16 bit sample has values from -32768 to 32767
        int xPrevRight = 0, yPrevRight = 0;
        for (int xAxis = rightLeft; xAxis < rightRight; xAxis++)
        {
            int yAxis = (int)(xCenterRight + (_waveRight[_waveRight.Length / 
            (rightRight - rightLeft) * xAxis] * yScaleRight));
            if (xAxis == 0)
            {
                xPrevRight = 0;
                yPrevRight = yAxis;
            }
            else
            {
                pen.Color = Color.LimeGreen;
                offScreenDC.DrawLine
                (pen, xPrevRight, yPrevRight, xAxis, yAxis);
                xPrevRight = xAxis;
                yPrevRight = yAxis;
            }
        }
        // Clean up
        pictureBox.Image = _canvasTimeDomain;
        offScreenDC.Dispose();
    }

    /// <span class="code-SummaryComment"><summary>
</span>
    /// Render frequency domain to PictureBox
    /// <span class="code-SummaryComment"></summary>
</span>
    /// <span class="code-SummaryComment"><param name="pictureBox"></param>
</span>
    public void RenderFrequencyDomain(ref PictureBox pictureBox)
    {
        // Set up for drawing
        _canvasFrequencyDomain = new Bitmap
                (pictureBox.Width, pictureBox.Height);
        Graphics offScreenDC = Graphics.FromImage(_canvasFrequencyDomain);
        SolidBrush brush = new System.Drawing.SolidBrush
                    (Color.FromArgb(0, 0, 0));
        Pen pen = new System.Drawing.Pen(Color.WhiteSmoke);
        // Determine channel boundaries
        int width = _canvasFrequencyDomain.Width;
        int center = _canvasFrequencyDomain.Height / 2;
        int height = _canvasFrequencyDomain.Height;
        offScreenDC.DrawLine(pen, 0, center, width, center);
        int leftLeft = 0;
        int leftTop = 0;
        int leftRight = width;
        int leftBottom = center - 1;
        int rightLeft = 0;
        int rightTop = center + 1;
        int rightRight = width;
        int rightBottom = height;
        // Draw left channel
        for (int xAxis = leftLeft; xAxis < leftRight; xAxis++)
        {
            double amplitude = (int)_fftLeft[(int)(((double)(_fftLeft.Length)
                         / (double)(width)) * xAxis)];
            if (amplitude < 0) // Drop negative values
            amplitude = 0;
            int yAxis = (int)(leftTop + 
                ((leftBottom - leftTop) * amplitude) / 100);  
                        // Arbitrary factor
            pen.Color = Color.FromArgb(0, 0, (int)amplitude % 255);
            offScreenDC.DrawLine(pen, xAxis, leftTop, xAxis, yAxis);
        }
        // Draw right channel
        for (int xAxis = rightLeft; xAxis < rightRight; xAxis++)
        {
            double amplitude = (int)_fftRight[(int)(((double)
            (_fftRight.Length) / (double)(width)) * xAxis)];
            if (amplitude < 0)
            amplitude = 0;
            int yAxis = (int)(rightBottom - 
            ((rightBottom - rightTop) * amplitude) / 100);
            pen.Color = Color.FromArgb(0, 0, (int)amplitude % 255);
            offScreenDC.DrawLine(pen, xAxis, rightBottom, xAxis, yAxis);
        }
        // Clean up
        pictureBox.Image = _canvasFrequencyDomain;
        offScreenDC.Dispose();
    }
}

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Jeff Morton
Systems / Hardware Administrator
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
sanosay9-Aug-12 4:09
membersanosay9-Aug-12 4:09 
QuestionProblems with Windows 7 Pin
valeriopini13-Mar-12 1:51
membervaleriopini13-Mar-12 1:51 
GeneralMy vote of 1 Pin
rrossenbg12-Jan-12 6:33
memberrrossenbg12-Jan-12 6:33 
GeneralRe: My vote of 1 Pin
sanosay9-Aug-12 4:08
membersanosay9-Aug-12 4:08 
Questionwhat are the values representing in arrays? [modified] Pin
D__I___O31-Aug-11 1:11
memberD__I___O31-Aug-11 1:11 
Questionis this code possible to save as Wav or MP3 File after recording process Pin
myatthu198618-Aug-11 0:37
membermyatthu198618-Aug-11 0:37 
GeneralMy vote of 5 Pin
AlexB4716-Jun-11 0:02
memberAlexB4716-Jun-11 0:02 
Generali want to convert it into WPF Pin
vishal_h19-May-11 0:21
membervishal_h19-May-11 0:21 
GeneralProb with the frequency domain Pin
joeblogg6-Jan-11 9:03
memberjoeblogg6-Jan-11 9:03 
GeneralRe: Prob with the frequency domain Pin
Piligrim200729-Jan-11 6:31
memberPiligrim200729-Jan-11 6:31 
QuestionHow to get the frequency in real-time? Pin
gmajkun22-Dec-10 20:54
groupgmajkun22-Dec-10 20:54 
AnswerRe: How to get the frequency in real-time? Pin
Piligrim200729-Jan-11 6:37
memberPiligrim200729-Jan-11 6:37 
GeneralMy vote of 5 Pin
Dennis Betten3-Nov-10 2:41
memberDennis Betten3-Nov-10 2:41 
GeneralError Pin
dreje20106-Oct-10 0:53
memberdreje20106-Oct-10 0:53 
QuestionDoes this analyse the code with in milisecond accurancy? Pin
Rahdu15-Sep-10 10:50
memberRahdu15-Sep-10 10:50 
GeneralDetect Peak on single channel Pin
survcopt5-Mar-10 4:47
membersurvcopt5-Mar-10 4:47 
Questionhow can I determine the sound level without waveFormat ? Pin
fasialkk24-Feb-10 14:44
memberfasialkk24-Feb-10 14:44 
QuestionHow To Open Recorded Wav File Pin
TV Mogul14-Jan-10 3:38
memberTV Mogul14-Jan-10 3:38 
Generalimportant reguest Pin
seka2087-Dec-09 23:04
memberseka2087-Dec-09 23:04 
QuestionDoes not work in Windows 7 (64bit) Why? Pin
a.mueller2-Dec-09 6:31
membera.mueller2-Dec-09 6:31 
AnswerRe: Does not work in Windows 7 (64bit) Why? Pin
eweewf15-Feb-10 9:42
membereweewf15-Feb-10 9:42 
64-Bit should be the first clue. :P

Didn't work on my Windows Server 2008, 64-Bit either. Then I thought that winmm.dll must be a 32-Bit dll. As a result, any application using 32-bit DLL files needs to change it's configuration, or exceptions will occur...

Visual Studio 2008

Click the "Build" -> "Configuration Manager..." menu option. Click the "Active solution platform" drop-down menu and select "New...". A dialog will open, change the type to "x86", click "OK", and rebuild. Smile | :)
QuestionIs this available in VB.NET? Pin
Simon Williamson2-Apr-09 2:21
memberSimon Williamson2-Apr-09 2:21 
GeneralMedia file Pin
Babgi26-Mar-09 10:19
memberBabgi26-Mar-09 10:19 
GeneralRe: Media file Pin
gamosemalaka30-May-09 23:00
membergamosemalaka30-May-09 23:00 
GeneralRe: Media file Pin
diogopms27-Jun-09 16:37
memberdiogopms27-Jun-09 16:37 
GeneralRe: Media file Pin
Ankur Dukare11-Mar-10 23:34
memberAnkur Dukare11-Mar-10 23:34 
GeneralRe: Media file Pin
gerardo_00115-Mar-10 9:15
membergerardo_00115-Mar-10 9:15 
QuestionWhere is the Documentation? Pin
Member 603819619-Mar-09 8:37
memberMember 603819619-Mar-09 8:37 
QuestionaudioFrameSize in waveformat correct ? Pin
radio_gaga8-Mar-09 9:16
memberradio_gaga8-Mar-09 9:16 
AnswerRe: audioFrameSize in waveformat correct ? Pin
Jeff Morton8-Mar-09 10:59
memberJeff Morton8-Mar-09 10:59 
GeneralIt won't work on desktop PC Pin
max_99014-Feb-09 1:15
membermax_99014-Feb-09 1:15 
QuestionInput delays Pin
Joe Wright1-Sep-08 8:12
memberJoe Wright1-Sep-08 8:12 
AnswerRe: Input delays Pin
Jeff Morton1-Sep-08 13:45
memberJeff Morton1-Sep-08 13:45 
GeneralIt works! Pin
Member 470880128-Aug-08 22:08
memberMember 470880128-Aug-08 22:08 
GeneralMedia File Pin
jonhbt13-Aug-08 23:36
memberjonhbt13-Aug-08 23:36 
GeneralRe: Media File Pin
Babgi26-Mar-09 10:12
memberBabgi26-Mar-09 10:12 
GeneralRe: Media File Pin
Ankur Dukare11-Mar-10 23:35
memberAnkur Dukare11-Mar-10 23:35 
GeneralRe: Media File Pin
Eric Hdz25-Apr-15 15:43
memberEric Hdz25-Apr-15 15:43 
GeneralShutdown addition Pin
firmwaredsp31-Oct-07 23:26
memberfirmwaredsp31-Oct-07 23:26 
GeneralRe: Shutdown addition Pin
Ri Qen-Sin20-Nov-07 18:39
memberRi Qen-Sin20-Nov-07 18:39 
GeneralRe: Shutdown addition Pin
Jeff Morton27-Nov-07 3:57
memberJeff Morton27-Nov-07 3:57 
GeneralRe: Shutdown addition Pin
firmwaredsp28-Nov-07 0:21
memberfirmwaredsp28-Nov-07 0:21 
QuestionHow it works?? Pin
Ibrahim Dwaikat28-Oct-07 10:22
memberIbrahim Dwaikat28-Oct-07 10:22 
AnswerRe: How it works?? Pin
j_morton28-Oct-07 10:29
memberj_morton28-Oct-07 10:29 
GeneralRe: How it works?? Pin
Ibrahim Dwaikat28-Oct-07 10:34
memberIbrahim Dwaikat28-Oct-07 10:34 
GeneralRe: How it works?? Pin
firmwaredsp31-Oct-07 23:47
memberfirmwaredsp31-Oct-07 23:47 
GeneralRe: How it works?? Pin
Jeff Morton28-Nov-07 4:44
memberJeff Morton28-Nov-07 4:44 
GeneralVery nice Pin
blackjack215023-Oct-07 2:01
memberblackjack215023-Oct-07 2:01 
QuestionHow To Get Frequency, or Period of the Wave? Pin
Paul Chin PC29-Sep-07 5:45
memberPaul Chin PC29-Sep-07 5:45 
QuestionDoesn't work :( Pin
Vladimir Grankovsky27-Aug-07 2:39
memberVladimir Grankovsky27-Aug-07 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 15 Aug 2007
Article Copyright 2007 by Jeff Morton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid