public void Update(GameTime gameTime) { system.update(); if (IsSongPlaying()) { uint length = 0; uint position = 0; CurrentSong.getLength(out length, FMOD.TIMEUNIT.MS); Channel.getPosition(out position, FMOD.TIMEUNIT.MS); if (position == length) { NextSongEvent.Invoke(); } } #region Update System & FFT Algorithm if (FFTEnabled) { // Perform a Fast Fourier Transform int windowSize = spectrum.Length; //Samples. IntPtr data; uint length; //hanning bpm MyDSP.setParameterInt((int)FMOD.DSP_FFT.WINDOWTYPE, (int)FMOD.DSP_FFT_WINDOW.HAMMING); MyDSP.setParameterInt((int)FMOD.DSP_FFT.WINDOWSIZE, windowSize); MyDSP.getParameterData((int)FMOD.DSP_FFT.SPECTRUMDATA, out data, out length); MyDSP.getParameterFloat((int)FMOD.DSP_FFT.DOMINANT_FREQ, out higher_freq); fftParameter = (FMOD.DSP_PARAMETER_FFT)Marshal.PtrToStructure(data, typeof(FMOD.DSP_PARAMETER_FFT)); #region BPMDetection if (fftParameter.spectrum.Length != 0) { for (int i = 0; i < windowSize; i++) { if (fftParameter.spectrum.Length == 2) { spectrum[i] = (fftParameter.spectrum[0][i] + fftParameter.spectrum[1][i]) / 2f; } } } previousSpectrum = spectrum; #endregion } #endregion }
public static void Update(GameTime gameTime) { system.update(); if (IsSongPlaying()) { uint length = 0; uint position = 0; CurrentSong.getLength(out length, FMOD.TIMEUNIT.MS); Channel.getPosition(out position, FMOD.TIMEUNIT.MS); if (position == length) { NextSongEvent.Invoke(); } // BPM Method 1 (More research) #region Update System & FFT Algorithm if (FFTEnabled) { // Perform a Fast Fourier Transform int windowSize = spectrum.Length; //Samples. IntPtr data; uint fft_length; //hanning bpm MyDSP.setParameterInt((int)FMOD.DSP_FFT.WINDOWTYPE, (int)FMOD.DSP_FFT_WINDOW.HAMMING); MyDSP.setParameterInt((int)FMOD.DSP_FFT.WINDOWSIZE, windowSize); MyDSP.getParameterData((int)FMOD.DSP_FFT.SPECTRUMDATA, out data, out fft_length); MyDSP.getParameterFloat((int)FMOD.DSP_FFT.DOMINANT_FREQ, out higher_freq); fftParameter = (FMOD.DSP_PARAMETER_FFT)Marshal.PtrToStructure(data, typeof(FMOD.DSP_PARAMETER_FFT)); #region BPMDetection if (fftParameter.spectrum.Length != 0) { for (int i = 0; i < windowSize; i++) { if (fftParameter.spectrum.Length == 2) { spectrum[i] = (fftParameter.spectrum[0][i] + fftParameter.spectrum[1][i]) / 2f; } } } previousSpectrum = spectrum; #endregion } #endregion // BPM Method 2 (Working! but with small sync bugs) float bps = (60f / BPM); bpm_elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds; if (bpm_elapsed > bps) { gotBeat = true; offBeatDelay += (float)gameTime.ElapsedGameTime.TotalSeconds; if (offBeatDelay > bps) { gotBeat = false; bpm_elapsed = 0f; offBeatDelay = 0f; } } } Channel.setVolume(masterVolume * musicVolume); }