public static WaveFile FromFile(string fileName) { var wav = new WaveFile(); FileStream filestream = null; try { filestream = new FileStream(fileName, FileMode.Open, FileAccess.Read); using (BinaryReader file = new BinaryReader(filestream)) { filestream = null; if (Encoding.ASCII.GetString(file.ReadBytes(4)) != "RIFF") { throw new InvalidDataException(); } //// file size file.ReadInt32(); if (Encoding.ASCII.GetString(file.ReadBytes(4)) != "WAVE") { throw new InvalidDataException(); } if (Encoding.ASCII.GetString(file.ReadBytes(4)) != "fmt ") { throw new InvalidDataException(); } int fmtSize = file.ReadInt32(); if (fmtSize < 16) { throw new InvalidDataException(); } if (file.ReadInt16() != 1) { throw new NotSupportedException(); } wav.Channels = file.ReadInt16(); wav.SampleRate = file.ReadInt32(); int avgBytesPerSec = file.ReadInt32(); int blockAlign = file.ReadInt16(); wav.BitsPerSample = file.ReadInt16(); if (blockAlign != wav.BlockAlign) { throw new InvalidDataException(); } if (avgBytesPerSec != wav.AvgBytesPerSec) { throw new InvalidDataException(); } file.BaseStream.Seek(fmtSize - 16, SeekOrigin.Current); while (Encoding.ASCII.GetString(file.ReadBytes(4)) != "data") { int chunkSize = file.ReadInt32(); file.BaseStream.Seek(chunkSize, SeekOrigin.Current); } int dataSize = file.ReadInt32(); wav.Data = file.ReadBytes(dataSize); } } finally { if (filestream != null) { filestream.Dispose(); } } return wav; }
public Stream RetrieveWaveStream(int start, int end) { var data = this.RetrieveRawData(start, end); var wav = new WaveFile { Channels = this.Channels, SampleRate = this.SampleRate, BitsPerSample = this.BitsPerSample, Data = data }; var header = wav.BuildHeader(); var stream = new MemoryStream(header.Length + data.Length); try { stream.Write(header, 0, header.Length); stream.Write(data, 0, data.Length); stream.Seek(0, SeekOrigin.Begin); } catch { stream.Dispose(); throw; } return stream; }
public static WaveFile FromFile(string fileName) { var wav = new WaveFile(); FileStream filestream = null; try { filestream = new FileStream(fileName, FileMode.Open, FileAccess.Read); using (BinaryReader file = new BinaryReader(filestream)) { filestream = null; if (Encoding.ASCII.GetString(file.ReadBytes(4)) != "RIFF") { throw new InvalidDataException(); } //// file size file.ReadInt32(); if (Encoding.ASCII.GetString(file.ReadBytes(4)) != "WAVE") { throw new InvalidDataException(); } if (Encoding.ASCII.GetString(file.ReadBytes(4)) != "fmt ") { throw new InvalidDataException(); } int fmtSize = file.ReadInt32(); if (fmtSize < 16) { throw new InvalidDataException(); } if (file.ReadInt16() != 1) { throw new NotSupportedException(); } wav.Channels = file.ReadInt16(); wav.SampleRate = file.ReadInt32(); int avgBytesPerSec = file.ReadInt32(); int blockAlign = file.ReadInt16(); wav.BitsPerSample = file.ReadInt16(); if (blockAlign != wav.BlockAlign) { throw new InvalidDataException(); } if (avgBytesPerSec != wav.AvgBytesPerSec) { throw new InvalidDataException(); } file.BaseStream.Seek(fmtSize - 16, SeekOrigin.Current); while (Encoding.ASCII.GetString(file.ReadBytes(4)) != "data") { int chunkSize = file.ReadInt32(); file.BaseStream.Seek(chunkSize, SeekOrigin.Current); } int dataSize = file.ReadInt32(); wav.Data = file.ReadBytes(dataSize); } } finally { if (filestream != null) { filestream.Dispose(); } } return(wav); }
public void SaveAsWave(string fileName) { var data = this.RetrieveRawData(); var wav = new WaveFile { Channels = this.Channels, SampleRate = this.SampleRate, BitsPerSample = this.BitsPerSample, Data = data }; wav.Save(fileName); }
public void SetRawDataFromWave(string fileName) { var wav = WaveFile.FromFile(fileName); this.SetRawData(wav.Data, wav.BitsPerSample, wav.SampleRate, wav.Channels); }