2008-04-04 23:05:03 +02:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Licensed under the MIT/X11 license.
|
|
|
|
|
* Copyright (c) 2006-2008 the OpenTK Team.
|
|
|
|
|
* This notice may not be removed from any source distribution.
|
|
|
|
|
* See license.txt for licensing details.
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
2008-04-04 21:46:08 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace OpenTK.Audio
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Encapsulates a sound stream and provides decoding and streaming capabilities.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="SampleType"></typeparam>
|
2008-04-06 16:32:59 +02:00
|
|
|
|
public class AudioReader : IDisposable
|
2008-04-04 21:46:08 +02:00
|
|
|
|
{
|
|
|
|
|
static object reader_lock = new object();
|
2008-04-06 16:32:59 +02:00
|
|
|
|
static List<AudioReader> readers = new List<AudioReader>();
|
2008-04-04 21:46:08 +02:00
|
|
|
|
|
|
|
|
|
bool disposed;
|
|
|
|
|
Stream stream;
|
2008-04-06 16:32:59 +02:00
|
|
|
|
AudioReader implementation;
|
2008-04-04 21:46:08 +02:00
|
|
|
|
|
|
|
|
|
#region --- Constructors ---
|
|
|
|
|
|
2008-04-06 16:32:59 +02:00
|
|
|
|
#region static AudioReader()
|
2008-04-04 21:46:08 +02:00
|
|
|
|
|
2008-04-06 16:32:59 +02:00
|
|
|
|
static AudioReader()
|
2008-04-04 21:46:08 +02:00
|
|
|
|
{
|
2008-04-06 16:32:59 +02:00
|
|
|
|
// TODO: Plugin architecture for sound formats. This is overkill now that we only have a WaveReader (future proofing).
|
|
|
|
|
readers.Add(new WaveReader());
|
2008-04-04 21:46:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-04-06 16:32:59 +02:00
|
|
|
|
#region protected AudioReader()
|
2008-04-04 21:46:08 +02:00
|
|
|
|
|
2008-04-06 16:32:59 +02:00
|
|
|
|
protected AudioReader() { }
|
2008-04-04 21:46:08 +02:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-04-06 16:32:59 +02:00
|
|
|
|
#region public AudioReader(string filename)
|
2008-04-04 21:46:08 +02:00
|
|
|
|
|
2008-04-06 16:32:59 +02:00
|
|
|
|
/// <summary>Creates a new AudioReader that can read the specified sound file.</summary>
|
2008-04-04 21:46:08 +02:00
|
|
|
|
/// <param name="filename">The path to the sound file.</param>
|
2008-04-06 16:32:59 +02:00
|
|
|
|
/// <returns>A new OpenTK.Audio.AudioReader, which can be used to read from the specified sound file.</returns>
|
|
|
|
|
public AudioReader(string filename)
|
2008-04-05 00:14:42 +02:00
|
|
|
|
: this(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
2008-04-04 21:46:08 +02:00
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-04-06 16:32:59 +02:00
|
|
|
|
#region public AudioReader(Stream s)
|
2008-04-04 21:46:08 +02:00
|
|
|
|
|
2008-04-06 16:32:59 +02:00
|
|
|
|
/// <summary>Creates a new AudioReader that can read the specified soundstream.</summary>
|
2008-04-04 21:46:08 +02:00
|
|
|
|
/// <param name="s">The System.IO.Stream to read from.</param>
|
2008-04-06 16:32:59 +02:00
|
|
|
|
/// <returns>A new OpenTK.Audio.AudioReader, which can be used to read from the specified sound stream.</returns>
|
|
|
|
|
public AudioReader(Stream s)
|
2008-04-04 21:46:08 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
lock (reader_lock)
|
|
|
|
|
{
|
2008-04-06 16:32:59 +02:00
|
|
|
|
foreach (AudioReader reader in readers)
|
2008-04-04 21:46:08 +02:00
|
|
|
|
{
|
|
|
|
|
long pos = s.Position;
|
|
|
|
|
if (reader.Supports(s))
|
|
|
|
|
{
|
|
|
|
|
s.Position = pos;
|
2008-04-06 16:32:59 +02:00
|
|
|
|
implementation = (AudioReader)
|
2008-04-04 21:46:08 +02:00
|
|
|
|
reader.GetType().GetConstructor(
|
|
|
|
|
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public |
|
|
|
|
|
System.Reflection.BindingFlags.Instance,
|
|
|
|
|
null,
|
|
|
|
|
new Type[] { typeof(Stream) },
|
|
|
|
|
null)
|
|
|
|
|
.Invoke(new object[] { s });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
s.Position = pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-07-04 18:41:17 +02:00
|
|
|
|
catch (Exception)
|
2008-04-04 21:46:08 +02:00
|
|
|
|
{
|
|
|
|
|
s.Close();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
throw new NotSupportedException("Could not find a decoder for the specified sound stream.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Public Members ---
|
|
|
|
|
|
|
|
|
|
#region public virtual bool Supports(Stream s)
|
|
|
|
|
|
|
|
|
|
/// <summary>When overriden in a derived class, checks if the decoder supports the specified sound stream.</summary>
|
|
|
|
|
/// <param name="s">The System.IO.Stream to check.</param>
|
|
|
|
|
/// <returns>True if the sound stream is supported; false otherwise.</returns>
|
|
|
|
|
public virtual bool Supports(Stream s)
|
|
|
|
|
{
|
|
|
|
|
if (implementation != null)
|
|
|
|
|
return implementation.Supports(s);
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public virtual SoundData<SampleType> ReadSamples(long count)
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// When overriden in a derived class, reads and decodes the specified number of samples from the sound stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="samples">The number of samples to read and decode.</param>
|
|
|
|
|
/// <returns>An OpenTK.Audio.SoundData object that contains the decoded buffer.</returns>
|
|
|
|
|
public virtual SoundData ReadSamples(long count)
|
|
|
|
|
{
|
|
|
|
|
if (implementation != null)
|
|
|
|
|
return implementation.ReadSamples(count);
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public virtual SoundData<SampleType> ReadToEnd()
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// When overriden in a derived class, reads and decodes the sound stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>An OpenTK.Audio.SoundData object that contains the decoded buffer.</returns>
|
|
|
|
|
public virtual SoundData ReadToEnd()
|
|
|
|
|
{
|
|
|
|
|
if (implementation != null)
|
|
|
|
|
return implementation.ReadToEnd();
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public virtual int Frequency
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual int Frequency
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (implementation != null)
|
|
|
|
|
return implementation.Frequency;
|
|
|
|
|
else
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
protected set
|
|
|
|
|
{
|
|
|
|
|
if (implementation != null)
|
|
|
|
|
implementation.Frequency = value;
|
|
|
|
|
else
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-04-05 00:14:42 +02:00
|
|
|
|
#region public virtual bool EndOfFile
|
2008-04-04 21:46:08 +02:00
|
|
|
|
|
2008-04-05 00:14:42 +02:00
|
|
|
|
public virtual bool EndOfFile
|
2008-04-04 21:46:08 +02:00
|
|
|
|
{
|
2008-04-05 00:14:42 +02:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (implementation != null)
|
|
|
|
|
return implementation.EndOfFile;
|
2008-04-04 21:46:08 +02:00
|
|
|
|
|
2008-04-05 00:14:42 +02:00
|
|
|
|
return this.Stream.Position >= this.Stream.Length;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-04 21:46:08 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Protected Members ---
|
|
|
|
|
|
|
|
|
|
protected virtual Stream Stream
|
|
|
|
|
{
|
|
|
|
|
get { return stream; }
|
|
|
|
|
set { stream = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
|
|
2008-04-06 16:32:59 +02:00
|
|
|
|
/// <summary>Closes the underlying Stream and disposes of the AudioReader resources.</summary>
|
2008-04-04 21:46:08 +02:00
|
|
|
|
public virtual void Dispose()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Dispose(bool manual)
|
|
|
|
|
{
|
|
|
|
|
if (!disposed)
|
|
|
|
|
{
|
|
|
|
|
if (manual)
|
|
|
|
|
if (this.Stream != null)
|
|
|
|
|
this.Stream.Close();
|
|
|
|
|
|
|
|
|
|
disposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-06 16:32:59 +02:00
|
|
|
|
~AudioReader()
|
2008-04-04 21:46:08 +02:00
|
|
|
|
{
|
|
|
|
|
this.Dispose(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|