PageRenderTime 16ms CodeModel.GetById 14ms app.highlight 0ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 7return function(self, sel, atoms)
 8    local id2sample = {}
 9    local sample2player = {}
10    local maxplayers = atoms[1] or 16
11    if not atoms[1] then pd.post("Safeload-logic: Setting max number of loaders to 16") end
12    self.inlets = 1
13    self.outlets = 1
14    function self:in_1(id,atoms) 
15        local id = atoms[1]
16        local file = atoms[2]
17        local chs = atoms[3]
18        local player = nil
19        local op = nil
20      
21        if id2sample[file] == nil then id2sample[file] = {} end
22
23        -- check what id is playing now
24        local oldsample = id2sample[id]
25        if oldsample == file then 
26            return 
27        end
28        id2sample[id] = file
29        -- check if sample is alredy loaded
30        -- if loaded, add id to id2sample 
31        if sample2player[file] ~= nil then
32            player = sample2player[file]
33            id2sample[file] = { [id] = id }
34            id2sample[id] =  file 
35            sample2player[player].count = sample2player[player].count + 1
36        else
37            -- if not loaded, check for free player
38            for i = 1, maxplayers do
39              if sample2player[i] == nil then
40                  player = i
41                  sample2player[file] = i
42                  sample2player[i] = { [file] = file, count = 1 }
43                  break
44              end
45            end
46        end
47        if not player then 
48            player = sample2player[oldsample] 
49        else
50            op = sample2player[oldsample]
51            if op then
52                sample2player[op].count = sample2player[op].count - 1
53            end
54        end
55        if not player then
56            pd.post("No free player! (and I don't know what to do)")
57        else
58            if oldsample and op then
59                if sample2player[op].count == 0 then 
60                    sample2player[op] = nil 
61                    sample2player[oldsample] = nil
62                end
63            end
64            self:outlet(1, "list", { player, file, chs, id })
65        end
66    end
67
68    return true
69end