Skip to content

7in7 – Day Two

7in7 logoWell, today went really well.  I actually chose a harder task for today than yesterday and, whata-ya-know… It went much better.

I had a weird idea this morning as I was driving to work.  What
if I could see the sounds, make abstract art from the everyday sounds around me.  That is what brought me to make Hearsee.

Hearsee takes a recorded audio segment and converts it byte by byte into an image.  See the sounds of the world around you in a whole new way!


In order to create this app, I needed to implement sound recording and playback. To do that, you need to make use of the Microsoft.Xna.Framework and here are the steps to do it:

  1. First you need to add the following reference to your project:


  2. In your class you will have to add the following using statements:
    using System.Windows.Threading;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using System.Windows.Media;


  3. You will have to add the following class level variables to accommodate the recording and playback of sound:
    Microphone microphone = Microphone.Default;
    byte[] buffer;
    MemoryStream stream = new MemoryStream();
    SoundEffect sound; 
  4. XNA runs differently than Silverlight in that it has a cyclic execution model rather than Silverlight’s event driven model.  In order to take advantage of the XNA classes properly, you need to add the following into the constructor of your class:

    // Timer to simulate the XNA game loop (Microphone is from XNA) DispatcherTimer dt = new DispatcherTimer(); dt.Interval = TimeSpan.FromMilliseconds(50); dt.Tick += delegate {

    try {



    catch { }

    }; dt.Start();



  5. You will then need to add a handler for the Microphone’s BufferReady event:

    microphone.BufferReady +=

                 new EventHandler<EventArgs>(microphone_BufferReady);

  6. Now you are ready to start recording:

    /// <summary> /// Uses the Microphone object's methods /// to start the recording of sound. /// </summary> private void StartRecordingSound() { //check to see if the Microphone has been started. //if it has, there is no reason to start it again. if (microphone.State != MicrophoneState.Started) { //create a new stream to create a whole new sound. //if you want to append onto existing stream, //do not include this line. stream = new MemoryStream(); //set the buffer duration microphone.BufferDuration =

    TimeSpan.FromMilliseconds(1000); //set the buffer to a new byte array buffer = new byte[microphone.GetSampleSizeInBytes( microphone.BufferDuration)]; //start up the microphone microphone.Start(); } }

  7. To stop recording you only need to call the Microphone’s Stop method:
            /// <summary>
            /// Stops a currently recording Microphone object.
            /// </summary>
            private void StopRecording()
                //if the Microphone's state is not started, 
                //there is no reason to stop it.
                if (microphone.State == MicrophoneState.Started)
  8. And finally, to playback what you have recorded, add the following method and call it on a button_click event:
            /// <summary>
            /// Plays the audio currently being held in the stream
            /// </summary>
            private void PlaySound()
                //if the stream is empty, there
                //is no reason to try to play it.
                if (stream.Length > 0)
                    //set the SoundEffect sound to hold the 
                    //newly recorded audio.
                    sound = new SoundEffect(stream.ToArray(),
                        microphone.SampleRate, AudioChannels.Mono);
                    //play the sound as a sound effect.


And there you have it!  All the code you need to record and play sound in your Windows Phone Silverlight app!

There are a few ideas I have that I didn’t have time to get done, like pinch/zooming, saving images and taking an image and converting it to sound, but that is what updates are for.

I’ll admit that this one was more of a Just for Me app, not really sure if anyone else will want it, but, if you have read any of my earlier posts, you will know that I build apps that I think are interesting.   I am not necessarily driven by what I think I will sell a lot of.  I’m really happy about this little app, especially since I was told by several people that it couldn’t be done.

Watch for updates on this app and others here!

Posted in Challenges, Devices, Windows Phone 7, XNA.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

or, reply to this post via trackback.