-
Notifications
You must be signed in to change notification settings - Fork 1
/
AudioManager.cs
234 lines (191 loc) · 7.01 KB
/
AudioManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#region File Description
//-----------------------------------------------------------------------------
// AudioManager.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
#endregion
namespace WizardWars
{
/// <summary>
/// Component that manages audio playback for all cues.
/// </summary>
/// <remarks>Based on a similar class found in the NetRumble starter kit.</remarks>
public class AudioManager : GameComponent
{
#region Singleton
/// <summary>
/// The singleton for this type.
/// </summary>
private static AudioManager audioManager = null;
#endregion
#region Audio Data
/// <summary>
/// The audio engine used to play all cues.
/// </summary>
private AudioEngine audioEngine;
/// <summary>
/// The soundbank that contains all cues.
/// </summary>
private SoundBank soundBank;
/// <summary>
/// The wavebank with all wave files for this game.
/// </summary>
private WaveBank waveBank;
/// <summary>
/// The cue for the current music.
/// </summary>
private Cue musicCue;
#endregion
#region Initialization Methods
/// <summary>
/// Constructs the manager for audio playback of all cues.
/// </summary>
/// <param name="game">The game that this component will be attached to.</param>
/// <param name="settingsFile">The filename of the XACT settings file.</param>
/// <param name="waveBankFile">The filename of the XACT wavebank file.</param>
/// <param name="soundBankFile">The filename of the XACT soundbank file.</param>
private AudioManager(Game game, string settingsFile, string waveBankFile,
string soundBankFile)
: base(game)
{
try
{
audioEngine = new AudioEngine(settingsFile);
waveBank = new WaveBank(audioEngine, waveBankFile);
soundBank = new SoundBank(audioEngine, soundBankFile);
}
catch (NoAudioHardwareException)
{
// silently fall back to silence
audioEngine = null;
waveBank = null;
soundBank = null;
}
}
/// <summary>
/// Initialize the static AudioManager functionality.
/// </summary>
/// <param name="game">The game that this component will be attached to.</param>
/// <param name="settingsFile">The filename of the XACT settings file.</param>
/// <param name="waveBankFile">The filename of the XACT wavebank file.</param>
/// <param name="soundBankFile">The filename of the XACT soundbank file.</param>
public static void Initialize(Game game, string settingsFile,
string waveBankFile, string soundBankFile)
{
audioManager = new AudioManager(game, settingsFile, waveBankFile,
soundBankFile);
if (game != null)
{
game.Components.Add(audioManager);
}
}
#endregion
#region Cue Methods
/// <summary>
/// Retrieve a cue by name.
/// </summary>
/// <param name="cueName">The name of the cue requested.</param>
/// <returns>The cue corresponding to the name provided.</returns>
public static Cue GetCue(string cueName)
{
if ((audioManager == null) || (audioManager.audioEngine == null) ||
(audioManager.soundBank == null) || (audioManager.waveBank == null))
{
return null;
}
return audioManager.soundBank.GetCue(cueName);
}
/// <summary>
/// Plays a cue by name.
/// </summary>
/// <param name="cueName">The name of the cue to play.</param>
public static void PlayCue(string cueName)
{
if ((audioManager != null) && (audioManager.audioEngine != null) &&
(audioManager.soundBank != null) && (audioManager.waveBank != null))
{
audioManager.soundBank.PlayCue(cueName);
}
}
/// <summary>
/// Play music by cue name.
/// </summary>
/// <param name="musicCueName">The name of the music cue.</param>
public static void PlayMusic(string musicCueName)
{
if ((audioManager == null) || (audioManager.audioEngine == null) ||
(audioManager.soundBank == null) || (audioManager.waveBank == null))
{
return;
}
// stop the old music cue
if (audioManager.musicCue != null)
{
audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
}
// get the new music cue, if any
audioManager.musicCue = GetCue(musicCueName);
// start the new music cue, if any
if (audioManager.musicCue != null)
{
audioManager.musicCue.Play();
}
}
#endregion
#region Updating Methods
/// <summary>
/// Update the audio manager, particularly the engine.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// update the audio engine
if (audioEngine != null)
{
audioEngine.Update();
}
base.Update(gameTime);
}
#endregion
#region Instance Disposal Methods
/// <summary>
/// Clean up the component when it is disposing.
/// </summary>
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
if (soundBank != null)
{
soundBank.Dispose();
soundBank = null;
}
if (waveBank != null)
{
waveBank.Dispose();
waveBank = null;
}
if (audioEngine != null)
{
audioEngine.Dispose();
audioEngine = null;
}
}
}
finally
{
base.Dispose(disposing);
}
}
#endregion
}
}