sawass sawass

sawass sawass

  • NA
  • 36
  • 23k

explain of a class.

Jun 7 2011 4:57 AM
Hi,
Is someone can help me what is the role of this class:

 public class MeteringStream : WaveStream
    {
        public WaveStream SourceStream { get; private set; }
        public int SamplesPerNotification { get; set; }

        float[] maxSamples;
        int sampleCount;

        public event EventHandler<StreamVolumeEventArgs> StreamVolume;

         MeteringStream(WaveStream sourceStream) :
            this(sourceStream, sourceStream.WaveFormat.SampleRate / 10)
       {
        }

        public MeteringStream(WaveStream sourceStream, int samplesPerNotification)
        {
            SourceStream = sourceStream;
            if (sourceStream.WaveFormat.BitsPerSample != 32)
                throw new ArgumentException("Metering Stream expects 32 bit floating point audio", "sourceStream");
            maxSamples = new float[sourceStream.WaveFormat.Channels];
            this.SamplesPerNotification = samplesPerNotification;
        }

        public override WaveFormat WaveFormat
        {
            get { return SourceStream.WaveFormat; }
        }

        public override long Length
        {
            get { return SourceStream.Length; }
        }

        public override long Position
        {
            get { return SourceStream.Position; }
            set { SourceStream.Position = value; }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int bytesRead = SourceStream.Read(buffer, offset, count);
            ProcessData(buffer, offset, bytesRead);
            return bytesRead;
        }

        private void ProcessData(byte[] buffer, int offset, int count)
        {
            int index = 0;
            while (index < count)
            {
                for (int channel = 0; channel < maxSamples.Length; channel++)
                {
                    float sampleValue = Math.Abs(BitConverter.ToSingle(buffer, offset + index));
                    maxSamples[channel] = Math.Max(maxSamples[channel], sampleValue);
                    index += 4;
                }
                sampleCount++;
                if (sampleCount >= SamplesPerNotification)
                {
                    RaiseStreamVolumeNotification();
                    sampleCount = 0;
                    Array.Clear(maxSamples, 0, maxSamples.Length);

                }

            }
        }

        private void RaiseStreamVolumeNotification()
        {
            if (StreamVolume != null)
            {
                StreamVolume(this, new StreamVolumeEventArgs() { MaxSampleValues = (float[])maxSamples.Clone() });
            }
        }
    }

    public class StreamVolumeEventArgs : EventArgs
    {
        public float[] MaxSampleValues { get; set; }
    }

and the internal class StreamVolumeEventArgs : EventArgs
Thanks in advance.





Answers (6)