File: SoundBank.cs

package info (click to toggle)
monogame 2.5.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,060 kB
  • ctags: 10,325
  • sloc: cs: 65,996; xml: 591; makefile: 22; ansic: 8
file content (235 lines) | stat: -rwxr-xr-x 7,892 bytes parent folder | download | duplicates (2)
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
#region License
/*
MIT License
Copyright © 2006 The Mono.Xna Team

All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion License

using System;
using System.IO;
using System.Collections.Generic;

namespace Microsoft.Xna.Framework.Audio
{
    public class SoundBank : IDisposable
    {
        string name;
		string filename;
		AudioEngine audioengine;
		WaveBank[] waveBanks;
		Dictionary<string, Cue> cues = new Dictionary<string, Cue>();
        
		bool loaded = false;
		
        public SoundBank(AudioEngine audioEngine, string fileName)
        {
            // Check for windows-style directory separator character
            filename = fileName.Replace('\\',Path.DirectorySeparatorChar);
			audioengine = audioEngine;
		}
		
		//Defer loading because some programs load soundbanks before wavebanks
		private void Load() {	
			FileStream soundbankstream = new FileStream(filename, FileMode.Open);
            BinaryReader soundbankreader = new BinaryReader(soundbankstream);
            
			//Parse the SoundBank.
			//Thanks to Liandril for "xactxtract" for some of the offsets
			
			uint magic = soundbankreader.ReadUInt32 ();
			if (magic != 0x4B424453) { //"SDBK"
				throw new Exception("Bad soundbank format");
			}
			
			uint toolVersion = soundbankreader.ReadUInt16 ();
			uint formatVersion = soundbankreader.ReadUInt16 ();
			if (formatVersion != 46) {
#if DEBUG
				Console.WriteLine ("Warning: SoundBank format not supported");
#endif
			}
			
			uint crc = soundbankreader.ReadUInt16 ();
			//TODO: Verify crc (FCS16)
			
			soundbankreader.ReadBytes(8); //unkn
			uint platform = soundbankreader.ReadByte(); //???
			
			uint numSimpleCues = soundbankreader.ReadUInt16 ();
			uint numComplexCues = soundbankreader.ReadUInt16 ();
			soundbankreader.ReadUInt16 (); //unkn
			uint numTotalCues = soundbankreader.ReadUInt16 ();
			uint numWaveBanks = soundbankreader.ReadByte ();
			uint numSounds = soundbankreader.ReadUInt16 ();
			uint cueNameTableLen = soundbankreader.ReadUInt16 ();
			soundbankreader.ReadUInt16 (); //unkn
			
			uint simpleCuesOffset = soundbankreader.ReadUInt32 ();
			uint complexCuesOffset = soundbankreader.ReadUInt32 (); //unkn
			uint cueNamesOffset = soundbankreader.ReadUInt32 ();
			soundbankreader.ReadUInt32 (); //unkn
			uint variationTablesOffset = soundbankreader.ReadUInt32 ();
			soundbankreader.ReadUInt32 (); //unkn
			uint waveBankNameTableOffset = soundbankreader.ReadUInt32 ();
			uint cueNameHashTableOffset = soundbankreader.ReadUInt32 ();
			uint cueNameHashValsOffset = soundbankreader.ReadUInt32 ();
			uint soundsOffset = soundbankreader.ReadUInt32 ();
			
			name = System.Text.Encoding.UTF8.GetString(soundbankreader.ReadBytes(64)).Replace("\0","");
			
			
			//parse wave bank name table
			soundbankstream.Seek (waveBankNameTableOffset, SeekOrigin.Begin);
			waveBanks = new WaveBank[numWaveBanks];
			for (int i=0; i<numWaveBanks; i++) {
				string bankname = System.Text.Encoding.UTF8.GetString(soundbankreader.ReadBytes(64)).Replace("\0","");
				waveBanks[i] = audioengine.Wavebanks[bankname];
			}
			
			//parse cue name table
			soundbankstream.Seek (cueNamesOffset, SeekOrigin.Begin);
			string[] cueNames = System.Text.Encoding.UTF8.GetString(soundbankreader.ReadBytes((int)cueNameTableLen)).Split('\0');
			soundbankstream.Seek (simpleCuesOffset, SeekOrigin.Begin);
			for (int i=0; i<numSimpleCues; i++) {
				byte flags = soundbankreader.ReadByte ();
				uint soundOffset = soundbankreader.ReadUInt32 ();
				XactSound sound = new XactSound(this, soundbankreader, soundOffset);
				Cue cue = new Cue(cueNames[i], sound);
				
				cues.Add(cue.Name, cue);
			}
			
			soundbankstream.Seek (complexCuesOffset, SeekOrigin.Begin);
			for (int i=0; i<numComplexCues; i++) {
				Cue cue;
				
				byte flags = soundbankreader.ReadByte ();
				if (((flags >> 2) & 1) != 0) {
					//not sure :/
					uint soundOffset = soundbankreader.ReadUInt32 ();
					soundbankreader.ReadUInt32 (); //unkn
					
					XactSound sound = new XactSound(this, soundbankreader, soundOffset);
					cue = new Cue(cueNames[numSimpleCues+i], sound);
				} else {
					uint variationTableOffset = soundbankreader.ReadUInt32 ();
					uint transitionTableOffset = soundbankreader.ReadUInt32 ();
					
					//parse variation table
					long savepos = soundbankstream.Position;
					soundbankstream.Seek (variationTableOffset, SeekOrigin.Begin);
					
					uint numEntries = soundbankreader.ReadUInt16 ();
					uint variationflags = soundbankreader.ReadUInt16 ();
					soundbankreader.ReadByte ();
					soundbankreader.ReadUInt16 ();
					soundbankreader.ReadByte ();
					
					XactSound[] cueSounds = new XactSound[numEntries];
					float[] probs = new float[numEntries];
					
					uint tableType = (variationflags >> 3) & 0x7;
					for (int j=0; j<numEntries; j++) {
						switch (tableType) {
						case 0: //Wave
						{
							uint trackIndex = soundbankreader.ReadUInt16 ();
							byte waveBankIndex = soundbankreader.ReadByte ();
							byte weightMin = soundbankreader.ReadByte ();
							byte weightMax = soundbankreader.ReadByte ();
	
							cueSounds[j] = new XactSound(this.GetWave(waveBankIndex, trackIndex));
							break;
						}
						case 1:
						{
							uint soundOffset = soundbankreader.ReadUInt32 ();
							byte weightMin = soundbankreader.ReadByte ();
							byte weightMax = soundbankreader.ReadByte ();
							
							cueSounds[j] = new XactSound(this, soundbankreader, soundOffset);
							break;
						}
						case 4: //CompactWave
						{
							uint trackIndex = soundbankreader.ReadUInt16 ();
							byte waveBankIndex = soundbankreader.ReadByte ();
							cueSounds[j] = new XactSound(this.GetWave(waveBankIndex, trackIndex));
							break;
						}
						default:
							throw new NotImplementedException();
						}
					}
					
					soundbankstream.Seek (savepos, SeekOrigin.Begin);
					
					cue = new Cue(cueNames[numSimpleCues+i], cueSounds, probs);
				}
				
				//Instance Limit
				soundbankreader.ReadUInt32 ();
				soundbankreader.ReadByte ();
				soundbankreader.ReadByte ();
				
				cues.Add(cue.Name, cue);
			}
			
			soundbankreader.Close ();
			soundbankstream.Close ();
			
			loaded = true;
        }
		
		internal SoundEffectInstance GetWave(byte waveBankIndex, uint trackIndex) {
			return waveBanks[waveBankIndex].sounds[trackIndex];
		}
		
        public Cue GetCue(string name)
        {
			if (!loaded) Load ();
			
			//Does this have to return /new/ Cue instances?
			return cues[name];
        }
		
		public void PlayCue(string name)
		{
			var musicCue = GetCue(name);
            musicCue.Play();
		}
		
		public void PlayCue (string name, AudioListener listener, AudioEmitter emitter)
		{
			throw new NotImplementedException();
		}

		#region IDisposable implementation
		public void Dispose ()
		{
			throw new NotImplementedException ();
		}
		#endregion
    }
}