【发布时间】:2016-03-23 14:03:33
【问题描述】:
我正在创建一个使用 Unity5 游戏引擎对振幅做出反应的音频可视化器。我使用对数、RMS 值和 .GetOutputData 计算幅度。
最初我从 Update 类中调用了我的函数,但它的反应时间太慢了,并且可视化器似乎与音乐不同步。我现在从 OnGUI 类调用我的函数,但它仍然不是对音乐的实时反应。
我应该何时或如何调用我的函数来获得最佳的实时音频可视化?有更好的方法吗?
感谢您的帮助。
我的可视化功能:
void OnGUI ()
{
//PROCESS AMPLITUDE:
/***/
///RETRIEVE THE AUDIO-SOURCE SAMPLES:
//Declare and Initialize an array to store the samples;
float[] samples = new float[iSamples];
//Pass the samples' array to the audio-source;
auTheCurrentSong.GetOutputData (samples, 0);
///CALCULATE THE RMS VOLTAGE VALUE:
//Declare and Initialize the sum of the squared samples;
float fTotalSquaredSamples = 0.0f;
//FOR each sample in the samples' array...
for (int counter=0; counter < iSamples; counter++) {
//... Calculate the sum of the squared samples;
fTotalSquaredSamples += Mathf.Pow (samples [counter], 2);
}
//Calculate the average or mean of the total squared samples;
float fMeanSquaredSamples = fTotalSquaredSamples/iSamples;
//Calc ulate the root mean square (RMS) value;
float fRMS = Mathf.Sqrt (fMeanSquaredSamples);
//CALCULATE THE DECIBEL VALUE FOR OUTPUT:
//Calculate the decibel value;
float fdBValue = 20*Mathf.Log10(fRMS/fReference);
//Clamp dB values:
//IF dB values are less than -160...
if (fdBValue < -160)
{
//...Clamp dB values to -160;
fdBValue = -160;
}
//RESPOND TO PRODUCT:
//Debug the decibel values to check for irregularities;
Debug.Log (fdBValue);
//Scale a 3-D cube vertically depending on the dB value;
transform.localScale = new Vector3 (1.0f, fdBValue, 0.0f);
/***/
}
【问题讨论】:
-
如果任何答案对您有用,如果您接受该答案,对其他人会有所帮助。如果没有,请随时评论给出的答案,或更新您的问题。
-
我目前正在处理多个项目,现在没有时间关注这个特定的项目。并且因为我暂时无法删除或隐藏此问题,您只需要等到我可以返回此线程并提供反馈。同时,如果您有同样的问题,请查看是否有任何答案适合您,然后如果你希望你可以分享你的结果。 :) 否则请耐心等待。
标签: c# audio unity3d unityscript