public static Stream CreateStream(Stream waveData, WaveFormat format) { MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); writer.Write(System.Text.Encoding.ASCII.GetBytes("RIFF".ToCharArray())); writer.Write((Int32)(waveData.Length + 36)); writer.Write(System.Text.Encoding.ASCII.GetBytes("WAVEfmt ".ToCharArray())); writer.Write((Int32)16); writer.Write((Int16)format.wFormatTag); writer.Write((Int16)format.nChannels); writer.Write((Int32)format.nSamplesPerSec); writer.Write((Int32)format.nAvgBytesPerSec); writer.Write((Int16)format.nBlockAlign); writer.Write((Int16)format.wBitsPerSample); writer.Write(System.Text.Encoding.ASCII.GetBytes("data".ToCharArray())); writer.Write((Int32)waveData.Length); waveData.Seek(0, SeekOrigin.Begin); byte[] b = new byte[waveData.Length]; waveData.Read(b, 0, (int)waveData.Length); writer.Write(b); writer.Seek(0, SeekOrigin.Begin); return stream; }
private void CopyHeader(Stream destinationStream) { BinaryReader reader = new BinaryReader(m_Stream); BinaryWriter writer = new BinaryWriter(destinationStream); if (CopyChunk(reader, writer) != "RIFF") throw new Exception("Fromat file tidak valid"); writer.Write(reader.ReadInt32()); if (CopyChunk(reader, writer) != "WAVE") throw new Exception("Fromat file tidak valid"); if (CopyChunk(reader, writer) != "fmt ") throw new Exception("Fromat file tidak valid"); int len = reader.ReadInt32(); if (len < 16) { throw new Exception("Invalid file format"); } else { writer.Write(len); } m_Format = new WaveFormat(22050, 16, 2); m_Format.wFormatTag = reader.ReadInt16(); m_Format.nChannels = reader.ReadInt16(); m_Format.nSamplesPerSec = reader.ReadInt32(); m_Format.nAvgBytesPerSec = reader.ReadInt32(); m_Format.nBlockAlign = reader.ReadInt16(); m_Format.wBitsPerSample = reader.ReadInt16(); writer.Write(m_Format.wFormatTag); writer.Write(m_Format.nChannels); writer.Write(m_Format.nSamplesPerSec); writer.Write(m_Format.nAvgBytesPerSec); writer.Write(m_Format.nBlockAlign); writer.Write(m_Format.wBitsPerSample); len -= 16; writer.Write(reader.ReadBytes(len)); len = 0; while (m_Stream.Position < m_Stream.Length && CopyChunk(reader, writer) != "data") ; if (m_Stream.Position >= m_Stream.Length) throw new Exception("Fromat file tidak valid"); m_Length = reader.ReadInt32(); writer.Write(m_Length); m_DataPos = m_Stream.Position; Position = 0; }
private void ReadHeader() { BinaryReader Reader = new BinaryReader(m_Stream); if (ReadChunk(Reader) != "RIFF") throw new Exception("Fromat file tidak valid"); Reader.ReadInt32(); if (ReadChunk(Reader) != "WAVE") throw new Exception("Fromat file tidak valid"); if (ReadChunk(Reader) != "fmt ") throw new Exception("Fromat file tidak valid"); int len = Reader.ReadInt32(); if (len < 16) throw new Exception("Fromat file tidak valid"); m_Format = new WaveFormat(22050, 16, 2); m_Format.wFormatTag = Reader.ReadInt16(); m_Format.nChannels = Reader.ReadInt16(); m_Format.nSamplesPerSec = Reader.ReadInt32(); m_Format.nAvgBytesPerSec = Reader.ReadInt32(); m_Format.nBlockAlign = Reader.ReadInt16(); m_Format.wBitsPerSample = Reader.ReadInt16(); len -= 16; while (len > 0) { Reader.ReadByte(); len--; } while (m_Stream.Position < m_Stream.Length && ReadChunk(Reader) != "data") ; if (m_Stream.Position >= m_Stream.Length) throw new Exception("Fromat file tidak valid"); m_Length = Reader.ReadInt32(); m_DataPos = m_Stream.Position; Position = 0; }
public static extern int waveInOpen(out IntPtr phwi, int uDeviceID, WaveFormat lpFormat, WaveDelegate dwCallback, int dwInstance, int dwFlags);