File: AudioReader.cs

package info (click to toggle)
opentk 1.0.20101006%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 38,896 kB
  • ctags: 68,704
  • sloc: cs: 424,330; xml: 96,546; ansic: 3,597; makefile: 24
file content (235 lines) | stat: -rw-r--r-- 7,498 bytes parent folder | download
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 ---
/* 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;
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>
    public class AudioReader : IDisposable
    {
        static object reader_lock = new object();
        static List<AudioReader> readers = new List<AudioReader>();

        bool disposed;
        Stream stream;
        AudioReader implementation;

        #region --- Constructors ---

        #region static AudioReader()

        static AudioReader()
        {
            // TODO: Plugin architecture for sound formats. This is overkill now that we only have a WaveReader (future proofing).
            readers.Add(new WaveReader());
        }

        #endregion

        #region protected AudioReader()

        protected AudioReader() { }

        #endregion

        #region public AudioReader(string filename)

        /// <summary>Creates a new AudioReader that can read the specified sound file.</summary>
        /// <param name="filename">The path to the sound file.</param>
        /// <returns>A new OpenTK.Audio.AudioReader, which can be used to read from the specified sound file.</returns>
        public AudioReader(string filename)
            : this(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        { }

        #endregion

        #region public AudioReader(Stream s)

        /// <summary>Creates a new AudioReader that can read the specified soundstream.</summary>
        /// <param name="s">The System.IO.Stream to read from.</param>
        /// <returns>A new OpenTK.Audio.AudioReader, which can be used to read from the specified sound stream.</returns>
        public AudioReader(Stream s)
        {
            try
            {
                lock (reader_lock)
                {
                    foreach (AudioReader reader in readers)
                    {
                        long pos = s.Position;
                        if (reader.Supports(s))
                        {
                            s.Position = pos;
                            implementation = (AudioReader)
                                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;
                    }
                }
            }
            catch (Exception)
            {
                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="count">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

        #region public virtual bool EndOfFile

        /// <summary>
        /// Returns true if the AudioReader has reached the end of the file.
        /// </summary>
        public virtual bool EndOfFile
        {
            get
            {
                if (implementation != null)
                    return implementation.EndOfFile;

                return this.Stream.Position >= this.Stream.Length;
            }
        }
        #endregion

        #endregion

        #region --- Protected Members ---

        /// <summary>
        /// Gets or sets the input <see cref="System.IO.Stream"/> of the AudioReader.
        /// </summary>
        protected virtual Stream Stream
        {
            get { return stream; }
            set { stream = value; }
        }

        #endregion

        #region IDisposable Members

        /// <summary>Closes the underlying Stream and disposes of the AudioReader resources.</summary>
        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;
            }
        }

        /// <summary>
        /// Finalizes this AudioReader.
        /// </summary>
        ~AudioReader()
        {
            this.Dispose(false);
        }

        #endregion
    }
}