Root/
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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | #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. */ /* The unxwb project, written by Luigi Auriemma, was released in 2006 under the * GNU General Public License, version 2.0: * * * While the unxwb project was released under the GPL, Luigi has given express * permission to the MonoGame project to use code from unxwb under the MonoGame * project license. See LICENSE for details. * * The unxwb website can be found here: * */ #endregion #region Using Statements using System; using System.IO; #endregion namespace Microsoft.Xna.Framework.Audio { public class WaveBank : IDisposable { #region Private Sound Entry Container Class // Used to store sound entry data, mainly for streaming WaveBanks private class SoundStreamEntry { public uint PlayOffset { get ; private set ; } public uint PlayLength { get ; private set ; } public uint Codec { get ; private set ; } public uint Frequency { get ; private set ; } public uint Channels { get ; private set ; } public uint LoopOffset { get ; private set ; } public uint LoopLength { get ; private set ; } public uint Alignment { get ; private set ; } public uint BitDepth { get ; private set ; } public SoundStreamEntry( uint playOffset, uint playLength, uint codec, uint frequency, uint channels, uint loopOffset, uint loopLength, uint alignment, uint bitDepth ) { PlayOffset = playOffset; PlayLength = playLength; Codec = codec; Frequency = frequency; Channels = channels; LoopOffset = loopOffset; LoopLength = loopLength; Alignment = alignment; BitDepth = bitDepth; } } #endregion #region Public Properties public bool IsDisposed { get ; private set ; } public bool IsPrepared { get ; private set ; } public bool IsInUse { get { throw new NotImplementedException( "Cue wave entry dependency tracking!" ); } } #endregion #region Private Variables // We keep this in order to Dispose ourselves later. private AudioEngine INTERNAL_baseEngine; private string INTERNAL_name; // These are only used for streaming WaveBanks private BinaryReader INTERNAL_waveBankReader; private SoundStreamEntry[] INTERNAL_soundStreamEntries; // Stores the actual wavedata private SoundEffect[] INTERNAL_sounds; #endregion #region Disposing Event public event EventHandler<EventArgs> Disposing; #endregion #region Public Constructors public WaveBank( AudioEngine audioEngine, string nonStreamingWaveBankFilename ) { if (audioEngine == null ) { throw new ArgumentNullException( "audioEngine" ); } if (String.IsNullOrEmpty(nonStreamingWaveBankFilename)) { throw new ArgumentNullException( "nonStreamingWaveBankFilename" ); } using (Stream stream = TitleContainer.OpenStream(nonStreamingWaveBankFilename)) using (BinaryReader reader = new BinaryReader(stream)) { LoadWaveBank(audioEngine, reader, false ); } } public WaveBank( AudioEngine audioEngine, string streamingWaveBankFilename, int offset, short packetsize ) { /* Note that offset and packetsize go unused, * because we're frauds and aren't actually streaming. * -flibit */ if (audioEngine == null ) { throw new ArgumentNullException( "audioEngine" ); } if (String.IsNullOrEmpty(streamingWaveBankFilename)) { throw new ArgumentNullException( "streamingWaveBankFilename" ); } INTERNAL_waveBankReader = new BinaryReader( TitleContainer.OpenStream(streamingWaveBankFilename) ); LoadWaveBank(audioEngine, INTERNAL_waveBankReader, true ); } #endregion #region Destructor ~WaveBank() { Dispose( true ); } #endregion #region Public Dispose Method public void Dispose() { Dispose( false ); } #endregion #region Protected Dispose Method protected virtual void Dispose( bool disposing) { if (!IsDisposed) { if (Disposing != null ) { Disposing.Invoke( this , null ); } foreach (SoundEffect se in INTERNAL_sounds) { if (se != null ) { se.Dispose(); } } INTERNAL_baseEngine.INTERNAL_removeWaveBank(INTERNAL_name); INTERNAL_sounds = null ; if (INTERNAL_waveBankReader != null ) { INTERNAL_waveBankReader.Close(); INTERNAL_waveBankReader = null ; } IsDisposed = true ; IsPrepared = false ; } } #endregion #region Internal Method internal SoundEffect INTERNAL_getTrack( ushort track) { if (INTERNAL_sounds[track] == null ) { LoadWaveEntry( INTERNAL_soundStreamEntries[track], track, INTERNAL_waveBankReader ); } return INTERNAL_sounds[track]; } #endregion #region Private WaveBank Load Method private void LoadWaveBank(AudioEngine audioEngine, BinaryReader reader, bool streaming) { /* Until we finish the LoadWaveBank process, this WaveBank is NOT * ready to run. For us this doesn't really matter, but the game * could be loading WaveBanks asynchronously, so let's be careful. * -flibit */ IsPrepared = false ; INTERNAL_baseEngine = audioEngine; // Check the file header. Should be 'WBND' if (reader.ReadUInt32() != 0x444E4257) { throw new ArgumentException( "WBND format not recognized!" ); } // Check the content version. Assuming XNA4 Refresh. if (reader.ReadUInt32() != AudioEngine.ContentVersion) { throw new ArgumentException( "WBND Content version!" ); } // Check the tool version. Assuming XNA4 Refresh. if (reader.ReadUInt32() != 44) { throw new ArgumentException( "WBND Tool version!" ); } // Obtain WaveBank chunk offsets/lengths uint [] offsets = new uint [5]; uint [] lengths = new uint [5]; for ( int i = 0; i < 5; i += 1) { offsets[i] = reader.ReadUInt32(); lengths[i] = reader.ReadUInt32(); } // Seek to the first offset, obtain WaveBank info reader.BaseStream.Seek(offsets[0], SeekOrigin.Begin); // IsStreaming bool, unused reader.ReadUInt16(); // WaveBank Flags ushort wavebankFlags = reader.ReadUInt16(); // bool containsEntryNames = (wavebankFlags & 0x0001) != 0; bool compact = (wavebankFlags & 0x0002) != 0; // bool syncDisabled = (wavebankFlags & 0x0004) != 0; // bool containsSeekTables = (wavebankFlags & 0x0008) != 0; // WaveBank Entry Count uint numEntries = reader.ReadUInt32(); // WaveBank Name INTERNAL_name = System.Text.Encoding.UTF8.GetString( reader.ReadBytes(64), 0, 64 ).Replace( "\0" , "" ); // WaveBank entry information uint metadataElementSize = reader.ReadUInt32(); reader.ReadUInt32(); // nameElementSize uint alignment = reader.ReadUInt32(); // Determine the generic play region offset uint playRegionOffset = offsets[4]; if (playRegionOffset == 0) { playRegionOffset = offsets[1] + (numEntries * metadataElementSize); } // Entry format. Read early for Compact data uint entryFormat = 0; if (compact) { entryFormat = reader.ReadUInt32(); } // Read in the wavedata INTERNAL_sounds = new SoundEffect[numEntries]; if (streaming) { INTERNAL_soundStreamEntries = new SoundStreamEntry[numEntries]; } uint curOffset = offsets[1]; for ( int curEntry = 0; curEntry < numEntries; curEntry += 1) { // Seek to the current entry reader.BaseStream.Seek(curOffset, SeekOrigin.Begin); // Entry Information uint entryPlayOffset = 0; uint entryPlayLength = 0; uint entryLoopOffset = 0; uint entryLoopLength = 0; // Obtain Entry Information if (compact) { uint entryLength = reader.ReadUInt32(); entryPlayOffset = (entryLength & ((1 << 21) - 1)) * alignment; entryPlayLength = (entryLength >> 21) & ((1 << 11) - 1); // FIXME: Deviation Length reader.BaseStream.Seek( curOffset + metadataElementSize, SeekOrigin.Begin ); if (curEntry == (numEntries - 1)) { // Last track, last length. entryLength = lengths[4]; } else { entryLength = ( ( reader.ReadUInt32() & ((1 << 21) - 1) ) * alignment ); } entryPlayLength = entryLength - entryPlayOffset; } else { if (metadataElementSize >= 4) reader.ReadUInt32(); // Flags/Duration, unused if (metadataElementSize >= 8) entryFormat = reader.ReadUInt32(); if (metadataElementSize >= 12) entryPlayOffset = reader.ReadUInt32(); if (metadataElementSize >= 16) entryPlayLength = reader.ReadUInt32(); if (metadataElementSize >= 20) entryLoopOffset = reader.ReadUInt32(); if (metadataElementSize >= 24) entryLoopLength = reader.ReadUInt32(); else { // FIXME: This is a bit hacky. if (entryPlayLength != 0) { entryPlayLength = lengths[4]; } } } // Update seek offsets curOffset += metadataElementSize; entryPlayOffset += playRegionOffset; // Parse Format for Wavedata information uint entryCodec = (entryFormat >> 0) & ((1 << 2) - 1); uint entryChannels = (entryFormat >> 2) & ((1 << 3) - 1); uint entryFrequency = (entryFormat >> (2 + 3)) & ((1 << 18) - 1); uint entryAlignment = (entryFormat >> (2 + 3 + 18)) & ((1 << 8) - 1); uint entryBitDepth = (entryFormat >> (2 + 3 + 18 + 8)); if (streaming) { INTERNAL_soundStreamEntries[curEntry] = new SoundStreamEntry( entryPlayOffset, entryPlayLength, entryCodec, entryFrequency, entryChannels, entryLoopOffset, entryLoopLength, entryAlignment, entryBitDepth ); } else { SoundStreamEntry filler = new SoundStreamEntry( entryPlayOffset, entryPlayLength, entryCodec, entryFrequency, entryChannels, entryLoopOffset, entryLoopLength, entryAlignment, entryBitDepth ); LoadWaveEntry(filler, ( ushort ) curEntry, reader); } } // Add this WaveBank to the AudioEngine Dictionary audioEngine.INTERNAL_addWaveBank(INTERNAL_name, this ); // Finally. IsDisposed = false ; IsPrepared = true ; } #endregion #region Private WaveBank Entry Load Method private void LoadWaveEntry(SoundStreamEntry entry, ushort track, BinaryReader reader) { // Read Wavedata reader.BaseStream.Seek(entry.PlayOffset, SeekOrigin.Begin); byte [] entryData = reader.ReadBytes(( int ) entry.PlayLength); // Load SoundEffect based on codec if (entry.Codec == 0x0) // PCM { INTERNAL_sounds[track] = new SoundEffect( "WaveBank Sound" , entryData, entry.Frequency, entry.Channels, entry.LoopOffset, entry.LoopLength, false , entry.BitDepth ); } else if (entry.Codec == 0x2) // ADPCM { INTERNAL_sounds[track] = new SoundEffect( "WaveBank Sound" , entryData, entry.Frequency, entry.Channels, entry.LoopOffset, entry.LoopLength, true , (entry.Alignment + 16) * 2 ); } else // Includes 0x1 - XMA, 0x3 - WMA { throw new NotSupportedException( "Rebuild your WaveBanks with ADPCM!" ); } } #endregion } } |