private byte[] MakeWaveHader(int data_count, int channels, int rate, int bps) { var data_size = data_count * (bps / 8) * channels; var file_size = data_size + 36; var riff = new RiffHeader(); riff.riff_head = GetBytes("RIFF"); riff.file_size = (uint)file_size; riff.riff_type = GetBytes("WAVE"); var fmt = new RiffFormatChunk(); fmt.chunkID = GetBytes("fmt "); fmt.chunkSize = 16; fmt.wFormatTag = 1; fmt.wChannels = (ushort)channels; fmt.dwSamplesPerSec = (uint)rate; fmt.dwAvgBytesPerSec = (uint)(rate * (bps / 8) * channels); fmt.wBlockAlign = (ushort)((bps / 8) * channels); fmt.wBitsPerSample = (ushort)bps; var data = new RiffDataChunk(); data.head = GetBytes("data"); data.size = (uint)data_size; var size = Marshal.SizeOf(riff) + Marshal.SizeOf(fmt) + Marshal.SizeOf(data); var riff_ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(riff, riff_ptr, false); var fmt_ptr = new IntPtr(riff_ptr.ToInt32() + Marshal.SizeOf(riff)); Marshal.StructureToPtr(fmt, fmt_ptr, false); var data_ptr = new IntPtr(fmt_ptr.ToInt32() + Marshal.SizeOf(fmt)); Marshal.StructureToPtr(data, data_ptr, false); var header = new byte[size]; Marshal.Copy(riff_ptr, header, 0, size); Marshal.FreeHGlobal(riff_ptr); // Marshal.FreeHGlobal(fmt_ptr); //riff_ptrを開放すると同時に開放されるので不要 // Marshal.FreeHGlobal(data_ptr); //riff_ptrを開放すると同時に開放されるので不要 return(header); }
public static RiffHeader GetRiffHeader(Stream stream) { var id = new byte[4]; stream.Read(id, 0, id.Length); while (Encoding.ASCII.GetString(id) != "RIFF") { stream.Position -= 3; stream.Read(id, 0, id.Length); } stream.Position -= 4; var header = new RiffHeader(); var rawData = new byte[12]; stream.Read(rawData, 0, rawData.Length); header.ID = Encoding.ASCII.GetString(rawData.Take(4).ToArray()); header.Size = BitConverter.ToUInt32(rawData.Skip(4).Take(4).ToArray(), 0); header.Format = Encoding.ASCII.GetString(rawData.Skip(8).ToArray()); return(header); }
/// <summary> /// Creates a RIFF object with data from the given RIFF-compatable stream. /// </summary> /// <param name="input">a stream containing RIFF data</param> public RIFF(Stream input) { if (!input.CanRead) { throw new ArgumentException("The RIFF parser requires a readable stream.", "input"); } long initial = input.Position; int headerLength = Marshal.SizeOf(typeof(RiffHeader)); int innerHeaderLength = Marshal.SizeOf(typeof(RiffBlockHeader)); byte[] data = new byte[headerLength]; input.Read(data, 0, headerLength); header = Reinterpret.Memory <RiffHeader>(data); blocks = new List <RiffBlock>(); while (input.Position - initial < header.HeaderBlockHeader.Length + innerHeaderLength) { blocks.Add(new RiffBlock(input)); } }
/* #region Private Methods */ private void ReadHeader() { //See here: http://soundfile.sapp.org/doc/WaveFormat/ var firstBytes = new byte[36]; this.FileStream.Read(firstBytes, 0, 36); this.FileStream.Seek(0, SeekOrigin.Begin); var riffHeader = ReadASCII(4); var chunkLen = ReadUInt32(); var format = ReadASCII(4); var subChunk1Id = ReadASCII(4); var subChunk1Size = ReadUInt32(); var audioFmt = ReadUInt16(); var numChannels = ReadUInt16(); var sampleRate = ReadUInt32(); var byteRate = ReadUInt32(); //== SampleRate * NumChannels * BitsPerSample / 8 var blockAlign = ReadUInt16(); var bitsPerSample = ReadUInt16(); var retval = new RiffHeader() { Bytes = firstBytes, RIFFHeader = riffHeader, ChunkLength = chunkLen, Format = format, AudioFormat = audioFmt, NumChannels = numChannels, SampleRate = sampleRate, ByteRate = byteRate, BlockAlign = blockAlign, BitsPerSample = bitsPerSample }; this.RiffHeader = retval; }
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("The Wave file format is invalid.") Data = new short[DataChunk.Size / 2]; byte[] bytes = new byte[DataChunk.Size]; stream.Read(bytes, 0, bytes.Length); stream.Dispose(); for (int i = 0; i < Data.Length; i++) { Data[i] = BitConverter.ToInt16(new byte[2] { bytes[2 * i], bytes[2 * i + 1] }, 0); } }
/// <summary> /// Creates an empty RIFF container file. /// </summary> /// <param name="type">a fourcc typecode to embed in the header</param> public RIFF(string type) { header = new RiffHeader(Reinterpret.FourCC(type, ByteOrder.BigEndian)); blocks = new List <RiffBlock>(); }