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 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Assets\AudioAnalysis.cs" />
<Compile Include="Assets\Scripts\AI\AI.cs" />
<Compile Include="Assets\Scripts\AI\CurrentPlayer.cs" />
<Compile Include="Assets\Scripts\Effects\BigEnabler.cs" />
......@@ -57,7 +56,7 @@
<Compile Include="Assets\Scripts\Effects\Rotator.cs" />
<Compile Include="Assets\Scripts\Effects\SmallEnabler.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_Control.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
guid: 7da32a16da5a37d49b11e79aa0e9b773
timeCreated: 1480262695
guid: a425aed1037a97a48af5677546c45216
timeCreated: 1490113009
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
......@@ -46,7 +46,7 @@ public class AI : MonoBehaviour {
}
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.
{
state_manager (); //Manages the AI's different states
......
......@@ -10,7 +10,7 @@ public class BigEnabler : MonoBehaviour {
}
// Update is called once per frame
void Update () {
if (AudioManager.instance.GetAveragePitch ()[2] > 0.5f) {
if (AM.i.GetPitch ()[2] > 0.5f) {
Activated = true;
} else {
Activated = false;
......
......@@ -10,7 +10,7 @@ public class MediumEnabler : MonoBehaviour {
}
// Update is called once per frame
void Update () {
if (AudioManager.instance.GetAveragePitch ()[1] < 0.5f) {
if (AM.i.GetPitch ()[1] < 0.5f) {
Activated = true;
} else {
Activated = false;
......
......@@ -10,7 +10,7 @@ public class SmallEnabler : MonoBehaviour {
}
// Update is called once per frame
void Update () {
if (AudioManager.instance.GetAveragePitch ()[0] < 0.5f) {
if (AM.i.GetPitch ()[0] < 0.5f) {
Activated = true;
} else {
Activated = false;
......
using UnityEngine;
//Singleton class for managing audio
public class AudioManager : MonoBehaviour {
public static AudioManager instance;
public class AM : MonoBehaviour {
public static AM i;
//Global
private AudioSource mic_source;
public float volume_range_min;
public float volume_range_max;
public bool linear = true;
public int buffer_size;
public float light_angle_value;
public float light_angle_max;
public float splash_force_value;
public float color_range_min = 3;
public float color_range_max = 40;
public bool linear_color_change = true;
public float light_angle_scaler = 20;
public float light_angle_max = 130;
public float splash_force_scaler = 100;
public int buffer_size = 32;
private float rms;
private float vol;
......@@ -22,13 +21,15 @@ public class AudioManager : MonoBehaviour {
private float high_pitch = 0;
void Start () {
instance = this;
i = this;
//Sets up the audiosource to use the Microphone
mic_source = GetComponent<AudioSource> ();
// print ("AudioManager initialized");
mic_source.clip = Microphone.Start ("Built-in Microphone", true, 1, 256); //Microphone setup (null will select the default Microphone);
mic_source.loop = true;
mic_source.Play ();
mic_source.volume = 0.0f;
mic_source.clip = Microphone.Start ("Built-in Microphone", true, 1, buffer_size); //Select the Microphone as input
mic_source.pitch = 0.95f; //slightly slower to avoid stuttering
mic_source.loop = true; //loops the second of audio
mic_source.Play (); //starts the playback
mic_source.volume = 0.0f; //Mutes the audiosource
}
void Update () {
......@@ -36,62 +37,66 @@ public class AudioManager : MonoBehaviour {
AmplitudeAnalysis ();
}
//Anaylsis function:
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)
mic_source.clip.GetData (spec_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++) {
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
vol = 0;
if (!(vol < -30))
vol = 20 * Mathf.Log10 (rms / 0.1f);
else vol = -30;
}
public float GetRMS () {
return rms;
}
if (!(vol < 0))
vol = 20 * Mathf.Log10 (rms / 0.1f); //Amplitude in dB
else vol = 0;
public float GetVolume(){
return vol;
}
public void PitchAnalysis () {
int buf_size = 256;
int buf_size = buffer_size;
float[] spec_data = new float[buf_size];
AudioListener.GetSpectrumData (spec_data, 0, FFTWindow.Rectangular);
print (spec_data[100]);
mic_source.GetSpectrumData (spec_data, 0, FFTWindow.Rectangular);
//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 < 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)
}
//Medium Pitch
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++) {
mid_pitch_sum += Mathf.Abs (spec_data[spec_index]); //Only grabs the second Third values of the array (so the "low" pitch values)
int mid_cut = (buf_size / 6) - low_cut;
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<
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++) {
high_pitch_sum += Mathf.Abs (spec_data[spec_index]); //Only grabs the third Third values of the array (so the "low" pitch values)
int high_cut = buf_size - mid_cut;
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;
med_pitch = mid_pitch_sum / 80;
high_pitch = high_pitch_sum / 156;
low_pitch = low_pitch_sum / low_cut;
med_pitch = mid_pitch_sum / mid_cut;
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);
}
......@@ -99,21 +104,21 @@ public class AudioManager : MonoBehaviour {
bool threshold_reached = PlayerSound.instance.GetComponentInChildren<Light> ().spotAngle > light_angle_max;
if (!threshold_reached) {
return rms * light_angle_value;
return rms * light_angle_scaler;
} else {
return light_angle_max;
}
}
public int GetSplashForce () {
return (int) (rms * splash_force_value);
return (int) (rms * splash_force_scaler);
}
public Color GetLightColor () {
public Color GetLightColor_vol () {
Color output;
//Linear interpolation:
if (linear) {
float r = Mathf.Abs ((rms - volume_range_min) / volume_range_max);
if (linear_color_change) {
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);
} else {
......@@ -122,4 +127,9 @@ public class AudioManager : MonoBehaviour {
return output;
}
public Color GetLightColor_pitch () {
Color output = new Color (0, 0, 0, 1);
return output;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 0affc6b8d6d55dd4692f15bf27ff327d
timeCreated: 1490099850
guid: 8ccadfc64067d6e4db4be7af2125a53f
timeCreated: 1490115276
licenseType: Free
MonoImporter:
serializedVersion: 2
......
......@@ -6,6 +6,7 @@ public class PlayerSound : MonoBehaviour {
//LIGHT
private Light light_self;
public bool use_mic = false;
public bool pitch_2_color = false;
// Use this for initialization
void Start () {
......@@ -16,9 +17,11 @@ public class PlayerSound : MonoBehaviour {
// Update is called once per frame
void Update () {
if (use_mic && this.gameObject.tag == "Player") {
light_self.spotAngle = AM.i.GetLightAngle ();
light_self.spotAngle = AudioManager.instance.GetLightAngle ();;
light_self.color = AudioManager.instance.GetLightColor ();;
if (!pitch_2_color)
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 {
}
// Update is called once per frame
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) {
splash_force_mic = AudioManager.instance.GetSplashForce ();
if (5 < update_rate)
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
}
int[] current_buffer;
......
......@@ -10,7 +10,7 @@ public class Info : MonoBehaviour {
}
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 {
}
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:
- component: {fileID: 231648941}
- component: {fileID: 231648943}
m_Layer: 5
m_Name: Power
m_Name: Stats2
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
......@@ -6911,7 +6911,7 @@ RectTransform:
m_AnchorMin: {x: 1, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 70, y: 30}
m_SizeDelta: {x: 200, y: 30}
m_Pivot: {x: 1, y: 1}
--- !u!114 &231648941
MonoBehaviour:
......@@ -6945,7 +6945,7 @@ MonoBehaviour:
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Power
m_Text: Stats2
--- !u!222 &231648942
CanvasRenderer:
m_ObjectHideFlags: 0
......@@ -10285,27 +10285,27 @@ Prefab:
- target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a,
type: 2}
propertyPath: dampner
value: 0.98
value: 0.998
objectReference: {fileID: 0}
- target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a,
type: 2}
propertyPath: spread_bit_shift
value: 2
value: 1
objectReference: {fileID: 0}
- target: {fileID: 33696797248011756, guid: ef8622c6f07c11e47aae43d93e8e076a,
type: 2}
propertyPath: m_Mesh
value:
objectReference: {fileID: 4300000, guid: 07450b72d45f7884181c594c53bcbcf0, type: 2}
objectReference: {fileID: 4300000, guid: a425aed1037a97a48af5677546c45216, type: 2}
- target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a,
type: 2}
propertyPath: cols
value: 32
value: 64
objectReference: {fileID: 0}
- target: {fileID: 114249406734343344, guid: ef8622c6f07c11e47aae43d93e8e076a,
type: 2}
propertyPath: rows
value: 32
value: 64
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: ef8622c6f07c11e47aae43d93e8e076a, type: 2}
......@@ -28896,8 +28896,8 @@ GameObject:
m_Component:
- component: {fileID: 1200467580}
- component: {fileID: 1200467583}
- component: {fileID: 1200467579}
- component: {fileID: 1200467581}
- component: {fileID: 1200467579}
m_Layer: 0
m_Name: World
m_TagString: Untagged
......@@ -28913,16 +28913,16 @@ MonoBehaviour:
m_GameObject: {fileID: 1200467578}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7da32a16da5a37d49b11e79aa0e9b773, type: 3}
m_Script: {fileID: 11500000, guid: 8ccadfc64067d6e4db4be7af2125a53f, type: 3}
m_Name:
m_EditorClassIdentifier:
volume_range_min: 0
volume_range_max: 30
linear: 1
buffer_size: 22050
light_angle_value: 10
color_range_min: 3
color_range_max: 40
linear_color_change: 1
light_angle_scaler: 5
light_angle_max: 130
splash_force_value: 20
splash_force_scaler: 5
buffer_size: 64
--- !u!4 &1200467580
Transform:
m_ObjectHideFlags: 0
0000.5837028e.0000
0000.583702a0.0000
\ No newline at end of file
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment