Exemplo n.º 1
0
 private void SetInstance()
 {
     if (AudioSampler.Instance == null)
     {
         _instance = this;
     }
     else
     {
         Debug.LogError("Another AudioSampler instance already exists. Destroy existing before switching instances");
     }
 }
Exemplo n.º 2
0
        public static void InstantiateSampler(AudioSamplerType toSwitchTo)
        {
            if (AudioSampler.Instance != null)
            {
                DestroyImmediate(AudioSampler.Instance);
            }


            if (_samplerGo == null)
            {
                _samplerGo = new GameObject("AudioSampler");
            }


            AudioSampler addedSampler = null;

            switch (toSwitchTo)
            {
            case AudioSamplerType.Local:
            {
                addedSampler = _samplerGo.AddComponent <AudioSamplerLocal> ();
            }
            break;

            case AudioSamplerType.Stream:
            {
                addedSampler = _samplerGo.AddComponent <AudioSamplerStream> ();
            }
            break;
            }

            if (addedSampler != null)
            {
                addedSampler.SetInstance();
            }
            else
            {
                Debug.LogError("Unable to switch to specified audio sampler type. Setting type to None instead");
            }
        }
Exemplo n.º 3
0
        private void Update()
        {
            double[] audioSamples     = samplerInstance.GetSamples();
            double[] currentFFTOutput = samplerInstance.GetFFTOutputBins(ref audioSamples);

            if (currentFFTOutput == null || currentFFTOutput.Length == 0)
            {
                return;
            }

            if (_currentFFTOutput == null || _currentFFTOutput.Length == 0 || _currentFFTOutput.Length != currentFFTOutput.Length)
            {
                _currentFFTOutput = new double[currentFFTOutput.Length];
            }


            for (int i = 0; i < _currentFFTOutput.Length; ++i)
            {
                _currentFFTOutput [i] = currentFFTOutput [i];
            }

            AudioSampler.NormalizeFFTOutput(ref _currentFFTOutput, AudioSampler.Instance.ClipFrequency);

            for (int i = 0; i < _currentFFTOutput.Length; ++i)
            {
                if (_currentFFTOutput [i] < _debugMinEnergy)
                {
                    _debugMinEnergy = _currentFFTOutput [i];
                }
                else if (_currentFFTOutput [i] > _debugMaxEnergy)
                {
                    _debugMaxEnergy = _currentFFTOutput [i];
                }
            }

            for (int i = 0; i < _registeredEvents.Count; ++i)
            {
                BaseTemporalConductorEvent temporalEvent = _registeredEvents[i];

                //remove destroyed Events
                if (temporalEvent.Type == BaseTemporalConductorEvent.EventType.None)
                {
                    _registeredEvents.RemoveAt(i);
                    --i;
                    continue;
                }

                float energy     = (float)RetrieveEnergyInRange(ref _currentFFTOutput, temporalEvent.FrequencyRange, 44100);
                float prevEnergy = (float)RetrieveEnergyInRange(ref _prevFFTOutput, temporalEvent.FrequencyRange, 44100);
                float delta      = energy - prevEnergy;

                switch (temporalEvent.Type)
                {
                case BaseTemporalConductorEvent.EventType.Stream:
                {
                    temporalEvent.Invoke(energy, delta);
                }
                break;

                case BaseTemporalConductorEvent.EventType.Beat:
                {
                    BeatEvent beatEvent = (BeatEvent)temporalEvent;
                    beatEvent.UpdateBeatEvent(energy);

                    float minNormalizedDelta = beatEvent.MinNormalizedDelta;
                    if (minNormalizedDelta <= 0)
                    {
                        break;
                    }

                    float traversalVal = ((1 - beatEvent.AverageEnergy) * minNormalizedDelta);
                    bool  isBeat       = delta > traversalVal;

                    //if delta is greater than the specified threshold, invoke event
                    if (isBeat)
                    {
                        temporalEvent.Invoke(energy, delta);
                    }
                }
                break;
                }
            }
        }