void Start() { audioSource = GetComponent <AudioSource>(); _lightBarController = GameObject.FindWithTag("LightBar").GetComponent <LightBarController>(); // Process audio as it plays realTimeSpectrum = new float[1024]; realTimeSpectralFluxAnalyzer = new SpectralFluxAnalyzer(Threshold); this.sampleRate = AudioSettings.outputSampleRate; // Preprocess entire audio file upfront preProcessedSpectralFluxAnalyzer = new SpectralFluxAnalyzer(Threshold); // Need all audio samples. If in stereo, samples will return with left and right channels interweaved // [L,R,L,R,L,R] multiChannelSamples = new float[audioSource.clip.samples * audioSource.clip.channels]; numChannels = audioSource.clip.channels; numTotalSamples = audioSource.clip.samples; clipLength = audioSource.clip.length; // We are not evaluating the audio as it is being played by Unity, so we need the clip's sampling rate this.sampleRate = audioSource.clip.frequency; audioSource.clip.GetData(multiChannelSamples, 0); Debug.Log("GetData done"); Thread bgThread = new Thread(this.getFullSpectrumThreaded); Debug.Log("Starting Background Thread"); bgThread.Start(); }
void Update() { if (_lightBarController == null) { _lightBarController = GameObject.FindGameObjectWithTag("LightBar").GetComponent <LightBarController>(); } // Real-time if (!finishedSampling) { audioSource.GetSpectrumData(realTimeSpectrum, 0, FFTWindow.BlackmanHarris); realTimeSpectralFluxAnalyzer.analyzeSpectrum(realTimeSpectrum, audioSource.time); _lightBarController.UpdateState(realTimeSpectralFluxAnalyzer.spectralFluxSamples); } // Preprocessed if (finishedSampling) { int indexToPlot = getIndexFromTime(audioSource.time) / 1024; _lightBarController.UpdateState(preProcessedSpectralFluxAnalyzer.spectralFluxSamples, indexToPlot); } }