fna-workbench

fna-workbench Git Source Tree


Root/src/Audio/SoundBank.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
 * Copyright 2009-2016 Ethan Lee and the MonoGame Team
 *
 * Released under the Microsoft Public License.
 * See LICENSE for details.
 */
#endregion
 
#region Using Statements
using System;
using System.Collections.Generic;
using System.IO;
#endregion
 
namespace Microsoft.Xna.Framework.Audio
{
    public class SoundBank : IDisposable
    {
        #region Public Properties
 
        public bool IsDisposed
        {
            get;
            private set;
        }
 
        public bool IsInUse
        {
            get
            {
                throw new NotImplementedException("Bank Cue instance count tracking!");
            }
        }
 
        #endregion
 
        #region Private Variables
 
        private AudioEngine INTERNAL_baseEngine;
 
        private List<string> INTERNAL_waveBankNames;
        private Dictionary<string, CueData> INTERNAL_cueData;
 
        #endregion
 
        #region Disposing Event
 
        public event EventHandler<EventArgs> Disposing;
 
        #endregion
 
        #region Public Constructor
 
        public SoundBank(AudioEngine audioEngine, string filename)
        {
            if (audioEngine == null)
            {
                throw new ArgumentNullException("audioEngine");
            }
            if (String.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }
 
            INTERNAL_baseEngine = audioEngine;
 
            using (Stream soundBankStream = TitleContainer.OpenStream(filename))
            using (BinaryReader reader = new BinaryReader(soundBankStream))
            {
                // Check the file header. Should be 'SDBK'
                if (reader.ReadUInt32() != 0x4B424453)
                {
                    throw new ArgumentException("SDBK format not recognized!");
                }
 
                // Check the content version. Assuming XNA4 Refresh.
                if (reader.ReadUInt16() != AudioEngine.ContentVersion)
                {
                    throw new ArgumentException("SDBK Content version!");
                }
 
                // Check the tool version. Assuming XNA4 Refresh.
                if (reader.ReadUInt16() != 43)
                {
                    throw new ArgumentException("SDBK Tool version!");
                }
 
                // CRC, unused
                reader.ReadUInt16();
 
                // Last modified, unused
                reader.ReadUInt64();
 
                // Unknown value, Internet suggests platform
                reader.ReadByte();
 
                // Cue Counts
                ushort numCueSimple = reader.ReadUInt16();
                ushort numCueComplex = reader.ReadUInt16();
 
                // Unknown value
                reader.ReadUInt16();
 
                // Total Cues, unused
                reader.ReadUInt16();
 
                // Number of associated WaveBanks
                byte numWaveBanks = reader.ReadByte();
 
                // Unknown, Internet suggest number of "sounds"
                reader.ReadUInt16();
 
                // Cue Name Table Length
                ushort cueNameTableLength = reader.ReadUInt16();
 
                // Unknown value
                reader.ReadUInt16();
 
                // Cue Offsets
                uint cueSimpleOffset = reader.ReadUInt32();
                uint cueComplexOffset = reader.ReadUInt32();
 
                // Cue Name Table Offset
                uint cueNameTableOffset = reader.ReadUInt32();
 
                // Unknown value
                reader.ReadUInt32();
 
                // Variable Tables Offset, unused
                reader.ReadUInt32();
 
                // Unknown value
                reader.ReadUInt32();
 
                // WaveBank Name Table Offset
                uint waveBankNameTableOffset = reader.ReadUInt32();
 
                // Cue Name Hash Offsets, unused
                reader.ReadUInt32();
                reader.ReadUInt32();
 
                // Unknown value, Internet suggest "sounds" offset
                reader.ReadUInt32();
 
                // SoundBank Name, unused
                reader.ReadBytes(64);
 
                // Parse WaveBank names
                soundBankStream.Seek(waveBankNameTableOffset, SeekOrigin.Begin);
                INTERNAL_waveBankNames = new List<string>();
                for (byte i = 0; i < numWaveBanks; i += 1)
                {
                    INTERNAL_waveBankNames.Add(
                        System.Text.Encoding.UTF8.GetString(
                            reader.ReadBytes(64), 0, 64
                        ).Replace("\0", "")
                    );
                }
 
                // Parse Cue name list
                soundBankStream.Seek(cueNameTableOffset, SeekOrigin.Begin);
                string[] cueNames = System.Text.Encoding.UTF8.GetString(
                    reader.ReadBytes(cueNameTableLength),
                    0,
                    cueNameTableLength
                ).Split('\0');
 
                // Create our CueData Dictionary
                INTERNAL_cueData = new Dictionary<string, CueData>();
 
                // Parse Simple Cues
                soundBankStream.Seek(cueSimpleOffset, SeekOrigin.Begin);
                for (ushort i = 0; i < numCueSimple; i += 1)
                {
                    // Cue flags, unused
                    reader.ReadByte();
 
                    // Cue Sound Offset
                    uint offset = reader.ReadUInt32();
 
                    // Store this for when we're done reading the sound.
                    long curPos = reader.BaseStream.Position;
 
                    // Go to the sound in the Bank.
                    reader.BaseStream.Seek(offset, SeekOrigin.Begin);
 
                    // Parse the Sound
                    INTERNAL_cueData.Add(
                        cueNames[i],
                        new CueData(new XACTSound(reader))
                    );
 
                    // Back to where we were...
                    reader.BaseStream.Seek(curPos, SeekOrigin.Begin);
                }
 
                // Parse Complex Cues
                soundBankStream.Seek(cueComplexOffset, SeekOrigin.Begin);
                for (ushort i = 0; i < numCueComplex; i += 1)
                {
                    // Cue flags
                    byte cueFlags = reader.ReadByte();
 
                    if ((cueFlags & 0x04) != 0) // FIXME: ???
                    {
                        // Cue Sound Offset
                        uint offset = reader.ReadUInt32();
 
                        // Unknown value
                        reader.ReadUInt32();
 
                        // Store this for when we're done reading the sound.
                        long curPos = reader.BaseStream.Position;
 
                        // Go to the sound in the bank
                        reader.BaseStream.Seek(offset, SeekOrigin.Begin);
 
                        // Parse the Sound
                        INTERNAL_cueData.Add(
                            cueNames[numCueSimple + i],
                            new CueData(new XACTSound(reader))
                        );
 
                        // Back to where we were...
                        reader.BaseStream.Seek(curPos, SeekOrigin.Begin);
                    }
                    else
                    {
                        // Variation Table Offset for this Cue
                        uint offset = reader.ReadUInt32();
 
                        // Transition Table Offset for this Cue, unused
                        reader.ReadUInt32();
 
                        // Store this for when we're done reading the Variation Table
                        long curPos = reader.BaseStream.Position;
 
                        // Seek to the Variation Table in the file
                        reader.BaseStream.Seek(offset, SeekOrigin.Begin);
 
                        // Number of Variations in the Table
                        ushort numVariations = reader.ReadUInt16();
 
                        // Variation Table Flags
                        ushort varTableFlags = reader.ReadUInt16();
 
                        // Unknown value
                        reader.ReadUInt16();
 
                        // Probability Control Variable, if applicable
                        ushort variable = reader.ReadUInt16();
 
                        // Create data for the CueData
                        XACTSound[] cueSounds = new XACTSound[numVariations];
                        float[,] cueProbs = new float[numVariations, 2];
 
                        // Used to determine Variation storage format
                        int varTableType = (varTableFlags >> 3) & 0x0007;
 
                        for (ushort j = 0; j < numVariations; j += 1)
                        {
                            if (varTableType == 0)
                            {
                                // Wave with byte min/max
                                ushort track = reader.ReadUInt16();
                                byte waveBank = reader.ReadByte();
                                byte wMin = reader.ReadByte();
                                byte wMax = reader.ReadByte();
 
                                // Create the Sound
                                cueSounds[j] = new XACTSound(track, waveBank);
 
                                // Calculate probability based on weight
                                cueProbs[j, 0] = wMax / 255.0f;
                                cueProbs[j, 1] = wMin / 255.0f;
                            }
                            else if (varTableType == 1)
                            {
                                // Complex with byte min/max
                                uint varOffset = reader.ReadUInt32();
                                byte wMin = reader.ReadByte();
                                byte wMax = reader.ReadByte();
 
                                // Store for sound read
                                long varPos = reader.BaseStream.Position;
 
                                // Seek to the sound in the Bank
                                reader.BaseStream.Seek(varOffset, SeekOrigin.Begin);
 
                                // Read the sound
                                cueSounds[j] = new XACTSound(reader);
 
                                // Back to where we were...
                                reader.BaseStream.Seek(varPos, SeekOrigin.Begin);
 
                                // Calculate probability based on weight
                                cueProbs[j, 0] = wMax / 255.0f;
                                cueProbs[j, 1] = wMin / 255.0f;
                            }
                            else if (varTableType == 3)
                            {
                                // Complex with float min/max
                                uint varOffset = reader.ReadUInt32();
                                float wMin = reader.ReadSingle();
                                float wMax = reader.ReadSingle();
 
                                // Unknown value
                                reader.ReadUInt32();
 
                                // Store for sound read
                                long varPos = reader.BaseStream.Position;
 
                                // Seek to the sound in the Bank
                                reader.BaseStream.Seek(varOffset, SeekOrigin.Begin);
 
                                // Read the sound
                                cueSounds[j] = new XACTSound(reader);
 
                                // Back to where we were...
                                reader.BaseStream.Seek(varPos, SeekOrigin.Begin);
 
                                // Calculate probability based on weight
                                cueProbs[j, 0] = wMax;
                                cueProbs[j, 1] = wMin;
                            }
                            else if (varTableType == 4)
                            {
                                // Compact Wave
                                ushort track = reader.ReadUInt16();
                                byte waveBank = reader.ReadByte();
 
                                // Create the Sound
                                cueSounds[j] = new XACTSound(track, waveBank);
 
                                // FIXME: Assume Sound weight is 100%
                                cueProbs[j, 0] = 1.0f;
                                cueProbs[j, 1] = 0.0f;
                            }
                            else
                            {
                                throw new NotSupportedException();
                            }
                        }
 
                        // Back to where we were...
                        reader.BaseStream.Seek(curPos, SeekOrigin.Begin);
 
                        // Add Built CueData to Dictionary
                        INTERNAL_cueData.Add(
                            cueNames[numCueSimple + i],
                            new CueData(
                                cueSounds,
                                cueProbs,
                                (varTableType == 3) ? INTERNAL_baseEngine.INTERNAL_getVariableName(variable) : String.Empty
                            )
                        );
                    }
 
                    // Cue instance limit
                    byte instanceLimit = reader.ReadByte();
 
                    // Fade In/Out
                    ushort fadeIn = reader.ReadUInt16();
                    ushort fadeOut = reader.ReadUInt16();
 
                    // Cue max instance behavior
                    byte behavior = reader.ReadByte();
 
                    INTERNAL_cueData[cueNames[numCueSimple + i]].SetLimit(
                        instanceLimit,
                        behavior,
                        fadeIn,
                        fadeOut
                    );
                }
            }
            IsDisposed = false;
        }
 
        #endregion
 
        #region Destructor
 
        ~SoundBank()
        {
            Dispose(true);
        }
 
        #endregion
 
        #region Public Dispose Method
 
        public void Dispose()
        {
            Dispose(false);
        }
 
        #endregion
 
        #region Protected Dispose Method
 
        protected void Dispose(bool disposing)
        {
            if (!IsDisposed)
            {
                if (Disposing != null)
                {
                    Disposing.Invoke(this, null);
                }
                INTERNAL_waveBankNames.Clear();
                INTERNAL_cueData.Clear();
                IsDisposed = true;
            }
        }
 
        #endregion
 
        #region Public Methods
 
        public Cue GetCue(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (!INTERNAL_cueData.ContainsKey(name))
            {
                throw new ArgumentException("Cue name not found: " + name);
            }
            return new Cue(
                INTERNAL_baseEngine,
                INTERNAL_waveBankNames,
                name,
                INTERNAL_cueData[name],
                false
            );
        }
 
        public void PlayCue(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (!INTERNAL_cueData.ContainsKey(name))
            {
                throw new InvalidOperationException("name not found!");
            }
            Cue newCue = new Cue(
                INTERNAL_baseEngine,
                INTERNAL_waveBankNames,
                name,
                INTERNAL_cueData[name],
                true
            );
            newCue.Play();
        }
 
        public void PlayCue(
            string name,
            AudioListener listener,
            AudioEmitter emitter
        ) {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (!INTERNAL_cueData.ContainsKey(name))
            {
                throw new InvalidOperationException("name not found!");
            }
            Cue newCue = new Cue(
                INTERNAL_baseEngine,
                INTERNAL_waveBankNames,
                name,
                INTERNAL_cueData[name],
                true
            );
            newCue.Apply3D(listener, emitter);
            newCue.Play();
        }
 
        #endregion
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.09083s using 11 queries.