Skip to content

Commit a785204

Browse files
committed
enable to change color from FFT
1 parent eefe4e5 commit a785204

File tree

6 files changed

+106
-70
lines changed

6 files changed

+106
-70
lines changed

Assets/Material/shockwave.mat

0 Bytes
Binary file not shown.

Assets/Script/MicAnalyzer.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

Assets/Script/MicrophoneSource.cs.meta renamed to Assets/Script/MicAnalyzer.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Script/MicrophoneSource.cs

Lines changed: 0 additions & 64 deletions
This file was deleted.

Assets/Shader/test2.shader

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
float4 color : COLOR;
1919
};
2020
// Propertiesで指定した値を取得
21-
float _WaveNum, _WaveScale, _Opacity, _Loudness;
21+
float _WaveNum, _WaveScale, _Opacity, _Loudness,
22+
_LowSpectrum, _MidSpectrum, _HighSpectrum;
2223
float4 _Color;
24+
2325
//
2426
void surf (Input IN, inout SurfaceOutput o) {
25-
// 反射の割合 青と緑は半分反射
26-
o.Albedo = _Color.rgb;
27-
o.Albedo = float4((_SinTime.x+1.0) * 2, (_SinTime.y+1.0) * 2, (_SinTime.z) * 2, 0.1);
27+
o.Albedo = float4(_LowSpectrum, _MidSpectrum, _HighSpectrum, 0.1);
2828
// 半透明
2929
o.Alpha = _Opacity;
3030
}

Assets/main.unity

296 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)