public class ExampleScript : MonoBehaviour {
public CriAtomSource atomSource;
public int outputSpectrumBands = 8;
private CriAtomExPlayerOutputAnalyzer analyzer;
private float[] linearSpectrumLevels;
private float[] logSpectrumLevels;
void Start ()
{
CriAtomExPlayerOutputAnalyzer.Type[] analyzerType = new CriAtomExPlayerOutputAnalyzer.Type[1];
CriAtomExPlayerOutputAnalyzer.Config[] analyzerConfig = new CriAtomExPlayerOutputAnalyzer.Config[1];
analyzerType[0] = CriAtomExPlayerOutputAnalyzer.Type.SpectrumAnalyzer;
analyzerConfig[0] = new CriAtomExPlayerOutputAnalyzer.Config(CriAtomExPlayerOutputAnalyzer.MaximumSpectrumBands);
this.analyzer = new CriAtomExPlayerOutputAnalyzer(analyzerType, analyzerConfig);
this.atomSource.AttachToAnalyzer(analyzer);
linearSpectrumLevels = new float[CriAtomExPlayerOutputAnalyzer.MaximumSpectrumBands];
logSpectrumLevels = new float[outputSpectrumBands];
}
void OnDestroy()
{
this.analyzer.DetachExPlayer();
this.analyzer.Dispose();
}
void Update ()
{
analyzer.GetSpectrumLevels(ref linearSpectrumLevels);
SplitBandsOnLogScale(ref linearSpectrumLevels, ref logSpectrumLevels);
for (int i = 0; i < logSpectrumLevels.Length; i++) {
logSpectrumLevels[i] = 20.0f * Mathf.Log10(logSpectrumLevels[i]);
}
}
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;
}
public void GetSpectrumLevels(out float[] result)
{
result = this.logSpectrumLevels;
}
}