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();
        }
    }
Example #2
0
    private IEnumerator AsyncAnalyze(int frames)
    {
        frames = Mathf.Clamp(frames, 0, totalFrames);
        //AudioClip.GetData only works on the main thread
        float[] s = new float[frames * frameSpacing * 2];
        audioSource.clip.GetData(s, 0);

#if NETFX_CORE
        IAsyncAction action = Windows.System.Threading.ThreadPool.RunAsync(workItem => BackGroundAnalyze(s));

        while (action.Status == AsyncStatus.Started)
        {
            yield return(null);
        }
#else
        Thread analyzeThread = new Thread(BackGroundAnalyze);
        analyzeThread.Start(s);

        while (analyzeThread.IsAlive)
        {
            yield return(null);
        }
#endif
        //if a new song is queued at this point, stop
        if (queueRoutine != null)
        {
            analyzeRoutine = null;
            yield break;
        }

        _lastFrame = frames - 1;

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

        if (lastFrame > totalFrames - 2)
        {
            EndOfAnalysis();
        }

        songLoaded = true;

        analyzeRoutine = null;

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