Commit a7256bb1 authored by Nathan De Castro's avatar Nathan De Castro
Browse files

Cleaned up code and sorted pith detection

parent 2a8caa54
......@@ -2,22 +2,42 @@ using UnityEngine;
// A simple rotating script for the walls of the player area
public class BigEnabler : MonoBehaviour {
public bool Activated = false;
Renderer[] rendererComponents;
public float volume_threshold = 20.0f;
public float pitch_threshold = 0.1f;
public float fade_speed = 0.5f;
public bool volume_based = true;
public bool pitch_based = false;
private bool Activated = false;
private FadeInAndOut enabler;
void Start () {
rendererComponents = GetComponentsInChildren<Renderer> ();
enabler = GetComponent<FadeInAndOut> ();
}
// Update is called once per frame
void Update () {
if (AM.i.GetRMS () > 10.0f) {
Activated = true;
} else {
Activated = false;
volume_based = !pitch_based;
pitch_based = !volume_based;
if (volume_based) {
if (AM.i.GetRMS () > volume_threshold && Activated == false) {
Activated = true;
enabler.FadeIn (fade_speed);
} else if (AM.i.GetRMS () < volume_threshold && Activated == true) {
Activated = false;
enabler.FadeOut (fade_speed);
}
}
foreach (Renderer component in rendererComponents) {
component.enabled = Activated;
if (pitch_based) {
if (AM.i.GetComplexPitch ()[2] > pitch_threshold && Activated == false) {
Activated = true;
enabler.FadeIn (fade_speed);
} else if (AM.i.GetComplexPitch ()[2] < pitch_threshold && Activated == true) {
Activated = false;
enabler.FadeOut (fade_speed);
}
}
}
}
\ No newline at end of file
......@@ -2,22 +2,42 @@ using UnityEngine;
// A simple rotating script for the walls of the player area
public class MediumEnabler : MonoBehaviour {
public bool Activated = false;
Renderer[] rendererComponents;
public float volume_threshold = 10.0f;
public float pitch_threshold = 0.1f;
public float fade_speed = 0.5f;
public bool volume_based = true;
public bool pitch_based = false;
private bool Activated = false;
private FadeInAndOut enabler;
void Start () {
rendererComponents = GetComponentsInChildren<Renderer> ();
enabler = GetComponent<FadeInAndOut> ();
}
// Update is called once per frame
void Update () {
if (AM.i.GetRMS () > 6.0f) {
Activated = true;
} else {
Activated = false;
volume_based = !pitch_based;
pitch_based = !volume_based;
if (volume_based) {
if (AM.i.GetRMS () > volume_threshold && Activated == false) {
Activated = true;
enabler.FadeIn (fade_speed);
} else if (AM.i.GetRMS () < volume_threshold && Activated == true) {
Activated = false;
enabler.FadeOut (fade_speed);
}
}
foreach (Renderer component in rendererComponents) {
component.enabled = Activated;
if (pitch_based) {
if (AM.i.GetComplexPitch ()[1] > pitch_threshold && Activated == false) {
Activated = true;
enabler.FadeIn (fade_speed);
} else if (AM.i.GetComplexPitch ()[1] < pitch_threshold && Activated == true) {
Activated = false;
enabler.FadeOut (fade_speed);
}
}
}
}
\ No newline at end of file
......@@ -2,22 +2,42 @@ using UnityEngine;
// A simple rotating script for the walls of the player area
public class SmallEnabler : MonoBehaviour {
public bool Activated = false;
Renderer[] rendererComponents;
public float volume_threshold = 3.0f;
public float pitch_threshold = 0.1f;
public float fade_speed = 0.5f;
public bool volume_based = true;
public bool pitch_based = false;
private bool Activated = false;
private FadeInAndOut enabler;
void Start () {
rendererComponents = GetComponentsInChildren<Renderer> ();
enabler = GetComponent<FadeInAndOut> ();
}
// Update is called once per frame
void Update () {
if (AM.i.GetRMS () > 2.0f) {
Activated = true;
} else {
Activated = false;
volume_based = !pitch_based;
pitch_based = !volume_based;
if (volume_based) {
if (AM.i.GetRMS () > volume_threshold && Activated == false) {
Activated = true;
enabler.FadeIn (fade_speed);
} else if (AM.i.GetRMS () < volume_threshold && Activated == true) {
Activated = false;
enabler.FadeOut (fade_speed);
}
}
foreach (Renderer component in rendererComponents) {
component.enabled = Activated;
if (pitch_based) {
if (AM.i.GetComplexPitch ()[0] > pitch_threshold && Activated == false) {
Activated = true;
enabler.FadeIn (fade_speed);
} else if (AM.i.GetComplexPitch ()[0] < pitch_threshold && Activated == true) {
Activated = false;
enabler.FadeOut (fade_speed);
}
}
}
}
\ No newline at end of file
......@@ -7,10 +7,14 @@ public class SwitchState : MonoBehaviour {
public GameObject switch_off;
static public bool switch_status;
static public bool player_near_switch;
public bool start_with_light = true;
// Use this for initialization
void Start () {
main_light.enabled = false;
if (start_with_light)
main_light.enabled = true;
else
main_light.enabled = false;
}
// Update is called once per frame
......
......@@ -5,29 +5,36 @@ public class AM : MonoBehaviour {
public static AM i;
private AudioSource mic_source;
public GameObject cube;
//Globals
public bool pitch_based = false;
public float color_range_min = 3;
public float color_range_max = 40;
public bool use_pitch = false;
public float light_angle_scaler = 20;
public float light_angle_max = 130;
public float splash_force_scaler = 100;
public int buffer_size = 64;
public float audio_update_rate = 40.0f;
private float[] spec_data;
private float[] samp_data;
private int spec_size = 1024;
private int samp_size = 1024;
private int buffer_size = 44100;
private float rms;
private float vol;
private float max_energy_bin = 0;
private float average_pitch = 0;
private float low_pitch = 0;
private float med_pitch = 0;
private float high_pitch = 0;
private float[] spec_data;
private float average_pitch_low = 0;
private float average_pitch_mid = 0;
private float average_pitch_high = 0;
// private float low_pitch = 0;
// private float med_pitch = 0;
// private float high_pitch = 0;
void Start () {
i = this;
spec_data = new float[spec_size];
samp_data = new float[samp_size];
//Sets up the audiosource to use the Microphone
mic_source = GetComponent<AudioSource> ();
......@@ -37,23 +44,27 @@ public class AM : MonoBehaviour {
mic_source.Play (); //starts the playback
mic_source.volume = 0.5f; //Mutes the audiosource
spec_data = new float[buffer_size];
InvokeRepeating ("PitchAnalysis", 0, 1.0f / audio_update_rate); // Updates at 40 fps
InvokeRepeating ("AmplitudeAnalysis", 0, 1.0f / audio_update_rate); //Updates at 40 fps
InvokeRepeating ("BufferPopulator", 0, 1.0f / audio_update_rate); //Begins another thread
}
void FixedUpdate () {
AmplitudeAnalysis ();
AveragePitchAnalysis ();
//ComplexPitchAnalysis ();
}
public void BufferPopulator () {
AudioListener.GetSpectrumData (spec_data, 0, FFTWindow.Hamming);
mic_source.clip.GetData (samp_data, 0);
}
//Anaylsis function:
public void AmplitudeAnalysis () {
int buf_size = buffer_size;
float[] samp_data = new float[buf_size]; //Creates an array pre-allocated (We want it quite small to reduce latency, I made it tweakable from the inspector)
mic_source.clip.GetData (samp_data, 0); //Stores all the output data into the array I pre-allocated
float vol_sum = 0;
for (int i = 0; i < buf_size; i++) {
for (int i = 0; i < samp_size; i++) {
vol_sum += Mathf.Abs (samp_data[i] * samp_data[i]); //Calculates the square sum of all the data
}
rms = Mathf.Sqrt (vol_sum / buf_size) * 100; //RMS value
rms = Mathf.Sqrt (vol_sum / samp_size) * 100; //RMS value
vol = 0;
if (!(vol < 0))
......@@ -61,66 +72,71 @@ public class AM : MonoBehaviour {
else vol = 0;
}
public void PitchAnalysis () {
AudioListener.GetSpectrumData (spec_data, 0, FFTWindow.Rectangular);
public void AveragePitchAnalysis () {
/*
Low = 64Hz
Mid = 512Hz
High = 1024Hz
*/
//Attemps to isolate the bin with the Highest energy
float spec_sum = 0;
for (int i = 1; i < spec_data.Length; i++) {
spec_sum += Mathf.Abs (spec_data[i] * spec_data[i]);
}
float Low = spec_data[2] + spec_data[3] + spec_data[4];
if (Low > 0.00001) average_pitch_low = Low * 100;
else average_pitch_low = 0;
average_pitch = convert_to_freq (spec_sum / spec_data.Length);
float Mid = spec_data[22] + spec_data[23] + spec_data[24];
if (Mid > 0.00001) average_pitch_mid = Mid * 100;
else average_pitch_mid = 0;
float High = spec_data[44] + spec_data[45] + spec_data[46] + spec_data[47] + spec_data[48] + spec_data[49];
if (High > 0.00001) average_pitch_high = High * 100;
else average_pitch_high = 0;
}
public void ComplexPitchAnalysis () {
//Seperates the array in 3 from lowest pitch to Highest
//Low Pitch
float low_pitch_sum = 0; //Inits a float to store the sum of all the index arrays
for (int spec_index = 0; spec_index < 5; spec_index++) {
low_pitch_sum += Mathf.Abs (spec_data[spec_index] * spec_data[spec_index]); //Only grabs the first Third values of the array (so the "low" pitch values) squared
low_pitch_sum += Mathf.Abs (spec_data[spec_index] * spec_data[spec_index]); //Only grMathf.abs the first Third values of the array (so the "low" pitch values) squared
}
//Medium Pitch
float mid_pitch_sum = 0; //Inits a float to store the sum of all the index arrays
for (int spec_index = 5; spec_index < 30; spec_index++) {
mid_pitch_sum += Mathf.Abs (spec_data[spec_index] * spec_data[spec_index]); //Only grabs the second Third values of the array (so the "mid" pitch values) squared
mid_pitch_sum += Mathf.Abs (spec_data[spec_index] * spec_data[spec_index]); //Only grMathf.abs the second Third values of the array (so the "mid" pitch values) squared
}
//High Pitch
float high_pitch_sum = 0; //Inits a float to store the sum of all the index arrays
for (int spec_index = 30; spec_index < spec_data.Length; spec_index++) {
high_pitch_sum += Mathf.Abs (spec_data[spec_index] * spec_data[spec_index]); //Only grabs the third Third values of the array (so the "high" pitch values) squared
high_pitch_sum += Mathf.Abs (spec_data[spec_index] * spec_data[spec_index]); //Only grMathf.abs the third Third values of the array (so the "high" pitch values) squared
}
low_pitch = Mathf.Sqrt (low_pitch_sum / 5) * Mathf.Exp (5);
med_pitch = Mathf.Sqrt (mid_pitch_sum / 25) * Mathf.Exp (7);
high_pitch = Mathf.Sqrt (high_pitch_sum / 34) * Mathf.Exp (7);
low_pitch = map (0.0f, 2.0f, 0, 1000, low_pitch);
med_pitch = map (0.0f, 2.0f, 0, 1000, med_pitch);
high_pitch = map (0.0f, 2.0f, 0, 1000, high_pitch);
// low_pitch = Mathf.Log (low_pitch_sum / 5);
// med_pitch = Mathf.Log (mid_pitch_sum / 25);
// high_pitch = Mathf.Log (high_pitch_sum / 34);
}
//Getter functions:
public float GetRMS () {
return rms;
}
public float GetVolume () {
return vol;
}
public Vector3 GetPitch () {
return new Vector3 (low_pitch, med_pitch, high_pitch);
public float GetAveragePitch () {
return average_pitch_mid;
}
public float GetMaxEnergyBin () {
return average_pitch;
public Vector3 GetComplexPitch () {
return new Vector3 (average_pitch_low, average_pitch_mid, average_pitch_high);
}
public float GetLightAngle () {
bool threshold_reached = PlayerSound.instance.GetComponentInChildren<Light> ().spotAngle > light_angle_max;
bool threshold_reached = PlayerSound.instance.GetComponentInChildren<Light> ().spotAngle >= light_angle_max;
if (!threshold_reached || true) {
if (!threshold_reached) {
return rms * light_angle_scaler;
} else {
return light_angle_max;
......@@ -134,12 +150,11 @@ public class AM : MonoBehaviour {
public Color GetLightColor () {
Color output;
//Linear interpolation:
if (!use_pitch) {
float r = Mathf.Abs ((rms - color_range_min) / color_range_max);
float b = Mathf.Abs (1 - r);
output = new Color (r, 0, b, 0.8f);
if (pitch_based) {
output = new Color (average_pitch_low, average_pitch_mid, average_pitch_high);
return output;
} else {
float r = Mathf.Abs ((max_energy_bin - color_range_min) / color_range_max);
float r = Mathf.Abs ((rms - color_range_min) / color_range_max);
float b = Mathf.Abs (1 - r);
output = new Color (r, 0, b, 0.8f);
}
......
/*
FadeObjectInOut.cs
Hayden Scott-Baron (Dock) - http://starfruitgames.com
6 Dec 2012
This allows you to easily fade an object and its children.
If an object is already partially faded it will continue from there.
If you choose a different speed, it will use the new speed.
NOTE: Requires materials with a shader that allows transparency through color.
*/
using System.Collections;
using UnityEngine;
public class FadeInAndOut : MonoBehaviour {
// publically editable speed
public float fadeDelay = 0.0f;
public float fadeTime = 0.5f;
public bool fadeInOnStart = false;
public bool fadeOutOnStart = false;
private bool logInitialFadeSequence = false;
// store colours
private Color[] colors;
// allow automatic fading on the start of the scene
IEnumerator Start () {
//yield return null;
yield return new WaitForSeconds (fadeDelay);
if (fadeInOnStart) {
logInitialFadeSequence = true;
FadeIn ();
}
if (fadeOutOnStart) {
FadeOut (fadeTime);
}
}
// check the alpha value of most opaque object
float MaxAlpha () {
float maxAlpha = 0.0f;
Renderer[] rendererObjects = GetComponentsInChildren<Renderer> ();
foreach (Renderer item in rendererObjects) {
maxAlpha = Mathf.Max (maxAlpha, item.material.color.a);
}
return maxAlpha;
}
// fade sequence
IEnumerator FadeSequence (float fadingOutTime) {
// log fading direction, then precalculate fading speed as a multiplier
bool fadingOut = (fadingOutTime < 0.0f);
float fadingOutSpeed = 1.0f / fadingOutTime;
// grab all child objects
Renderer[] rendererObjects = GetComponentsInChildren<Renderer> ();
if (colors == null) {
//create a cache of colors if necessary
colors = new Color[rendererObjects.Length];
// store the original colours for all child objects
for (int i = 0; i < rendererObjects.Length; i++) {
colors[i] = rendererObjects[i].material.color;
}
}
// make all objects visible
for (int i = 0; i < rendererObjects.Length; i++) {
rendererObjects[i].enabled = true;
}
// get current max alpha
float alphaValue = MaxAlpha ();
// This is a special case for objects that are set to fade in on start.
// it will treat them as alpha 0, despite them not being so.
if (logInitialFadeSequence && !fadingOut) {
alphaValue = 0.0f;
logInitialFadeSequence = false;
}
// iterate to change alpha value
while ((alphaValue >= 0.0f && fadingOut) || (alphaValue <= 1.0f && !fadingOut)) {
alphaValue += Time.deltaTime * fadingOutSpeed;
for (int i = 0; i < rendererObjects.Length; i++) {
Color newColor = (colors != null ? colors[i] : rendererObjects[i].material.color);
newColor.a = Mathf.Min (newColor.a, alphaValue);
newColor.a = Mathf.Clamp (newColor.a, 0.0f, 1.0f);
rendererObjects[i].material.SetColor ("_Color", newColor);
}
yield return null;
}
// turn objects off after fading out
if (fadingOut) {
for (int i = 0; i < rendererObjects.Length; i++) {
rendererObjects[i].enabled = false;
}
}
}
public void FadeIn () {
FadeIn (fadeTime);
}
public void FadeOut () {
FadeOut (fadeTime);
}
public void FadeIn (float newFadeTime) {
StopAllCoroutines ();
StartCoroutine ("FadeSequence", newFadeTime);
}
public void FadeOut (float newFadeTime) {
StopAllCoroutines ();
StartCoroutine ("FadeSequence", -newFadeTime);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 29863b658421bdf4dba48fd6ab88f92d
timeCreated: 1490712898
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -5,7 +5,6 @@ public class PlayerSound : MonoBehaviour {
//LIGHT
private Light light_self;
public bool use_mic = false;
// Use this for initialization
void Start () {
......@@ -15,7 +14,7 @@ public class PlayerSound : MonoBehaviour {
// Update is called once per frame
void Update () {
if (use_mic && this.gameObject.tag == "Player") {
if (this.gameObject.tag == "Player") {
light_self.spotAngle = AM.i.GetLightAngle ();
light_self.color = AM.i.GetLightColor ();
}
......
......@@ -3,25 +3,25 @@ using UnityEngine;
public class RippleEffect : MonoBehaviour {
private int[] buffer1; //Is the first buffer of vertices
private int[] buffer2; //Is the second buffer of vertices
private int[] vertex_indexes; //Stores the indexes of the vertices
public int cols; //Number of the columns we want our plane to have (The bigger this is, the more computer intensive the calculations will be)
public int rows; //Number of the rows we want our plane to have
public float dampner; //How fast we want the wave to fade out
public float threshold = 5.0f;
public int spread_bit_shift;
static public int update_rate = 5;
//Because we want a whole plane to distort, we need the plane have more then just 4 vertices to simulate the wave
//This is why we need to create our own plane with many many vertices
private int[] buffer1; //Is the first buffer of vertices
private int[] buffer2; //Is the second buffer of vertices
private int[] vertex_indexes; //Stores the indexes of the vertices
private int splash_force_mic = 0; //Variable that will update the splash_force according to the mic
private Mesh mesh; //Will store the created mesh
private Vector3[] vertices; //private Vector3[] normals ;
public float dampner; //How fast we want the wave to fade out
private int splash_force = 150; //How strongly we want our splash to be
private int splash_force_mic = 0; //Variable that will update the splash_force according to the mic
private bool swap_buffer = true; //Switches between the first and the second buffer
static public int update_rate = 5;
public int spread_bit_shift;
public float threshold = 5.0f;
private Vector3[] current_vertices;
private bool swap_buffer = true; //Switches between the first and the second buffer
public int cols; //Number of the columns we want our plane to have (The bigger this is, the more computer intensive the calculations will be)
public int rows; //Number of the rows we want our plane to have
void Start () {
MeshFilter mesh_filter = GetComponent<MeshFilter> (); //Attaches the mesh filter in he object to the mesh filter variable
......@@ -57,27 +57,15 @@ public class RippleEffect : MonoBehaviour {
{
// initiates the values of each vertices according to their distance from the center such as the further you are from it the weak the force is.
int position = ((y * (cols + 1)) + x);
if (PlayerSound.instance.use_mic) {
buffer1[position] = splash_force_mic;
buffer1[position - 1] = splash_force_mic;
buffer1[position + 1] = splash_force_mic;
buffer1[position + (cols + 1)] = splash_force_mic;
buffer1[position + (cols + 1) + 1] = splash_force_mic;
buffer1[position + (cols + 1) - 1] = splash_force_mic;
buffer1[position - (cols + 1)] = splash_force_mic;
buffer1[position - (cols + 1) + 1] = splash_force_mic;
buffer1[position - (cols + 1) - 1] = splash_force_mic;
} else {
buffer1[position] = splash_force;
buffer1[position - 1] = splash_force;
buffer1[position + 1] = splash_force;
buffer1[position + (cols + 1)] = splash_force;