SoundManager (日本語)

これは Bartbes と Nevon により制作されたゲームである Volley Brawl で使われている音声管理器です。

Copyright (c) 2011, Bart van Strien and Tommy Brunn
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

soundmanager = {}
soundmanager.queue = {}
soundmanager.playlist = {}
soundmanager.currentsong = -1

local function shuffle(a, b)
   return math.random(1, 2) == 1
end

-- 魔法をかけます。
function soundmanager:play(sndData)
   -- 音声データからソースを作成します。
   local src = love.audio.newSource(sndData, "static")
   -- キューへ配置します。
   table.insert(self.queue, src)
   -- そして再生します。
   love.audio.play(src)
end

-- 音楽に魔法をかけます。
function soundmanager:playMusic(first, ...)
   -- 現在再生中の音楽を全て停止します。
   for i, v in ipairs(self.playlist) do
      love.audio.stop(v)
   end
   -- テーブルまたは vararg が渡された場合に、
   -- 再生リストを組み立てるかどうか決定します。
   if type(first) == "table" then
      self.playlist = first
   else
      self.playlist = {first, ...}
   end
   self.currentsong = 1
   -- 再生
   love.audio.play(self.playlist[1])
end

-- なにかをかき混ぜます'
function soundmanager:shuffle(first, ...)
   local playlist
   if type(first) == "table" then
      playlist = first
   else
      playlist = {first, ...}
   end
   table.sort(playlist, shuffle)
   return unpack(playlist)
end

-- 更新
function soundmanager:update(dt)
   -- キューにある音声の再生が完了した、および削除されたか確認します。
   local removelist = {}
   for i, v in ipairs(self.queue) do
      if v:isStopped() then
         table.insert(removelist, i)
      end
   end
   -- ループにあるものは削除できないため、別のループを使用します。
   for i, v in ipairs(removelist) do
      table.remove(self.queue, v-i+1)
   end
   -- 必要であればプレイリストを進めます。
   if self.currentsong ~= -1 and self.playlist and self.playlist[self.currentsong]:isStopped() then
      self.currentsong = self.currentsong + 1
      if self.currentsong > #self.playlist then
         self.currentsong = 1
      end
      love.audio.play(self.playlist[self.currentsong])
   end
end