COMPUTER PROGRAMMING 2 Chapter 7 Sound. Objectives Find out how to prepare sounds for inclusion in Microsoft XNA projects. Incorporate sounds into XNA.

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved. 1 Outline 3.1Introduction 3.2Game Loop Components 3.3How to Implement in C# 3.4Adding Image to XNA Project.
Advertisements

SETTING UP A PROJECT Adobe Premiere Pro CS6. Getting started… Adobe Premiere Pro project file stores links to all the video and sound files-aka…clips.
Constructor and New Fields // Don't synch draw() with vertical retrace of monitor graphics.SynchronizeWithVerticalRetrace = false; IsFixedTimeStep = true;
Creating Games For Windows, Xbox 360, and Windows Phone 7 Ryan Plemons
11 Reaction Timer Game Session 8.1. Session Overview  Find out how an XNA program can measure the passage of time and trigger events at certain points.
Written by: Dr. JJ Shepherd
Audio 靜宜大學資工系 蔡奇偉 副教授 © 內容大綱 簡介 Recording Playback Sample Rate and Sample Quality Mono & Stereo Sound Sound Formats, Compression, and Codecs XNA.
Chapter 6 Jam! Implementing Audio in Android Apps.
Chapter 6: Jam! Implementing Audio in Android Apps.
Windows XP Basics OVERVIEW Next.
Computer Science – Game DesignUC Santa Cruz Today Announcements – Imagine cup theme is required for all games Only one team has signed up on imagine cup.
Playing Sounds inside XNA Game Design Experience Professor Jim Whitehead February 23, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
System Resources INFO1119 (Fall 2012).
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
CHAPTER 1 XNA Game Studio 4.0. Your First Project A computer game is not just a program—it is also lots of other bits and pieces that make playing the.
11 Games and Content Session 4.1. Session Overview  Show how games are made up of program code and content  Find out about the content management system.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
CHAPTER 4 Images XNA Game Studio 4.0. Objectives Find out how the Content Manager lets you add pictures to Microsoft XNA games. Discover how pictures.
Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 11 Adding Sound and Video. Chapter 11 Lessons 1.Work with sound 2.Specify synchronization options 3.Modify sounds 4.Use ActionScript with sound.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
COMPUTER PROGRAMMING 2 Timers. Game Idea: Mob Reaction Timer Use a timer variable to keep track of time and a variable for each player to measure the.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
Using Namepsaces  This section lists the namespaces that the application will be using frequently. Saves the programmer from specifying a fully qualified.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Developing the Game User Interface (UI) Lesson 5.
1 Useful Tools for Making Video Games Part V An overview of.
11 Adding Sounds Session 7.1. Session Overview  Find out how to capture and manipulate sound on a Windows PC  Show how sound is managed as an item of.
Microsoft Tech Days 2012 Cheezia: Developing a Windows Phone XNA Game Rodrigo Barretto Software Engineer - MCPD on Windows Phone
11 Using the Keyboard Session Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types.
Rob Miles. Creating a Working MoodLight We know that colours in XNA are stored as values which represent the amount of red, blue, green and transparency.
CHAPTER 10 Using C# Methods to Solve Problem XNA Game Studio 4.0.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
XNA An Introduction. What XNA is… Microsoft® XNA™ is composed of industry- leading software, services, resources, and communities focused on enabling.
Chapter Two Creating a First Project in Visual Basic.
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
Getting Started with OPC.NET OPC.NET Software Client Interface Client Base Server Base OPC Wrapper OPC COM Server Server Interface WCF Alternate.
11 Getting Player Input Using a Gamepad Session 3.1.
Aaron Yuen 1 Outline 9.1Storyboarding & Charting Method for Game Design 9.2Game State in Game Development 9.3Game State Management 9.4GUI Assets for Gameplay.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region.
CHAPTER 6 Multiplayer XNA Game Studio 4.0. Objectives Discover how to detect and use individual button-press events in a game. Learn how to create and.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
CS378 - Mobile Computing Audio.
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
© 2011 Delmar, Cengage Learning Chapter 11 Adding Sound and Video.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
11 Debugging Programs Session Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio.
11 Computers, C#, XNA, and You Session 1.1. Session Overview  Find out what computers are all about ...and what makes a great programmer  Discover.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
Rob Miles. Using data in an XNA game program An XNA game program Draw and Update methods that are called to run the game Colours are held in XNA as four.
11 Adding Vibration Effects Session Session Overview  Describe how the vibration feature of the gamepad works  Show how an XNA program can control.
Adobe AuditionProject 4 guide © 2012 Adobe Systems IncorporatedHow to record narration1 You can record narration for your video directly into Adobe Audition.
Section 4 The Basics of Windows 7. Windows 7 Versions  Home Premium  Professional  Specialized.
XNA Sound Effects. float volume, pitch, pan; SoundEffect boing1; SoundEffect boing2; boing1 = Content.Load ("boing1"); boing2 = Content.Load ("boing2");
2D Graphics CMT3311. This covers... How to make a transparent sprite How to add a sprite to your project and draw it Properties of sprites and how to.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
CHAPTER 5 Text XNA Game Studio 4.0. Objectives Discover how text is drawn using Microsoft XNA. Add some font resources to your XNA program. Draw some.
Using and Creating Sprites
Executive Reports, Instructions and Documentation
Presentation 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 (“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 SoundEffect reference 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 ("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 ("music");//.mp3 file in // Content Folder

A button to start/resume playback B 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(); }