Exemplo n.º 1
0
        public static MemoryStream GetWavAsMemoryStream(this Stream data, int sampleRate, int audioChannels, int bitsPerSample)
        {
            MemoryStream memoryStream = new MemoryStream();

            WavExtensions.WriteHeader((Stream)memoryStream, sampleRate, audioChannels, bitsPerSample);
            WavExtensions.SeekPastHeader((Stream)memoryStream);
            data.Position = 0L;
            data.CopyTo((Stream)memoryStream);
            WavExtensions.UpdateHeader((Stream)memoryStream);
            return(memoryStream);
        }
Exemplo n.º 2
0
        private static void WriteHeader(Stream stream, int sampleRate, int audioChannels = 1, int bitsPerSample = 16)
        {
            int      num      = bitsPerSample / 8;
            Encoding utF8     = Encoding.UTF8;
            long     position = stream.Position;

            stream.Seek(0, SeekOrigin.Begin);
            stream.Write(utF8.GetBytes("RIFF"), 0, 4);
            stream.Write(BitConverter.GetBytes(0), 0, 4);
            stream.Write(utF8.GetBytes("WAVE"), 0, 4);
            stream.Write(utF8.GetBytes("fmt "), 0, 4);
            stream.Write(BitConverter.GetBytes(16), 0, 4);
            stream.Write(BitConverter.GetBytes(1), 0, 2);
            stream.Write(BitConverter.GetBytes((short)audioChannels), 0, 2);
            stream.Write(BitConverter.GetBytes(sampleRate), 0, 4);
            stream.Write(BitConverter.GetBytes(sampleRate * num * audioChannels), 0, 4);
            stream.Write(BitConverter.GetBytes((short)num), 0, 2);
            stream.Write(BitConverter.GetBytes((short)bitsPerSample), 0, 2);
            stream.Write(utF8.GetBytes("data"), 0, 4);
            stream.Write(BitConverter.GetBytes(0), 0, 4);
            WavExtensions.UpdateHeader(stream);
            stream.Seek(position, SeekOrigin.Begin);
        }