예제 #1
0
파일: Resampler.cs 프로젝트: Kontrast/PBS
        public unsafe Samples Resample(Samples source, float targetBitrate)
        {
            var values = source.Values;
            var k = targetBitrate / source.BitRate;
            var newLength = (int)Math.Round(k * source.Values.Length);
            var resValues = new float[newLength];
            var l = values.Length;

            fixed (float* valuesPtr = values)
            {
                var ptr = valuesPtr;
                for (int i = 0; i < l; i++)
                {
                    var j = (int)(k * i);
                    resValues[j] += *ptr;
                    ptr++;
                }
            }

            l = resValues.Length;

            fixed (float* valuesPtr = resValues)
            {
                var ptr = valuesPtr;
                for (int i = 0; i < l; i++)
                {
                    *ptr *= k;
                    ptr++;
                }
            }

            return new Samples() { Values = resValues, BitRate = targetBitrate };
        }
예제 #2
0
        public void Process(AudioRecord item, AudioInfo info)
        {
            var tempogram = new Tempogram();

            var s = info.Samples;

            s = new EnvelopeProcessor(factory).Build(info.Samples, 32, false);
            var s2 = new Samples() { Values = new float[s.Values.Length], BitRate = s.BitRate };
            var intensity = 0;

            for (int i = 0; i < s.Values.Length - 1; i++)
            {
                var d = s.Values[i + 1] - s.Values[i];
                var dd = d > 0 ? d : 0;
                s.Values[i] = dd;
                s2.Values[i] = d;
                if (d > minAmplitudeChangeForIntensityRate)
                    intensity++;
            }
            s.Values[s.Values.Length - 1] = 0;
            s2.Values[s.Values.Length - 1] = 0;

            var time = s.Values.Length / s.BitRate;//time of sound

            var maxShift = (int)(s.Values.Length * (maxRithmDuration / time));

            var autoCorr1 = AutoCorr(s.Values, maxShift, 5);
            var autoCorr2 = AutoCorr(s2.Values, maxShift, 2);
            var l = (float)autoCorr1.Length;
            var k = Math.Log(2);
            var list1 = new List<KeyValuePair<float, float>>();
            var list2 = new List<KeyValuePair<float, float>>();
            for (int i = 0; i < l; i++)
            {
                var j = i / (float)l;
                j = (float)(Math.Log(j + 1) / k);
                list1.Add(new KeyValuePair<float, float>(j, autoCorr1[i]));

                var v = autoCorr2[i];
                list2.Add(new KeyValuePair<float, float>(j, v > 0 ? v : 0));
            }
            tempogram.LongTempogram.Build(list1);
            tempogram.ShortTempogram.Build(list2);

            tempogram.Intensity = (float)intensity / time;

            CalcTempo(tempogram);

            //save to audio item
            item.Data.Add(tempogram);
        }
예제 #3
0
파일: Envelope.cs 프로젝트: Kontrast/PBS
 public Envelope(Samples samples)
 {
     var values = samples.Values;
     //pack
     packedValues = new byte[values.Length / 2];
     for (int i = 0; i < values.Length; i += 2)
     {
         var v1 = (int)(16 * values[i]);
         var v2 = (int)(16 * values[i + 1]);
         if (v1 > 15) v1 = 15;
         if (v2 > 15) v2 = 15;
         packedValues[i / 2] = (byte)((v1 << 4) + v2);
     }
 }
예제 #4
0
        /// <summary>
        /// Amplitude envelope builder
        /// </summary>
        public Samples Build(Samples source, int targetBitRate = 20, bool differentiate = false)
        {
            float k = 1f * targetBitRate / source.BitRate;
            var values = source.Values;
            var resValues = new float[(int)(values.Length * k) + 1];

            for (int i = values.Length - 2; i >= 0; i--)
            {
                var ii = (int)(i * k);
                var prev = resValues[ii];
                var f = values[i];
                if (differentiate) f = values[i + 1] - f;
                if (f < 0) f = -f;

                if (prev < f) resValues[ii] = f;
            }

            return new Samples() { BitRate = targetBitRate, Values = resValues };
        }