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="SoundPlayback.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.Linq;
using System.Text;
using Microsoft.DirectX.DirectSound;

namespace SoundFiltering
{
    public class SoundPlayback : IDisposable
    {
        /// <summary>
        /// Device.
        /// </summary>
        protected Device m_src = null;

        /// <summary>
        /// Secondary buffer.
        /// </summary>
        protected SecondaryBuffer m_secondaryBuffer = null;

        /// <summary>
        /// Location 
        /// </summary>
        protected int Location = 0;

        /// <summary>
        /// Disposed flag.
        /// </summary>
        protected bool m_disposed = false;

        /// <summary>
        /// Dispose.
        /// </summary>
        void IDisposable.Dispose()
        {
            Dispose(true);
        }

        /// <summary>
        /// Destructor.
        /// </summary>
        ~SoundPlayback()
        {
            Dispose(false);
        }

        /// <summary>
        /// Dispose.
        /// </summary>
        /// <param name="disposing"></param>
        public void Dispose(bool disposing)
        {
            if (m_disposed) return;
            m_disposed = true;
            
            GC.SuppressFinalize(this);

            m_secondaryBuffer.Dispose();
            m_src.Dispose();
        }

        /// <summary>
        /// Setup
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="fileName"></param>
        public void Setup(System.Windows.Forms.Control owner, string fileName)
        {
            m_src = new Device();
            m_src.SetCooperativeLevel(owner, CooperativeLevel.Priority);
            m_secondaryBuffer = new SecondaryBuffer(fileName, m_src);
            m_secondaryBuffer.SetCurrentPosition(0);
        }

        /// <summary>
        /// Get data.
        /// </summary>
        /// <param name="buffer"></param>
        public bool GetData(float[] buffer)
        {
            try
            {
                int bits = m_secondaryBuffer.Format.BitsPerSample;
                byte[] buf = new byte[buffer.Length * bits / 8];
                System.IO.MemoryStream memStream = new System.IO.MemoryStream(buf);
                memStream.Position = 0;
                //  memStream.Read(m_secondaryBuffer, 0, buffer.Length);
                m_secondaryBuffer.Read(Location, memStream, buffer.Length, LockFlag.None);
                Location += buffer.Length;
                int k = 0;
                if (bits == 16)
                {
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        int l = buf[k++];
                        int u = buf[k++];
                        buffer[i] = (float)(l + (u * 256));
                    }
                }
                else
                {
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        buffer[i] = (float)(buf[k++]);
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}

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 | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 7 Nov 2012
Article Copyright 2012 by HoshiKata
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid