Click here to Skip to main content
Click here to Skip to main content
Go to top

Sound activated switch in WPF

, 22 May 2011
Rate this:
Please Sign up or sign in to vote.
Sound activated code snippet to control your application with sound variations.

ApplicationImage1.JPG

Introduction

The article is a sound activated switch done in WPF (.NET 4.0) for controlling your applications or functions based on sound variation. I have used the NAudio library to capture sound signals. The application has been designed to be very simple and easy to use.

NAudio is licensed under Microsoft Public License (MS-PL) so you will be able to use it wherever you want, even in commercial applications, so feel free to use it in your applications.

Background

There was a requirement to activate a switch when a tennis ball is hit (related to a tennis coaching application). The application had to record a video a couple of seconds before the ball was hit.

I did some research and found a very good article in channel9 (mentioned in the References section) which contains the major functionality, but was meant for a different purpose. I tweaked it a little to create this sound activated switch; you can even call it a clap switch in C# / WPF.

Using the code

NAudio includes a class called WaveIn which is the main class we are going to use for this application. WaveIn also contains a static function called DeviceCount for getting the count of input devices.

The WaveInCapabilities class is used to get the device information from waveInDevice (which is represented as an integer).

An event called DataAvailable is raised when there is an input sound signal (which can be even plotted). The event will be the total bytes recorded and the buffer object, which means that the event will be triggered in frequent intervals (almost real-time) and will contain an array of the buffered values, which will be a byte array, and for our application, it should be converted to float.

We then compare the sound levels and make the necessary changes to sensitivity levels (0-100) which will be compared with the sound values. You can probably trigger your other functions from here.

private float bigValue;
WaveIn waveIn;
private double MaxValue;
private void button1_Click(object sender, RoutedEventArgs e)
{
    if (Convert.ToInt16(textBox1.Text) > 100)
    {
        MessageBox.Show("Invalid Value");
        return;
    }
    else
        MaxValue = Convert.ToDouble(textBox1.Text) / 100;
    bigValue = 0;
    waveIn= new WaveIn();
    int waveInDevices = WaveIn.DeviceCount;
    //get the devicecount

    for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
    {
        WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
    }
    //load in the deviceinfo using the GetCapabilties function

           
    waveIn.DeviceNumber = 0;
    waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
    int sampleRate = 8000; // 8 kHz
    int channels = 1; // mono
    waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
    //Setting the format
    waveIn.StartRecording();
}

void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
    for (int index = 0; index < e.BytesRecorded; index += 2)
    {
        short sample = (short)((e.Buffer[index + 1] << 8) |
                                e.Buffer[index + 0]);
        float sample32 = sample / 32768f;
        label1.Content = sample32.ToString();
        if (bigValue < sample32)
        {
            bigValue = sample32;
            label2.Content = bigValue.ToString();
            if (bigValue > MaxValue)
            {
                waveIn.StopRecording();
                
                MessageBox.Show("Did you Clap?");
            }
        }
    }
}

Where can I use this code?

I am using this application for switching of video streams based on the voice. I also see various applications in your day to day applications where you would want to control a part of your application with sound activation, like show a status report when your manager / boss starts making weird sounds Smile | :) ; you need to keep the sensitivity level accordingly though.

Clap switches are traditionally used in electronic applications and you will see a wide variety of applications related to clap switches; an advanced form of this application will be voice recognition (which is already available) in the Speech to Text facility in SAPI.

NAudio provides some samples where you can easily work on sound visualization in .NET, thought .NET is not the best solution for this, and it performs fairly above expectations when it comes to audio functions.

Reference

The article named .NET Voice by Mark Heath is the real source behind this article. I just added some frills to his and created a different function.

License

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

Share

About the Author

JohnExalt
Architect Exalt Integral Solutions
India India
I have been a programmer since 1999, having worked on multiple platforms and technologies since then

Comments and Discussions

 
QuestionConversion PinmemberVet Ralph15-Oct-12 7:57 

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 | Mobile
Web04 | 2.8.140926.1 | Last Updated 22 May 2011
Article Copyright 2011 by JohnExalt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid