Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Tagged as

Visualizing Sound

, 6 Nov 2012 CPOL
Listen or playback sound and visualize the frequency spread.
// -----------------------------------------------------------------------
// <copyright file="Form1.cs" company="None.">
//  By Philip R. Braica (HoshiKata@aol.com, VeryMadSci@gmail.com)
//
//  Distributed under the The Code Project Open License (CPOL)
//  http://www.codeproject.com/info/cpol10.aspx
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SoundFiltering
{
    /// <summary>
    /// Form class.
    /// </summary>
    public partial class Form1 : Form
    {
        protected SoundSource source = null;
        protected SoundPlayback playback = null;


        /// <summary>
        /// Constructor.
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// On closing.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            if (source != null)
            {
                source.Dispose(true);
            }
            if (playback != null)
            {
                playback.Dispose(true);
            }
        }


        /// <summary>
        /// Listen button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (source != null) return;
            source = new SoundSource();
            source.ProcessFFTData += new SoundSource.ProcessData_Delegate(source_ProcessFFTData);
            source.ProcessData += new SoundSource.ProcessData_Delegate(source_ProcessData);
            source.Start();
        }

        /// <summary>
        /// Visualize the data in the upper graph.
        /// </summary>
        /// <param name="data"></param>
        void source_ProcessData(float[] data)
        {
            soundVisualizer1.AppendData(data);
            soundVisualizer1.TriggerRedraw();
        }

        /// <summary>
        /// Process the data of the FFT.
        /// </summary>
        /// <param name="data"></param>
        void source_ProcessFFTData(float[] data)
        {
            float[] tmp = new float[data.Length / 2];
            for (int i = 0; i < tmp.Length; i++) tmp[i] = data[i];
            if (graph1.Lines.Count < 1)
            {
                graph1.Lines.Add(new Graph.Line());
            }
            graph1.Lines[0].Update(tmp);
            graph1.TriggerRedraw();
        }

        /// <summary>
        /// Play a file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (playback == null)
            {
                playback = new SoundPlayback();
            }
            DialogResult dr = openFileDialog1.ShowDialog();
            if (!((dr == DialogResult.OK) || (dr == DialogResult.Yes))) return;
            playback.Setup(this, openFileDialog1.FileName);
            timer1.Enabled = true;
        }

        /// <summary>
        /// Playback timer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            float[] buff = new float[1024];
            if (playback.GetData(buff) == false)
            {
                timer1.Enabled = false;
                return;
            }
     
            source_ProcessData(buff);

            float[] fft = new float[buff.Length];
            Math.FFT.Forward(buff, fft);
            source_ProcessFFTData(fft);
            source_ProcessData(fft);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

HoshiKata
Software Developer (Senior) KMC Systems
United States United States
Phil is a Principal Software developer focusing on weird yet practical algorithms that run the gamut of embedded and desktop (PID loops, Kalman filters, FFTs, client-server SOAP bindings, ASIC design, communication protocols, game engines, robotics).
 
In his personal life he is a part time mad scientist, full time dad, and studies small circle jujitsu, plays guitar and piano.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 7 Nov 2012
Article Copyright 2012 by HoshiKata
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid