Benignly Designed Sound Manager
BDSM overwrites love.audio.newSource, love.audio.play and love.audio.stop, so that one source can be played multiple times.
love.audio.newSource(what, how)
Returns a new bdsm source. The parameters are the same as for the old love.audio.newSource().
love.audio.play(source) and source:play()
Plays the source and returns a handle to the player. The player is the same as love.audio's Source object. In contrast to love.audio, you can safely ignore the handles.
love.audio.stop(source) and source:stop()
Stops all instances of source. If source is omitted, it will stop all the sources.
The source code is rather short: Warning: OLD CODE
- Code: Select all
local newSource = love.audio.newSource
local stop = love.audio.stop
local function remove_stopped(sources)
local remove = {}
for _, s in pairs(sources) do
remove[s] = true
end
for s, _ in pairs(remove) do
sources[s] = nil
end
end
function love.audio.newSource(what, how)
return {
play = function(self)
remove_stopped(self.handles)
local s = newSource(what, how)
s:setLooping(self.looping)
s:setPitch(self.pitch)
s:setVolume(self.volume)
self.handles[s] = s
s:play()
return s
end,
stop = function(self)
for _, s in pairs(self.handles) do
s:stop()
end
self.handles = {}
end,
setLooping = function(self, v) self.looping = not not v end,
setPitch = function(self, v) self.pitch = tonumber(v) end,
setVolume = function(self, v) self.volume = tonumber(v) end,
looping = false,
pitch = 1,
volume = 1,
handles = {},
}
end
function love.audio.play(what)
assert(what.handles, "Can only play source objects.")
return what:play()
end
function love.audio.stop(what)
if what and what.stop then return what:stop() end
stop()
end
Example code:
- Code: Select all
require 'bdsm'
function love.load()
music = love.audio.newSource('music.ogg', 'stream')
music:setLooping(true) -- set default value for all players
music:setVolume(.3)
love.audio.play(music) -- the handle can be ignored
woosh = love.audio.newSource('woosh.ogg', 'static')
love.graphics.setFont(30)
end
function love.draw()
love.graphics.print('Press a key!', 20,20)
end
function love.keypressed()
local h = woosh:play() -- same as love.audio.play(woosh)
h:setPitch(.5 + math.random() * 3) -- set pitch for individual players
end
Can you see yourself using this?
If not: why?
What can be improved?
Edit:
This is now on github: https://github.com/vrld/Stuff/tree/master/bdsm