/// <summary> /// Loads a wav file from a stream. /// </summary> /// <param name="Input">Stream</param> /// <returns>Loaded Wav file.</returns> /// <exception cref="IOException">If the input is not a valid WAVE File</exception> public static WavAudio FromStream(Stream Input) { if (Input.Length - Input.Position < 12) { throw new IOException("Invalid WAVE file."); } WavAudio Result = new WavAudio(); using (BinaryReader rd = new BinaryReader(Input)) { // File Format documented here: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ int ChunkID = rd.ReadInt32(); // 46464952 = FFIR ( = RIFF backwards) int ChunkSize = rd.ReadInt32(); int Format = rd.ReadInt32(); // 45564157 = EVAW ( = WAVE backwards) if (ChunkID != 0x46464952 || Format != 0x45564157) { throw new IOException("Invalid WAVE file."); } int SubChunk1ID = rd.ReadInt32(); int SubChunk1Size = rd.ReadInt32(); ushort AudioFormat = rd.ReadUInt16(); if (AudioFormat != 1) { throw new Exception("Only uncompressed WAVE files (in PCM format) are supported."); } Result.nrChannels = rd.ReadUInt16(); Result.sampleRate = rd.ReadInt32(); Result.byteRate = rd.ReadInt32(); Result.blockAlign = rd.ReadUInt16(); Result.bitsPerSample = rd.ReadUInt16(); int SubChunk2ID = rd.ReadInt32(); int SubChunk2Size = rd.ReadInt32(); Result.data = new byte[SubChunk2Size]; rd.Read(Result.data, 0, SubChunk2Size); } return(Result); }
/// <summary> /// Loads a wav file from a stream. /// </summary> /// <param name="Input">Stream</param> /// <returns>Loaded Wav file.</returns> /// <exception cref="IOException">If the input is not a valid WAVE File</exception> public static WavAudio FromStream(Stream Input) { if (Input.Length - Input.Position < 12) throw new IOException("Invalid WAVE file."); WavAudio Result = new WavAudio(); using (BinaryReader rd = new BinaryReader(Input)) { // File Format documented here: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ int ChunkID = rd.ReadInt32(); // 46464952 = FFIR ( = RIFF backwards) int ChunkSize = rd.ReadInt32(); int Format = rd.ReadInt32(); // 45564157 = EVAW ( = WAVE backwards) if (ChunkID != 0x46464952 || Format != 0x45564157) throw new IOException("Invalid WAVE file."); int SubChunk1ID = rd.ReadInt32(); int SubChunk1Size = rd.ReadInt32(); ushort AudioFormat = rd.ReadUInt16(); if (AudioFormat != 1) throw new Exception("Only uncompressed WAVE files (in PCM format) are supported."); Result.nrChannels = rd.ReadUInt16(); Result.sampleRate = rd.ReadInt32(); Result.byteRate = rd.ReadInt32(); Result.blockAlign = rd.ReadUInt16(); Result.bitsPerSample = rd.ReadUInt16(); int SubChunk2ID = rd.ReadInt32(); int SubChunk2Size = rd.ReadInt32(); Result.data = new byte[SubChunk2Size]; rd.Read(Result.data, 0, SubChunk2Size); } return Result; }
/// <summary> /// Uploads and prepares an audio sample. To remove the audio from memory (to make room for other audio samples) /// call <see cref="UnloadAudio"/>. /// </summary> /// <param name="Audio">Wav audio.</param> /// <returns>Audio Sample handle</returns> public static int UploadAudioSample(WavAudio Audio) { ALFormat Format; switch (Audio.NrChannels) { case 1: switch (Audio.BitsPerSample) { case 8: Format = ALFormat.Mono8; break; case 16: Format = ALFormat.Mono16; break; default: throw new Exception("Only 8 or 16 bits per sample supported."); } break; case 2: switch (Audio.BitsPerSample) { case 8: Format = ALFormat.Stereo8; break; case 16: Format = ALFormat.Stereo16; break; default: throw new Exception("Only 8 or 16 bits per sample supported."); } break; default: throw new Exception("Only mono or stereo audio samples supported."); } return UploadAudioSample(Audio.Data, Format, Audio.SampleRate); }