Preview only show first 10 pages with watermark. For full document please download

Session 7.1 Adding Sounds

   EMBED


Share

Transcript

1 Session 7.1 Adding Sounds Chapter 7.1: Adding Sounds 2 Session Overview  Find out how to capture and manipulate sound on a Windows PC  Show how sound is managed as an item of XNA content  Discover how to create SoundEffect instances and control their playback within an XNA game program  Create an XNA program that implements a drum kit Chapter 7.1: Adding Sounds Game Idea: “Gamepad Drum Kit”  The idea of this game is to implement a simple drum kit  Each of the buttons on the gamepad will be allocated a different drum type  When the button is pressed that drum sample is played 3 Chapter 7.1: Adding Sounds 4 Computers and Sound  As far as a computer is concerned a sound sample is just another file of data  The computer can run programs that can work with this type of data  Windows Media Player will provide playback of sound data  The Audacity program will playback data and also allow us to capture and manipulate sound Chapter 7.1: Adding Sounds Sound and Computers  Computers work with numbers  A sound is represented by a sequence of numbers, each of which gives the intensity of the sound at a particular instant in time  The above waveform is that for a kick drum 5 Chapter 7.1: Adding Sounds 6 Sound and Compression  To get high-quality sound, a computer needs to sample a sound signal many thousands of times a second  This produces a lot of data  However, some of this data can be discarded because of the way that the human ear works  Loud sounds at a particular frequency will mask quiet sounds at a similar frequency  This is the basis of MP3 (and other) compression Chapter 7.1: Adding Sounds 7 XNA Sound Effects  XNA games can contain “Sound Effect” content  These are sound samples that can be played under program control  These should not be supplied as compressed sound  Instead they should be recorded using the uncompressed WAV format  The WAV format is a standard for “raw” sound samples Chapter 7.1: Adding Sounds Creating Sound Samples Using Audacity  Audacity is a free Open Source program that can be used to edit audio signals  It lets you capture and edit WAV files  You can also add a plug-in to allow the program to work with MP3 files 8 Chapter 7.1: Adding Sounds Using Audacity  You can obtain Audacity from: http://audacity.sourceforge.net/  Once you have downloaded the program you can also download an install an MP3 plug-in that lets you work with compressed audio files  Remember to respect copyright laws if you use audio files that you did not create 9 Chapter 7.1: Adding Sounds 1. Sound Capture  We can capture some sound effects for use in our drums  This in fact turns the gamepad drum machine into a kind of sampler 10 Chapter 7.1: Adding Sounds Sound Content  I have created some sound samples that we can use for the gamepad drum kit  These were recorded using Audacity and then trimmed to the right size 11 Chapter 7.1: Adding Sounds Adding Sound Content  Sound content is added in just the same way as other content  The Content Manager has filters to process audio content  Note that you can only use WAV files to create SoundEffect objects 12 Chapter 7.1: Adding Sounds Browsing for Sound Content  You can add sounds like any other content  Note that you can select multiple items and add them all at once 13 Chapter 7.1: Adding Sounds Sound Content in the Project  Once the content is added it will be made part of the project, as with images  When the project is built and transferred to the target the sound files are sent as well  You can mix images, fonts, and sound effects in the same content folder 14 Chapter 7.1: Adding Sounds The SoundEffect Type  The XNA Framework provides a type which can be used to create sound effects in games  This type is for “spot” effects such as gunshots or explosions  An instance of the SoundEffect type can hold a sound sample and be asked to play that sound  Each XNA device has hardware that allows a number of samples to be played at the same time  The Zune is the most restricted in this respect 15 Chapter 7.1: Adding Sounds 16 Adding SoundEffect Values to the Game World // Game World SoundEffect kick; SoundEffect cymbolTing; SoundEffect snare; SoundEffect top;  The sounds that we want to play will be part of the Game World for the Drum Pad game  The sound values will be loaded when the LoadContent method runs  The samples will be played in the Update method Chapter 7.1: Adding Sounds 17 Loading SoundEffect Values in LoadContent protected override void LoadContent() { // Create a new SpriteBatch spriteBatch = new SpriteBatch(GraphicsDevice); kick = Content.Load("kick"); cymbolTing = Content.Load("cymbolTing"); snare = Content.Load("snare"); top = Content.Load("top"); }  The sound effects are loaded into memory along with other content  The SoundEffect version of Load is used for this Chapter 7.1: Adding Sounds Playing Sounds // test if A has been pressed since the last Update if (oldpad1.Buttons.A == ButtonState.Released && pad1.Buttons.A == ButtonState.Pressed) { snare.Play(); }  This is the same edge detection code as before  If button A has been pressed the snare drum sample is played  Once playback has started the sound plays automatically 18 Chapter 7.1: Adding Sounds 2. Drum Pad  The program loads a sound sample which can then be played using the gamepad 19 Chapter 7.1: Adding Sounds 20 Summary  Computers manage sounds as files of data  The sounds themselves can be held in uncompressed (WAV) format or compressed (MP3 or WMA format)  Sound effects can only be created from uncompressed (WAV) format files  Sounds are managed by XNA as any other content  The SoundEffect type is used to create sound effect variables in the Game World that can be played during a game