Created
April 22, 2018 19:55
-
-
Save satansama6/6de3818e0adcad01234c4565dfe743bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
// This is how you use it. Basically just request AudioPeer.bandBuffer[i] | |
public class AudioBars : MonoBehaviour | |
{ | |
public List<GameObject> bars = new List<GameObject>(8); | |
public float scaleMultiplier = 0.1f; | |
public float sunScale; | |
public SunVisualizer sun; | |
private void Update() | |
{ | |
for (int i = 0; i < bars.Count; i++) | |
{ | |
if (bars[i] != null) | |
bars[i].transform.localScale = new Vector3(1, AudioPeer.bandBuffer[i % 8] * scaleMultiplier + 0.5f, 1); | |
} | |
float avarage = 0; | |
for (int i = 0; i < bars.Count; i++) | |
{ | |
avarage += AudioPeer.bandBuffer[i % 8]; | |
} | |
avarage /= bars.Count; | |
sun.Visualize(avarage * sunScale); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Audio; | |
public class AudioPeer : MonoBehaviour | |
{ | |
public AudioSource audiosource; | |
public static float[] samples = new float[512]; | |
public static float[] frequencyBands = new float[8]; | |
public static float[] bandBuffer = new float[8]; | |
private float[] bufferDecreese = new float[8]; | |
// Use this for initialization | |
private void Start() | |
{ | |
if (audiosource == null) | |
{ | |
audiosource = GetComponent<AudioSource>(); | |
} | |
for (int i = 0; i < Microphone.devices.Length; i++) | |
{ | |
//Debug.Log(Microphone.devices[i]); ; | |
} | |
// audiosource.clip = Microphone.Start(Microphone.devices[0].ToString(), true, 1000, AudioSettings.outputSampleRate); | |
// audiosource.outputAudioMixerGroup = audioMixerGroup; | |
audiosource.Play(); | |
} | |
// Update is called once per frame | |
private void Update() | |
{ | |
GetSpectrumAudioSource(); | |
MakeFrequencyBand(); | |
BandBuffer(); | |
} | |
private void GetSpectrumAudioSource() | |
{ | |
audiosource.GetSpectrumData(samples, 0, FFTWindow.Blackman); | |
} | |
private void MakeFrequencyBand() | |
{ | |
int count = 0; | |
for (int i = 0; i < frequencyBands.Length; i++) | |
{ | |
int sampleCount = 8; | |
float avarage = 0; | |
for (int j = 0; j < sampleCount; j++) | |
{ | |
avarage += samples[count] * (count + 1); | |
count++; | |
} | |
frequencyBands[i] = avarage / sampleCount; | |
} | |
} | |
private void BandBuffer() | |
{ | |
for (int i = 0; i < bandBuffer.Length; i++) | |
{ | |
if (frequencyBands[i] > bandBuffer[i]) | |
{ | |
bandBuffer[i] = frequencyBands[i]; | |
bufferDecreese[i] = 0.0005f; | |
} | |
if (frequencyBands[i] < bandBuffer[i]) | |
{ | |
bandBuffer[i] -= bufferDecreese[i]; | |
if (bandBuffer[i] < 0.001f) | |
{ | |
bandBuffer[i] = 0.001f; | |
} | |
bufferDecreese[i] *= 1.2f; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment