aboutsummaryrefslogtreecommitdiff
path: root/src/sound.lua
blob: 4d31861ace69ed84c186a1b7101bd1a9885d4b6e (plain)
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
local sound_bank = {}

function load_sounds_from(path)
  path = path or "res/sound"
  local files = lf.getDirectoryItems(path)

  for _, file in ipairs(files) do
    local filepath = path.."/"..file

    if lf.getInfo(filepath).type == "directory" then
      load_sounds_from(filepath)
    else
      if is_filetype(filepath, {"wav", "ogg", "mp3"}) then
        sound_bank[filepath] = {
          size = 1,
          [1] = la.newSource(filepath, "static"),
        }
      end
    end
  end
end

function play_sound(path)
  sound = sound_bank[path]
  local source = nil

  for i = 1, sound.size do
    if not sound[i]:isPlaying() then
      source = sound[i]
    end
  end
  if not source then
    sound.size = sound.size + 1
    sound[sound.size] = sound[1]:clone()
    source = sound[sound.size]
  end
  la.play(source)
end