/safeload-logic.pd_luax

http://github.com/jpburstrom/neu · Unknown · 69 lines · 65 code · 4 blank · 0 comment · 0 complexity · 78185d20229a2dd74b1c6080c7896328 MD5 · raw file

  1. --Maps incoming (playerID, sample, channels) to (preloaderID, sample, channels)
  2. --Taking care of all bad things,
  3. --in the code below, "player" is a bad name for "preloader".
  4. --and "id" is perhaps the $0 of the sampleplayer abstraction.
  5. --Johannes Burstrรถm 2009. (excuse the code soup)
  6. return function(self, sel, atoms)
  7. local id2sample = {}
  8. local sample2player = {}
  9. local maxplayers = atoms[1] or 16
  10. if not atoms[1] then pd.post("Safeload-logic: Setting max number of loaders to 16") end
  11. self.inlets = 1
  12. self.outlets = 1
  13. function self:in_1(id,atoms)
  14. local id = atoms[1]
  15. local file = atoms[2]
  16. local chs = atoms[3]
  17. local player = nil
  18. local op = nil
  19. if id2sample[file] == nil then id2sample[file] = {} end
  20. -- check what id is playing now
  21. local oldsample = id2sample[id]
  22. if oldsample == file then
  23. return
  24. end
  25. id2sample[id] = file
  26. -- check if sample is alredy loaded
  27. -- if loaded, add id to id2sample
  28. if sample2player[file] ~= nil then
  29. player = sample2player[file]
  30. id2sample[file] = { [id] = id }
  31. id2sample[id] = file
  32. sample2player[player].count = sample2player[player].count + 1
  33. else
  34. -- if not loaded, check for free player
  35. for i = 1, maxplayers do
  36. if sample2player[i] == nil then
  37. player = i
  38. sample2player[file] = i
  39. sample2player[i] = { [file] = file, count = 1 }
  40. break
  41. end
  42. end
  43. end
  44. if not player then
  45. player = sample2player[oldsample]
  46. else
  47. op = sample2player[oldsample]
  48. if op then
  49. sample2player[op].count = sample2player[op].count - 1
  50. end
  51. end
  52. if not player then
  53. pd.post("No free player! (and I don't know what to do)")
  54. else
  55. if oldsample and op then
  56. if sample2player[op].count == 0 then
  57. sample2player[op] = nil
  58. sample2player[oldsample] = nil
  59. end
  60. end
  61. self:outlet(1, "list", { player, file, chs, id })
  62. end
  63. end
  64. return true
  65. end