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
|
#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.IO;
using OpenTK.Audio;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace OpenTK.Audio
{
internal sealed class WaveReader : AudioReader
{
SoundData decoded_data;
//RIFF header
string signature;
int riff_chunck_size;
string format;
//WAVE header
string format_signature;
int format_chunk_size;
short audio_format;
short channels;
int sample_rate;
int byte_rate;
short block_align;
short bits_per_sample;
//DATA header
string data_signature;
int data_chunk_size;
BinaryReader reader;
internal WaveReader() { }
internal WaveReader(Stream s)
{
if (s == null) throw new ArgumentNullException();
if (!s.CanRead) throw new ArgumentException("Cannot read from specified Stream.");
reader = new BinaryReader(s);
this.Stream = s;
}
#if false
/// <summary>
/// Writes the WaveSound's data to the specified OpenAL buffer in the correct format.
/// </summary>
/// <param name="bid">
/// A <see cref="System.UInt32"/>
/// </param>
public void WriteToBuffer(uint bid)
{
unsafe
{
//fix the array as a byte
fixed (byte* p_Data = _Data)
{
if (Channels == 1)
{
if (BitsPerSample == 16)
{
Console.Write("Uploading 16-bit mono data to OpenAL...");
AL.BufferData(bid, ALFormat.Mono16, (IntPtr)p_Data, _Data.Length, SampleRate);
}
else
{
if (BitsPerSample == 8)
{
Console.Write("Uploading 8-bit mono data to OpenAL...");
AL.BufferData(bid, ALFormat.Mono8, (IntPtr)p_Data, _Data.Length, SampleRate);
}
}
}
else
{
if (Channels == 2)
{
if (BitsPerSample == 16)
{
Console.Write("Uploading 16-bit stereo data to OpenAL...");
AL.BufferData(bid, ALFormat.Stereo16, (IntPtr)p_Data, _Data.Length, SampleRate);
}
else
{
if (BitsPerSample == 8)
{
Console.Write("Uploading 8-bit stereo data to OpenAL...");
AL.BufferData(bid, ALFormat.Stereo8, (IntPtr)p_Data, _Data.Length, SampleRate);
}
}
}
}
}
}
}
/// <summary>
/// Writes all relevent information about the WaveSound to the console window.
/// </summary>
public void DumpParamsToConsole()
{
Console.WriteLine("AudioFormat:" + AudioFormat);
Console.WriteLine("Channels:" + Channels);
Console.WriteLine("SampleRate:" + SampleRate);
Console.WriteLine("ByteRate:" + ByteRate);
Console.WriteLine("BlockAlign:" + BlockAlign);
Console.WriteLine("BitsPerSample:" + BitsPerSample);
}
/// <value>
/// Returns the WaveSound's raw sound data as an array of bytes.
/// </value>
public byte[] Data
{
get
{
return _Data;
}
}
#endif
#region --- Public Members ---
#region public override bool Supports(Stream s)
/// <summary>
/// Checks whether the specified stream contains valid WAVE/RIFF buffer.
/// </summary>
/// <param name="s">The System.IO.Stream to check.</param>
/// <returns>True if the stream is a valid WAVE/RIFF file; false otherwise.</returns>
public override bool Supports(Stream s)
{
BinaryReader reader = new BinaryReader(s);
return this.ReadHeaders(reader);
}
#endregion
#region public override SoundData ReadSamples(int samples)
/// <summary>
/// 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 override SoundData ReadSamples(long samples)
{
if (samples > reader.BaseStream.Length - reader.BaseStream.Position)
samples = reader.BaseStream.Length - reader.BaseStream.Position;
//while (samples > decoded_data.Data.Length * (bits_per_sample / 8))
// Array.Resize<byte>(ref decoded_data.Data, decoded_data.Data.Length * 2);
decoded_data = new SoundData(new SoundFormat(channels, bits_per_sample, sample_rate),
reader.ReadBytes((int)samples));
return decoded_data;
}
#endregion
#region public override SoundData ReadToEnd()
/// <summary>
/// Reads and decodes the sound stream.
/// </summary>
/// <returns>An OpenTK.Audio.SoundData object that contains the decoded buffer.</returns>
public override SoundData ReadToEnd()
{
try
{
//read the buffer into a byte array, even if the format is 16 bit
//decoded_data = new byte[data_chunk_size];
decoded_data = new SoundData(new SoundFormat(channels, bits_per_sample, sample_rate),
reader.ReadBytes((int)reader.BaseStream.Length));
Debug.WriteLine("decoded!");
//return new SoundData(decoded_data, new SoundFormat(channels, bits_per_sample, sample_rate));
return decoded_data;
}
catch (AudioReaderException)
{
reader.Close();
throw;
}
}
#endregion
#endregion
#region --- Protected Members ---
protected override Stream Stream
{
get { return base.Stream; }
set
{
base.Stream = value;
if (!ReadHeaders(reader))
throw new AudioReaderException("Invalid WAVE/RIFF file: invalid or corrupt signature.");
Debug.Write(String.Format("Opened WAVE/RIFF file: ({0}, {1}, {2}, {3}) ", sample_rate.ToString(), bits_per_sample.ToString(),
channels.ToString(), audio_format.ToString()));
}
}
#endregion
#region --- Private Members ---
// Tries to read the WAVE/RIFF headers, and returns true if they are valid.
bool ReadHeaders(BinaryReader reader)
{
// Don't explicitly call reader.Close()/.Dispose(), as that will close the inner stream.
// There's no such danger if the BinaryReader isn't explicitly destroyed.
// RIFF header
signature = new string(reader.ReadChars(4));
if (signature != "RIFF")
return false;
riff_chunck_size = reader.ReadInt32();
format = new string(reader.ReadChars(4));
if (format != "WAVE")
return false;
// WAVE header
format_signature = new string(reader.ReadChars(4));
if (format_signature != "fmt ")
return false;
format_chunk_size = reader.ReadInt32();
audio_format = reader.ReadInt16();
channels = reader.ReadInt16();
sample_rate = reader.ReadInt32();
byte_rate = reader.ReadInt32();
block_align = reader.ReadInt16();
bits_per_sample = reader.ReadInt16();
data_signature = new string(reader.ReadChars(4));
if (data_signature != "data")
return false;
data_chunk_size = reader.ReadInt32();
return true;
}
#endregion
}
}
|