GetSpectrumData() private method

private GetSpectrumData ( int numSamples, int channel, FFTWindow window ) : float[]
numSamples int
channel int
window FFTWindow
return float[]
コード例 #1
0
ファイル: uVisualizer.cs プロジェクト: MrFlyingChip/FlashLive
 System.Collections.IEnumerator updateViz()
 {
     while (true)
     {
         theAudioSource.GetSpectrumData(_SpectrumLeft, 0, myFFTWindow);
         updateSectrumViz(_SpectrumLeft);
         yield return(new WaitForFixedUpdate());
     }
 }
コード例 #2
0
 /**
  * <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;
 }
コード例 #3
0
 static int GetSpectrumData(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 4);
         UnityEngine.AudioSource obj = (UnityEngine.AudioSource)ToLua.CheckObject <UnityEngine.AudioSource>(L, 1);
         float[] arg0 = ToLua.CheckNumberArray <float>(L, 2);
         int     arg1 = (int)LuaDLL.luaL_checkinteger(L, 3);
         UnityEngine.FFTWindow arg2 = (UnityEngine.FFTWindow)ToLua.CheckObject(L, 4, TypeTraits <UnityEngine.FFTWindow> .type);
         obj.GetSpectrumData(arg0, arg1, arg2);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
コード例 #4
0
 static public int GetSpectrumData(IntPtr l)
 {
     try {
         UnityEngine.AudioSource self = (UnityEngine.AudioSource)checkSelf(l);
         System.Single[]         a1;
         checkType(l, 2, out a1);
         System.Int32 a2;
         checkType(l, 3, out a2);
         UnityEngine.FFTWindow a3;
         checkEnum(l, 4, out a3);
         self.GetSpectrumData(a1, a2, a3);
         return(0);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
コード例 #5
0
    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;
    }
コード例 #6
0
ファイル: Flapi.cs プロジェクト: breathinggames/bg_bloid
        /// <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;
        }
コード例 #7
0
 // Update is called once per frame
 void BuildSend()
 {
     theAudioSource.GetSpectrumData(_Spectrum, 0, myFFTWindow);
     send_data(_Spectrum);
 }
コード例 #8
0
		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;
		}
コード例 #9
0
ファイル: AudioPreprocessor.cs プロジェクト: sulai1/VFX
        void Update()
        {
            if (spectrum == null || data.Length != windowSize)
            {
                data     = new float[windowSize];
                spectrum = new float[windowSize];
            }
            if (Buffer == null || Buffer.Length != bufferSize)
            {
                CreateBuffer();
            }
            if (audioSource != null)
            {
                //audioSource.GetOutputData(data, 0);
                //// Get Wave
                int offsetSamples = (audioSource.timeSamples > data.Length) ? audioSource.timeSamples - data.Length : 0;
                audioSource.clip.GetData(data, offsetSamples);

                //float[] real = new float[windowSize * 2];
                //float[] imaginary = new float[windowSize * 2];
                //for (int i = 0; i < windowSize; i++)
                //    real[i] = data[i];
                //for (int i = windowSize; i < windowSize * 2; i++)
                //    if (!Buffer.IsEmpty)
                //        real[i] = data[i-windowSize];
                //    else
                //        real[i] = 0;
                //for (int i = 0; i < real.Length; i++)
                //    real[i] *= 0.5f * (1f - Mathf.Cos(i / (float)windowSize / 2f));
                //MathNet.Numerics.IntegralTransforms.Fourier.Forward(real,imaginary);
                //for (int i = 0; i < windowSize * .012f; i++)
                //{
                //    spectrum[i] = (float)real[i];
                //}
                audioSource.GetSpectrumData(spectrum, 0, FFTWindow.Rectangular);

                Peak  = Mathf.Max(Peak, data.Max(x => Mathf.Abs(x)));
                FPeak = Mathf.Max(FPeak, spectrum.Max());
                Buffer.Insert(SampleRate, Normalize(data, Peak), Normalize(spectrum, FPeak));

                bufferEvent.Invoke(Buffer);
                waveformEvent.Invoke(Buffer.Data().ToArray());
                spectrumEvent.Invoke(Buffer.Current.spectrum);

                for (int i = 0; i < Buffer.Current.spectrum.Length; i++)
                {
                    float val   = Buffer.Current.spectrum[i] * 255;
                    Color color = Color.HSVToRGB(1, 1, val);
                    color.a = val;
                    texture.SetPixel(i, index, color);
                }

                texture.Apply();
                textureEvent.Invoke(texture, index);
                index = (index + 1) % Buffer.Count;
            }
            else
            {
                Debug.LogWarning("No Audio Available!");
            }
        }