|
| 1 | +--[[ |
| 2 | +This software is distributed under the terms of the MIT license, also |
| 3 | +known as the Expat license. |
| 4 | +
|
| 5 | +Copyright (C) 2011 by Bart van Strien and Tommy Brunn |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in |
| 15 | +all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +THE SOFTWARE. |
| 24 | +]] |
| 25 | + |
| 26 | +soundmanager = {} |
| 27 | +soundmanager.queue = {} |
| 28 | +soundmanager.playlist = {} |
| 29 | +soundmanager.currentsong = -1 |
| 30 | +soundmanager.playsfx = true |
| 31 | +soundmanager.playmusic = true |
| 32 | + |
| 33 | +local function shuffle(a, b) |
| 34 | + return math.random(1, 2) == 1 |
| 35 | +end |
| 36 | + |
| 37 | +--do the magic |
| 38 | +function soundmanager:play(sndData) |
| 39 | + if not self.playsfx then return end |
| 40 | + --make a source out of the sound data |
| 41 | + local src = love.audio.newSource(sndData, "static") |
| 42 | + --put it in the queue |
| 43 | + table.insert(self.queue, src) |
| 44 | + --and play it |
| 45 | + love.audio.play(src) |
| 46 | +end |
| 47 | + |
| 48 | +--do the music magic |
| 49 | +function soundmanager:playMusic(first, ...) |
| 50 | + if not self.playmusic then return end |
| 51 | + self:stopMusic() |
| 52 | + --decide if we were passed a table or a vararg, |
| 53 | + --and assemble the playlist |
| 54 | + if type(first) == "table" then |
| 55 | + self.playlist = first |
| 56 | + else |
| 57 | + self.playlist = {first, ...} |
| 58 | + end |
| 59 | + self.currentsong = 1 |
| 60 | + --play |
| 61 | + love.audio.play(self.playlist[1]) |
| 62 | +end |
| 63 | + |
| 64 | +function soundmanager:stopMusic() |
| 65 | + --stop all currently playing music |
| 66 | + for i, v in ipairs(self.playlist) do |
| 67 | + love.audio.stop(v) |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +--do some shufflin' |
| 72 | +function soundmanager:shuffle(first, ...) |
| 73 | + local playlist |
| 74 | + if type(first) == "table" then |
| 75 | + playlist = first |
| 76 | + else |
| 77 | + playlist = {first, ...} |
| 78 | + end |
| 79 | + table.sort(playlist, shuffle) |
| 80 | + return unpack(playlist) |
| 81 | +end |
| 82 | + |
| 83 | +--update |
| 84 | +function soundmanager:update(dt) |
| 85 | + --check which sounds in the queue have finished, and remove them |
| 86 | + local removelist = {} |
| 87 | + for i, v in ipairs(self.queue) do |
| 88 | + if v:isStopped() then |
| 89 | + table.insert(removelist, i) |
| 90 | + end |
| 91 | + end |
| 92 | + --we can't remove them in the loop, so use another loop |
| 93 | + for i, v in ipairs(removelist) do |
| 94 | + table.remove(self.queue, v-i+1) |
| 95 | + end |
| 96 | + --advance the playlist if necessary |
| 97 | + if self.playmusic and self.currentsong ~= -1 and self.playlist and self.playlist[self.currentsong]:isStopped() then |
| 98 | + self.currentsong = self.currentsong + 1 |
| 99 | + if self.currentsong > #self.playlist then |
| 100 | + self.currentsong = 1 |
| 101 | + end |
| 102 | + love.audio.play(self.playlist[self.currentsong]) |
| 103 | + end |
| 104 | +end |
0 commit comments