/// <summary> /// Constructor that initializes a WAV.Enumerator on a <see cref="Stream"/>. /// </summary> /// <param name="stream">The <see cref="Stream"/> to read from.</param> /// <param name="channel">The channel to read, or <see cref="AllChannels"/>.</param> /// <param name="SampleRate">The sample rate of the WAV data in Hertz units.</param> public Enumerator(Stream stream, int channel, out double SampleRate) { data = null; s = stream; this.channel = channel; // read RIFF chunk RIFFChunk riff = new RIFFChunk(stream); if (riff.riffType != "WAVE") { throw new WrongFileFormatException("Not a WAV file"); } // read format chunk Chunk chunk = new Chunk(stream); if (chunk.id != "fmt ") { throw new WrongFileFormatException("Invalid file format"); } format = new FormatChunk(chunk); if (format.format != 1) { throw new NotSupportedException("Only PCM WAV format is supported"); } SampleRate = sampleRate = format.samplesPerSecond; // skip non data chunks chunk = new Chunk(stream); while (!chunk.EndOfFile && chunk.id != "data") { chunk.Skip(); chunk = new Chunk(stream); } if (chunk.EndOfFile) { throw new WrongFileFormatException("Invalid file format"); } //inititalize data chunk data = new DataChunk(chunk, format); }
public static Chunk ReadChunk(ref BinaryReader reader) { int data = reader.ReadInt32(); if (data == 0x46464952) // RIFF chunk { RIFFChunk riff = new RIFFChunk(); riff.chunk_id = data; riff.file_size = reader.ReadInt32(); riff.riff_type = reader.ReadInt32(); return(riff); } else if (data == 0x20746D66) // fmt chunk { fmtChunk fmt = new fmtChunk(); fmt.chunk_id = data; fmt.chunk_size = reader.ReadInt32(); fmt.format = reader.ReadInt16(); fmt.channels = reader.ReadInt16(); fmt.sample_rate = reader.ReadInt32(); fmt.bytes_per_sec = reader.ReadInt32(); fmt.block_align = reader.ReadInt16(); fmt.bits_per_sample = reader.ReadInt16(); return(fmt); } else if (data == 0x61746164) // data chunk { dataChunk data_chunk = new dataChunk(); data_chunk.chunk_id = data; data_chunk.chunk_size = reader.ReadInt32(); return(data_chunk); } else { Chunk c = new Chunk(); c.chunk_id = data; return(c); } }
public static RIFFChunk ReadRiff(Stream file) { file.Position = 0; RIFFChunk riff = new RIFFChunk(); byte[] b = new byte[4]; file.Read(b, 0, 4); if (Encoding.ASCII.GetString(b) == "RIFF") { riff.ID = "RIFF".ToCharArray(); file.Read(b, 0, 4); riff.Size = BitConverter.ToInt32(b, 0); file.Read(b, 0, 4); riff.Format = Encoding.ASCII.GetString(b).ToCharArray(); } else { riff.ID = "ERR!".ToCharArray(); riff.Format = "EMPT".ToCharArray(); } return(riff); }
// Token: 0x060002F8 RID: 760 RVA: 0x0000B3B4 File Offset: 0x000095B4 public static Wave FromStream(Stream stream) { Wave result; using (RIFF riff = new RIFF(stream)) { Wave wave = new Wave(); if (riff.Identification != "WAVE") { throw new Exception("Given Stream does not contain a WAVE file."); } riff.GotoChunk(riff.Items.Chunks.Single((RIFFChunk chunk) => chunk.Identification == "fmt ")); wave.Format = (WaveFormat)riff.BinaryReader.Read16(); wave.ChannelCount = riff.BinaryReader.Read16(); wave.SampleRate = riff.BinaryReader.Read32(); riff.BinaryReader.Step(8L); wave.BitsPerSample = riff.BinaryReader.Read16(); RIFFChunk riffchunk = riff.Items.Chunks.Single((RIFFChunk chunk) => chunk.Identification == "data"); riff.GotoChunk(riffchunk); wave.Data = riff.BinaryReader.Read8s((int)riffchunk.Length); result = wave; } return(result); }
unsafe private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { try { RIFFChunk riff = null; fmtChunk fmt = null; dataChunk data_chunk = null; Audio.loopback = false; if (Audio.rb_mon_l == null || Audio.rb_mon_r == null) { Audio.rb_mon_l = new RingBufferFloat(Math.Max(buffer_size, 1024) * 1024); Audio.rb_mon_r = new RingBufferFloat(Math.Max(buffer_size, 1024) * 1024); } else { Audio.rb_mon_l.Reset(); Audio.rb_mon_r.Reset(); } btnPlay.BackColor = Color.WhiteSmoke; btnPlay.Text = "Play"; float[] buf_left = new float[2048]; float[] buf_right = new float[2048]; byte[] data = new byte[16384]; byte[] temp = new byte[4]; int count = 0, buflen = 0; string file = openFileDialog1.FileName; Stream file_stream = File.Open(file, FileMode.Open, FileAccess.Read); reader = new BinaryReader(file_stream); string[] vals = file.Split('\\'); this.Text = " Recorder/Player " + vals[vals.Length - 1]; try { if (reader.PeekChar() != 'R') { reader.Close(); MessageBox.Show("File is not in the correct format.", "Wrong File Format", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } while ((data_chunk == null || riff == null || fmt == null) && reader.PeekChar() != -1) { Chunk chunk = Chunk.ReadChunk(ref reader); if (chunk.GetType() == typeof(RIFFChunk)) { riff = (RIFFChunk)chunk; } else if (chunk.GetType() == typeof(fmtChunk)) { fmt = (fmtChunk)chunk; } else if (chunk.GetType() == typeof(dataChunk)) { data_chunk = (dataChunk)chunk; } } if (reader.PeekChar() == -1) { reader.Close(); MessageBox.Show("File is not in the correct format.", "Wrong File Format", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (riff.riff_type != 0x45564157) { reader.Close(); MessageBox.Show("File is not an RIFF Wave file.", "Wrong file format", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if ((fmt.sample_rate != Audio.SampleRate) || fmt.format == 1) { reader.Close(); MessageBox.Show("File has the wrong sample rate.", "Wrong Sample Rate", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (fmt.channels != 2) { reader.Close(); MessageBox.Show("Wave File is not stereo.", "Wrong Number of Channels", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } catch (Exception ex) { if (reader != null) { reader.Close(); } MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace.ToString(), "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } fixed(float *input_l = &buf_left[0]) fixed(float *input_r = &buf_right[0]) { while (Audio.rb_mon_l.WriteSpace() >= 2048) { count = reader.Read(data, 0, 16384); buflen = Math.Min(count, 16384); for (int i = 0; i < (buflen / 8); i++) { buf_left[i] = BitConverter.ToSingle(data, (i * 8)); buf_right[i] = BitConverter.ToSingle(data, (i * 8 + 4)); } Audio.rb_mon_l.WritePtr(input_l, 2048); Audio.rb_mon_r.WritePtr(input_r, 2048); if (buflen < 16384) { reader.Close(); return; } } reader.Close(); } } catch (Exception ex) { Debug.Write(ex.ToString()); if (reader != null) { reader.Close(); } } }
public static Chunk ReadChunk(ref BinaryReader reader) { int data = reader.ReadInt32(); if (data == 0x46464952) // RIFF chunk { RIFFChunk riff = new RIFFChunk(); riff.chunk_id = data; riff.file_size = reader.ReadInt32(); riff.riff_type = reader.ReadInt32(); return riff; } else if (data == 0x20746D66) // fmt chunk { fmtChunk fmt = new fmtChunk(); fmt.chunk_id = data; fmt.chunk_size = reader.ReadInt32(); fmt.format = reader.ReadInt16(); fmt.channels = reader.ReadInt16(); fmt.sample_rate = reader.ReadInt32(); fmt.bytes_per_sec = reader.ReadInt32(); fmt.block_align = reader.ReadInt16(); fmt.bits_per_sample = reader.ReadInt16(); return fmt; } else if (data == 0x61746164) // data chunk { dataChunk data_chunk = new dataChunk(); data_chunk.chunk_id = data; data_chunk.chunk_size = reader.ReadInt32(); return data_chunk; } else { Chunk c = new Chunk(); c.chunk_id = data; return c; } }