PageRenderTime 26ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/midilogic.pd_luax

http://github.com/jpburstrom/neu
Unknown | 175 lines | 168 code | 7 blank | 0 comment | 0 complexity | 6c25d424ac27061f6e2a5317546a2575 MD5 | raw file
  1-- "Logic" for page 2 of controller
  2-- A complete mess
  3return function(self, sel, atoms)
  4    function mode(sel)
  5    -- select main mode
  6        self:update_controller(self.v.mode, sel)
  7        self.v.mode = sel
  8        if sel == 3 or sel == 4 then 
  9            if self:calc_preset() then
 10                self.v.action = self.v.recstates[sel][self.curpreset]
 11                self:show_rec_play_action()
 12            else 
 13                action(0)
 14            end
 15        else 
 16            action(1)
 17            self:show_rec_play_action(1)
 18        end
 19        self.v.pendingsave = false
 20        self:do_save_blink()
 21        for i = self.ctl[2], self.ctl[2]+3 do self:ctlout(0, i) end
 22    end
 23    function action(sel)
 24    -- select action: load, save...
 25        prev = self.v.action
 26        self.v.action = sel
 27        if self.v.mode == 3 or self.v.mode == 4 then
 28            if self.v.action == prev then
 29                self:outlet(1, "list", {self.v.mode, "stop", self.curpreset})
 30                self.v.action = 0
 31            elseif self.v.action == 1 then
 32                self:outlet(1, "list", {self.v.mode, "play", self.curpreset})
 33            elseif self.v.action == 2 then
 34                self:outlet(1, "list", {self.v.mode, "rec", self.curpreset})
 35            end
 36            self.v.recstates[self.v.mode][self.curpreset] = self.v.action
 37            self:show_rec_play_action()
 38        else
 39            if self.v.action == 2 then
 40                if not self.v.pendingsave then 
 41                    self.v.pendingsave = true
 42                    self:verbose("Choose bank+slot, then press save again.")
 43                else 
 44                    self.v.pendingsave = false
 45                    self:verbose("Saved as " .. tostring(self.curpreset) ..".")
 46                    self:outlet(1, "list", {self.v.mode, "save", self.v.inst[self.v.mode], self.curpreset})
 47                end
 48                self:do_save_blink()
 49            elseif self.v.pendingsave then 
 50                self.v.pendingsave = false
 51                self:do_save_blink()
 52            end
 53        end
 54    end
 55    function bank(sel)
 56    -- select preset bank
 57        self:verbose(string.format("Bank %d chosen", sel))
 58        self.v.bank[self.v.mode] = sel
 59    end
 60    function slot(sel)
 61    -- select preset slot
 62        self:verbose(string.format("Slot %d chosen", sel))
 63        self.v.slot[self.v.mode] = sel
 64        self:preset()
 65    end
 66    function instsel(sel)
 67    --select instrument - on which instrument to apply the change
 68        if self.v.inst[self.v.mode] then self:ctlout(0, self.v.inst[self.v.mode]) end
 69        self.v.inst[self.v.mode] = sel
 70        self:ctlout(127, sel)
 71        self:outlet(1, "list", {self.v.mode, "inst", sel})
 72    end
 73
 74    --START defining variables
 75    self.midi = {}
 76    -- mode, action, bank, slot, inst
 77    self.ctl = {65,105,73,77,33}
 78    for i = self.ctl[1], self.ctl[1]+7 do self.midi[i] = {mode, i - self.ctl[1] + 1 } end
 79    for i = self.ctl[2], self.ctl[2]+3 do self.midi[i] = {action, i - self.ctl[2] + 1} end
 80    for i = self.ctl[3], self.ctl[3]+3 do self.midi[i] = {bank, i - self.ctl[3]} end
 81    for i = self.ctl[4], self.ctl[4]+3 do self.midi[i] = {slot, i - self.ctl[4]} end
 82    for i = self.ctl[5], self.ctl[5]+7 do self.midi[i] = {instsel, i - self.ctl[5] + 1 } end
 83    self.v = {}
 84    self.v.mode = 1
 85    self.v.action = 1
 86    self.v.pendingsave = false
 87    self.v.doneinput = false
 88    self.v.recstates = {}
 89    for i = 3,4 do 
 90        self.v.recstates[i] = {} 
 91        for j = 0,15 do self.v.recstates[i][j] = 0 end 
 92    end
 93    self.v.bank = {}
 94    self.v.slot = {}
 95    self.v.inst = {}
 96    for i = 1,8 do self.v.bank[i], self.v.inst[i], self.v.slot[i] = 0, 1, 0 end
 97    self.curpreset = 0
 98    --END defining variables
 99
100    --Inlets: Midi ctlin
101    self.inlets = 1
102    --Outlets: Messages to dsp, messages to ui/controller
103    self.outlets = 2
104
105    function self:in_1_list(sel, atoms)
106        --Midi ctlin messages (value, control). No error checking!
107        s = (sel[1] == 0 or sel[1] == 127) and self.midi[sel[2]] or nil
108        if s then s[1](s[2]) end
109        if sel[1] == 0 then self:ctlout(126, sel[2]) end
110    end
111
112    function self:calc_preset()
113        -- Calculate preset number from bank/slot
114        bank = self.v.bank[self.v.mode]
115        slot = self.v.slot[self.v.mode]
116        if bank ~= nil and slot ~= nil then
117            sel = (bank * 4) + slot
118            self.curpreset = sel
119            return true
120        else
121            return false
122        end
123    end
124
125    function self:preset()
126        --Change preset and send out load message, in case we need to
127        if self:calc_preset() then
128            self:verbose(string.format("Preset %d chosen", self.curpreset))
129        else return
130        end
131        inst = self.v.inst[self.v.mode]
132        if self.v.action == 1 then
133            self:verbose(string.format(
134            "Mode %d loading preset %d", self.v.mode, sel))
135            self:outlet(1, "list", {self.v.mode, "load", inst, sel} )
136        else self:outlet(1, "list", {self.v.mode, "select", inst, sel} )
137        end
138        if self.v.mode == 3 or self.v.mode == 4 then
139            self.v.action = self.v.recstates[self.v.mode][self.curpreset]
140            self:show_rec_play_action()
141        end
142
143    end
144    function self:update_controller(cur, new)
145        --Update controllers on mode change
146        --args: current (previous) mode, new mode
147        self:ctlout(0, self.v.bank[cur] + self.ctl[3])
148        self:ctlout(127, self.v.bank[new] + self.ctl[3])
149        self:ctlout(0, self.v.slot[cur] + self.ctl[4])
150        self:ctlout(127, self.v.slot[new] + self.ctl[4])
151        self:ctlout(0, self.v.inst[cur] )
152        self:ctlout(127, self.v.inst[new])
153    end
154    function self:ctlout(v, c)
155        --Send ctlout messages
156        self:outlet(2, "ctlout", {v, c})
157    end
158    function self:verbose(s)
159        --Print method which might be commented out
160        pd.post(s)
161    end
162    function self:do_save_blink()
163        --start/stop save blink
164        if self.v.pendingsave then i = 1 else i = 0 end
165        self:outlet(2, "blink", {"save", i}) 
166    end
167    function self:show_rec_play_action(a)
168        --play/rec/stop blink/blink/no-blink
169        a = a or self.v.action + 1
170        actions = {"stop", "play", "rec"}
171        self:outlet (2, "blink", {actions[a]})
172    end
173
174    return true
175end