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

squishWaveform() private method

private squishWaveform ( byte rawsamples2Byte, int length ) : byte[]
rawsamples2Byte byte
length int
return byte[]
        private byte[] squishWaveform(byte[] rawsamples2Byte, int length)
        {
            float[] result = new float[length];
            float maxResult = 0;
            int countThresh = (rawsamples2Byte.Length) / length;
            int ws = countThresh / 2;

            for (int i = 0; i < length; i++)
            {
                float sum = 0;
                var n = 0;

                for (int j = -ws; j < ws; j++)
                {
                    var index = j + (i * countThresh + countThresh / 2);
                    if (index < 0 || index >= rawsamples2Byte.Length)
                        continue;
                    sbyte sample = (sbyte)rawsamples2Byte[index];
                    sum += Math.Abs((short)sample);
                    n++;
                }
                result[i] = sum / n;
                maxResult = Math.Max(maxResult, result[i]);
            }

            //var result2 = new byte[length];
            //for (int i = 0; i < length; i++)
            //	result2[i] = (byte)Math.Floor(result[i] * 255 / maxResult);
            //return result2;

            var result3 = new byte[length];
            for (int i = 1; i < length - 1; i++)
            {
                var xi = Math.Floor(result[i-1] * 255 / maxResult);
                var xii = Math.Floor(result[i] * 255 / maxResult);
                var xiii = Math.Floor(result[i+1] * 255 / maxResult);
                result3[i] = (byte)((xi + 2*xii + xiii) / 4);
            }
            result3[0] = (byte)Math.Floor(result[0] * 255 / maxResult);
            result3[length-1] = (byte)Math.Floor(result[length-1] * 255 / maxResult);

            return result3;
        }