|
| 1 | +using UnityEngine; |
| 2 | +using System.Collections; |
| 3 | + |
| 4 | +// 空のAudio Sourceをアタッチ |
| 5 | +[RequireComponent (typeof (AudioSource))] |
| 6 | +public class MicAnalyzer : MonoBehaviour { |
| 7 | + // 現在の音量を読み取る |
| 8 | + public float GetLoundness() { |
| 9 | + return loudness; |
| 10 | + } |
| 11 | + // 感度 |
| 12 | + public float sensitivity = 100; |
| 13 | + // 音量 |
| 14 | + float loudness; |
| 15 | + // 前フレームの音量 |
| 16 | + float lastLoudness; |
| 17 | + |
| 18 | + // レンジでコントロールできるようにする |
| 19 | + [Range(0,0.95f)] |
| 20 | + public float lastLoudnessInfluence; |
| 21 | + |
| 22 | + private AudioSource audio; |
| 23 | + |
| 24 | + // マテリアル |
| 25 | + private Material mat; |
| 26 | + |
| 27 | + public int resolution = 1024; |
| 28 | + public float lowFreqThreshold = 14700, midFreqThreshold = 29400, highFreqThreshold = 44100; |
| 29 | + public float lowEnhance = 1f, midEnhance = 10f, highEnhance = 100f; |
| 30 | + |
| 31 | + // Use this for initialization |
| 32 | + void Start () { |
| 33 | + // マテリアルの作成 |
| 34 | + mat = GetComponent<Renderer>().material; |
| 35 | + // 空のAudio Sourceを取得 |
| 36 | + audio = GetComponent<AudioSource>(); |
| 37 | + // Audio Source の Audio Clipをマイク入力に設定 |
| 38 | + audio.clip = Microphone.Start(null, true, 10, 44100); |
| 39 | + |
| 40 | + // 無音状態でずっと再生しておく |
| 41 | + audio.loop = true; |
| 42 | + audio.mute = true; |
| 43 | + |
| 44 | + // マイクがReady状態になるまで待機 |
| 45 | + while (Microphone.GetPosition(null) <= 0) {} |
| 46 | + // 再生開始 |
| 47 | + audio.Play(); |
| 48 | + } |
| 49 | + |
| 50 | + // Update is called once per frame |
| 51 | + void Update () { |
| 52 | + CalcLoudness(); |
| 53 | + AnalyzeSpectrum(); |
| 54 | + } |
| 55 | + // 現フレームの音量を計算 |
| 56 | + void CalcLoudness() { |
| 57 | + lastLoudness = loudness; |
| 58 | + loudness = GetAveragedVolume() * sensitivity * (1 - lastLoudnessInfluence ) + lastLoudness * lastLoudnessInfluence; |
| 59 | + |
| 60 | + // ボリューム |
| 61 | + mat.SetFloat("_Loudness", loudness); |
| 62 | + } |
| 63 | + // 現フレームのAudioClipから平均的な音量を取得 |
| 64 | + float GetAveragedVolume() { |
| 65 | + int sampleData = 256; |
| 66 | + float[] data = new float[sampleData]; |
| 67 | + float _audio = 0; |
| 68 | + GetComponent<AudioSource>().GetOutputData(data, 0); |
| 69 | + foreach( float d in data) { |
| 70 | + _audio += Mathf.Abs(d); |
| 71 | + } |
| 72 | + return _audio / sampleData; |
| 73 | + } |
| 74 | + void AnalyzeSpectrum() { |
| 75 | + var spectrum = audio.GetSpectrumData(resolution, 0, FFTWindow.BlackmanHarris); |
| 76 | + var deltaFreq = AudioSettings.outputSampleRate / resolution; |
| 77 | + float low = 0f, mid = 0f, high = 0f; |
| 78 | + |
| 79 | + for (var i = 0; i < resolution; ++i) { |
| 80 | + var freq = deltaFreq * i; |
| 81 | + if (freq <= lowFreqThreshold) { |
| 82 | + low += spectrum[i]; |
| 83 | + } |
| 84 | + else if (freq <= midFreqThreshold) { |
| 85 | + mid += spectrum[i]; |
| 86 | + } |
| 87 | + else if (freq <= highFreqThreshold) { |
| 88 | + high += spectrum[i]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + low *= lowEnhance; |
| 93 | + mid *= midEnhance; |
| 94 | + high *= highEnhance; |
| 95 | + // ボリューム |
| 96 | + mat.SetFloat("_LowSpectrum", low); |
| 97 | + mat.SetFloat("_MidSpectrum", mid); |
| 98 | + mat.SetFloat("_HighSpectrum", high); |
| 99 | + } |
| 100 | +} |
0 commit comments