Wednesday, August 31, 2011

Silverlight 5 : Low-Latency Sound Effects


I just keep switching between Windows Phone and Silverlight and in office its WPF,so many technical flavors in a week I taste,Well today I am going to talk on Silverlight back again.Lot many features announced with Silverlight 5 beta.Few weeks back I did share few things with you like Text improvement and Window class etc. and I got very good response for that from all of you.Thanks for that.

Today I am going to talk on some interesting feature set of Low-Latency Sound Effects and how you can incorporate that inside your application.This feature is useful especially in Media and Entertainment related apps and bit away from actual Line of Business apps.For example, Gun shots,Game Over-Game Start sounds,Level change sounds,Notifications etc.For XNA developers who write games for Windows Phone 7 are in good position to understand this since there they use this SoundEffects heavily.

For this you need to take help of XNA library which will give you SoundEffect class,so you need Namespace :

using Microsoft.Xna.Framework.Audio;

Structure of SoundEffect class is like this :

public sealed class SoundEffect : IDisposable
        public SoundEffect(byte[] buffer, int sampleRate, AudioChannels channels);
        public SoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength);

        public TimeSpan Duration { get; }
        public bool IsDisposed { get; }
        public static float MasterVolume { get; set; }

        public SoundEffectInstance CreateInstance();
        public void Dispose();
        public static SoundEffect FromStream(Stream stream);
        public static TimeSpan GetSampleDuration(int sizeInBytes, int sampleRate, AudioChannels channels);
        public static int GetSampleSizeInBytes(TimeSpan duration, int sampleRate, AudioChannels channels);
        public bool Play();
        public bool Play(float volume, float pitch, float pan);

Demo :

I am not a hardcore Game developer but being movie buff,I am putting one Horror Image and on loading of that I am playing some scary music and on clicking on that image some more sound effects will be played.So this is background of this demo,first let me share how it will look like, well its simple image of ghost and rest is game of sound effects.


*Note : Image used here is taken from internet and used here just for demo purpose,Image have its own respective copyrights.

Design :

For this demo you can use any design of your choice,I am just putting one scary image,I was actually planning to make some short movie with animations and change opacity etc, but I am leaving that creative part for you.


So I am just focusing on playing sound effect.

Namespace :

using Microsoft.Xna.Framework.Audio;

C# Code :

private void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            var ScramStream = Application.GetResourceStream(new Uri("Shot.wav", UriKind.RelativeOrAbsolute));
            ScreamEffect = SoundEffect.FromStream(ScramStream.Stream);

This will load “Shot.wav” file and play the same once you click on that Image. Make sure that the “wav” file is PCM encoded file and falls between 22.5,48.5khz sample rate else your “wav” format will not get load and hence you will not have any sound effect.Also it should be 8 or 16 bit mono or stereo.

If you want to play effect and tweak the things then SoundEffectInstance will help you to do this.Sound.You can use properties like Pitch,Pan Volume etc.See code below, you will get idea how you can do things with SoundEffectInstance

SoundEffectInstance HorrorSound = ScreamEffect.CreateInstance();
HorrorSound.Pitch = 3.0f;
HorrorSound.Pan = 1.5f;

What you can do more is to put this code in Timer (Instance of DispatcherTimer) and play the same.If you want to dig further you can visit MSDN for more on SoundEffect Class here

So, This is all about the Low-Latency Sound Effect in Silverlight 5 beta.Soon I will share few more Multimedia related features in coming days and will try to cover remaining features of Silverlight 5 Beta in coming weeks on which I haven’t discuss anything here.Till then you can try this demo and let me know your feedback for the same.


No comments: