-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleaf_audiofx.lua
126 lines (84 loc) · 3.08 KB
/
leaf_audiofx.lua
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function leaf.playlist(main, back, ...)
leaf.tapes = {}
local cntt = 0
for _, track in ipairs({...}) do
leaf.tapes[cntt] = love.audio.newSource('tracks/' .. track, 'static')
cntt = cntt + 1
end
if main then
assert(love.filesystem.getInfo('tracks/' .. main), 'main tape not found')
leaf.tapes.main = love.audio.newSource('tracks/' .. main, 'stream')
end
if back then
assert(love.filesystem.getInfo('tracks/' .. back), 'main tape not found')
leaf.tapes.back = love.audio.newSource('tracks/' .. back, 'stream')
end
end
local gramophone = {}
function gramophone.theme()
leaf.tapes.main:setLooping(true)
leaf.tapes.main:play()
leaf.tapes.back:setLooping(true)
leaf.tapes.back:play()
end
function gramophone.set(thm, stt)
assert(leaf.tapes, 'no playlist defined yet')
if thm == 'main' then
if stt then leaf.tapes.main:resume()
else leaf.tapes.main:pause() end
end
if thm == 'back' then
if stt then leaf.tapes.back:resume()
else leaf.tapes.back:pause() end
end
end
function gramophone.play(tape, track, loop)
if loop == nil then loop = false end
assert(leaf.tapes, 'no playlist defined yet')
assert(tape and track, 'invalid gramophone\'s parameters')
-- 0 to 7 tracks --
track = math.min(math.max(track, 0), 7)
assert(leaf.tapes[tape], 'invalid tape ('.. tape .. ')')
leaf.tapes[tape]:setLooping(loop)
leaf.tapes[tape]:setVolume((8 - track) / 8)
leaf.tapes[tape]:play()
end
function gramophone.pause(track)
if not leaf.disco[track] then return end
leaf.disco[track]:pause()
end
function gramophone.resume(track)
if not leaf.disco[track] then return end
leaf.disco[track]:resume()
end
function gramophone.fade_in(track, speed)
-- Make speed/s as speed/dt
speed = (speed * love.timer.getDelta()) / love.timer.getFPS()
if not leaf.disco[track] then return end
if leaf.disco[track].fade then
-- Resume if is not playing --
if leaf.disco[track].fade == 0 then
leaf.disco[track]:resume()
end
-- While not at max --
if leaf.disco[track].fade < 1 then
local volume = math.min(leaf.disco[track].fade + speed, 1)
leaf.disco[track]:setVolume(volume)
leaf.disco[track].fade = leaf.disco[track]:getVolume()
-- Clear fade at the end --
else leaf.disco[track].fade = nil end
else leaf.disco[track].fade = leaf.disco[track]:getVolume() end
end
function gramophone.fadeout(track, speed)
-- Make speed/s as speed/dt
speed = (speed * love.timer.getDelta()) / love.timer.getFPS()
if not leaf.disco[track] then return end
if not leaf.disco[track].fade then
leaf.disco[track].fade = leaf.disco[track]:getVolume()
elseif leaf.disco[track].fade > 0 then
local valume = math.max(leaf.disco[track].fade - speed, 0)
leaf.disco[track]:setVolume(valume)
leaf.disco[track].fade = leaf.disco[track]:getVolume()
else gramophone.stop(track) end
end
leaf.gramo = gramophone