예제 #1
0
    void InsertArray(FFTFreqBand[] sampleArr, int size, FFTFreqBand sample)
    {
        List <FFTFreqBand> store = new List <FFTFreqBand>();

        for (int i = 0; i < sampleArr.Length; i++)
        {
            //Is the new value higher than the value from the list?
            if (sample.Value < sampleArr[i].Value)
            {
                //Save the value from the original list
                store.Add(sampleArr[i]);
            }
            else
            {
                //Save the new value
                store.Add(sample);
                //Save the remaining part of the list
                for (int j = i; j < size; j++)
                {
                    //As long as it's not empty
                    if (sampleArr[j].Value > 0.0f)
                    {
                        store.Add(sampleArr[j]);
                    }
                    else
                    {
                        break;
                    }
                }
                break;
            }
        }
        int count = 0;

        foreach (FFTFreqBand ret in store)
        {
            if (count < size)
            {
                sampleArr[count] = ret;
                count++;
            }
            else
            {
                break;
            }
        }
    }
예제 #2
0
    private void AvgPitchCalc()
    {
        DecibelDetectionClosing(decibelDetectionClosingValue);
        if (ValueDetection(detectionOpeningValue))
        {
            //Checking all the samples
            for (int i = 0; i < (samples.Length / 4); ++i)
            {
                //Get the value of a sample
                float v = samples[i];

                //high-pass filter
                if (v > 0.001f)
                {
                    //Iterate though saved values
                    float       itFreq = i * freqperBand;
                    FFTFreqBand itV    = new FFTFreqBand(v, i, itFreq);

                    InsertArray(samplesStored, samplesStored.Length, itV);
                }
            }

            if (samplesStored[0].Freq > 27.5f) //we use 27.5hz since it's the lowest frequency a piano can make.
            {
                FFTFreqBand itVal = new FFTFreqBand(0.0f, 0.0f, 0.0f);

                //Delete the frequency samples that their band indexs are adjecent based on their lowest value
                for (int i = 0; i < samplesStored.Length; i++)
                {
                    itVal = samplesStored[i];
                    for (int j = 0; j < samplesStored.Length; j++)
                    {
                        //Don't compare the iterated frequency with itself.
                        if (i != j)
                        {
                            //Look for adjecent frequency bands
                            if (itVal.Index + 1 == samplesStored[j].Index || itVal.Index - 1 == samplesStored[j].Index)
                            {
                                //Delete the band with less value than the other.
                                if (itVal.Value > samplesStored[j].Value)
                                {
                                    samplesStored[j] = new FFTFreqBand(0.0f, 0.0f, 0.0f);
                                }
                            }
                        }
                    }
                    samplesStored[i] = itVal;
                }

                //Save the frequencies in another list for optimized calculations
                for (int i = 0; i < samplesStored.Length; i++)
                {
                    samplesSavedFreq[i] = samplesStored[i].Freq;
                }

                fundamentalFrequencyOutput = 4186.0f;
                FFTFreqBand fundamentalSample = new FFTFreqBand(0.0f, 0.0f, 0.0f);
                for (int i = 0; i < samplesSavedFreq.Length; i++)
                {
                    //Get the lowest frequency detected and save it.
                    if (samplesSavedFreq[i] > 0.0f && samplesSavedFreq[i] < fundamentalFrequencyOutput)
                    {
                        fundamentalFrequencyOutput = samplesSavedFreq[i];
                    }
                }
            }
        }
        else
        {
            //avgPitchDisplay.text = "AVGPitch";
            samplesStored = new FFTFreqBand[10];
        }
    }