Skip to content

Commit 7fd1e96

Browse files
author
Corne van Rensburg
committed
made a few changes
1 parent 600ab80 commit 7fd1e96

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Audio/AudioController.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ private class AudioJob {
3838
public AudioAction action;
3939
public AudioType type;
4040
public bool fade;
41-
public float delay;
41+
public WaitForSeconds delay;
4242

4343
public AudioJob(AudioAction _action, AudioType _type, bool _fade, float _delay) {
4444
action = _action;
4545
type = _type;
4646
fade = _fade;
47-
delay = _delay;
47+
delay = _delay > 0f ? new WaitForSeconds(_delay) : null;
4848
}
4949
}
5050

@@ -118,12 +118,13 @@ private void RemoveConflictingJobs(AudioType _type) {
118118

119119
// cancel jobs that share the same audio track
120120
AudioType _conflictAudio = AudioType.None;
121+
AudioTrack _audioTrackNeeded = GetAudioTrack(_type, "Get Audio Track Needed");
121122
foreach (DictionaryEntry _entry in m_JobTable) {
122123
AudioType _audioType = (AudioType)_entry.Key;
123124
AudioTrack _audioTrackInUse = GetAudioTrack(_audioType, "Get Audio Track In Use");
124-
AudioTrack _audioTrackNeeded = GetAudioTrack(_type, "Get Audio Track Needed");
125125
if (_audioTrackInUse.source == _audioTrackNeeded.source) {
126126
_conflictAudio = _audioType;
127+
break;
127128
}
128129
}
129130
if (_conflictAudio != AudioType.None) {
@@ -132,19 +133,23 @@ private void RemoveConflictingJobs(AudioType _type) {
132133
}
133134

134135
private IEnumerator RunAudioJob(AudioJob _job) {
135-
yield return new WaitForSeconds(_job.delay);
136+
if (_job.delay != null) yield return _job.delay;
136137

137138
AudioTrack _track = GetAudioTrack(_job.type); // track existence should be verified by now
138139
_track.source.clip = GetAudioClipFromAudioTrack(_job.type, _track);
139140

141+
float _initial = 0f;
142+
float _target = 1f;
140143
switch (_job.action) {
141144
case AudioAction.START:
142145
_track.source.Play();
143146
break;
147+
case AudioAction.STOP when !_job.fade:
148+
_track.source.Stop();
149+
break;
144150
case AudioAction.STOP:
145-
if (!_job.fade) {
146-
_track.source.Stop();
147-
}
151+
_initial = 1f;
152+
_target = 0f;
148153
break;
149154
case AudioAction.RESTART:
150155
_track.source.Stop();
@@ -154,17 +159,19 @@ private IEnumerator RunAudioJob(AudioJob _job) {
154159

155160
// fade volume
156161
if (_job.fade) {
157-
float _initial = _job.action == AudioAction.START || _job.action == AudioAction.RESTART ? 0 : 1;
158-
float _target = _initial == 0 ? 1 : 0;
159162
float _duration = 1.0f;
160163
float _timer = 0.0f;
161164

162-
while (_timer < _duration) {
165+
while (_timer <= _duration) {
163166
_track.source.volume = Mathf.Lerp(_initial, _target, _timer / _duration);
164167
_timer += Time.deltaTime;
165168
yield return null;
166169
}
167170

171+
// if _timer was 0.9999 and Time.deltaTime was 0.01 we would not have reached the target
172+
// make sure the volume is set to the value we want
173+
_track.source.volume = _target;
174+
168175
if (_job.action == AudioAction.STOP) {
169176
_track.source.Stop();
170177
}

0 commit comments

Comments
 (0)