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
    {
        [SecuritySafeCritical]
        public SoundEffect(byte[] buffer, int sampleRate, AudioChannels channels);
        [SecuritySafeCritical]
        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.

Horror1

*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.

Horror2

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);
            ScreamEffect.Play();
        }

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;
HorrorSound.Play();

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 http://msdn.microsoft.com/en-us/library/dd282429.aspx

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.

Vikram.

Saturday, August 20, 2011

Silverlight for Windows Phone Toolkit for ‘Mango’ : Getting Started

 

After a long time rather after a month got some free time to look at this place.Horrible work at office as usual.Well,before I start posting some cool stuff on Windows Phone 7.1 and Silverlight 5 again.Lets get ready with tools again.

Microsoft announced Silverlight for Windows Phone Toolkit recently and its worth downloading since it is now revised with some bug fixes, smooth performance and decent controls by which you can build good quality apps for your Mango Platform aka WP 7.1

Name : Silverlight for Windows Phone Toolkit SDK 7.1 (“Mango”) August 2011

Downloads :

msi : http://silverlight.codeplex.com/releases/view/71550#DownloadId=270984

nuget : http://nuget.org/List/Packages/SilverlightToolkitWP

Some new in this Toolkit :

  • ExpanderView - for building expanding and collapsing items
  • PhoneTextBox - for enhanced text box with action icon support, watermarking, etc.
  • DateTimePickers - for Date and Time into apps
  • HubTitle- for building animated and informative tiles
  • LockablePivots - for displaying current item only  usually used with multiple selection
  • MultiSelectList - for multiple selection with lists of data scenarios
  • LongListSelector - redesigned for Mango to give smooth scroll

Where to Report Bugs, feedback suggestions for this Toolkit ? :

http://silverlight.codeplex.com/workitem/list/basic

So this is sufficient for you people to get ready with the tools and very soon I will start posting samples and quick demos on top of this toolkit.Not much break now and I will be back here soon.

Why I & this blog was on silent mode for last couple of weeks :

  • Done some presentations on Silverlight & Windows Phone 7 in community last month
  • Building some cool demos on Silverlight 5,Work in Progress

Some random snaps from my recent Silverlight & Windows Phone 7 sessions in community at Pune, India :

So folks, see you very soon with some good quality stuff on Silverlight 5 and Windows Phone 7.1 here,have a great week ahead !

Vikram.