using CriWare; using CriWare.Assets; using System; using System.Collections.Generic; using UnityEngine; public class AnalyerGenData : MonoBehaviour { // Start is called before the first frame update private CriAtomExOutputAnalyzer analyzer; float playtime; private List list; CriAtomSourceForAsset atomAsset; public Action> onPlay; bool islist = false; void Start() { //EditorAudioManager.Instance.Init(); //EditorAudioManager.Instance.Start(); nowtime = 0; list = new List(); analyzer = new CriAtomExOutputAnalyzer(NewConfig()); GenOnePeriodData(); } public async void SetOnPlay(Action> action, int time, string musicName) { //ResetData(); //AttachAnalyzer(); playtime = time; onPlay = action; // EditorAudioManager.Instance.RegisterAnalyzer(analyzer); //await EditorAudioManager.Instance.PlayAudio("DemoProj_M1"); atomAsset = GameObject.Find("Audio(Singleton)").GetComponent(); DebugUtil.Log("时间" + atomAsset.time); //atomAsset.Play("bgm_01"); islist = true; } void GenOnePeriodData() { float[] temp3 = new float[16]; for (int i = 0; i < 16; i++) { float y = amplitude * Mathf.Sin(i * (2 * (Mathf.PI / 16))); y = y + 2; temp3[i] = y; } temp3[temp3.Length - 1] = 2; list.Add(temp3); float[] lastArray = temp3; for (int i = 0; i < 16; i++) { float[] newArray =new float[16]; for (int j = 0; j < 16; j++) { newArray[(j + 1) % 16] = lastArray[j]; } for (int k = 0; k < 16; k++) { lastArray[k] = (newArray[k]); } list.Add(newArray); } } void GetNewOnePeriodData() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 16; j++) { } } } private bool SplitBandsOnLogScale(ref float[] iLinearLvs, ref float[] oLogLvs) { if (iLinearLvs == null || oLogLvs == null) return false; float lowBand, highBand, tmpVal; int lowBandIdx, highBandIdx, i, j; int iBandCnt = iLinearLvs.Length; int oBandCnt = oLogLvs.Length; for (i = 0; i < oBandCnt; ++i) { /* 指定の対数スケールバンドに対応する、リニア周波数軸上の範囲を求める */ lowBand = Mathf.Pow((float)iBandCnt, (float)i / (float)oBandCnt); highBand = Mathf.Pow((float)iBandCnt, (float)(i + 1) / (float)oBandCnt); /* 両端を丸める */ lowBandIdx = Mathf.FloorToInt(lowBand) - 1; highBandIdx = Mathf.FloorToInt(highBand) - 1; /* 範囲内のレベルの平均値を求める */ tmpVal = 0; for (j = lowBandIdx; j <= highBandIdx; ++j) { tmpVal += iLinearLvs[j]; } tmpVal = tmpVal / (float)(highBandIdx - lowBandIdx + 1); /* 対数スケールのレベル値を出力する */ oLogLvs[i] = tmpVal; } return true; } void ResetData() { list.Clear(); nowtime = 0; } void AttachAnalyzer() { atomAsset.AttachToAnalyzer(analyzer); } CriWare.CriAtomExOutputAnalyzer.Config NewConfig() { CriWare.CriAtomExOutputAnalyzer.Config config = new CriAtomExOutputAnalyzer.Config(); config.enableSpectrumAnalyzer = true; config.numSpectrumAnalyzerBands = 16; return config; } float[] levels = new float[16]; float nowtime; // Update is called once per frame public float amplitude = 1.0f; // 振幅 public float frequency = 1.0f; // 频率 public float speed = 1.0f; // 波形变化速度 public int minAmplitude = 1; // 最小振幅 public int maxAmplitude = 5; // 最大振幅 void Update() { if (!islist) { return; } //EditorAudioManager.Instance.Update(Time.deltaTime); //if (nowtime <= playtime) ////{ //// float[] temp = new float[16]; //// float[] logSpectrumLevels = new float[16]; //// analyzer.GetSpectrumLevels(ref temp); //// SplitBandsOnLogScale(ref temp, ref logSpectrumLevels); //// //for (int i = 0; i < temp.Length; i++) //// //{ //// // if (temp[i] * 100 > 10) //// // { //// // temp[i] = Mathf.Clamp((2 + temp[i] * 100), 1, 5); //// // } //// // else //// // { //// // temp[i] = Mathf.Clamp((2 - temp[i] * 100), 1, 5); //// // } //// // //temp[i] = (Mathf.Clamp((10 * Mathf.Log10(temp[i])), 1, 5)); //// //} //// list.Add(logSpectrumLevels); //// nowtime += Time.deltaTime; //} //else //{ if (islist) { onPlay(list); islist = false; // EditorAudioManager.Instance.StopAllMusic(); // } } } private void OnDestroy() { //EditorAudioManager.Instance.UnregisterAnalyzer(GameObject.Find("Audio(Singleton)"), analyzer); } }