本文整理汇总了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;
}