public static FormatChunk GetFormatChunk(Stream stream) { var formatChunk=new FormatChunk(); var rawData=new byte[24]; stream.Read(rawData,0,rawData.Length); formatChunk.ID=Encoding.ASCII.GetString(rawData.Take(4).ToArray()); formatChunk.Size=BitConverter.ToInt32(rawData.Skip(4).Take(4).ToArray(),0); formatChunk.FormatTag=BitConverter.ToInt16(rawData.Skip(8).Take(2).ToArray(),0); formatChunk.Channels=BitConverter.ToUInt16(rawData.Skip(10).Take(2).ToArray(),0); formatChunk.SamplesPerSecond=BitConverter.ToUInt32(rawData.Skip(12).Take(4).ToArray(),0); formatChunk.AverageBytesPerSecond=BitConverter.ToUInt32(rawData.Skip(16).Take(4).ToArray(),0); formatChunk.BlockAlign=BitConverter.ToUInt16(rawData.Skip(20).Take(2).ToArray(),0); formatChunk.BitsPerSecond=BitConverter.ToUInt16(rawData.Skip(22).ToArray(),0); return formatChunk; }
public static FormatChunk GetFormatChunk(Stream stream) { var formatChunk = new FormatChunk(); var rawData = new byte[24]; stream.Read(rawData, 0, rawData.Length); formatChunk.ID = Encoding.ASCII.GetString(rawData.Take(4).ToArray()); formatChunk.Size = BitConverter.ToInt32(rawData.Skip(4).Take(4).ToArray(), 0); formatChunk.FormatTag = BitConverter.ToInt16(rawData.Skip(8).Take(2).ToArray(), 0); formatChunk.Channels = BitConverter.ToUInt16(rawData.Skip(10).Take(2).ToArray(), 0); formatChunk.SamplesPerSecond = BitConverter.ToUInt32(rawData.Skip(12).Take(4).ToArray(), 0); formatChunk.AverageBytesPerSecond = BitConverter.ToUInt32(rawData.Skip(16).Take(4).ToArray(), 0); formatChunk.BlockAlign = BitConverter.ToUInt16(rawData.Skip(20).Take(2).ToArray(), 0); formatChunk.BitsPerSecond = BitConverter.ToUInt16(rawData.Skip(22).ToArray(), 0); return(formatChunk); }
public WaveFile(string fileName) { var stream = File.OpenRead(fileName); RiffHeader = RiffHeader.GetRiffHeader(stream); var pos = stream.Position; FormatChunk = FormatChunk.GetFormatChunk(stream); stream.Position = pos + FormatChunk.Size + 8; DataChunk = DataChunk.GetDataChunk(stream); if (stream.Length < 12 + 8 + FormatChunk.Size + 8 + DataChunk.Size) { throw new InvalidDataException("WAVEファイルの形式が不正です。"); } var waveDataStream = new BinaryReader(stream); Data = new short[DataChunk.Size / 2]; unsafe { fixed(short *ptr = &Data[0]) using (var unmanagedStream = new UnmanagedMemoryStream((byte *)ptr, DataChunk.Size, DataChunk.Size, FileAccess.Write)) stream.CopyTo(unmanagedStream); } stream.Dispose(); }