-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundManager.lua
118 lines (108 loc) · 3.2 KB
/
soundManager.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
SoundManager = {
Songs = {},
Items = {},
Length = {},
Volumes = {},
PlayTime = 0,
Count = 0,
Delay = 0,
Volume = 1,
Current = "",
Mode = "random"
}
-- Creates a new instance of the sound manager.
-- (Object) o: The base object to create this manager on.
function SoundManager:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o.Items = {}
o.Length = {}
o.Songs = {}
return o
end
-- Sets the volume for all sources in the manager.
-- (float) volume: the intensity of the sound.
function SoundManager:setVolume(volume)
print("Setting volume to " .. volume)
self.Volume = volume
for _, song in ipairs(self.Songs) do
print(song)
self.Items[song]:setVolume(volume)
end
end
-- Sets the mode for this sound manager.
-- (String) mode: loop, random, linear, noloop
function SoundManager:setMode(mode)
self.Mode = mode
end
-- Adds a new song to the manager.
-- (String) name: the name this song will have in this manager.
-- (String) path: the path to the sound file.
-- (int) length: the length (in seconds) of this sound
function SoundManager:addSong(name, path, length)
table.insert(self.Songs, name)
self.Items[name] = love.audio.newSource(path, "stream")
self.Length[name] = length
self.Items[name]:setVolume(self.Volume)
self.Count = self.Count + 1
end
-- Plays the song in this playlist with the given name.
-- (String) name: the name of the registered song.
function SoundManager:play(name)
if not name then
error("A name of a song must be provided.")
elseif not self.Items[name] then
error("The song " .. name .. " could not be found.")
end
self.Items[name]:play()
self.PlayTime = 0
self.Current = name
end
-- Stops the song according to Love2D behavior.
-- (String) name: the name of the song to stop.
function SoundManager:stop(name)
name = name or self.Current
self.Items[name]:stop()
end
-- Pauses the song according to Love2D behavior.
-- (String) name: the name of the song to stop.
function SoundManager:pause(name)
name = name or self.Current
self.Items[name]:pause()
end
-- Sets the delay between songs being played when one finishes.
-- (number) delay: The time to wait between playing another sound.
function SoundManager:setDelay(delay)
self.Delay = delay
end
-- Updates the sound manager.
function SoundManager:update(dt)
self.PlayTime = self.PlayTime + dt
if not self.Length[self.Current] then
print("No song selected. Selecting a random one...")
song = self.Songs[love.math.random(#self.Songs)]
print(song .. " selected.")
self:play(song)
elseif (self.PlayTime > self.Length[self.Current] + self.Delay) then
print("Stopping song " .. self.Current)
self:stop(self.Current)
local song
if self.Mode == "loop" then
print("Looping " .. self.Current)
song = self.Current
elseif self.Mode == "random" then
song = self.Songs[love.math.random(#self.Songs)]
elseif self.Mode == "linear" then
print("linear play mode hasn't been implemented.")
return
elseif self.Mode == "noloop" then
return
else
error("Invalid mode " .. self.Mode)
end
print("Now playing " .. song)
self:play(song)
end
-- Reset the volume for non-managed sound resource
end