21
Sound Effects and Audio Tran Minh Triet – Nguyen Khac H Faculty of Information Technology University of Science, VNU-HCM

Sound Effects and Audio

Embed Size (px)

DESCRIPTION

Programing game in XNA

Citation preview

Page 1: Sound Effects and Audio

Sound Effects and AudioSound Effects and Audio

Tran Minh Triet – Nguyen Khac HuyFaculty of Information TechnologyUniversity of Science, VNU-HCM

Page 2: Sound Effects and Audio

XNA and SoundXNA and Sound

XNA 3.0+ allows us to access sounds in two ways

Direct accessMicrosoft Cross-Platform Audio Creation Tool (XACT)

Before 3.0, you could only use XACT

Page 3: Sound Effects and Audio

Direct AccessDirect Access

Place mp3 or wav files into your content folder.Access wav with SoundEffectAccess mp3 with Song

Page 4: Sound Effects and Audio

Direct Access - ExampleDirect Access - Example

Start a new “XNA Windows Game” project

Add two member variablesSoundEffect soundEffect;Song song

Looking for start.wav, Kalimba.mp3 media in SimpleSounds Demo project

Adding the media to Content

Page 5: Sound Effects and Audio

Direct Access - ExampleDirect Access - Example

Page 6: Sound Effects and Audio

Direct Access - ExampleDirect Access - Example

In LoadContent add the following lines//Load the sound soundEffect =

Content.Load<SoundEffect>(@"Audio\start");

song = Content.Load<Song>(@"Audio\Kalimba");

//Play the soundMediaPlayer.Play(song);

Page 7: Sound Effects and Audio

Direct Access - ExampleDirect Access - Example

In Update add the following lines// TODO: Add your update logic hereif

(Keyboard.GetState().IsKeyDown(Keys.Space))

soundEffect.Play();

Build and compile the project

Trying to listen the sounds which are played in the same time

Page 8: Sound Effects and Audio

XACTXACT

Use this to bundle raw wave files together

Add effects to sound

Generated by the Microsoft Cross-Platform Audio Creation Tool, known as XACT

Not a sound editor, just used to bundle files together or change effects.

Page 9: Sound Effects and Audio

XACT - How to doXACT - How to do

Making Sounds with XNA Game Studio and XACT

Step 1: Get A Wave File

Step 2: Create an XACT Project with Wave Files

Step 3: Load an XACT Project Through the Content Pipeline

Step 4: Play Sounds Using the Audio API

Page 10: Sound Effects and Audio

Step 1: Get a wave fileStep 1: Get a wave file

Audio in XNA Game Studio is, at heart, wave-based

Repairing the media by resusing start.wav file

Don’t need to include the start.wav file as content in the project

Go to the next step

Page 11: Sound Effects and Audio

Step 2: Create an XACT ProjectStep 2: Create an XACT Project

Start XACT by choosing Start Programs ➤ ➤Microsoft XNA Game Studio 3.0 Tools ➤ ➤Cross-Platform Audio Creation Tool 2 (XACT2)

In the XACT main window, choose File ➤New Project, and save it as mysound.

On the left side of the window, right-click Wave Bank and select New Wave Bank

Page 12: Sound Effects and Audio

Step 2: Create an XACT ProjectStep 2: Create an XACT Project

From the new wave bank, right click and choose Insert Wave File(s)

Also need to create a New Sound Bank

Drag the ‘start’ wave bank to the Sound Bank panel

Looking at the panel on the left bottom corner, configure settings as you want

Save the project as MySounds.xap

Page 13: Sound Effects and Audio

Step 2: Create an XACT ProjectStep 2: Create an XACT Project

Page 14: Sound Effects and Audio

Step 3: Load XACTStep 3: Load XACT

There are three objects to manage the file contents

AudioEngine

Reference to the audio services

Adjust settings to create the wave and sound banks

Generating the .xgs file when XAP file is compiled

Page 15: Sound Effects and Audio

Step 3: Load XACTStep 3: Load XACT

WaveBank

A collection of wave files

Pass as parameters the AudioEngine object

Generating the .xwb file

It’s not explicitly used, but needing to create

SoundBankA collection of sound cues

Page 16: Sound Effects and Audio

Step 3: Load XACTStep 3: Load XACT

Adding the mysound.xap file to Content

Create audio objects

// Audio objects

AudioEngine audioEngine;

WaveBank waveBank;

SoundBank soundBank;

Page 17: Sound Effects and Audio

Step 3: Load XACTStep 3: Load XACT

Initialize the audio components

audioEngine = new AudioEngine(@"Content\MySound.xgs");

// Assume the default names for the wave and sound banks.

// To change these names, change properties in XACT.

waveBank = new WaveBank(audioEngine, @"Content\Wave Bank.xwb");

soundBank = new SoundBank(audioEngine, @"Content\Sound Bank.xsb");

Page 18: Sound Effects and Audio

Step 4: Using Audio in GamesStep 4: Using Audio in Games

Calling a simple method PlayCue to play

soundBank.PlayCue(“start");

Page 19: Sound Effects and Audio

Step 4: Using Audio in GamesStep 4: Using Audio in Games

Calling a simple method PlayCue to play

soundBank.PlayCue(“start");

OrCue mySound;

mySound = soundBank.GetCue(" start");

mySound.Play();

Page 20: Sound Effects and Audio

Q&AQ&A

?

Page 21: Sound Effects and Audio

ReferencesReferences

XNA framework inputhttp://msdn.microsoft.com/en-us/library/bb203895(XNAGameStudio.30).aspx

EbookLearning XNA 3.0 – Aaron ReedBeginning XNA 2.0 Game Programming