aboutsummaryrefslogtreecommitdiff
path: root/src/sound.lua
blob: 5b530fc42729c4440fb590faf0fb8f9be1acd9d6 (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
40
41
42
local sound_bank = {}

function load_sounds_from(path)
  path = path or "res/mono"
  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"),
        }
        sound_bank[filepath][1]:setRelative(true)
        sound_bank[filepath][1]:setPosition(0, 0, 0)
      end
    end
  end
end

function play_sound(path, x, y)
  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
  source:setPosition(x or 0, y or 0, 0)
  la.play(source)
end