Proof of concept for Silverlight and Speex microphone capture

There is a lot of buzz due to the new capabilities of Silverlight 4, one of them are webcam and microphone capturing.

Here are two great examples that you can use as a starting point: Accessing Web Camera and Microphone by Tim Heuer and Silverlight 4 Augmented Reality Proof Of Concept by René Schulte.

The problem is that in the current API for video and audio capturing, there is support only for RAW data, and that means 32bpp ARGB video and PCM audio samples. Also you have nothing in the server side to handle streaming from client to server, which prevents you from develop any real scenario of video/audio communication tool.

However, this is only the current Beta stage and I’m pretty sure Microsoft will push hard for complement this new features in the incoming versions.

Meanwhile, I handle to port (from JSpeex) the Speex audio codec completely to C# and modified the Tim Heuer’s sample to encode the audio. I uploaded the source code to CodePlex at CSpeex – http://cspeex.codeplex.com/. There is a new button that allows you to save the recorded audio. The audio is encoded in real time to a memory Ogg stream and when you press “Save Audio” button, this stream is decoded and saved as a WAV in your computer.

Here it is the code snippet for the encoding part.

protected override void OnSamples(long sampleTime, long sampleDuration, byte[] sampleData)
{
    for (int i = 0; i < sampleData.Length; )
    {
        int len = Math.Min(sampleData.Length - i, temp.Length - tempOffset);

        Buffer.BlockCopy(sampleData, i, temp, tempOffset, len);

        if (len < temp.Length - tempOffset)
        {
            tempOffset += len;
        }
        else
        {
            tempOffset = 0;

            speexEncoder.processData(temp, 0, temp.Length);

            int encsize = speexEncoder.getProcessedData(temp, 0);

            if (encsize > 0 && (memFile.InnerStream.Position + encsize < ((MemoryStream)memFile.InnerStream).Capacity))
            {
                 writer.writePacket(temp, 0, encsize);
             }
         }

         i += len;
    }
}

Remember, there is a long path to a real chat application, since you need to handle how to upload the encoded stream to the server and do the actual communication between two parties.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
This entry was posted in Silverlight. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

11 Comments

  1. Posted December 10, 2009 at 4:07 am | Permalink

    Very nice! I found the JSpeex implementation a couple days ago, and was getting ready to try this port myself, when I found yours. Very handy.

  2. Posted December 10, 2009 at 5:12 am | Permalink

    Great stuff and keep up the good work!
    I just subscribed to your blog feed. :)

  3. Posted December 10, 2009 at 10:03 am | Permalink

    Thank you! for the comments.

  4. Posted December 11, 2009 at 6:27 pm | Permalink

    By the way, we just got this working with a round-trip from the client to a home-grown media server, decoded the audio in C++ with the latest Speex codec, mixed it with other decoded streams, re-encoded it all, sent it back to the clients, and played it back, all in real-time. Many thanks for putting this out there: this was the big missing piece.

  5. Posted December 11, 2009 at 6:36 pm | Permalink

    Glad to hear your good results.

  6. WGraham
    Posted December 17, 2009 at 1:05 am | Permalink

    I cannot get this working for the life of me. Alden, could you send me an e-mail so I can talk to you?

  7. Posted December 21, 2009 at 7:09 am | Permalink

    Hi Guys, this is wonderful work. since we are working on a multiplayer game for Facebook that can use this, I am offering you my development time for the networking part of this in either UDP or TCP and the server side to receive and retransmit to the subscriber clients in you guys also help on the video part. Please let me know and I can help on the networking part pretty quickly…

  8. Posted December 28, 2009 at 11:26 am | Permalink

    Hi @Nader Rahimizad
    Thanks for your offer and interest. As you can see, this is only a proof of concept. My bet is that Microsoft will address this shortcoming in the final release with an integrated solution for audio and video. If you need to release something before that, please, feel free to take the codeplex code and play with it. Regards.

  9. Posted April 20, 2010 at 6:41 am | Permalink

    this is cool, thanks for sharing the code

  10. bassam
    Posted May 15, 2010 at 3:52 am | Permalink

    good control,
    But i cound’nt manage to get the encoded stream to save it to server thru WCF, it’s just save to file disk, and also i can’t play it back when i capture the audio, can you give code snipt or an example?

    Thanks

  11. Posted May 20, 2010 at 6:11 am | Permalink

    are you planning on applying the patch that Ken Smith has uploaded to codeplex? Any idea on when you will post an updated download file?

    Also, do you know where i can find more information on how to acoomplish what Ken has done in “mixed it with other decoded streams, re-encoded it all” any sample code out there? I need to blend 4 player streams and rebroadcast to all the players. Thanks in advance for your guidance

One Trackback

  1. [...] This post was mentioned on Twitter by René Schulte, Alden Torres. Alden Torres said: @timheuer Thanks for your Camera and Microphone example, I added Speex audio codec http://bit.ly/6Y0ncF [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • What am I running?

    WordPress trunk, rev. 15157
    Nginx 0.8.36
    PHP 5.3.2-1ubuntu4
    APC 3.1.3p1
    WP Super Cache 0.9.9
    Ubuntu 10.04 LTS
  • Meta