internal override void Process(Sample Sample)
        {
            if (Sample.Silent)
            {
                return;
            }

            double minFreq    = Sample.MinFreq;
            double maxFreq    = Sample.MaxFreq;
            double sampleRate = Sample.Format.SamplesPerSecond;

            double[] spectr = new double[Sample.Spectrogram.Length];
            for (int i = 0; i < spectr.Length; i++)
            {
                spectr[i] = Spectrometer.Abs2(Sample.Spectrogram[i]);
            }

            int usefullMinSpectr = Math.Max(0,
                                            (int)(minFreq * spectr.Length / sampleRate));
            int usefullMaxSpectr = Math.Min(spectr.Length,
                                            (int)(maxFreq * spectr.Length / sampleRate) + 1);

            int[] peakIndices = FindPeaks(spectr, usefullMinSpectr, usefullMaxSpectr - usefullMinSpectr,
                                          PeaksCount);

            if (Array.IndexOf(peakIndices, usefullMinSpectr) >= 0)
            {
                Sample.FundamentalFreq = 0;
                return;
            }

            const int verifyFragmentOffset = 0;

            int verifyFragmentLength = (int)(sampleRate / minFreq);

            double minPeakValue       = Double.PositiveInfinity;
            int    minPeakIndex       = 0;
            int    minOptimalInterval = 0;

            for (int i = 0; i < peakIndices.Length; i++)
            {
                int    index            = peakIndices[i];
                int    binIntervalStart = spectr.Length / (index + 1);
                int    binIntervalEnd   = spectr.Length / index;
                int    interval;
                double peakValue;

                ScanSignalIntervals(Sample.EqualizedData, verifyFragmentOffset, verifyFragmentLength,
                                    binIntervalStart, binIntervalEnd, out interval, out peakValue);

                if (peakValue < minPeakValue)
                {
                    minPeakValue       = peakValue;
                    minPeakIndex       = index;
                    minOptimalInterval = interval;
                }
            }

            Sample.FundamentalFreq = (double)sampleRate / minOptimalInterval;
        }
Ejemplo n.º 2
0
        public SoundProcessor(UpdateMuscialSymbolDelegate UpdateMuscialSymbolDelegate, Settings settings)
        {
            Preprocessor  = new Preprocessor();
            Splitter      = new KvantSound.Splitter(OnSplittedSamples);
            Equalizer     = new KvantSound.Equalizer();
            Normalizer    = new KvantSound.Normalizer();
            Spectrometer  = new KvantSound.Spectrometer();
            PitchDetector = new PitchDetector();
            this.UpdateMuscialSymbolDelegate = UpdateMuscialSymbolDelegate;

            this.settings = settings ?? new Settings();
            ApplySettings();
        }
Ejemplo n.º 3
0
        internal override void Process(Sample Sample)
        {
            double[] Spectr = new double[Sample.Spectrogram.Length];
            for (int i = 0; i < Spectr.Length; i++)
            {
                Spectr[i] = Spectrometer.Abs2(Sample.Spectrogram[i]);
            }

            double[][] DownSamples = new double[DownSamplesCount][];

            for (int i = 0; i < DownSamplesCount; i++)
            {
                int    DownSampleLength = Spectr.Length / (i + 2);
                double Ratio            = (double)DownSampleLength / Spectr.Length;

                DownSamples[i] = new double[DownSampleLength];
                for (int j = 0; j < Spectr.Length; j++)
                {
                    DownSamples[i][(int)(j * Ratio)] += Spectr[j];
                }
                for (int j = 0; j < DownSamples[i].Length; j++)
                {
                    DownSamples[i][j] *= Ratio;
                }
            }

            double[] SampleMult = new double[DownSamples[DownSamplesCount - 1].Length];
            double   Max        = 0;
            int      IndOfMax   = 0;

            for (int i = 0; i < SampleMult.Length; i++)
            {
                SampleMult[i] = Spectr[i];
                for (int j = 0; j < DownSamplesCount; j++)
                {
                    SampleMult[i] *= DownSamples[j][i];
                }
                if (Max < SampleMult[i])
                {
                    Max      = SampleMult[i];
                    IndOfMax = i;
                }
            }

            Sample.FundamentalFreq = (double)IndOfMax / Spectr.Length * Sample.Format.SamplesPerSecond;
        }