Beispiel #1
0
    public void SpawnMonster([NotNull] SpectralFluxInfo point, ObjectPooler objectPooler, string tag)
    {
        if (point.IsPeak && count > 10)
        {
            Vector3 position = new Vector3(0, 0, 0);
            if (tag == "Zombie")
            {
                position = new Vector3(0, 0.5f, 20);
            }
            else if (tag == "Skeleton")
            {
                position = new Vector3(-3, 0.5f, 20);
            }
            else if (tag == "Ghoost")
            {
                position = new Vector3(3, 1, 20);
            }
            //else if (tag == "Zombie2") position = new Vector3(5, 0, 0);
            //else if (tag == "Skeleton2") position = new Vector3(10, 0, 0);
            //else if (tag == "Ghoost2") position = new Vector3(15, 0, 0);

            objectPooler.SpawnFromPool(tag, position, Quaternion.identity);
            count = 0;
        }
        else
        {
            count++;
        }
    }
Beispiel #2
0
    public void AnalyzeSpectrum(float[] spectrum, float time)
    {
        // Set spectrum
        SetCurSpectrum(spectrum);

        // Get current spectral flux from spectrum
        SpectralFluxInfo curInfo = new SpectralFluxInfo();

        curInfo.time         = time;
        curInfo.spectralFlux = CalculateRectifiedSpectralFlux();
        spectralFluxSamples.Add(curInfo);

        // We have enough samples to detect a peak
        if (spectralFluxSamples.Count >= thresholdWindowSize)
        {
            // Get Flux threshold of time window surrounding index to process
            spectralFluxSamples[indexToProcess].unscaledThreshold = GetUnscaledFluxThreshold(indexToProcess);

            // Now that we are processed at n, n-1 has neighbors (n-2, n) to determine peak
            int indexToDetectPeak = indexToProcess - 1;

            spectralFluxSamples[indexToDetectPeak].previous = spectralFluxSamples[indexToDetectPeak - 1];
            spectralFluxSamples[indexToDetectPeak].next     = spectralFluxSamples[indexToDetectPeak + 1];

            indexToProcess++;
        }
    }
Beispiel #3
0
    public void analyzeSpectrum()
    {
        float time = spectrumGenerator.getCurTimeStamp();


        float[] mids = new float[4];


        System.Array.Copy(spectrumGenerator.getCurSpectrum(), 8, mids, 0, 4);


        // Set spectrum
        setCurSpectrum(mids);

        // Get current spectral flux from spectrum
        SpectralFluxInfo curInfo = new SpectralFluxInfo();

        curInfo.time         = time;
        curInfo.spectralFlux = calculateRectifiedSpectralFlux();
        spectralFluxSamples.Add(curInfo);

        // We have enough samples to detect a peak
        if (spectralFluxSamples.Count >= thresholdWindowSize)
        {
            // Start the real audio that the player hears
            if (!actualPlayer.isPlaying)
            {
                actualPlayer.Play();
            }


            // Get Flux threshold of time window surrounding index to process
            spectralFluxSamples[indexToProcess].threshold = getFluxThreshold(indexToProcess);

            // Only keep amp amount above threshold to allow peak filtering
            spectralFluxSamples[indexToProcess].prunedSpectralFlux = getPrunedSpectralFlux(indexToProcess);

            // Now that we are processed at n, n-1 has neighbors (n-2, n) to determine peak
            int indexToDetectPeak = indexToProcess - 1;

            bool curPeak = isPeak(indexToDetectPeak);

            if (curPeak)
            {
                spectralFluxSamples[indexToDetectPeak].isPeak = true;
            }
            if (spectralFluxSamples[indexToDetectPeak].isPeak)
            {
                MidsPeak.Invoke();
            }
            //Debug.Log(spectralFluxSamples[indexToDetectPeak].isPeak);
            indexToProcess++;
        }
    }
Beispiel #4
0
 private bool IsPeakSample(SpectralFluxInfo info)
 {
     if (info.isPeak)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #5
0
 public void VisualizePoint([NotNull] SpectralFluxInfo point, GameObject target)
 {
     if (_animator == null)
     {
         _animator = target.GetComponent <Animator>();
     }
     if (point.IsPeak)
     {
         _animator.SetTrigger("Flash");
     }
 }
Beispiel #6
0
 public void VisualizePoint([NotNull] SpectralFluxInfo point, GameObject target)
 {
     if (_animator == null)
     {
         _animator = target.GetComponent <Animator>();
     }
     if (point.IsPeak)
     {
         //target.SetActive(!target.activeSelf);
         //Spawn Object instead
         _animator.SetTrigger("Flash");
     }
 }
    // analyze a new spectrum and determine where the beats are
    public void analyzeSpectrum(float[] spectrum, float time)
    {
        // Set spectrum
        setCurSpectrum(spectrum);

        // Get current spectral flux from spectrum

        float[] newFlux = calculateRectifiedSpectralFlux();
        for (int i = 0; i < frequencyBands; i++)
        {
            SpectralFluxInfo curInfo = new SpectralFluxInfo();
            curInfo.time         = time;
            curInfo.spectralFlux = newFlux[i];
            spectralFluxSamples[i].Add(curInfo);
        }

        // We have enough samples to detect a peak
        if (spectralFluxSamples[0].Count > thresholdWindowSize)
        {
            readyToDetect = true;
            // Now that we are processed at n, n-1 has neighbors (n-2, n) to determine peak
            int indexToDetectPeak = indexToProcess - 1;
            for (int i = 0; i < frequencyBands; i++)
            {
                spectralFluxSamples[i].RemoveAt(0);

                // Get Flux threshold of time window surrounding index to process
                spectralFluxSamples[i][indexToProcess].threshold = getFluxThreshold(indexToProcess, i);

                // Only keep amp amount above threshold to allow peak filtering
                spectralFluxSamples[i][indexToProcess].prunedSpectralFlux = getPrunedSpectralFlux(indexToProcess, i);



                bool curPeak = isPeak(indexToDetectPeak, i);

                if (curPeak)
                {
                    spectralFluxSamples[i][indexToDetectPeak].isPeak = true;
                }
            }
        }
        else
        {
            Debug.Log(string.Format("Not ready yet.  At spectral flux sample size of {0} growing to {1}", spectralFluxSamples[0].Count, thresholdWindowSize));
        }
    }
    public void AnalyzeSpectrum(float[] spectrum, float time)
    {
        // Set spectrum
        SetCurrentSpectrum(spectrum);

        // Get current spectral flux from spectrum
        var currentInfo = new SpectralFluxInfo();

        currentInfo.Time         = time;
        currentInfo.SpectralFlux = CalculateRectifiedSpectralFlux();
        SpectralFluxSamples.Add(currentInfo);

        // We have enough samples to detect a peak
        if (SpectralFluxSamples.Count >= _thresholdWindowSize)
        {
            // Get Flux threshold of time window surrounding index to process
            SpectralFluxSamples[_indexToProcess].Threshold = GetFluxThreshold(_indexToProcess);

            // Only keep amp amount above threshold to allow peak filtering
            SpectralFluxSamples[_indexToProcess].PrunedSpectralFlux = GetPrunedSpectralFlux(_indexToProcess);

            // Now that we are processed at n, n-1 has neighbors (n-2, n) to determine peak
            var indexToDetectPeak = _indexToProcess - 1;

            var curPeak = IsPeak(indexToDetectPeak);

            if (curPeak)
            {
                SpectralFluxSamples[indexToDetectPeak].IsPeak = true;
            }

            _indexToProcess++;
        }
        else
        {
            Debug.Log(
                string.Format(
                    "Not ready yet.  At spectral flux sample size of {0} growing to {1}",
                    SpectralFluxSamples.Count,
                    _thresholdWindowSize));
        }
    }
Beispiel #9
0
    private void BpmAnalysis()
    {
        Sound s   = Array.Find(songs, song => song.name == PlayerPrefs.GetString("selectedSong"));
        int   bpm = GetBpm(PlayerPrefs.GetString("selectedSong"));

        Debug.Log("BPM: " + bpm);
        float totalNumOfBeats = s.clip.length / 60 * bpm;

        Debug.Log("totalNumOfBeats: " + totalNumOfBeats);
        float interval = s.clip.length / totalNumOfBeats;

        Debug.Log("interval: " + interval);
        for (float i = 1.0f; i <= totalNumOfBeats - 5.0f; i += 1.0f)
        {
            SpectralFluxInfo info = new SpectralFluxInfo();
            info.time = interval * i;
            Debug.Log("Time interval: " + info.time);
            peakOfPeakSamples.Add(info);
        }
    }
Beispiel #10
0
    public void analyzeSpectrum(float[] spectrum, float time)
    {
        // Set spectrum
        UpdateSpectrum(spectrum);

        // Get current spectral flux from spectrum
        SpectralFluxInfo curInfo = new SpectralFluxInfo();

        curInfo.time         = time;
        curInfo.spectralFlux = calculateRectifiedSpectralFlux();
        spectralFluxSamples.Add(curInfo);

        // We have enough samples to detect a peak
        //Debug.Log("sample size is " + spectralFluxSamples.Count);
        if (spectralFluxSamples.Count >= thresholdWindowSize)
        {
            // Get Flux threshold of time window surrounding index to process
            spectralFluxSamples[indexToProcess].threshold = getFluxThreshold(indexToProcess);

            // Only keep amp amount above threshold to allow peak filtering
            spectralFluxSamples[indexToProcess].prunedSpectralFlux = getPrunedSpectralFlux(indexToProcess);

            // Now that we are processed at n, n-1 has neighbors (n-2, n) to determine peak
            int indexToDetectPeak = indexToProcess - 1;

            bool curPeak = isPeak(indexToDetectPeak);

            if (curPeak)
            {
                spectralFluxSamples[indexToDetectPeak].isPeak = true;
                BPM++;
                // Debug.Log("found a peak at time " +Time.time);
            }
            indexToProcess++;
        }
        else
        {
            Debug.Log(string.Format("Not ready yet.  At spectral flux sample size of {0} growing to {1}", spectralFluxSamples.Count, thresholdWindowSize));
        }
    }
Beispiel #11
0
    //PUBLIC FUNCTIONS

    /// <summary>
    /// Analyzes spectrum data to create an array of spectral flux samples.
    /// </summary>
    public void AnalyzeSpectrum(float[] spectrumData, float time)
    {
        //Set the current spectrum
        SetCurrSpectrum(spectrumData);

        //Get the current spectral flux
        SpectralFluxInfo currInfo = new SpectralFluxInfo();

        currInfo.time         = time;
        currInfo.spectralFlux = RectifySpectralFlux();
        //Add the current spectral flux to samples
        spectralFluxSamples.Add(currInfo);

        //Check if there are enough samples to detect peaks
        if (spectralFluxSamples.Count >= thresholdSamplesNeeded)
        {
            //Get the threshold at the index
            spectralFluxSamples[indexToProcess].threshold = GetFluxThreshold(indexToProcess);

            //Get the amplitude amounr above threshold to allow for peak filtering
            spectralFluxSamples[indexToProcess].prunedSpectralFlux = PruneSpectralFlux(indexToProcess);

            //Now process prior sample
            int indexToDetect = indexToProcess - 1;

            if (IsPeak(indexToDetect))
            {
                spectralFluxSamples[indexToDetect].isPeak = true;
            }
            ++indexToProcess;
        }
        else
        {
            //Debug.Log(string.Format("Not enough samples to detect peaks. Current spectral flux sample size of {0} growing to {1}", spectralFluxSamples.Count, thresholdSamplesNeeded));
        }
    }
Beispiel #12
0
 public void VisualizePoint([NotNull] SpectralFluxInfo point)
 {
 }
Beispiel #13
0
    private void AnalyzeFullSpectrum()
    {
        try
        {
            //We only retain the samples of combined channel over the time domain
            float[] processedSamples = new float[totalNumberOfSamples];

            int   numProcessed   = 0;
            float channelAverage = 0f;

            for (int i = 0; i < multiChannelSamples.Length; ++i)
            {
                channelAverage += multiChannelSamples[i];

                //Store the average of the combined channels
                if ((i + 1) % numberOfChannels == 0)
                {
                    processedSamples[numProcessed] = channelAverage / numberOfChannels;
                    ++numProcessed;
                    channelAverage = 0f;
                }
            }

            //Debug.Log("Channel combining done. Total number of samples processed: " + processedSamples.Length);

            //Execute Fast-Fourier Transform to return the spectrum data over time
            int spectrumSampleSize = 1024;
            int iterations         = processedSamples.Length / spectrumSampleSize;

            FFT fft = new FFT();
            fft.Initialize((System.UInt32)spectrumSampleSize);

            double[] sampleChunk = new double[spectrumSampleSize];
            for (int i = 0; i < iterations; ++i)
            {
                //Grab the current 1024 chunk of audio data
                System.Array.Copy(processedSamples, i * spectrumSampleSize, sampleChunk, 0, spectrumSampleSize);

                //Apply FFT
                double[] windowCoEf        = DSP.Window.Coefficients(DSP.Window.Type.Hanning, (uint)spectrumSampleSize);
                double[] scaledSampleChunk = DSP.Math.Multiply(sampleChunk, windowCoEf);
                double   scaleFactor       = DSP.Window.ScaleFactor.Signal(windowCoEf);

                //Perform FFT and convert output to magnitude
                Complex[] fftSpectrum       = fft.Execute(scaledSampleChunk);
                double[]  scaledFFTSpectrum = DSP.ConvertComplex.ToMagnitude(fftSpectrum);
                scaledFFTSpectrum = DSP.Math.Multiply(scaledFFTSpectrum, scaleFactor);

                //Convert the chunk values to a single point within audio timeline
                float currSongtime = GetSongtime(i) * spectrumSampleSize;

                //Analyze multitude data using SpectrumAnalyzer to detect peaks
                spectrumAnalyzer.AnalyzeSpectrum(System.Array.ConvertAll(scaledFFTSpectrum, x => (float)x), currSongtime);
            }

            //Debug.Log("Spectrum Analysis completed, now getting all Peak data samples.");
            for (float i = 0f; i <= clipLength; i += 0.0167f)
            {
                //Get the index from the audio time
                int index = GetIndex(i) / spectrumAnalyzer.numSamples;

                //Only run if index returned is within range of the spectrum analyzer sample-list
                if (index < spectrumAnalyzer.spectralFluxSamples.Count)
                {
                    SpectralFluxInfo info = spectrumAnalyzer.spectralFluxSamples[index];

                    //Make sure sample is a peak
                    if (info.isPeak)
                    {
                        peakInfo.Add(info);
                    }
                }
            }

            //Debug.Log("Spectrum Analysis and Peak Sampling done. Thread completed.");
            peakInfoSize = peakInfo.Count;
            isDone       = true;
        }
        catch (System.Exception ex)
        {
            //Error logging
            Debug.LogError(ex.ToString());
        }
    }
Beispiel #14
0
 public void SpawnMonster(SpectralFluxInfo point, ObjectPooler objectPooler, string tag)
 {
     Debug.Log(tag + " - " + point.ToString());
 }
Beispiel #15
0
    public void analyzeSpectrum(float[] spectrum, float time)
    {
        // Get current spectral flux from spectrum
        SpectralFluxInfo curInfo = new SpectralFluxInfo();

        curInfo.time         = time;
        curInfo.spectralFlux = calculateRectifiedSpectralFlux(512 + 256, 8192);
        spectralFluxSamplesTreble.Add(curInfo);

        // We have enough samples to detect a peak
        if (spectralFluxSamplesTreble.Count >= thresholdWindowSize)
        {
            // Get Flux threshold of time window surrounding index to process
            spectralFluxSamplesTreble[indexToProcess].threshold = getFluxThreshold(spectralFluxSamplesTreble, indexToProcess);

            // Only keep amp amount above threshold to allow peak filtering
            spectralFluxSamplesTreble[indexToProcess].prunedSpectralFlux = getPrunedSpectralFlux(spectralFluxSamplesTreble, indexToProcess);

            // Now that we are processed at n, n-1 has neighbors (n-2, n) to determine peak
            int indexToDetectPeak = indexToProcess - 1;

            bool curPeak = isPeak(spectralFluxSamplesTreble, indexToDetectPeak);
            if (curPeak)
            {
                spectralFluxSamplesTreble[indexToDetectPeak].isPeak = true;
                {
                    int   bin       = DetermineHighestBin(pastFrequencies[pastFrequencies.Index() - thresholdWindowSize], 0, 13);
                    int   note      = GetNoteFromBin(bin);
                    float frequency = GetFrequencyFromBin(bin);
                    if (frequency == 0)
                    {
                        return;
                    }
                    //Debug.Log(string.Format("Peak: " + indexToDetectPeak + " Frequency: " + frequency + " Note: " + (NOTE_NAMES)(note % 12)));
                    note += 120;
                    pm.Get <HueManager>().SetColor(0, pm.Get <HueHelper>("HueHelperBass").colorsInOrder[note % 12]);
                    pm.Get <HueManager>().SetColor(1, pm.Get <HueHelper>("HueHelperBass").colorsInOrder[note % 12]);
                    pm.Get <HueHelper>("HueHelperBass").UpdateNote(note % 12);

                    /******************************************/
                    int   volBin        = DetermineHighestBin(pastOutputData[pastOutputData.Index() - thresholdWindowSize], 0, 13);
                    float noteIntensity = pastOutputData[pastOutputData.Index() - thresholdWindowSize][volBin];
                    //pm.Get<HueHelper>("HueHelperBass").GetComponent<ColorSpheres>().SetIntensity(note % 12, noteIntensity);
                    /******************************************/
                }
                {
                    int   bin       = DetermineHighestBin(pastFrequencies[pastFrequencies.Index() - thresholdWindowSize], 13, 200);
                    int   note      = GetNoteFromBin(bin);
                    float frequency = GetFrequencyFromBin(bin);
                    if (frequency == 0)
                    {
                        return;
                    }
                    //Debug.Log(string.Format("Peak: " + indexToDetectPeak + " Frequency: " + frequency + " Note: " + (NOTE_NAMES)(note % 12)));
                    note += 120;
                    pm.Get <HueManager>().SetColor(2, pm.Get <HueHelper>("HueHelperTreble").colorsInOrder[note % 12]);
                    pm.Get <HueManager>().SetColor(3, pm.Get <HueHelper>("HueHelperTreble").colorsInOrder[note % 12]);
                    pm.Get <HueHelper>("HueHelperTreble").UpdateNote(note % 12);

                    /******************************************/
                    int   volBin        = DetermineHighestBin(pastOutputData[pastOutputData.Index() - thresholdWindowSize], 13, 200);
                    float noteIntensity = pastOutputData[pastOutputData.Index() - thresholdWindowSize][volBin];
                    //pm.Get<HueHelper>("HueHelperTreble").GetComponent<ColorSpheres>().SetIntensity(note % 12, noteIntensity);
                    /******************************************/
                }
            }
        }
    }
 public void VisualizePoint(SpectralFluxInfo point, GameObject target)
 {
     throw new System.NotImplementedException();
 }
 public void VisualizePoint(SpectralFluxInfo point)
 {
     Debug.Log(point.ToString());
 }
Beispiel #18
0
    private void SpawnNotes()
    {
        // spawn the notes only if the onset detector is ready
        if (onsetDetection.readyToDetect)
        {
            // bass
            if (Time.time > timeNextSpawn[3])
            {
                SpectralFluxInfo sample = onsetDetection.spectralFluxSamples[0][onsetDetection.indexToProcess - 2];
                if (sample.isPeak)
                {
                    GameObject selectedLane = laneSpawns[3];
                    selectedLane.GetComponent <NoteSpawner>().SpawnNote(spawnDistance);
                    timeNextSpawn[3] = 1f / spawnrate + Time.time;
                }
            }

            // snare
            if (Time.time > timeNextSpawn[1])
            {
                SpectralFluxInfo sample = onsetDetection.spectralFluxSamples[2][onsetDetection.indexToProcess - 2];
                if (sample.isPeak)
                {
                    GameObject selectedLane = laneSpawns[1];
                    selectedLane.GetComponent <NoteSpawner>().SpawnNote(spawnDistance);
                    timeNextSpawn[1] = 1f / spawnrate + Time.time;
                }
            }

            // tomf
            if (Time.time > timeNextSpawn[5])
            {
                SpectralFluxInfo sample = onsetDetection.spectralFluxSamples[5][onsetDetection.indexToProcess - 2];
                if (sample.isPeak)
                {
                    GameObject selectedLane = laneSpawns[5];
                    selectedLane.GetComponent <NoteSpawner>().SpawnNote(spawnDistance);
                    timeNextSpawn[5] = 1f / spawnrate + Time.time;
                }
            }

            // crash
            if (Time.time > timeNextSpawn[0])
            {
                SpectralFluxInfo sample = onsetDetection.spectralFluxSamples[7][onsetDetection.indexToProcess - 2];
                if (sample.isPeak)
                {
                    GameObject selectedLane = laneSpawns[0];
                    selectedLane.GetComponent <NoteSpawner>().SpawnNote(spawnDistance);
                    timeNextSpawn[0] = 1f / spawnrate + Time.time;
                }
            }

            /*
             * for (int i = 0; i < 1; i++)
             * {
             *  SpectralFluxInfo sample = onsetDetection.spectralFluxSamples[i][onsetDetection.indexToProcess - 2];
             *  if (sample.isPeak)
             *  {
             *      GameObject selectedLane = laneSpawns[i];
             *      selectedLane.GetComponent<NoteSpawner>().SpawnNote(spawnDistance);
             *      timeNextSpawn = 1f / spawnrate + Time.time;
             *  }
             * }*/
        }
    }
Beispiel #19
0
 public void VisualizePoint(SpectralFluxInfo point, GameObject target)
 {
     Debug.Log(target.ToString() + " - " + point.ToString());
 }