-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVolumeFlag.cs
91 lines (76 loc) · 2.69 KB
/
VolumeFlag.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/*
* @Author: Sean Rannie
* @Date: Mar/19/2019
*
* An abstract class that evaluates the recording
*/
public abstract class VolumeFlag : MonoBehaviour
{
[Header("Set in Inspector")]
public Text prompt; //Text that is written to
public string triggeredText = "Flag has been triggered!"; //The string that will be written to text field
public string untriggeredText = "Flag has not been triggered!";
public float minPercentTrigger = 0.5f; //Percent needed to trigger flag
public bool enable = true; //Enables evaluation
public bool debug = false; //Enables debug methods
protected bool _flag; //Flag value
private int _numTriggered; //Number of samples triggered
private int _sampleSize; //Number of samples
//Triggered at starting time
public void Start()
{
if (GetComponent<MicrophoneReader>() != null)
GetComponent<MicrophoneReader>().AddToEvaluatorDelegate(this);
else
Debug.LogError("ERROR: VolumeFlag added to object without microphone listener.");
}
//Evaluates a single average
public void Evaluate(float avg)
{
//If enabled
if (enable)
{
CheckVolume(avg);
if (prompt != null)
prompt.text = _flag ? triggeredText : untriggeredText;
if (debug && _flag)
Debug.Log(triggeredText);
}
}
//Evaluates a collection of samples
public void Evaluate(Queue<float> samples)
{
//If enabled
if (enable)
{
_sampleSize = samples.Count;
foreach(float sample in samples)
{
if (CheckVolume(sample))
_numTriggered++;
}
//Flag is triggered by percent threshold
_flag = PercentFlag >= minPercentTrigger;
if(prompt != null)
prompt.text = _flag ? triggeredText : untriggeredText;
if (debug && _flag)
Debug.Log(triggeredText);
}
}
//Checks a single volume sample
protected abstract bool CheckVolume(float avg);
//Property of the flag's value
public bool Flag
{
get { return _flag; }
}
//Property of the percent flag's value
public virtual float PercentFlag
{
get { return (float)_numTriggered / _sampleSize; }
}
}