1 / 24

Chapter 7 Sound

Chapter 7 Sound. Computer Programming 2. 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

orrick
Download Presentation

Chapter 7 Sound

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 Sound Computer Programming 2

  2. Objectives • Find out how to prepare sounds for inclusion in Microsoft XNA projects. • Incorporate sounds into XNA. • Play the sounds from within your programs.

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

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

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

  6. 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(); //SoundEffectobject }

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

  8. 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); float pitch between -1 and 1 snare is a SoundEffect reference float pan between -1 (left) and 1 (right) float volume between 0 and 1

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

  10. The SoundEffectInstanceClass • 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.

  11. The SoundEffectInstanceClass • Declaring a SoundEffectInstance object SoundEffectInstancenameSoundEffectInstance; • 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(); }

  12. The SoundEffectInstanceClass • 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.

  13. Controlling a Sound Effect Instance //makes the sound repeat by restarting playback when it stops protected override void Update(GameTimegameTime) { // 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); }

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

  15. 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).

  16. 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;

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

  18. Throwing a NullReferenceException

  19. Checking for Null References • It is actually possible for a program to fail with a null reference if the CreateInstancemethod 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.

  20. 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 }

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

  22. Playing Songs Using the MediaPlayerClass • 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.

  23. Playing Songs Using the MediaPlayerClass • 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

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

More Related