diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-03-18 17:50:33 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-03-18 18:03:04 +0000 |
| commit | a49382c7b46c7bdf1c105c0783c7bdcd0bb70e4a (patch) | |
| tree | c1f800736e1905514aa913630ee31a720e770c9e /lib | |
| parent | docs/internal: add builtins and invoke modules (diff) | |
| download | alive-a49382c7b46c7bdf1c105c0783c7bdcd0bb70e4a.tar.gz alive-a49382c7b46c7bdf1c105c0783c7bdcd0bb70e4a.zip | |
fork ops before eval cycle
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/midi/core.moon | 2 | ||||
| -rw-r--r-- | lib/midi/launchctl.moon | 44 | ||||
| -rw-r--r-- | lib/random.moon | 10 | ||||
| -rw-r--r-- | lib/time.moon | 43 | ||||
| -rw-r--r-- | lib/util.moon | 33 |
5 files changed, 51 insertions, 81 deletions
diff --git a/lib/midi/core.moon b/lib/midi/core.moon index d5259ad..8378b1d 100644 --- a/lib/midi/core.moon +++ b/lib/midi/core.moon @@ -101,7 +101,7 @@ apply_range = (range, val) -> when 'rad' then val / 64 * math.pi when 'deg' then val / 128 * 360 else - error "unknown range #{@range}" + error "unknown range #{range}" elseif range.type == 'num' val / 128 * range\unwrap! else diff --git a/lib/midi/launchctl.moon b/lib/midi/launchctl.moon index 6408528..9f29922 100644 --- a/lib/midi/launchctl.moon +++ b/lib/midi/launchctl.moon @@ -19,9 +19,7 @@ range can be one of: - 'deg' [ 0 - 360[ - (num) [ 0 - num[" - new: => - super 'num' - @steps = {} + new: => super 'num' setup: (inputs) => { port, i, start, @@ -42,23 +40,23 @@ range can be one of: { :port, :i, :start, :chan, :steps, :range } = @inputs if steps\dirty! - while steps! > #@steps - table.insert @steps, 0 - while steps! < #@steps - table.remove @steps + while steps! > #@state + table.insert @state, 0 + while steps! < #@state + table.remove @state - curr_i = i! % #@steps + curr_i = i! % #@state if port\dirty! changed = false for msg in port!\receive! if msg.status == 'control-change' and msg.chan == chan! rel_i = msg.a - start! - if rel_i >= 0 and rel_i < #@steps - @steps[rel_i+1] = msg.b + if rel_i >= 0 and rel_i < #@state + @state[rel_i+1] = msg.b changed = rel_i == curr_i - @out\set apply_range range, @steps[curr_i+1] if changed + @out\set apply_range range, @state[curr_i+1] if changed else - @out\set apply_range range, @steps[curr_i+1] + @out\set apply_range range, @state[curr_i+1] class gate_seq extends Op @doc: "(launctl/gate-seq port i start chan [steps]) - Gate-Sequencer @@ -68,7 +66,7 @@ steps defaults to 8." new: => super 'bool', false - @steps = {} + @state = {} setup: (inputs) => { port, i, start, chan, steps } = match 'midi/port num num num num?', inputs @@ -91,34 +89,34 @@ steps defaults to 8." display: (i, active) => { :port, :start, :chan } = @unwrap_all! - port\send 'note-on', chan, (start + i), light @steps[i+1], active + port\send 'note-on', chan, (start + i), light @state[i+1], active tick: => { :port, :i, :start, :chan, :steps } = @inputs if steps\dirty! - while steps! > #@steps - table.insert @steps, false - while steps! < #@steps - table.remove @steps + while steps! > #@state + table.insert @state, false + while steps! < #@state + table.remove @state - curr_i = i! % #@steps + curr_i = i! % #@state if port\dirty! for msg in port!\receive! if msg.status == 'note-on' and msg.chan == chan! rel_i = msg.a - start! - if rel_i >= 0 and rel_i < #@steps - @steps[rel_i+1] = not @steps[rel_i+1] + if rel_i >= 0 and rel_i < #@state + @state[rel_i+1] = not @state[rel_i+1] @display rel_i, rel_i == curr_i if i\dirty! - prev_i = (curr_i - 1) % #@steps + prev_i = (curr_i - 1) % #@state @display curr_i, true @display prev_i, false - @out\set @steps[curr_i+1] + @out\set @state[curr_i+1] { 'gate-seq': gate_seq diff --git a/lib/random.moon b/lib/random.moon index 7cabdf0..b97671d 100644 --- a/lib/random.moon +++ b/lib/random.moon @@ -8,7 +8,7 @@ apply_range = (range, val) -> when 'rad' then val * 2 * math.pi when 'deg' then val * 360 else - error "unknown range #{@range}" + error "unknown range #{range}" elseif range.type == 'num' val * range\unwrap! else @@ -31,7 +31,7 @@ generates a random value in range on create and trigger. super 'num' @gen! - gen: => @val = math.random! + gen: => @state = { math.random! } setup: (inputs) => { trig, range } = match 'bang? any?', inputs @@ -41,7 +41,7 @@ generates a random value in range on create and trigger. tick: => @gen! if @inputs.trig and @inputs.trig\dirty! - @out\set apply_range @inputs.range, @val + @out\set apply_range @inputs.range, @state[1] vec_ = (n) -> class vec extends Op @@ -54,7 +54,7 @@ each component is in range. super "vec#{n}" @gen! - gen: => @val = for i=1,n do math.random! + gen: => @state = for i=1,n do math.random! setup: (inputs) => { trig, range } = match 'bang? any?', inputs @@ -64,7 +64,7 @@ each component is in range. tick: => @gen! if @inputs.trig and @inputs.trig\dirty! - @out\set [apply_range @inputs.range, v for v in *@val] + @out\set [apply_range @inputs.range, v for v in *@state] vec.__name = "vec#{n}" vec diff --git a/lib/time.moon b/lib/time.moon index 6e70355..8e9aded 100644 --- a/lib/time.moon +++ b/lib/time.moon @@ -47,7 +47,7 @@ wave selects the wave shape from the following: new: => super 'num' - @phase = 0 + @state.phase or= 0 default_wave = Value.str 'sin' setup: (inputs, scope) => @@ -62,11 +62,11 @@ wave selects the wave shape from the following: if @inputs.clock\dirty! { :clock, :freq, :wave } = @unwrap_all! - @phase += clock.dt * freq + @state.phase += clock.dt * freq @out\set switch wave - when 'sin' then .5 + .5 * math.cos @phase * tau - when 'saw' then @phase % 1 - when 'tri' then math.abs (2*@phase % 2) - 1 + when 'sin' then .5 + .5 * math.cos @state.phase * tau + when 'saw' then @state.phase % 1 + when 'tri' then math.abs (2*@state.phase % 2) - 1 else error "unknown wave type" class ramp extends Op @@ -76,7 +76,7 @@ ramps from 0 to max (default same as ramp) once every period seconds." new: => super 'num' - @phase = 0 + @state.phase or= 0 setup: (inputs, scope) => { clock, period, max } = match 'clock? num num?', inputs @@ -90,21 +90,22 @@ ramps from 0 to max (default same as ramp) once every period seconds." if clock_dirty { :clock, :period, :max } = @unwrap_all! max or= period - @phase += clock.dt / period + @state.phase += clock.dt / period - while @phase >= 1 - @phase -= 1 + while @state.phase >= 1 + @state.phase -= 1 if clock_dirty or (@inputs.max and @inputs.max\dirty!) - @out\set @phase * max + @out\set @state.phase * max class tick extends Op @doc: "(tick [clock] period) - count ticks counts upwards by one every period seconds and returns the number of completed ticks." new: => - @phase, @count = 0, 0 - super 'num', @count + super 'num', 0 + @state.phase or= 0 + @state.count or= 0 setup: (inputs, scope) => { clock, period } = match 'clock? num', inputs @@ -115,12 +116,12 @@ counts upwards by one every period seconds and returns the number of completed t tick: => if @inputs.clock\dirty! { :clock, :period, :max } = @unwrap_all! - @phase += clock.dt / period + @state.phase += clock.dt / period - while @phase >= 1 - @phase -= 1 - @count += 1 - @out\set @count + while @state.phase >= 1 + @state.phase -= 1 + @state.count += 1 + @out\set @state.count class every extends Op @doc: "(every [clock] period) - trigger every period seconds @@ -128,7 +129,7 @@ class every extends Op returns true once every period seconds." new: => super 'bang' - @phase = 0 + @state.phase or= 0 setup: (inputs, scope) => { clock, period } = match 'clock? num', inputs @@ -139,10 +140,10 @@ returns true once every period seconds." tick: => if @inputs.clock\dirty! { :clock, :period, :max } = @unwrap_all! - @phase += clock.dt / period + @state.phase += clock.dt / period - while @phase >= 1 - @phase -= 1 + while @state.phase >= 1 + @state.phase -= 1 @out\set true { diff --git a/lib/util.moon b/lib/util.moon index 79ee721..1fddca4 100644 --- a/lib/util.moon +++ b/lib/util.moon @@ -71,35 +71,6 @@ when i is a num, it is (floor)ed and the matching argument (starting from 0) is if active and active\dirty! @out\set active! ---class switch_pause extends Op --- @doc: "(switch- i v0 [v1 v2...]) - switch and pause multiple inputs --- ---like (switch ...) except that the unused inputs are paused." --- --- setup: (@i, ...) => --- @choices = { ... } --- --- typ = @choices[1].type --- for inp in *@choices[2,] --- assert inp.type == typ, "not all values have the same type: #{typ} != #{inp.type}" --- --- @out = Stream typ --- @out --- --- tick: => --- i = @i\unwrap! --- active = switch i --- when true --- @choices[1] --- when false --- @choices[2] --- else --- i = 1 + (math.floor i) % #@choices --- @choices[i] --- --- @out\set if active --- active\unwrap! - class edge extends Op @doc: "(edge bool) - convert rising edges to bangs" new: => super 'bang' @@ -110,9 +81,9 @@ class edge extends Op tick: => now = @inputs.value! - if now and not @last + if now and not @state.last @out\set true - @last = now + @state.last = now class default extends Op @doc: "(default stream default) - provide a default value for an event stream |
