IEnumerator StartAnalyze()
    {
        foreach (Analysis analysis in analyses)
        {
            analysis.Init(totalFrames);
        }

        beatTracker.Init(frameLength);
        segmenter.Init();

        int count = preAnalyze ? totalFrames : lead + analysisWidth;

        float[] samples = new float[count * frameSpacing * channels];
        audioClip.GetData(samples, 0);

        analyze = new Thread(() => Analyze(samples));
        analyze.Start();

        while (analyze.IsAlive)
        {
            yield return(null);
        }

        if (_trackBeat)
        {
            beatTracker.FillStart();
        }

        if (lastFrame == totalFrames - 1)
        {
            OnAnalysisDone();
        }
    }
Exemple #2
0
    private void LoadNewSong(AudioClip audioClip)
    {
        audioSource.Stop();
        audioSource.clip = audioClip;

        totalSamples  = audioSource.clip.samples;
        totalSamples -= totalSamples % frameSpacing;
        totalFrames   = totalSamples / frameSpacing;
        _frameLength  = 1 / ((float)audioSource.clip.frequency / (float)frameSpacing);

        foreach (Analysis s in analyses)
        {
            s.Init(totalFrames);
        }

        beatTracker.Init(frameLength);
        segmenter.Init();

        analysisDone = false;

        currentFrame  = 0;
        _lastFrame    = 0;
        lastDataFrame = 0;

        songLoaded = false;

        if (!_preCalculate)
        {
            _storeAnalyses = false;
            analyzeRoutine = StartCoroutine(AsyncAnalyze(_lead + 300));
        }
        else
        {
            _lead = 300;
            if (_storeAnalyses)
            {
                if (File.Exists(Application.persistentDataPath + Path.DirectorySeparatorChar + audioSource.clip.name + ".rthm"))
                {
                    SongData songData = SongData.Deserialize(audioSource.clip.name);

                    if (songData.length == totalFrames)
                    {
                        _lastFrame   = totalFrames;
                        analysisDone = true;

                        foreach (AnalysisData data in songData.analyses)
                        {
                            foreach (Analysis a in analyses)
                            {
                                if (a.name == data.name)
                                {
                                    a.Init(data);
                                }
                            }
                        }

                        beatTracker.Init(songData);
                        segmenter.Init(songData);

                        songLoaded = true;

                        InitializeEventProviders();
                        gameObject.SendMessage("OnReadyToPlay", SendMessageOptions.DontRequireReceiver);

                        return;
                    }
                }
            }
            analyzeRoutine = StartCoroutine(AsyncAnalyze(totalFrames));
        }
    }