-
Notifications
You must be signed in to change notification settings - Fork 0
/
OggPlayer.cs
433 lines (382 loc) · 13.8 KB
/
OggPlayer.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
//
// OggPlayer.cs
//
// Author:
// El Dragon <thedragon@the-dragons-nest.co.uk>
//
// Copyright (c) 2010 Matthew Harris
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
namespace DragonOgg
{
/// <summary>
/// Abstraction for all OggPlayers to ensure standardisation of player formats
/// </summary>
public abstract class OggPlayer : IDisposable
{
#region "Current File"
protected OggFile m_CurrentFile; // Currently active file
/// <summary>
/// OggFile object representing the file currently loaded into the player
/// </summary>
public OggFile CurrentFile { get { return m_CurrentFile; } }
/// <summary>
/// Set the current file. Only valid when the player is stopped or no file has been set
/// </summary>
/// <param name="NewFile">
/// An <see cref="OggFile"/> object containg the file to set
/// </param>
public abstract bool SetCurrentFile(string FileName);
/// <summary>
/// Set the current file. Only valid when the player is stopped or no file has been set
/// </summary>
/// <param name="NewFilename">
/// A <see cref="System.String"/> containing the path to the file to set
/// </param>
public abstract bool SetCurrentFile(OggFile File);
#endregion
#region "State Event"
protected OggPlayerStatus m_PlayerState; // Player state
/// <summary>
/// Raised when the state of the player changes
/// </summary>
public event OggPlayerStateChangedHandler StateChanged;
/// <summary>
/// Current state of the player as an OggPlayerStatus enumeration.
/// Use OggUtilities.GetEnumString to convert into human-readable information
/// </summary>
public OggPlayerStatus PlayerState { get { return m_PlayerState; } }
protected void StateChange(OggPlayerStatus NewState) { StateChange(NewState, OggPlayerStateChanger.Internal); }
protected void StateChange(OggPlayerStatus NewState, OggPlayerStateChanger Reason)
{
if (StateChanged!=null) { StateChanged(this, new OggPlayerStateChangedArgs(m_PlayerState, NewState, Reason)); }
#if (DEBUG)
Console.WriteLine(DateTime.Now.ToLongTimeString() + "\tOggPlayer::StateChange -- From: " + OggUtilities.GetEnumString(m_PlayerState) + " -- To: " + OggUtilities.GetEnumString(NewState));
#endif
m_PlayerState = NewState;
}
#endregion
#region "Tick Event"
protected bool m_TickEnabled; // Tick control flag
protected float m_TickInterval = 1f; // Interval between tick events
protected float m_LastTick; // Last tick
/// <summary>
/// OggPlayer.Tick events will be raised every OggPlayer.TickInterval seconds of audio output if this is true
/// </summary>
public bool TickEnabled { get { return m_TickEnabled; } set { m_TickEnabled = value; } }
/// <summary>
/// Seconds between OggPlayer.Tick events (when OggPlayer.TickEnabled is true)
/// </summary>
public float TickInterval { get { return m_TickInterval; } set { m_TickInterval = value; } }
/// <summary>
/// Raised every TickInterval seconds of playvack when TickEnabled is true
/// </summary>
public event OggPlayerTickHandler Tick;
protected void SendTick(float PlaybackTime, float BufferTime)
{
if (Tick!=null) { Tick(this, new OggPlayerTickArgs(PlaybackTime, BufferTime)); }
}
#endregion
#region "Message Event"
/// <summary>
/// Raised when the player sends a message (e.g. a buffer under-run or on reaching the end of a file)
/// </summary>
public event OggPlayerMessageHandler PlayerMessage;
protected void SendMessage(OggPlayerMessageType Message) { SendMessage(Message, null); }
protected void SendMessage(OggPlayerMessageType Message, object Params)
{
if (PlayerMessage!=null) { PlayerMessage(this, new OggPlayerMessageArgs(Message, Params)); }
#if (DEBUG)
Console.WriteLine(DateTime.Now.ToLongTimeString() + "\tOggPlayer::SendMessage -- Message: " + OggUtilities.GetEnumString(Message));
#endif
}
#endregion
#region "Playback Control"
/// <summary>
/// Start playing the current file
/// </summary>
/// <returns>
/// An <see cref="OggPlayerCommandReturn"/> indicating the result of the operation
/// </returns>
public abstract OggPlayerCommandReturn Playback_Play();
/// <summary>
/// Stop playback.
/// Only valid if the player is playing or paused
/// </summary>
/// <returns>
/// An <see cref="OggPlayerCommandReturn"/> indicating the result of the operation
/// </returns>
public abstract OggPlayerCommandReturn Playback_Stop();
/// <summary>
/// Pause playback
/// Only valid if the player is playing
/// </summary>
/// <returns>
/// An <see cref="OggPlayerCommandReturn"/> indicating the result of the operation
/// </returns>
public abstract OggPlayerCommandReturn Playback_Pause();
/// <summary>
/// Unpause playback
/// Only valid if the player is paused
/// </summary>
/// <returns>
/// An <see cref="OggPlayerCommandReturn"/> indicating the result of the operation
/// </returns>
public abstract OggPlayerCommandReturn Playback_UnPause();
/// <summary>
/// Seek to a time
/// Only valid if the player is playing or paused
/// </summary>
/// <param name="RequestedTime">
/// A <see cref="System.Single"/> indicating the position in seconds within the file to seek to
/// </param>
/// <returns>
/// An <see cref="OggPlayerCommandReturn"/> indicating the result of the operation
/// </returns>
public abstract OggPlayerCommandReturn Playback_Seek(float RequestedTime);
#endregion
#region "Timing"
protected float m_PlayingOffset; // Current time in playback
/// <summary>
/// The current time of playback within the file
/// </summary>
public float AmountPlayed { get { return m_PlayingOffset; } }
/// <summary>
/// How much of the file has been played as a fraction of it's total (always returns between 0 & 1)
/// </summary>
public float FractionPlayed {
get {
if (m_CurrentFile==null) { return 0; }
float FE = m_PlayingOffset/float.Parse(m_CurrentFile.GetQuickTag(OggTags.Length));
if (FE>1) { return 1; } else if (FE<0) { return 0; } else { return FE; }
}
}
protected float m_BufferOffset; // Current time in buffer
/// <summary>
/// The current time of buffer within the file
/// </summary>
public float AmountBuffered { get { return m_BufferOffset; } }
/// <summary>
/// How much of the file has been buffered as a fraction of it's total (always returns between 0 & 1)
/// </summary>
public float FractionBuffered {
get {
if (m_CurrentFile==null) { return 0; }
float FE = m_BufferOffset/float.Parse(m_CurrentFile.GetQuickTag(OggTags.Length));
if (FE>1) { return 1; } else if (FE<0) { return 0; } else { return FE; }
}
}
/// <summary>
/// The length of the file in seconds
/// </summary>
public float FileLengthTime { get { if (m_CurrentFile==null) { return -1; } else { return float.Parse(m_CurrentFile.GetQuickTag(OggTags.Length)); } } }
#endregion
#region "OpenAL"
protected AudioContext m_Context; // Audio device context
protected uint m_Source; // Output source handle
protected ALError m_LastError; // OpenAL Error
/// <summary>
/// The last error from the OpenAL subsystem as an ALError enumeration.
/// Use OggUtilities.GetEnumString to convert into human readable information
/// </summary>
public ALError LastALError { get { return m_LastError; } }
protected bool InitSource()
{
try
{
// Create source
AL.GenSource(out m_Source);
// Configure the source listener
AL.Source(m_Source, ALSource3f.Position, 0.0f, 0.0f, 0.0f);
AL.Source(m_Source, ALSource3f.Velocity, 0.0f, 0.0f, 0.0f);
AL.Source(m_Source, ALSource3f.Direction, 0.0f, 0.0f, 0.0f);
AL.Source(m_Source, ALSourcef.RolloffFactor, 0.0f);
AL.Source(m_Source, ALSourceb.SourceRelative, true);
return true;
}
catch (Exception ex)
{
return false;
}
}
protected bool DestroySource()
{
try
{
if ((AL.GetSourceState(m_Source)==ALSourceState.Paused)||(AL.GetSourceState(m_Source)==ALSourceState.Playing))
{
AL.SourceStop(m_Source);
}
AL.DeleteSource(ref m_Source);
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
#region "Threading"
protected static readonly object StateLocker = new object();
protected static object OALLocker = new object();
protected int m_UpdateDelay; // Amount of time to wait at the end of each thread loop to allow other stuff to execute
/// <summary>
/// The amount of time to wait after each pass
/// </summary>
public int UpdateDelay { get { return m_UpdateDelay; } set { m_UpdateDelay = value; } }
#endregion
/// <summary>
/// IDisposable implementation
/// </summary>
public abstract void Dispose();
}
#region "Events"
/// <summary>
/// Event handler for changes in OggPlayer state
/// </summary>
public delegate void OggPlayerStateChangedHandler(object sender, OggPlayerStateChangedArgs e);
/// <summary>
/// Event arguments for OggPlayer StateChanged events
/// </summary>
public class OggPlayerStateChangedArgs : EventArgs
{
private OggPlayerStatus m_OldState;
private OggPlayerStatus m_NewState;
private OggPlayerStateChanger m_Changer;
/// <summary>
/// Constructor
/// </summary>
/// <param name="eOldState">
/// Original state of the player as an <see cref="OggPlayerStatus"/> enumeration
/// </param>
/// <param name="eNewState">
/// New state of the player as an <see cref="OggPlayerStatus"/> enumeration
/// </param>
/// <param name="eChanger">
/// Reason for the change in state as an <see cref="OggPlayerStateChanger"/> enumeration
/// </param>
public OggPlayerStateChangedArgs(OggPlayerStatus eOldState, OggPlayerStatus eNewState, OggPlayerStateChanger eChanger)
{
m_OldState = eOldState; m_NewState = eNewState; m_Changer = eChanger;
}
/// <summary>
/// Original state
/// </summary>
public OggPlayerStatus OldState { get { return m_OldState; } }
/// <summary>
/// New state
/// </summary>
public OggPlayerStatus NewState { get { return m_NewState; } }
/// <summary>
/// Reason for the change in state
/// </summary>
public OggPlayerStateChanger Changer { get { return m_Changer; } }
}
/// <summary>
/// Event handler for messages from an OggPlayer
/// </summary>
public delegate void OggPlayerMessageHandler(object sender, OggPlayerMessageArgs e);
/// <summary>
/// Event arguments for OggPlayer Message events
/// </summary>
public class OggPlayerMessageArgs : EventArgs
{
private OggPlayerMessageType m_Message;
private object m_Params;
/// <summary>
/// Constructor
/// </summary>
/// <param name="eMessage">
/// Type of message as an <see cref="OggPlayerMessageType"/> enumerator
/// </param>
public OggPlayerMessageArgs(OggPlayerMessageType eMessage)
{
m_Message = eMessage; m_Params = null;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="eMessage">
/// Type of message as an <see cref="OggPlayerMessageType"/> enumerator
/// </param>
/// <param name="eParams">
/// Message parameter(s). Type depends on the type of message
/// </param>
public OggPlayerMessageArgs(OggPlayerMessageType eMessage, object eParams)
{
m_Message = eMessage; m_Params = eParams;
}
/// <summary>
/// Type of message sent
/// </summary>
public OggPlayerMessageType Message { get { return m_Message; } }
/// <summary>
/// Parameter(s) for this message. Content depends on the type of message
/// </summary>
public object Params { get { return m_Params; } }
}
/// <summary>
/// Event handler for player tick events
/// </summary>
public delegate void OggPlayerTickHandler(object sender, OggPlayerTickArgs e);
/// <summary>
/// Event arguments for OggPlayer Tick events
/// </summary>
public class OggPlayerTickArgs : EventArgs
{
private float m_PlaybackTime;
private float m_BufferedTime;
/// <summary>
/// Constructor
/// </summary>
/// <param name="ePlaybackTime">
/// Current position in seconds of the audio output process as a <see cref="System.Single"/>
/// </param>
/// <param name="eBufferedTime">
/// Current position in seconds of the buffer process as a <see cref="System.Single"/>
/// </param>
public OggPlayerTickArgs(float ePlaybackTime, float eBufferedTime)
{
m_PlaybackTime = ePlaybackTime; m_BufferedTime = eBufferedTime;
}
/// <summary>
/// Current position in seconds of the audio output process
/// </summary>
public float PlaybackTime { get { return m_PlaybackTime; } }
/// <summary>
/// Current position in seconds of the buffer process
/// </summary>
public float BufferedTime { get { return m_BufferedTime; } }
}
#endregion
#region "Exceptions"
/// <summary>
/// Exception raised when there is an issue with interactions with the OpenAL source.
/// LastALError may have more information
/// </summary>
public class OggPlayerSourceException : Exception
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="Msg">
/// The message of the exception as a <see cref="System.String"/>
/// </param>
public OggPlayerSourceException(string Msg) : base(Msg) { }
}
#endregion
}