public Waveform CreateWaveform()
 {
     using (var fs = new FileStream(Path, FileMode.Open, FileAccess.Read))
     using (var reader = new BinaryReader(fs))
     {
         _header = ReadHeaderData(reader);
         //TODO: Find out why we want the ratio to be 80 when conditions are met.
         SampleRatio = _header.Channels == 2 ? 40 : 80;
         var sampleData = ReadSampleData(reader);
         return new Waveform(_header, new List<short>(sampleData), SampleRatio);
     }
 }
        private Header ReadHeaderData(BinaryReader r)
        {
            var header = new Header();

            var riffFileDescriptor = r.ReadChars(4);
            ValidateHeaderData(riffFileDescriptor, Header.RiffFileDescriptor);

            header.FileSize = r.ReadUInt32();
            var fileTypeHeader = r.ReadChars(4);
            ValidateHeaderData(fileTypeHeader, Header.FileTypeHeader);

            var formatChunkMarker = r.ReadChars(4);
            ValidateHeaderData(formatChunkMarker, Header.FormatChunkMarker);

            uint formatChunkSize = r.ReadUInt32();

            ushort audioFormat = r.ReadUInt16();
            if (audioFormat != Header.AudioFormat)
                throw new InvalidDataException("Audio format is incorrect: " + audioFormat);

            header.Channels = r.ReadUInt16();
            header.SampleRate = r.ReadUInt32();
            header.ByteRate = r.ReadUInt32();
            header.BlockAlign = r.ReadUInt16();
            header.BitsPerSample = r.ReadUInt16();

            /* There is a chance that the wave file will have a few extra bytes in its header. If
             * it does, then calculate how many and skip over them.
             */
            int extaFormatBytes = (Header.DefaultFormatChunkSize < formatChunkSize)
                ? (int)(formatChunkSize - Header.DefaultFormatChunkSize)
                : 0;

            r.ReadBytes(extaFormatBytes);

            var dataChunkMarker = r.ReadChars(4);
            ValidateHeaderData(dataChunkMarker, Header.DataChunkMarker);

            header.DataSize = r.ReadUInt32();

            if (r.BaseStream.Position != Header.DefaultHeaderByteSize + extaFormatBytes)
                throw new InvalidDataException("Stream is at the wrong position");

            return header;
        }
 /// <summary>
 /// Writes the header for the WAV file that the waveform data was sampled from.
 /// </summary>
 /// <param name="project"></param>
 /// <param name="header"></param>
 public static void WriteWaveFormHeader(Project project, Header header)
 {
     Log.Info("Saving waveform header file to " + project.Files.WaveFormHeader);
     using (var file = File.Open(project.Files.WaveFormHeader, FileMode.Create, FileAccess.Write))
     {
         var bin = new BinaryFormatter();
         bin.Serialize(file, header);
     }
     Log.Info("Waveform header file saved successfully");
 }
 public Waveform(Header h, List<short> d)
 {
     Header = h;
     Data = d;
 }
 private Waveform(Header header, List<short> d)
 {
     Header = header;
     Data = d;
 }
 public Waveform(Header header, List<short> data, int sampleRatio)
     : this(header, data)
 {
     SampleRatio = sampleRatio;
 }
 public AudioUtility(Project p)
 {
     _videoFile = p.Files.Video;
     _audioFile = Path.Combine(p.Folders.Cache, Path.GetFileNameWithoutExtension(_videoFile) + ".wav");
     _header = new Header();
 }