void LoadNextSong() { string sMusicDirectory = Path.Combine(App.UserPath(), "Music"); if (Directory.Exists(sMusicDirectory)) { string[] aFiles = Directory.GetFiles(sMusicDirectory); for (int i = 0; i < aFiles.Length; ++i) { int iIndex = m_LoadMusicIndex + i; iIndex %= aFiles.Length; ++m_LoadMusicIndex; if (File.Exists(aFiles[iIndex])) { print("Loading: " + aFiles[iIndex]); EnableMic(false); m_LoadMusicWWW = new WWW("file:///" + aFiles[iIndex]); m_CurrentLoadMusicState = LoadMusicState.WaitingForWWW; return; } } } // No clips found-- enable the mic m_CurrentLoadMusicState = LoadMusicState.MicMode; EnableMic(true); }
public void Activate(bool bActive) { m_Active = bActive; if (bActive) { //start by looking for music m_CurrentLoadMusicState = LoadMusicState.Playing; LoadNextSong(); m_Reaktor.enabled = true; } else { //turn off audio and the mic m_AudioSource.Stop(); EnableMic(false); m_Reaktor.enabled = false; } }
void Update() { if (m_Active) { //get fft and wave form data from unity/fmod m_AudioSource.GetOutputData(m_WaveFormFloats, 0); m_AudioSource.GetSpectrumData(m_SpectrumFloats, 0, FFTWindow.BlackmanHarris); m_SpectrumFloats.CopyTo(m_SpectrumFloatsTempArray, 0); int iHalfCapture = m_CaptureSize / 2; float fHalfFFTScale = 0.5f; for (int i = 0; i < iHalfCapture; ++i) { float fNormalized = (m_SpectrumFloatsTempArray[i * 2] + m_SpectrumFloatsTempArray[i * 2 + 1]) * fHalfFFTScale; m_SpectrumFloats[i] = fNormalized; m_SpectrumFloats[i + iHalfCapture] = fNormalized; } for (int i = 0; i < m_CaptureSize; ++i) { // Maybe the envelope should be a lookup function float fEnvelope = 1.0f - Mathf.Pow(Mathf.Abs(((float)i / (float)m_CaptureSize) - 0.5f) * 2, 3.0f); m_WaveFormFloatsSmooth[i] = Mathf.Lerp(m_WaveFormFloatsSmooth[i], m_WaveFormFloats[i], m_SmoothLerp) * fEnvelope; m_WaveFormRow[i].a = m_WaveFormFloatsSmooth[i] + 0.5f; } UpdateShaders(); if (m_CurrentLoadMusicState == LoadMusicState.Playing) { if (!m_AudioSource.isPlaying) { //loop music LoadNextSong(); } } else if (m_CurrentLoadMusicState == LoadMusicState.WaitingForWWW) { if (m_LoadMusicWWW.isDone) { if (m_LoadMusicWWW.GetAudioClip() == null) { LoadNextSong(); } else { m_AudioSource.clip = m_LoadMusicWWW.GetAudioClip(false); m_CurrentLoadMusicState = LoadMusicState.WaitingForAudio; } } } else if (m_CurrentLoadMusicState == LoadMusicState.WaitingForAudio) { if (m_AudioSource.clip.loadState == AudioDataLoadState.Loaded) { if (AudioManager.Enabled) { m_AudioSource.Play(); } m_CurrentLoadMusicState = LoadMusicState.WaitingForStart; } } else if (m_CurrentLoadMusicState == LoadMusicState.WaitingForStart) { if (m_AudioSource.isPlaying) { m_CurrentLoadMusicState = LoadMusicState.Playing; } } else if (m_CurrentLoadMusicState == LoadMusicState.MicMode) { m_MicResetCountdown -= Time.deltaTime; if (m_MicResetCountdown <= 0.0f) { EnableMic(false); EnableMic(true); } #if !UNITY_ANDROID && !UNITY_WEBGL //start playing on audio source when mic is ready to go if (Microphone.IsRecording("") && (Microphone.GetPosition("") > 0) && !m_AudioSource.isPlaying) { m_AudioSource.Play(); } #endif } m_WaveFormTexture.SetPixels(0, 0, m_WaveFormTextureWidth, 1, m_WaveFormRow); m_WaveFormTexture.Apply(); Shader.SetGlobalTexture("_WaveFormTex", m_WaveFormTexture); } }