forked from CartBlanche/MonoGame-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioManager.cs
338 lines (278 loc) · 10.5 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#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;
using System.Collections.Generic;
#endregion
namespace RolePlaying
{
/// <summary>
/// Component that manages audio playback for all cues.
/// </summary>
/// <remarks>
/// Similar to a class found in the Net Rumble starter kit on the
/// XNA Creators Club Online website (http://creators.xna.com).
/// </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;
#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 (String.IsNullOrEmpty(cueName) ||
(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);
}
}
#endregion
#region Music
/// <summary>
/// The cue for the music currently playing, if any.
/// </summary>
private Cue musicCue;
/// <summary>
/// Stack of music cue names, for layered music playback.
/// </summary>
private Stack<string> musicCueNameStack = new Stack<string>();
/// <summary>
/// Plays the desired music, clearing the stack of music cues.
/// </summary>
/// <param name="cueName">The name of the music cue to play.</param>
public static void PlayMusic(string cueName)
{
// start the new music cue
if (audioManager != null)
{
audioManager.musicCueNameStack.Clear();
PushMusic(cueName);
}
}
/// <summary>
/// Plays the music for this game, adding it to the music stack.
/// </summary>
/// <param name="cueName">The name of the music cue to play.</param>
public static void PushMusic(string cueName)
{
// start the new music cue
if ((audioManager != null) && (audioManager.audioEngine != null) &&
(audioManager.soundBank != null) && (audioManager.waveBank != null))
{
audioManager.musicCueNameStack.Push(cueName);
if ((audioManager.musicCue == null) ||
(audioManager.musicCue.Name != cueName))
{
if (audioManager.musicCue != null)
{
audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
//audioManager.musicCue.Dispose();
audioManager.musicCue = null;
}
audioManager.musicCue = GetCue(cueName);
if (audioManager.musicCue != null)
{
audioManager.musicCue.Play();
}
}
}
}
/// <summary>
/// Stops the current music and plays the previous music on the stack.
/// </summary>
public static void PopMusic()
{
// start the new music cue
if ((audioManager != null) && (audioManager.audioEngine != null) &&
(audioManager.soundBank != null) && (audioManager.waveBank != null))
{
string cueName = null;
if (audioManager.musicCueNameStack.Count > 0)
{
audioManager.musicCueNameStack.Pop();
if (audioManager.musicCueNameStack.Count > 0)
{
cueName = audioManager.musicCueNameStack.Peek();
}
}
if ((audioManager.musicCue == null) ||
(audioManager.musicCue.Name != cueName))
{
if (audioManager.musicCue != null)
{
audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
//audioManager.musicCue.Dispose();
audioManager.musicCue = null;
}
if (!String.IsNullOrEmpty(cueName))
{
audioManager.musicCue = GetCue(cueName);
if (audioManager.musicCue != null)
{
audioManager.musicCue.Play();
}
}
}
}
}
/// <summary>
/// Stop music playback, clearing the cue.
/// </summary>
public static void StopMusic()
{
if (audioManager != null)
{
audioManager.musicCueNameStack.Clear();
if (audioManager.musicCue != null)
{
audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
//audioManager.musicCue.Dispose();
audioManager.musicCue = null;
}
}
}
#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();
}
//if ((musicCue != null) && musicCue.IsStopped)
//{
// AudioManager.PopMusic();
//}
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)
{
StopMusic();
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
}
}