Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 31 August 2011

Silverlight 5 : Low-Latency Sound Effects

Posted on 10:46 by Unknown

 

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.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ▼  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ▼  August (2)
      • Silverlight 5 : Low-Latency Sound Effects
      • Silverlight for Windows Phone Toolkit for ‘Mango’ ...
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ►  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ►  February (4)
    • ►  January (6)
  • ►  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile