File: XactSound.cs

package info (click to toggle)
monogame 2.5.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,044 kB
  • sloc: cs: 65,996; xml: 591; makefile: 22; ansic: 8
file content (139 lines) | stat: -rwxr-xr-x 3,000 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
using System;
using System.IO;

namespace Microsoft.Xna.Framework.Audio
{
	internal class XactSound
	{
		bool complexSound;
		XactClip[] soundClips;
		SoundEffectInstance wave;
		
		public XactSound (SoundBank soundBank, BinaryReader soundReader, uint soundOffset)
		{
			long oldPosition = soundReader.BaseStream.Position;
			soundReader.BaseStream.Seek (soundOffset, SeekOrigin.Begin);
			
			byte flags = soundReader.ReadByte ();
			complexSound = (flags & 1) != 0;
			
			uint category = soundReader.ReadUInt16 ();
			soundReader.ReadByte (); //unkn
			uint volume = soundReader.ReadUInt16 (); //maybe pitch?
			soundReader.ReadByte (); //unkn
			uint entryLength = soundReader.ReadUInt16 ();
			
			uint numClips = 0;
			if (complexSound) {
				numClips = (uint)soundReader.ReadByte ();
			} else {
				uint trackIndex = soundReader.ReadUInt16 ();
				byte waveBankIndex = soundReader.ReadByte ();
				wave = soundBank.GetWave(waveBankIndex, trackIndex);
			}
			
			if ( (flags & 0x1E) != 0 ) {
				uint extraDataLen = soundReader.ReadUInt16 ();
				//TODO: Parse RPC+DSP stuff
				
				soundReader.BaseStream.Seek (extraDataLen, SeekOrigin.Current);
			}
			
			if (complexSound) {
				soundClips = new XactClip[numClips];
				for (int i=0; i<numClips; i++) {
					soundReader.ReadByte (); //unkn
					uint clipOffset = soundReader.ReadUInt32 ();
					soundReader.ReadUInt32 (); //unkn
					
					soundClips[i] = new XactClip(soundBank, soundReader, clipOffset);
				}
			}
			
			soundReader.BaseStream.Seek (oldPosition, SeekOrigin.Begin);
		}
		
//		public XactSound (Sound sound) {
//			complexSound = false;
//			wave = sound;
//		}
		public XactSound (SoundEffectInstance sound) {
			complexSound = false;
			wave = sound;
		}		
		public void Play() {
			if (complexSound) {
				foreach (XactClip clip in soundClips) {
					clip.Play();
				}
			} else {
				if (wave.State == SoundState.Playing) wave.Stop ();
				wave.Play ();
			}
		}
		
		public void Stop() {
			if (complexSound) {
				foreach (XactClip clip in soundClips) {
					clip.Stop();
				}
			} else {
				wave.Stop ();
			}
		}
		
		public void Pause() {
			if (complexSound) {
				foreach (XactClip clip in soundClips) {
					clip.Pause();
				}
			} else {
				wave.Pause ();
			}
		}
                
		public void Resume() {
			if (complexSound) {
				foreach (XactClip clip in soundClips) {
					clip.Play();
				}
			} else {
				wave.Resume ();
			}
		}
		
		public float Volume {
			get {
				if (complexSound) {
					return soundClips[0].Volume;
				} else {
					return wave.Volume;
				}
			}
			set {
				if (complexSound) {
					foreach (XactClip clip in soundClips) {
						clip.Volume = value;
					}
				} else {
					wave.Volume = value;
				}
			}
		}
		
		public bool Playing {
			get {
				if (complexSound) {
					foreach (XactClip clip in soundClips) {
						if (clip.Playing) return true;
					}
					return false;
				} else {
					return wave.State == SoundState.Playing;
				}
			}
		}
		
	}
}