internal SoundEffect( string name, byte[] buffer, uint sampleRate, uint channels, uint loopStart, uint loopLength, bool isADPCM, uint formatParameter ) { Name = name; INTERNAL_buffer = AudioDevice.GenBuffer( buffer, sampleRate, channels, loopStart, loopStart + loopLength, isADPCM, formatParameter ); }
private void INTERNAL_loadAudioStream(Stream s) { byte[] data; uint sampleRate = 0; uint numChannels = 0; bool isADPCM = false; uint formatParameter = 0; using (BinaryReader reader = new BinaryReader(s)) { // RIFF Signature string signature = new string(reader.ReadChars(4)); if (signature != "RIFF") { throw new NotSupportedException("Specified stream is not a wave file."); } reader.ReadUInt32(); // Riff Chunk Size string wformat = new string(reader.ReadChars(4)); if (wformat != "WAVE") { throw new NotSupportedException("Specified stream is not a wave file."); } // WAVE Header string format_signature = new string(reader.ReadChars(4)); while (format_signature != "fmt ") { reader.ReadBytes(reader.ReadInt32()); format_signature = new string(reader.ReadChars(4)); } int format_chunk_size = reader.ReadInt32(); // Header Information uint audio_format = reader.ReadUInt16(); // 2 numChannels = reader.ReadUInt16(); // 4 sampleRate = reader.ReadUInt32(); // 8 reader.ReadUInt32(); // 12, Byte Rate ushort blockAlign = reader.ReadUInt16(); // 14, Block Align ushort bitDepth = reader.ReadUInt16(); // 16, Bits Per Sample if (audio_format == 1) { System.Diagnostics.Debug.Assert(bitDepth == 8 || bitDepth == 16); formatParameter = (uint)(bitDepth / 16); // 1 for 16, 0 for 8 } else if (audio_format != 2) { isADPCM = true; formatParameter = (((blockAlign / numChannels) - 6) * 2); } else { throw new NotSupportedException("Wave format is not supported."); } // Reads residual bytes if (format_chunk_size > 16) { reader.ReadBytes(format_chunk_size - 16); } // data Signature string data_signature = new string(reader.ReadChars(4)); while (data_signature.ToLowerInvariant() != "data") { reader.ReadBytes(reader.ReadInt32()); data_signature = new string(reader.ReadChars(4)); } if (data_signature != "data") { throw new NotSupportedException("Specified wave file is not supported."); } int waveDataLength = reader.ReadInt32(); data = reader.ReadBytes(waveDataLength); } INTERNAL_buffer = AudioDevice.GenBuffer( data, sampleRate, numChannels, 0, 0, isADPCM, formatParameter ); }