Commit 1c7bb5ea authored by Nathan De Castro's avatar Nathan De Castro

cleaned up some code, working on pitch detection

parent 69ea7238
...@@ -49,7 +49,6 @@ ...@@ -49,7 +49,6 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Assets\AudioAnalysis.cs" />
<Compile Include="Assets\Scripts\AI\AI.cs" /> <Compile Include="Assets\Scripts\AI\AI.cs" />
<Compile Include="Assets\Scripts\AI\CurrentPlayer.cs" /> <Compile Include="Assets\Scripts\AI\CurrentPlayer.cs" />
<Compile Include="Assets\Scripts\Effects\BigEnabler.cs" /> <Compile Include="Assets\Scripts\Effects\BigEnabler.cs" />
...@@ -57,7 +56,7 @@ ...@@ -57,7 +56,7 @@
<Compile Include="Assets\Scripts\Effects\Rotator.cs" /> <Compile Include="Assets\Scripts\Effects\Rotator.cs" />
<Compile Include="Assets\Scripts\Effects\SmallEnabler.cs" /> <Compile Include="Assets\Scripts\Effects\SmallEnabler.cs" />
<Compile Include="Assets\Scripts\Effects\SwitchState.cs" /> <Compile Include="Assets\Scripts\Effects\SwitchState.cs" />
<Compile Include="Assets\Scripts\Tools\AudioManager.cs" /> <Compile Include="Assets\Scripts\Tools\AM.cs" />
<Compile Include="Assets\Scripts\Tools\FPS_Camera.cs" /> <Compile Include="Assets\Scripts\Tools\FPS_Camera.cs" />
<Compile Include="Assets\Scripts\Tools\FPS_Control.cs" /> <Compile Include="Assets\Scripts\Tools\FPS_Control.cs" />
<Compile Include="Assets\Scripts\Tools\GameSettings.cs" /> <Compile Include="Assets\Scripts\Tools\GameSettings.cs" />
......
 using UnityEngine;
public class AudioAnalysis : MonoBehaviour {
static public AudioAnalysis i;
public float RmsValue;
public float DbValue;
public float PitchValue;
private const int QSamples = 1024;
private const float RefValue = 0.1f;
private const float Threshold = 0.02f;
float[] _samples;
private float[] _spectrum;
private float _fSample;
void Start () {
i = this;
GetComponent<AudioSource> ().clip = Microphone.Start ("Built-in Microphone", true, 1, AudioSettings.outputSampleRate);
GetComponent<AudioSource> ().loop = true;
GetComponent<AudioSource> ().Play ();
GetComponent<AudioSource> ().volume = 0.0f;
_samples = new float[QSamples];
_spectrum = new float[QSamples];
_fSample = AudioSettings.outputSampleRate;
}
void Update () {
AnalyzeSound ();
}
void AnalyzeSound () {
GetComponent<AudioSource> ().GetOutputData (_samples, 0); // fill array with samples
int i;
float sum = 0;
for (i = 0; i < QSamples; i++) {
sum += _samples[i] * _samples[i]; // sum squared samples
}
RmsValue = Mathf.Sqrt (sum / QSamples); // rms = square root of average
DbValue = 20 * Mathf.Log10 (RmsValue / RefValue); // calculate dB
if (DbValue < -160) DbValue = -160; // clamp it to -160dB min
// get sound spectrum
GetComponent<AudioSource> ().GetSpectrumData (_spectrum, 0, FFTWindow.BlackmanHarris);
float maxV = 0;
var maxN = 0;
for (i = 0; i < QSamples; i++) { // find max
if (!(_spectrum[i] > maxV) || !(_spectrum[i] > Threshold))
continue;
maxV = _spectrum[i];
maxN = i; // maxN is the index of max
}
float freqN = maxN; // pass the index to a float variable
if (maxN > 0 && maxN < QSamples - 1) { // interpolate index using neighbours
var dL = _spectrum[maxN - 1] / _spectrum[maxN];
var dR = _spectrum[maxN + 1] / _spectrum[maxN];
freqN += 0.5f * (dR * dR - dL * dL);
}
PitchValue = freqN * (_fSample / 2) / QSamples; // convert index to frequency
}
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
fileFormatVersion: 2 fileFormatVersion: 2
guid: 7da32a16da5a37d49b11e79aa0e9b773 guid: a425aed1037a97a48af5677546c45216
timeCreated: 1480262695 timeCreated: 1490113009
licenseType: Free licenseType: Free
MonoImporter: NativeFormatImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData: userData:
assetBundleName: assetBundleName:
assetBundleVariant: assetBundleVariant:
...@@ -46,7 +46,7 @@ public class AI : MonoBehaviour { ...@@ -46,7 +46,7 @@ public class AI : MonoBehaviour {
} }
void FixedUpdate () { void FixedUpdate () {
ray_magnitude = AudioManager.instance.GetLightAngle() / ghost_awarness; ray_magnitude = AM.i.GetLightAngle() / ghost_awarness;
if (GameSettings.instance.lose_state == false && GameSettings.instance.win_state == false) //IF you die, these won't be called anymore. if (GameSettings.instance.lose_state == false && GameSettings.instance.win_state == false) //IF you die, these won't be called anymore.
{ {
state_manager (); //Manages the AI's different states state_manager (); //Manages the AI's different states
......
...@@ -10,7 +10,7 @@ public class BigEnabler : MonoBehaviour { ...@@ -10,7 +10,7 @@ public class BigEnabler : MonoBehaviour {
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
if (AudioManager.instance.GetAveragePitch ()[2] > 0.5f) { if (AM.i.GetPitch ()[2] > 0.5f) {
Activated = true; Activated = true;
} else { } else {
Activated = false; Activated = false;
......
...@@ -10,7 +10,7 @@ public class MediumEnabler : MonoBehaviour { ...@@ -10,7 +10,7 @@ public class MediumEnabler : MonoBehaviour {
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
if (AudioManager.instance.GetAveragePitch ()[1] < 0.5f) { if (AM.i.GetPitch ()[1] < 0.5f) {
Activated = true; Activated = true;
} else { } else {
Activated = false; Activated = false;
......
...@@ -10,7 +10,7 @@ public class SmallEnabler : MonoBehaviour { ...@@ -10,7 +10,7 @@ public class SmallEnabler : MonoBehaviour {
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
if (AudioManager.instance.GetAveragePitch ()[0] < 0.5f) { if (AM.i.GetPitch ()[0] < 0.5f) {
Activated = true; Activated = true;
} else { } else {
Activated = false; Activated = false;
......
using UnityEngine; using UnityEngine;
//Singleton class for managing audio //Singleton class for managing audio
public class AM : MonoBehaviour {
public class AudioManager : MonoBehaviour { public static AM i;
public static AudioManager instance;
//Global //Global
private AudioSource mic_source; private AudioSource mic_source;
public float volume_range_min; public float color_range_min = 3;
public float volume_range_max; public float color_range_max = 40;
public bool linear = true; public bool linear_color_change = true;
public int buffer_size; public float light_angle_scaler = 20;
public float light_angle_value; public float light_angle_max = 130;
public float light_angle_max; public float splash_force_scaler = 100;
public float splash_force_value; public int buffer_size = 32;
private float rms; private float rms;
private float vol; private float vol;
...@@ -22,13 +21,15 @@ public class AudioManager : MonoBehaviour { ...@@ -22,13 +21,15 @@ public class AudioManager : MonoBehaviour {
private float high_pitch = 0; private float high_pitch = 0;
void Start () { void Start () {
instance = this; i = this;
//Sets up the audiosource to use the Microphone
mic_source = GetComponent<AudioSource> (); mic_source = GetComponent<AudioSource> ();
// print ("AudioManager initialized"); mic_source.clip = Microphone.Start ("Built-in Microphone", true, 1, buffer_size); //Select the Microphone as input
mic_source.clip = Microphone.Start ("Built-in Microphone", true, 1, 256); //Microphone setup (null will select the default Microphone); mic_source.pitch = 0.95f; //slightly slower to avoid stuttering
mic_source.loop = true; mic_source.loop = true; //loops the second of audio
mic_source.Play (); mic_source.Play (); //starts the playback
mic_source.volume = 0.0f; mic_source.volume = 0.0f; //Mutes the audiosource
} }
void Update () { void Update () {
...@@ -36,62 +37,66 @@ public class AudioManager : MonoBehaviour { ...@@ -36,62 +37,66 @@ public class AudioManager : MonoBehaviour {
AmplitudeAnalysis (); AmplitudeAnalysis ();
} }
//Anaylsis function:
public void AmplitudeAnalysis () { public void AmplitudeAnalysis () {
int buf_size = 256; int buf_size = buffer_size;
float[] spec_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) float[] spec_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 (spec_data, 0); //Stores all the output data into the array I pre-allocated mic_source.clip.GetData (spec_data, 0); //Stores all the output data into the array I pre-allocated
float vol_sum = 0; float vol_sum = 0;
for (int i = 0; i < buf_size; i++) { for (int i = 0; i < buf_size; i++) {
vol_sum += Mathf.Abs (spec_data[i] * spec_data[i]); //Only grabs the second Third values of the array (so the "low" pitch values) vol_sum += Mathf.Abs (spec_data[i] * spec_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 / buf_size) * 100; //RMS value
vol = 0; vol = 0;
if (!(vol < -30)) if (!(vol < 0))
vol = 20 * Mathf.Log10 (rms / 0.1f); vol = 20 * Mathf.Log10 (rms / 0.1f); //Amplitude in dB
else vol = -30; else vol = 0;
}
public float GetRMS () {
return rms;
}
public float GetVolume(){
return vol;
} }
public void PitchAnalysis () { public void PitchAnalysis () {
int buf_size = 256; int buf_size = buffer_size;
float[] spec_data = new float[buf_size]; float[] spec_data = new float[buf_size];
AudioListener.GetSpectrumData (spec_data, 0, FFTWindow.Rectangular); mic_source.GetSpectrumData (spec_data, 0, FFTWindow.Rectangular);
print (spec_data[100]);
//Low Pitch //Low Pitch
float low_pitch_sum = 0; //Inits a float to store the sum of all the index arrays float low_pitch_sum = 0; //Inits a float to store the sum of all the index arrays
for (int spec_index = 0; spec_index < 20; spec_index++) { int low_cut = buf_size / 10;
for (int spec_index = 0; spec_index < low_cut; spec_index++) {
low_pitch_sum += Mathf.Abs (spec_data[spec_index]); //Only grabs the first Third values of the array (so the "low" pitch values) low_pitch_sum += Mathf.Abs (spec_data[spec_index]); //Only grabs the first Third values of the array (so the "low" pitch values)
} }
//Medium Pitch //Medium Pitch
float mid_pitch_sum = 0; //Inits a float to store the sum of all the index arrays float mid_pitch_sum = 0; //Inits a float to store the sum of all the index arrays
for (int spec_index = 20; spec_index < 100; spec_index++) { int mid_cut = (buf_size / 6) - low_cut;
mid_pitch_sum += Mathf.Abs (spec_data[spec_index]); //Only grabs the second Third values of the array (so the "low" pitch values) for (int spec_index = buf_size / 10; spec_index < mid_cut; spec_index++) {
mid_pitch_sum += Mathf.Abs (spec_data[low_cut + spec_index]); //Only grabs the second Third values of the array (so the "low" pitch values)
} }
//High Pitch< //High Pitch<
float high_pitch_sum = 0; //Inits a float to store the sum of all the index arrays float high_pitch_sum = 0; //Inits a float to store the sum of all the index arrays
for (int spec_index = 100; spec_index < spec_data.Length; spec_index++) { int high_cut = buf_size - mid_cut;
high_pitch_sum += Mathf.Abs (spec_data[spec_index]); //Only grabs the third Third values of the array (so the "low" pitch values) for (int spec_index = mid_cut; spec_index < high_cut; spec_index++) {
high_pitch_sum += Mathf.Abs (spec_data[mid_cut + spec_index]); //Only grabs the third Third values of the array (so the "low" pitch values)
} }
low_pitch = low_pitch_sum / 20; low_pitch = low_pitch_sum / low_cut;
med_pitch = mid_pitch_sum / 80; med_pitch = mid_pitch_sum / mid_cut;
high_pitch = high_pitch_sum / 156; high_pitch = high_pitch_sum / high_cut;
}
//Getter functions:
public float GetRMS () {
return rms;
} }
public Vector3 GetAveragePitch () { public float GetVolume () {
return vol;
}
public Vector3 GetPitch () {
return new Vector3 (low_pitch, med_pitch, high_pitch); return new Vector3 (low_pitch, med_pitch, high_pitch);
} }
...@@ -99,21 +104,21 @@ public class AudioManager : MonoBehaviour { ...@@ -99,21 +104,21 @@ public class AudioManager : MonoBehaviour {
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) { if (!threshold_reached) {
return rms * light_angle_value; return rms * light_angle_scaler;
} else { } else {
return light_angle_max; return light_angle_max;
} }
} }
public int GetSplashForce () { public int GetSplashForce () {
return (int) (rms * splash_force_value); return (int) (rms * splash_force_scaler);
} }
public Color GetLightColor () { public Color GetLightColor_vol () {
Color output; Color output;
//Linear interpolation: //Linear interpolation:
if (linear) { if (linear_color_change) {
float r = Mathf.Abs ((rms - volume_range_min) / volume_range_max); float r = Mathf.Abs ((rms - color_range_min) / color_range_max);
float b = Mathf.Abs (1 - r); float b = Mathf.Abs (1 - r);
output = new Color (r, 0, b, 0.8f); output = new Color (r, 0, b, 0.8f);
} else { } else {
...@@ -122,4 +127,9 @@ public class AudioManager : MonoBehaviour { ...@@ -122,4 +127,9 @@ public class AudioManager : MonoBehaviour {
return output; return output;
} }
public Color GetLightColor_pitch () {
Color output = new Color (0, 0, 0, 1);
return output;
}
} }
\ No newline at end of file
fileFormatVersion: 2 fileFormatVersion: 2
guid: 0affc6b8d6d55dd4692f15bf27ff327d guid: 8ccadfc64067d6e4db4be7af2125a53f
timeCreated: 1490099850 timeCreated: 1490115276
licenseType: Free licenseType: Free
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2
......
...@@ -6,6 +6,7 @@ public class PlayerSound : MonoBehaviour { ...@@ -6,6 +6,7 @@ public class PlayerSound : MonoBehaviour {
//LIGHT //LIGHT
private Light light_self; private Light light_self;
public bool use_mic = false; public bool use_mic = false;
public bool pitch_2_color = false;
// Use this for initialization // Use this for initialization
void Start () { void Start () {
...@@ -16,9 +17,11 @@ public class PlayerSound : MonoBehaviour { ...@@ -16,9 +17,11 @@ public class PlayerSound : MonoBehaviour {
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
if (use_mic && this.gameObject.tag == "Player") { if (use_mic && this.gameObject.tag == "Player") {
light_self.spotAngle = AM.i.GetLightAngle ();
light_self.spotAngle = AudioManager.instance.GetLightAngle ();; if (!pitch_2_color)
light_self.color = AudioManager.instance.GetLightColor ();; light_self.color = AM.i.GetLightColor_vol ();
else light_self.color = AM.i.GetLightColor_pitch ();
} }
} }
} }
\ No newline at end of file
...@@ -90,9 +90,12 @@ public class RippleEffect : MonoBehaviour { ...@@ -90,9 +90,12 @@ public class RippleEffect : MonoBehaviour {
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
update_rate = 3 + Mathf.Abs(20 - ((int) AudioManager.instance.GetRMS ()));
if (tick (update_rate) && PlayerSound.instance.use_mic == true && AudioManager.instance.GetRMS () >= 4f) { if (5 < update_rate)
splash_force_mic = AudioManager.instance.GetSplashForce (); update_rate = (int) (1 / AM.i.GetVolume()) * 10;
else update_rate = 5;
if (tick (update_rate) && PlayerSound.instance.use_mic == true && AM.i.GetRMS () >= 4f) {
splash_force_mic = AM.i.GetSplashForce ();
SplashDetect (); //Checks wether a splash has been initiated SplashDetect (); //Checks wether a splash has been initiated
} }
int[] current_buffer; int[] current_buffer;
......
...@@ -10,7 +10,7 @@ public class Info : MonoBehaviour { ...@@ -10,7 +10,7 @@ public class Info : MonoBehaviour {
} }
void Update () { void Update () {
info_text.text = "RMS: " + AudioManager.instance.GetRMS () + " Pitch: " + AudioManager.instance.GetAveragePitch (); info_text.text = "RMS: " + AM.i.GetRMS () + " dB: " + AM.i.GetVolume ();
} }
} }
\ No newline at end of file
...@@ -10,7 +10,6 @@ public class Visibility : MonoBehaviour { ...@@ -10,7 +10,6 @@ public class Visibility : MonoBehaviour {
} }
void Update () { void Update () {
info_text.text = "Power: " + RippleEffect.update_rate; info_text.text = "Pitch: " + AM.i.GetPitch ();
} }
} }
\ No newline at end of file
...@@ -6889,7 +6889,7 @@ GameObject: ...@@ -6889,7 +6889,7 @@ GameObject:
- component: {fileID: 231648941} - component: {fileID: 231648941}
- component: {fileID: 231648943} - component: {fileID: 231648943}
m_Layer: 5 m_Layer: 5
m_Name: Power m_Name: Stats2
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
...@@ -6911,7 +6911,7 @@ RectTransform: ...@@ -6911,7 +6911,7 @@ RectTransform:
m_AnchorMin: {x: 1, y: 1} m_AnchorMin: {x: 1, y: 1}
m_AnchorMax: {x: 1, y: 1} m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0} m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 70, y: 30} m_SizeDelta: {x: 200, y: 30}
m_Pivot: {x: 1, y: 1} m_Pivot: {x: 1, y: 1}
--- !u!114 &231648941 --- !u!114 &231648941
MonoBehaviour: MonoBehaviour:
...@@ -6945,7 +6945,7 @@ MonoBehaviour: ...@@ -6945,7 +6945,7 @@ MonoBehaviour:
m_HorizontalOverflow: 0 m_HorizontalOverflow: 0
m_VerticalOverflow: 0 m_VerticalOverflow: 0
m_LineSpacing: 1 m_LineSpacing: 1
m_Text: Power m_Text: Stats2
--- !u!222 &231648942 --- !u!222 &231648942
CanvasRenderer: CanvasRenderer:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -10285,27 +10285,27 @@ Prefab: ...@@ -10285,27 +10285,27 @@ Prefab:
- target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a, - target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a,
type: 2} type: 2}
propertyPath: dampner propertyPath: dampner
value: 0.98 value: 0.998
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a, - target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a,
type: 2} type: 2}
propertyPath: spread_bit_shift propertyPath: spread_bit_shift
value: 2 value: 1
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 33696797248011756, guid: ef8622c6f07c11e47aae43d93e8e076a, - target: {fileID: 33696797248011756, guid: ef8622c6f07c11e47aae43d93e8e076a,
type: 2} type: 2}
propertyPath: m_Mesh propertyPath: m_Mesh
value: value:
objectReference: {fileID: 4300000, guid: 07450b72d45f7884181c594c53bcbcf0, type: 2} objectReference: {fileID: 4300000, guid: a425aed1037a97a48af5677546c45216, type: 2}
- target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a, - target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a,
type: 2} type: 2}
propertyPath: cols propertyPath: cols
value: 32 value: 64
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a, - target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a,
type: 2} type: 2}
propertyPath: rows propertyPath: rows
value: 32 value: 64
objectReference: {fileID: 0} objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: ef8622c6f07c11e47aae43d93e8e076a, type: 2} m_ParentPrefab: {fileID: 100100000, guid: ef8622c6f07c11e47aae43d93e8e076a, type: 2}
...@@ -28896,8 +28896,8 @@ GameObject: ...@@ -28896,8 +28896,8 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 1200467580} - component: {fileID: 1200467580}
- component: {fileID: 1200467583} - component: {fileID: 1200467583}
- component: {fileID: 1200467579}
- component: {fileID: 1200467581} - component: {fileID: 1200467581}
- component: {fileID: 1200467579}
m_Layer: 0 m_Layer: 0
m_Name: World m_Name: World
m_TagString: Untagged m_TagString: Untagged
...@@ -28913,16 +28913,16 @@ MonoBehaviour: ...@@ -28913,16 +28913,16 @@ MonoBehaviour:
m_GameObject: {fileID: 1200467578} m_GameObject: {fileID: 1200467578}
m_Enabled: 1 m_Enabled: 1
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7da32a16da5a37d49b11e79aa0e9b773, type: 3} m_Script: {fileID: 11500000, guid: 8ccadfc64067d6e4db4be7af2125a53f, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
volume_range_min: 0 color_range_min: 3
volume_range_max: 30 color_range_max: 40
linear: 1 linear_color_change: 1
buffer_size: 22050 light_angle_scaler: 5
light_angle_value: 10
light_angle_max: 130 light_angle_max: 130
splash_force_value: 20 splash_force_scaler: 5
buffer_size: 64
--- !u!4 &1200467580 --- !u!4 &1200467580
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0