Click here to Skip to main content
6,594,932 members and growing! (13,810 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Audio     Intermediate License: The GNU General Public License (GPL)

Sound visualizer in C#

By Jeff Morton

Simple sound sampler with visualization
C# 2.0, Windows, .NET 2.0, GDI, VS2005, Dev
Posted:15 Aug 2007
Views:65,153
Bookmarked:132 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
22 votes for this article.
Popularity: 6.46 Rating: 4.81 out of 5
1 vote, 4.5%
1

2

3
2 votes, 9.1%
4
19 votes, 86.4%
5
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;
    }

    /// <summary>

    /// Process 16 bit sample

    /// </summary>

    /// <param name="wave"></param>

    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);
    }

    /// <summary>

    /// Render time domain to PictureBox

    /// </summary>

    /// <param name="pictureBox"></param>

    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();
    }

    /// <summary>

    /// Render frequency domain to PictureBox

    /// </summary>

    /// <param name="pictureBox"></param>

    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 (GPL)

About the Author

Jeff Morton


Member

Occupation: Systems / Hardware Administrator
Location: United States United States

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
GeneralIs this available in VB.NET? PinmemberSimon Williamson2:21 2 Apr '09  
GeneralMedia file PinmemberBabgi10:19 26 Mar '09  
GeneralRe: Media file Pinmembergamosemalaka23:00 30 May '09  
GeneralRe: Media file Pinmemberdiogopms16:37 27 Jun '09  
QuestionWhere is the Documentation? PinmemberMember 60381968:37 19 Mar '09  
QuestionaudioFrameSize in waveformat correct ? Pinmemberradio_gaga9:16 8 Mar '09  
AnswerRe: audioFrameSize in waveformat correct ? PinmemberJeff Morton10:59 8 Mar '09  
GeneralIt won't work on desktop PC Pinmembermax_99011:15 4 Feb '09  
QuestionInput delays PinmemberJoe Wright8:12 1 Sep '08  
AnswerRe: Input delays PinmemberJeff Morton13:45 1 Sep '08  
GeneralIt works! PinmemberMember 470880122:08 28 Aug '08  
GeneralMedia File Pinmemberjonhbt23:36 13 Aug '08  
GeneralRe: Media File PinmemberBabgi10:12 26 Mar '09  
GeneralShutdown addition Pinmemberfirmwaredsp23:26 31 Oct '07  
GeneralRe: Shutdown addition PinmemberRi Qen-Sin18:39 20 Nov '07  
GeneralRe: Shutdown addition PinmemberJeff Morton3:57 27 Nov '07  
GeneralRe: Shutdown addition Pinmemberfirmwaredsp0:21 28 Nov '07  
QuestionHow it works?? PinmemberIbrahim Dwaikat10:22 28 Oct '07  
AnswerRe: How it works?? Pinmemberj_morton10:29 28 Oct '07  
GeneralRe: How it works?? PinmemberIbrahim Dwaikat10:34 28 Oct '07  
GeneralRe: How it works?? Pinmemberfirmwaredsp23:47 31 Oct '07  
GeneralRe: How it works?? PinmemberJeff Morton4:44 28 Nov '07  
GeneralVery nice Pinmemberblackjack21502:01 23 Oct '07  
GeneralHow To Get Frequency, or Period of the Wave? PinmemberPaul Chin PC5:45 29 Sep '07  
QuestionDoesn't work :( PinmemberVladimir Grankovsky2:39 27 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Aug 2007
Editor: Genevieve Sovereign
Copyright 2007 by Jeff Morton
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project