-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathSoundResource.gd
83 lines (65 loc) · 1.97 KB
/
SoundResource.gd
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
## Resource used to carry an information how a sound should be played
class_name SoundResource
extends Resource
## Lowest random pitch
@export var pitch_min:float = 1.0
## Highest random pitch
@export var pitch_max:float = 1.0
## Volume of a played sound
@export_range(-80.0, +24.0) var volume:float = 0.0
## Time interval when sound is not played again
@export var retrigger_time:float = 0.032
## Pitch increase when repeated fast
@export var pitch_add:float = 0.0
## How fast pitch cooldown drops
@export var pitch_cooldown:float
## TODO: forgot the meaning in the code
@export var pitch_return:float
## Audio sample used
@export var sound:AudioStream
## keep track of trigger time
var last_play_time:float
## time since last trigger
var delta:float
## Audio player assigned to use this resource
var sound_player:SoundPlayer
## keep track of the pitch
var pitch:float
## Base audio sample getting, intended to be overriten
func get_sound()->AudioStream:
return sound
## Pitch calculation
func get_pitch()->float:
if delta < pitch_cooldown:
pitch = pitch + pitch_add
return pitch
elif delta < pitch_cooldown + pitch_return:
var mid_pitch:float = lerp(pitch_min, pitch_max, 0.5)
var t:float = (delta - pitch_cooldown) / pitch_return
pitch = lerp(pitch, mid_pitch, t)
else:
pitch = randf_range(pitch_min, pitch_max)
return pitch
## Volume getting function, meant to be overriten
func get_volume()->float:
return volume
## Plays the sound
func play(_sound_player:SoundPlayer)->void:
var time: = Time.get_ticks_msec() * 0.001
if time < last_play_time + retrigger_time:
return
delta = time - last_play_time
sound_player = _sound_player
last_play_time = time
sound_player.stream = get_sound()
sound_player.pitch_scale = get_pitch()
sound_player.volume_db = get_volume()
sound_player.play()
## Gets played by SoundManager singleton
func play_managed()->void:
SoundManager.play(self)
## Stops sound
func stop()->void:
if sound_player == null:
return
sound_player.stop()