Vidka.Core.ExternalOps.WaveformExtraction.squishWaveform16le C# (CSharp) Method

squishWaveform16le() private method

private squishWaveform16le ( byte rawsamples2Byte, int length ) : byte[]
rawsamples2Byte byte
length int
return byte[]
        private byte[] squishWaveform16le(byte[] rawsamples2Byte, int length)
        {
            var result = new byte[length];
            byte[] byte2 = new byte[2];
            int total = 0;
            int count = 0;
            int max = 0;
            int countThresh = (rawsamples2Byte.Length / 2) / length;
            int index = 0;
            for (int i = 0; i < rawsamples2Byte.Length / 2; i++)
            {
                byte2[0] = rawsamples2Byte[2 * i];
                byte2[1] = rawsamples2Byte[2 * i + 1];
                int sample = BitConverter.ToUInt16(byte2, 0);
                total += sample;
                count++;
                max = Math.Max(max, sample);
                if (count >= countThresh)
                {
                    var avg = total / count;
                    result[index] = (byte)(256 * avg / short.MaxValue);
                    // reset!!!
                    total = 0;
                    count = 0;
                    index++;
                }
                if (index >= result.Length)
                    break;
            }
            if (count > 0 && index < result.Length)
            {
                var avg = total / count;
                result[index] = (byte)(256 * avg / short.MaxValue);
            }
            return result;
        }