11 adding sounds session 7.1. session overview find out how to capture and manipulate sound on a...
TRANSCRIPT
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 2
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
Chapter 7.1: Adding Sounds 3
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 4
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
Chapter 7.1: Adding Sounds 5
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 6
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 7
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
Chapter 7.1: Adding Sounds 8
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
Chapter 7.1: Adding Sounds 9
1. Sound Capture
Chapter 7.1: Adding Sounds 10
We can capture some sound effects for use in our drums
This in fact turns the gamepad drum machine into a kind of sampler
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
Chapter 7.1: Adding Sounds 11
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
Chapter 7.1: Adding Sounds 12
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
Chapter 7.1: Adding Sounds 13
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
Chapter 7.1: Adding Sounds 14
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
Chapter 7.1: Adding Sounds 15
Adding SoundEffect Values to the Game World
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 16
// Game WorldSoundEffect kick;SoundEffect cymbolTing;SoundEffect snare;SoundEffect top;
// Game WorldSoundEffect kick;SoundEffect cymbolTing;SoundEffect snare;SoundEffect top;
Loading SoundEffect Values in LoadContent
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 17
protected override void LoadContent(){ // Create a new SpriteBatch spriteBatch = new SpriteBatch(GraphicsDevice);
kick = Content.Load<SoundEffect>("kick"); cymbolTing = Content.Load<SoundEffect>("cymbolTing"); snare = Content.Load<SoundEffect>("snare"); top = Content.Load<SoundEffect>("top");}
protected override void LoadContent(){ // Create a new SpriteBatch spriteBatch = new SpriteBatch(GraphicsDevice);
kick = Content.Load<SoundEffect>("kick"); cymbolTing = Content.Load<SoundEffect>("cymbolTing"); snare = Content.Load<SoundEffect>("snare"); top = Content.Load<SoundEffect>("top");}
Playing Sounds
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
Chapter 7.1: Adding Sounds 18
// test if A has been pressed since the last Updateif (oldpad1.Buttons.A == ButtonState.Released && pad1.Buttons.A == ButtonState.Pressed){ snare.Play();}
// test if A has been pressed since the last Updateif (oldpad1.Buttons.A == ButtonState.Released && pad1.Buttons.A == ButtonState.Pressed){ snare.Play();}
2. Drum Pad
Chapter 7.1: Adding Sounds 19
The program loads a sound sample which can then be played using the gamepad
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
Chapter 7.1: Adding Sounds 20
True/False Revision Quiz
Sound is held on a computer as a collection of numbers.
When a sound file is compressed all the loud sounds are discarded.
An XNA device can only play one sound effect at a time.
The SoundEffect type manages the playback of sound during a game.
Sound is just another type of XNA content.
Chapter 7.1: Adding Sounds 21
True/False Revision Quiz
Sound is held on a computer as a collection of numbers.
When a sound file is compressed all the loud sounds are discarded.
An XNA device can only play one sound effect at a time.
The SoundEffect type manages the playback of sound during a game.
Sound is just another type of XNA content.
Chapter 7.1: Adding Sounds 22
True/False Revision Quiz
Sound is held on a computer as a collection of numbers.
When a sound file is compressed all the loud sounds are discarded.
An XNA device can only play one sound effect at a time.
The SoundEffect type manages the playback of sound during a game.
Sound is just another type of XNA content.
Chapter 7.1: Adding Sounds 23
True/False Revision Quiz
Sound is held on a computer as a collection of numbers.
When a sound file is compressed all the loud sounds are discarded.
An XNA device can only play one sound effect at a time.
The SoundEffect type manages the playback of sound during a game.
Sound is just another type of XNA content.
Chapter 7.1: Adding Sounds 24
True/False Revision Quiz
Sound is held on a computer as a collection of numbers.
When a sound file is compressed all the loud sounds are discarded.
An XNA device can only play one sound effect at a time.
The SoundEffect type manages the playback of sound during a game.
Sound is just another type of XNA content.
Chapter 7.1: Adding Sounds 25
True/False Revision Quiz
Sound is held on a computer as a collection of numbers.
When a sound file is compressed all the loud sounds are discarded.
An XNA device can only play one sound effect at a time.
The SoundEffect type manages the playback of sound during a game.
Sound is just another type of XNA content.
Chapter 7.1: Adding Sounds 26