computer programming 2 chapter 7 sound. objectives find out how to prepare sounds for inclusion in...

24
COMPUTER PROGRAMMING 2 Chapter 7 Sound

Upload: joana-wayman

Post on 16-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

COMPUTER PROGRAMMING 2

Chapter 7 Sound

Objectives

Find out how to prepare sounds for inclusion in Microsoft XNA projects.

Incorporate sounds into XNA.

Play the sounds from within your programs.

Adding Sounds

Sounds and File Types The XNA Framework can play simple sound effects

from .wav files.Storing Sounds in Your Project

Start by right-clicking the Content folder in XNA Game Studio, then select Existing Item from the Add option

Adding Sounds

This causes the Add Existing Item - Content dialog box to appear. You can then navigate to the folder on your system

containing the sound files and open them.

Using Sounds in an XNA Program

Create a variable to hold the loaded content. SoundEffect

This represents a sound that you want to play.

Set the variable in the LoadContent Method. varName =

Content.Load<SoundEffect>(“fileName"); This code sets the SoundEffect variables with the

samples that they are going to play.

Use the resource in the game. You can use the edge detection code to detect when

to play the sounds.

Using Sounds in an XNA Program

// test if A has been pressed since the last // Update

if (oldpad1.Buttons.A == ButtonState.Released &&pad1.Buttons.A == ButtonState.Pressed){ snare.Play(); //SoundEffect object}

OverLoaded Methods

You can have methods with the same name – These are called overloaded methods.

The compiler knows which overloaded method to use because the parameter list must be different.

Number of parameters,

Order of the parameters,

Data type of parameters

Overloaded Play Method

You can use an overloaded version of the Play method to control the volume, pitch, and panning of a sound.

snare.Play(.5f, 1, 1);

snare is a SoundEffectreference

float pitch between -1 and 1

float volume between 0 and 1

float pan between -1 (left) and 1 (right)

Playing Background Music

You want the music to repeat when it finishes playing, and you’d also like a way to stop and start the music from within your program.

If a sound can be played successfully the Play method returns the Boolean value true.

The hardware in your Windows PC, Xbox, or Windows Phone has only a limited number of audio channels, and if the program tries to play too many sounds at the same time it will run out of hardware to play them on.

The good news is that you will have at least 32 channels to use, meaning your game can play up to 32 sounds at the same time.

The SoundEffectInstance Class

If we want to control sound playback, our game must create a “handle” object that is connected to the playing sound.

The game can then call methods on this handle object to control the sound playback.

The XNA class that is used to control a playing SoundEffect is called SoundEffectInstance because it will be controlling the playback of a sound effect.

The SoundEffectInstance Class

Declaring a SoundEffectInstance object SoundEffectInstance nameSoundEffectInstance;

The game will load the sound effect using the Content Manager as usual, and it will then ask the sound effect to create an instance of the sound

protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); shootSoundEffect = Content.Load<SoundEffect>("shootSound"); shootSoundEffectInstance = shootSoundEffect.CreateInstance(); }

The SoundEffectInstance Class

The CreateInstance method is provided by the SoundEffect class to allow a game to reserve a sound channel and connect a handle to it.

Note that this method does not cause the sound to start playing. It is rather like being given a remote control to a TV.

Controlling a Sound Effect Instance

//makes the sound repeat by restarting playback when it stops

protected override void Update(GameTime gameTime){// Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.A == ButtonState.Pressed) { if (shootSoundEffectInstance.State != SoundState.Playing) { shootSoundEffectInstance.Play(); } } else { shootSoundEffectInstance.Stop(); }base.Update(gameTime);}

Changing Instance Properties

easier way of making a sound repeat: we can just set the repeating property of the sound to true:

shootSoundEffectInstance.IsLooped = true;

The IsLooped property controls whether or not the sound plays repeatedly. Default is false.

Changing Instance Properties

You can modify the Pitch and Pan properties of a playing sound.

Pitch Changing the pitch of a sound makes it higher or lower,

and you can set the pitch value between –1 (half the original pitch) and +1 (double the original pitch).

Pan The Pan value lets you move the sound between the left

and right speakers. It can be set between –1 (hard left) and +1 (hard

right).

Changing Instance Properties

You can use the values from the gamepad thumbsticks to let us move our raygun sound around and change its pitch.

shootSoundEffectInstance.Pitch = pad1.ThumbSticks.Left.Y;

shootSoundEffectInstance.Pan = pad1.ThumbSticks.Left.X;

Throwing a NullReferenceException

Some types in C# are managed by reference.

This means that a variable of this type is actually a reference to an object in memory.

When you use the variable, the program follows the reference to the object it refers to and uses that object.

A reference that is null is not set to refer to an object, so any attempt to follow this reference causes the program to fail.

Throwing a NullReferenceException

Checking for Null References

It is actually possible for a program to fail with a null reference if the CreateInstance method is unable to allocate a sound channel to play our sound effect.

This would only happen if we were playing lots of sounds.

If CreateInstance can’t find a channel to play the sound, it will return a null value to indicate that more sounds cannot be played at the moment.

Checking for Null References

If we protected all use of the variable with tests like these, we would make sure that our program never crashed due to the value being set to null.

if (shootSoundEffectInstance != null ) { // if we get here, // we can use the sound }

The XACT Audio Tool

A professional standard game sound creation program that can be used to create very impressive sound effects, including automatic random selection of different sounds for a particular event and changing the pitch and volume of sounds as they play.

Playing Songs Using the MediaPlayer Class

Sound effects are not the best way to play longer sound samples, such as songs.

Sound data takes up too much program memory.

Use the media-playing features of XNA, which let your games use compressed .mp3 and .wma files as background music.

XNA is not able to use .mp3 or .wma files as content for sound effects, but it does have the ability to play such files using the MediaPlayer class.

Playing Songs Using the MediaPlayer Class

The MediaPlayer class provides a Play method that is used to start playback of a particular Song value. You can load a song as you would any other item of content.

// Game World // Song to be played by the MediaPlayer class Song music;

protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw // textures. spriteBatch = new SpriteBatch(GraphicsDevice); music = Content.Load<Song>("music"); // .mp3 file in

// Content Folder

A button to start/resume playbackB button to pause it

The MediaPlayer class provides a property called State that your program can use to determine whether or not it is presently playing a song.

pad1 = GamePad.GetState(PlayerIndex.One);

if (pad1.Buttons.A == ButtonState.Pressed){if (MediaPlayer.State == MediaState.Paused){MediaPlayer.Resume();}if (MediaPlayer.State == MediaState.Stopped){MediaPlayer.Play(music);}}if (pad1.Buttons.B == ButtonState.Pressed){if (MediaPlayer.State == MediaState.Playing){MediaPlayer.Pause();}}