File: Sound.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 (336 lines) | stat: -rw-r--r-- 6,460 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
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
using System;
using System.IO;
using OpenTK.Audio.OpenAL;
using OpenTK.Audio;

namespace Microsoft.Xna.Framework.Audio
{	
	internal class Sound : IDisposable
	{
		private static AudioContext context = null;
		
		private int bufferID;
		private int sourceID;
		private bool looping;
		// when looping, to stop we must first disable looping then stop, but we still want to user to "believe" it has looping activated
		private bool loopingCtrl;
		
		public double Duration
		{
			get
			{
				return Seconds;
			}
		}
		
		public double CurrentPosition
		{
			get
			{
				float seconds;
				AL.GetSource(sourceID, ALSourcef.SecOffset, out seconds);
				return (double)seconds;
			}
			set
			{
				AL.Source(sourceID, ALSourcef.SecOffset, (float)value);
			}
		}
			
		public bool Looping
		{
			get
			{
				return looping;
			}
			set
			{
				// just do something if it's really needed
				if (looping != value)
				{
					AL.Source(sourceID, ALSourceb.Looping, value);
					loopingCtrl = looping = value;
				}
			}
		}
		
		public float Pan
		{
			get;
			set;
		}
		
		public bool Playing
		{
			get
			{
				return AL.GetSourceState(sourceID) == ALSourceState.Playing;
			}
			set
			{
				// just do something if it's really needed
				if (value != Playing)
				{
					if (value)
						Play();
					else
						Stop();
				}
			}
		}
		
		public float Rate
		{
			get;
			set;
		}
		
		public float Volume
		{
			get
			{
				float volume;
				AL.GetSource(sourceID, ALSourcef.Gain, out volume);
				return volume;
			}
			set
			{
				if (value < 0f || value > 1f)
					throw new ArgumentException("Volume should be between 0 and 1.0");
				
				AL.Source(sourceID, ALSourcef.Gain, value);
			}
		}
		
		public Sound(string filename, float volume, bool looping)
		{
			ALFormat format;
			int size;
			int freq;
			byte[] data;
			Stream s;
			
			try
			{
		 		s = File.OpenRead(filename);
			}
			catch(IOException e)
			{
				throw new Content.ContentLoadException("Could not load audio data", e);
			}	
			
			data = AudioLoader.Load(s, out format, out size, out freq);
			
			s.Close();
			
			Initialize(data, format, size, freq, volume, looping);
		}
		
		public Sound(byte[] audiodata, float volume, bool looping) 
		{
			ALFormat format;
			int size;
			int freq;
			byte[] data;
			Stream s;
			
			try
			{				
		 		s = new MemoryStream(audiodata);
			}
			catch(IOException e)
			{
				throw new Content.ContentLoadException("Could not load audio data", e);
			}	
			
			data = AudioLoader.Load(s, out format, out size, out freq);
			
			s.Close();
			
			Initialize(data, format, size, freq, volume, looping);			
		}
		
		~Sound()
		{
			Dispose();	
		}
		
		private static void InitilizeSoundServices()
		{
			if (context == null)
				context = new AudioContext();
		}
		
		internal static void DisposeSoundServices()
		{
			if (context != null)
			{
				context.Dispose();
				context = null;
			}
		}
		
		private void Initialize(byte[] data, ALFormat format, int size, int frequency, 
		                        float volume, bool looping)
		{
			InitilizeSoundServices();
			
			bufferID = AL.GenBuffer();
			sourceID = AL.GenSource();
			
			try
			{
				// loads sound into buffer
				AL.BufferData(bufferID, format, data, size, frequency);
				
				// binds buffer to source
				AL.Source(sourceID, ALSourcei.Buffer, bufferID);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			Volume = volume;
			this.looping = looping;
		}
		
		public void Dispose()
		{
			if (bufferID == -1)
				return;			
			
			Stop();
			
			AL.DeleteSource(sourceID);
			AL.DeleteBuffer(bufferID);
			bufferID = -1;			
		}
		
		#region Buffer Info
		
		private static int GetSampleSize(int bits, int channels)
        {
            return (bits / 8) * channels;
        }
		
        /// <summary>
        /// Gets the number of channels in buffer
        /// > 1 is valid, but buffer won’t be positioned when played
        /// </summary>
        private int Channels
        {
            get
            {
                int rv;
                AL.GetBuffer(bufferID, ALGetBufferi.Channels, out rv);
                return rv;
            }
        }
        /// <summary>
        /// Gets the size of buffer in bytes
        /// </summary>
        private int Size
        {
            get
            {
                int rv;
                AL.GetBuffer(bufferID, ALGetBufferi.Size, out rv);
                return rv;
            }
        }
        /// <summary>
        /// Gets the bit depth of buffer
        /// </summary>
        private int Bits
        {
            get
            {
                int rv;
				AL.GetBuffer(bufferID, ALGetBufferi.Bits, out rv);
                return rv;
            }
        }
        /// <summary>
        /// Gets the frequency of buffer in Hz
        /// </summary>
        private int Frequency
        {
            get
            {
                int rv;
                AL.GetBuffer(bufferID, ALGetBufferi.Frequency, out rv);
                return rv;
            }
        }
        /// <summary>
        /// Gets the number of Samples by Size / GetSampleSize(Bits, Channels)
        /// </summary>
        private int Samples
        {
            get
            {
                return Size / GetSampleSize(Bits, Channels);
            }
        }
        /// <summary>
        /// Gets the seconds of play time in the buffer based on Samples / Frequency
        /// </summary>
        private double Seconds
        {
            get
            {
                return (double)Samples / (double)Frequency;
            }
        }
		
		#endregion		
		
        public void Resume()
        {
            Play();
        }
        
		public void Pause()
		{		
			AL.SourcePause(sourceID);
		}
		
		public void Play()
		{
			// if we must loop but looping was disabled
			if (looping && !loopingCtrl)
			{
				// then enable looping
				AL.Source(sourceID, ALSourceb.Looping, true);
				loopingCtrl	= true;
			}
			
			// and play
			AL.SourcePlay(sourceID);
		}
		
		public void Stop()
		{
			// if looping is enabled
			if (loopingCtrl)
			{
				// disable it
				AL.Source(sourceID, ALSourceb.Looping, false);
				loopingCtrl = false;
			}
			
			// and stop
			AL.SourceStop(sourceID);
		}
		
		public static Sound CreateAndPlay(string url, float volume, bool looping)
		{
			Sound sound = new Sound(url, volume, looping);
			
			sound.Play();
			
			return sound;
		}
	}
}