Example #1
0
        public static WavePeakData GenerateEmptyPeaks(string peakFileName, int totalSeconds)
        {
            int peaksPerSecond = Configuration.Settings.VideoControls.WaveformMinimumSampleRate;
            var peaks          = new List <WavePeak>
            {
                new WavePeak(1000, -1000)
            };
            var totalPeaks = peaksPerSecond * totalSeconds;

            for (int i = 0; i < totalPeaks; i++)
            {
                peaks.Add(new WavePeak(1, -1));
            }
            peaks.Add(new WavePeak(1000, -1000));

            // save results to file
            using (var stream = File.Create(peakFileName))
            {
                WaveHeader.WriteHeader(stream, peaksPerSecond, 2, 16, peaks.Count);
                byte[] buffer = new byte[4];
                foreach (var peak in peaks)
                {
                    WriteValue16Bit(buffer, 0, peak.Max);
                    WriteValue16Bit(buffer, 2, peak.Min);
                    stream.Write(buffer, 0, 4);
                }
            }

            return(new WavePeakData(peaksPerSecond, peaks));
        }
Example #2
0
 private void Initialize(Stream stream)
 {
     _stream = stream;
     Header  = new WaveHeader(_stream);
 }
Example #3
0
        /// <summary>
        /// Generates peaks and saves them to disk.
        /// </summary>
        /// <param name="delayInMilliseconds">Delay in milliseconds (normally zero)</param>
        /// <param name="peakFileName">Path of the output file</param>
        public WavePeakData GeneratePeaks(int delayInMilliseconds, string peakFileName)
        {
            int peaksPerSecond = Math.Min(Configuration.Settings.VideoControls.WaveformMinimumSampleRate, _header.SampleRate);

            // ensure that peaks per second is a factor of the sample rate
            while (_header.SampleRate % peaksPerSecond != 0)
            {
                peaksPerSecond++;
            }

            int delaySampleCount = (int)(_header.SampleRate * (delayInMilliseconds / TimeCode.BaseUnit));

            // ignore negative delays for now (pretty sure it can't happen in mkv and some places pass in -1 by mistake)
            delaySampleCount = Math.Max(delaySampleCount, 0);

            var   peaks = new List <WavePeak>();
            var   readSampleDataValue   = GetSampleDataReader();
            float sampleAndChannelScale = (float)GetSampleAndChannelScale();
            long  fileSampleCount       = _header.LengthInSamples;
            long  fileSampleOffset      = -delaySampleCount;
            int   chunkSampleCount      = _header.SampleRate / peaksPerSecond;

            byte[]  data         = new byte[chunkSampleCount * _header.BlockAlign];
            float[] chunkSamples = new float[chunkSampleCount];

            _stream.Seek(_header.DataStartPosition, SeekOrigin.Begin);

            // for negative delays, skip samples at the beginning
            if (fileSampleOffset > 0)
            {
                _stream.Seek(fileSampleOffset * _header.BlockAlign, SeekOrigin.Current);
            }

            while (fileSampleOffset < fileSampleCount)
            {
                // calculate how many samples to skip at the beginning (for positive delays)
                int startSkipSampleCount = 0;
                if (fileSampleOffset < 0)
                {
                    startSkipSampleCount = (int)Math.Min(-fileSampleOffset, chunkSampleCount);
                    fileSampleOffset    += startSkipSampleCount;
                }

                // calculate how many samples to read from the file
                long fileSamplesRemaining = fileSampleCount - Math.Max(fileSampleOffset, 0);
                int  fileReadSampleCount  = (int)Math.Min(fileSamplesRemaining, chunkSampleCount - startSkipSampleCount);

                // read samples from the file
                if (fileReadSampleCount > 0)
                {
                    int fileReadByteCount = fileReadSampleCount * _header.BlockAlign;
                    _stream.Read(data, 0, fileReadByteCount);
                    fileSampleOffset += fileReadSampleCount;

                    int chunkSampleOffset = 0;
                    int dataByteOffset    = 0;
                    while (dataByteOffset < fileReadByteCount)
                    {
                        float value = 0F;
                        for (int iChannel = 0; iChannel < _header.NumberOfChannels; iChannel++)
                        {
                            value += readSampleDataValue(data, ref dataByteOffset);
                        }
                        chunkSamples[chunkSampleOffset] = value * sampleAndChannelScale;
                        chunkSampleOffset += 1;
                    }
                }

                // calculate peaks
                peaks.Add(CalculatePeak(chunkSamples, fileReadSampleCount));
            }

            // save results to file
            using (var stream = File.Create(peakFileName))
            {
                WaveHeader.WriteHeader(stream, peaksPerSecond, 2, 16, peaks.Count);
                byte[] buffer = new byte[4];
                foreach (var peak in peaks)
                {
                    WriteValue16Bit(buffer, 0, peak.Max);
                    WriteValue16Bit(buffer, 2, peak.Min);
                    stream.Write(buffer, 0, 4);
                }
            }

            return(new WavePeakData(peaksPerSecond, peaks));
        }
Example #4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="stream">Stream of a wave file</param>
 public WavePeakGenerator(Stream stream)
 {
     _stream = stream;
     _header = new WaveHeader(_stream);
 }
 private void Initialize(Stream stream)
 {
     _stream = stream;
     Header = new WaveHeader(_stream);
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="stream">Stream of a wave file</param>
 public WavePeakGenerator(Stream stream)
 {
     _stream = stream;
     _header = new WaveHeader(_stream);
 }