Click here to Skip to main content
16,018,802 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
When i write
bwl.Write(buffer);


my Subchunk2Size is bigger than the initial file...

C#
using (var wfr = new WaveFileReader(fs))
          {
              var outputFormat = new WaveFormat(8000, 16, 1);
              using (var pcmStream = new WaveFormatConversionStream(outputFormat, wfr))
              {
                  using (var ms = new MemoryStream())
                  {
                      BinaryWriter bwl = new BinaryWriter(ms);

                      var bytesRead = -1;
                      while (bytesRead != 0)
                      {
                         var buffer = new byte[pcmStream.WaveFormat.AverageBytesPerSecond];
                          bytesRead = pcmStream.Read(buffer, 0, pcmStream.WaveFormat.AverageBytesPerSecond);

                          int    length = (int)fs.Length - 8;
                          program.channels = 1;
                          program.samplerate = 8000;;

                          program.BitsPerSample = 16;// br.ReadInt16();
                          program.DataLength = (int)ms.Length - 44;

                          bwl.Write(new char[4] { 'R', 'I', 'F', 'F' });
                          bwl.Write(length);
                          bwl.Write(new char[8] { 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ' });
                          bwl.Write((int)16);
                          bwl.Write((short)1);
                          bwl.Write(program.channels);
                          bwl.Write(program.samplerate);
                          bwl.Write((int)(program.samplerate * ((program.BitsPerSample * program.channels) / 8)));
                          bwl.Write((short)((program.BitsPerSample * program.channels) / 8));
                          bwl.Write(program.BitsPerSample);
                          bwl.Write(new char[4] { 'd', 'a', 't', 'a' });
                          //bw.Write(Header.DataLength);
                          bwl.Write(buffer);

                          System.IO.File.WriteAllBytes(@"C:Desktop\waveConvertBuff.wav", ms.GetBuffer());
                     }

                      bwl.Close();
                      fs.Close();
                      ms.Close();


                  }
              }

my waveConvertBuff.wav lenght is about 74h hoiur but when playing the initial length is correct (4 seconds)
Why?
Posted
Updated 20-Feb-14 20:48pm
v4

I recently developed and debugged this part of Wave writing, can share with you:
C#
namespace Wave {
    using System;
    using System.IO;
    using Encoding = System.Text.Encoding;

    public partial class WaveWriter : BinaryWriter, IDisposable {

        public WaveWriter(string fileName)
            : base(new FileStream(fileName, FileMode.Create, FileAccess.Write)) {
            Initialize(new WaveParameterWriter());
        } //WaveWriter
        public WaveWriter(string fileName, uint rate, short bits, short channels)
            : base(new FileStream(fileName, FileMode.Create, FileAccess.Write)) {
            Initialize(new WaveParameterWriter(rate, bits, channels));
        } //WaveWriter

        public WaveWriter(Stream stream) : base(stream) { Initialize(new WaveParameterWriter()); }
        public WaveWriter(Stream stream, uint rate, short bits, short channels)
            : base(stream) {
            Initialize(new WaveParameterWriter(rate, bits, channels));
        } //WaveWriter

        void Initialize(WaveParameterWriter parameterWriter) {
            this.waveParameterWriter = parameterWriter;
            WriteRiffHeader();
        } //Initialize

        void WriteRiffHeader() {
            Func<string, byte[]> serialize = (id) => { return Encoding.ASCII.GetBytes(id); };
            Write(serialize(DefinitionSet.ChunkId)); // chunk ID
            Write(audioDataSize + DefinitionSet.ChunkHeaderSize); // chunk Size
            Write(serialize(DefinitionSet.Format)); // format
            Write(serialize(DefinitionSet.SubChunkOneId)); // Subchunk1 ID
            Write(DefinitionSet.SubChunkOneSize); //subchunk1 size (18 for PCM)
            Write(DefinitionSet.AudioFormatPcm); // Audio Format, 1 for PCM
            // variable parameters: NumChannels, SampleRate, ByteRate, BlockAlign, BitsPerSample:
            waveParameterWriter.Write(this);
            Write(DefinitionSet.ExtraParameterSizePcm); // extra parameter size, 0 for PCM
            Write(serialize(DefinitionSet.SubChunkTwoId)); // // Subchunk2 ID
            Write(audioDataSize); //subchunk2 size
            audioStreamStart = this.BaseStream.Position;
        } //WriteRiffHeader

        void IDisposable.Dispose() { Close(); }
        public override void Close() {
            if (isClosed) return;
            audioDataSize = (uint)(this.BaseStream.Position - audioStreamStart);
            Seek(0, SeekOrigin.Begin);
            WriteRiffHeader();
            isClosed = true;
            base.Close();
        } //Close

        long audioStreamStart;
        WaveParameterWriter waveParameterWriter;
        uint audioDataSize;
        bool isClosed;

        sealed class WaveParameterWriter {
            internal WaveParameterWriter() : this(DefinitionSet.DefaultRate, DefinitionSet.DefaultBitsPerSample, DefinitionSet.DefaultChannels) { }
            internal WaveParameterWriter(uint rate, short bits, short channels) {
                this.channels = channels;
                this.sampleRate = rate;
                this.bitsPerSample = bits;
                this.blockAlign = (short)(channels * (bits / 8));
                this.byteRate = (uint)(sampleRate * blockAlign);
            } //WaveHeaderWriter
            internal void Write(BinaryWriter writer) {
                writer.Write(channels);
                writer.Write(sampleRate);
                writer.Write(byteRate);
                writer.Write(blockAlign);
                writer.Write(bitsPerSample);
            } //Write
            short channels;
            uint sampleRate;
            uint byteRate;
            short blockAlign;
            short bitsPerSample;
        } //class WaveHeaderWriter

    } //class WaveWriter

} //namespace Wave

It is based on the following definitions:
C#
using System;
using System.Collections.Generic;
namespace Wave {
    
    public partial class WaveWriter {

        static class DefinitionSet {

            // see https://ccrma.stanford.edu/courses/422/projects/WaveFormat
            internal const string ChunkId = "RIFF";
            internal const uint ChunkHeaderSize = 38;
            internal const string Format = "WAVE";
            internal const string SubChunkOneId = "fmt ";
            internal const uint SubChunkOneSize = 18;
            internal const short AudioFormatPcm = 1;
            // variable parameters: NumChannels, SampleRate, ByteRate, BlockAlign, BitsPerSample
            internal const short ExtraParameterSizePcm = 0;
            internal const string SubChunkTwoId = "data";

            internal const uint DefaultRate = 44100;
            internal const short DefaultBitsPerSample = 16;
            internal const short DefaultChannels = 2;

        } //class DefinitionSet

    } //class WaveWriter

} //namespace Wave


I tested it pretty well; it works.

Primarily, I used this piece of documentation: https://ccrma.stanford.edu/courses/422/projects/WaveFormat[^].

—SA
 
Share this answer
 
v2
Your class is prety cool, yesterday i missed up in reading my wave Header, it's when a write the data the problem. The length is bigger than original!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900