本文整理匯總了C#中UnityEngine.AudioSource.GetSpectrumData方法的典型用法代碼示例。如果您正苦於以下問題:C# AudioSource.GetSpectrumData方法的具體用法?C# AudioSource.GetSpectrumData怎麽用?C# AudioSource.GetSpectrumData使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類UnityEngine.AudioSource
的用法示例。
在下文中一共展示了AudioSource.GetSpectrumData方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: GetSpectrumData
/**
* <summary>AudioSourceの高速フーリエ変換結果を返す。</summary>
* <param name="audio">解析対象のAudioSource</param>
* <returns>周波數 -> パワー のPairのList</returns>
*/
public static List<KeyValuePair<float, float>> GetSpectrumData(AudioSource audio)
{
float[] spectrum = new float[NumSamples];
audio.GetSpectrumData(spectrum, 0, Window);
List<KeyValuePair<float, float>> result = new List<KeyValuePair<float,float>>();
for (int i = 0; i < spectrum.Length; i++)
{
float frequency = ((float)AudioSettings.outputSampleRate * i) / (2.0f * spectrum.Length);
result.Add(new KeyValuePair<float, float>(frequency, spectrum[i]));
}
return result;
}
示例2: GetFundamentalFrequency
public const FFTWindow Window = FFTWindow.BlackmanHarris; // フーリエ変換で用いる窓関數
#endregion Fields
#region Methods
/**
* <summary>AudioSourceの再生箇所の基本周波數を返す。</summary>
* <param name="audio">解析対象のAudioSource</param>
* <returns>基本周波數</returns>
*/
public static float GetFundamentalFrequency(AudioSource audio)
{
float[] spectrum = new float[NumSamples];
audio.GetSpectrumData(spectrum, 0, Window);
// 最大パワーを見つける
int maxIndex = Utility.ArrayArgmax<float>(spectrum);
// 基本周波數の候補とするパワー閾値を算出
float threshold = (float)System.Math.Log(spectrum[maxIndex] + 1.0f) / 10.0f;
// 低い周波數から、閾値を超えるパワーを持つピーク點を求める。
// それを基本周波數とする(いろいろ問題ありそうなアルゴリズムだ)。
int fundamentalIndex = maxIndex;
for (int i = 1; i < spectrum.Length - 1; i++)
{
if (spectrum[i - 1] <= spectrum[i] && spectrum[i] >= spectrum[i + 1])
{
float power = (float)System.Math.Log(spectrum[i] + 1.0f);
if (power > threshold)
{
fundamentalIndex = i;
break;
}
}
}
float freqIndex = (float)fundamentalIndex;
if (fundamentalIndex > 0 && fundamentalIndex < spectrum.Length - 1)
{
float dr = spectrum[fundamentalIndex + 1] / spectrum[fundamentalIndex];
float dl = spectrum[fundamentalIndex - 1] / spectrum[fundamentalIndex];
freqIndex += 0.5f * (dr * dr - dl * dl);
}
return freqIndex * AudioSettings.outputSampleRate / 2.0f / spectrum.Length;
}
示例3: GetAudioFrequency
/// <summary>
/// Gets the audio frequency.
/// </summary>
/// <returns>The audio frequency.</returns>
/// <param name="audio">Audio.</param>
public static float GetAudioFrequency(AudioSource audio)
{
float fundamentalFrequency = 0.0f;
float[] data = new float[1024];
audio.GetSpectrumData (data, 0, FFTWindow.BlackmanHarris);
float s = 0.0f;
int i = 0;
for (int j = 1; j < data.Length; j++) {
if (s < data [j]) {
s = data [j];
i = j;
}
}
fundamentalFrequency = i * _sampleRate / data.Length;
return fundamentalFrequency;
}
示例4: GetSpectrum
public static float[] GetSpectrum(AudioSource audio, int samples, float[] spectrumArray) {
if(spectrumArray == null || spectrumArray.Length <= 0) spectrumArray = new float[samples];
audio.GetSpectrumData (spectrumArray, 0, FFTWindow.Rectangular);
return spectrumArray;
}