diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/debug.moon | 16 | ||||
| -rw-r--r-- | lib/midi/core.moon | 66 | ||||
| -rw-r--r-- | lib/midi/init.moon | 50 | ||||
| -rw-r--r-- | lib/time.moon | 119 |
4 files changed, 129 insertions, 122 deletions
diff --git a/lib/debug.moon b/lib/debug.moon index 08a7f78..1457cee 100644 --- a/lib/debug.moon +++ b/lib/debug.moon @@ -1,15 +1,19 @@ import Op from require 'core' +import ValueInput, EventInput from require 'core.base' +import match from require 'core.pattern' class out extends Op - @doc: "(out name-str value) - log value to the console" + @doc: "(out [name-str?] value) - log value to the console" - setup: (params) => - super params - assert @inputs[2], "need a value" - @assert_types 'str', @inputs[2].type + setup: (inputs) => + { name, value } = match 'str? any', inputs + super + name: name and ValueInput name + value: ValueInput value tick: => - L\print @unwrap_inputs! + { :name, :value } = @unwrap_all! + L\print if name then name, value else value { :out diff --git a/lib/midi/core.moon b/lib/midi/core.moon index 7f661ed..3bf47a9 100644 --- a/lib/midi/core.moon +++ b/lib/midi/core.moon @@ -1,6 +1,8 @@ import RtMidiIn, RtMidiOut, RtMidi from require 'luartmidi' import band, bor, lshift, rshift from require 'bit32' import Op, Registry from require 'core' +import ValueInput, EventInput from require 'core.base' +import match from require 'core.pattern' MIDI = { [0x9]: 'note-on' @@ -62,58 +64,58 @@ class MidiPort class input extends Op @doc: "(midi/input name) - create a MIDI input port" - new: => - super 'midi/port' - @impulses = { Registry.active!.kr } + new: => super 'midi/port' - setup: (params) => - super params - @assert_types 'str' + setup: (inputs) => + { name } = match 'str', inputs + super + name: ValueInput name + root: EventInput Registry.active!.kr - tick: (first) => - { name } = @inputs - if first or name\dirty! - @out\set MidiPort find_port RtMidiIn, name\unwrap! + tick: => + if @inputs.name\dirty! + @out\set MidiPort find_port RtMidiIn, @inputs.name! @out\unwrap!\tick! class output extends Op @doc: "(midi/output name) - create a MIDI output port" - new: => - super 'midi/port' + new: => super 'midi/port' + + setup: (inputs) => + { name } = match 'str', inputs + super + name: ValueInput name + root: EventInput Registry.active!.kr - setup: (params) => - super params - @assert_types 'str' + tick: => + if @inputs.name\dirty! + @out\set MidiPort nil, find_port RtMidiOut, @inputs.name! - tick: (first) => - { name } = @inputs - if first or name\dirty! - @out\set MidiPort nil, find_port RtMidiOut, name\unwrap! + @out\unwrap!\tick! class inout extends Op @doc: "(midi/inout inname outname) - create a bidirectional MIDI port" - new: => - super 'midi/port' - @impulses = { Registry.active!.kr } - - setup: (params) => - super params - @assert_types 'str', 'str' + new: => super 'midi/port' - tick: (first) => - { inp, out } = @inputs + setup: (inputs) => + { inp, out } = match 'str, str', inputs + super + inp: ValueInput inp + out: ValueInput out + root: EventInput Registry.active!.kr - if first or inp\dirty! or out\dirty! - inp, out = inp\unwrap!, out\unwrap! - @out\set MidiPort (find_port RtMidiIn, inp), (find_port RtMidiOut, out) + tick: => + { :inp, :out } = @inputs + if inp\dirty! or out\dirty! + @out\set MidiPort (find_port RtMidiIn, inp!), (find_port RtMidiOut, out!) @out\unwrap!\tick! apply_range = (range, val) -> - if range.type == 'str' + if range\type! == 'str' switch range\unwrap! when 'raw' then val when 'uni' then val / 128 diff --git a/lib/midi/init.moon b/lib/midi/init.moon index b0f2f6a..0cbe5e0 100644 --- a/lib/midi/init.moon +++ b/lib/midi/init.moon @@ -1,5 +1,7 @@ import Value, Op from require 'core' import input, output, inout, apply_range from require 'lib.midi.core' +import ValueInput, EventInput from require 'core.base' +import match from require 'core.pattern' class gate extends Op @doc: "(midi/gate port note [chan]) - gate from note-on and note-off messages" @@ -7,18 +9,21 @@ class gate extends Op new: => super 'bool', false - setup: (params) => - super params - @inputs[3] or= Value.num -1 - @assert_types 'midi/port', 'num', 'num' - @impulses = { @inputs[1]\unwrap! } + setup: (inputs) => + { port, note, chan } = match '=midi/port num num?', inputs + super + port: EventInput port.value! + note: ValueInput note + chan: ValueInput chan or Value.num -1 tick: => - local port, note, chan - { port, note, chan } = [i\unwrap! for i in *@inputs] + { port, note, chan } = @inputs + + if note\dirty! or chan\dirty! + @out\set false for msg in port\receive! - if msg.a == note and (chan == -1 or msg.chan == chan) + if msg.a == note! and (chan == -1 or msg.chan == chan!) if msg.status == 'note-on' @out\set true elseif msg.status == 'note-off' @@ -37,30 +42,25 @@ range can be one of: new: => super 'num' - destroy: => - dispatch\detach @mask if @mask - setup: (params) => - super params - @inputs[3] or= Value.num -1 - @inputs[4] or= Value.str 'uni' - assert #@inputs == 4 - assert @inputs[4].type == 'num' or @inputs[4].type == 'str' - @assert_types 'midi/port', 'num', 'num' - @impulses = { @inputs[1]\unwrap! } + setup: (inputs) => + { port, cc, chan, range } = match '=midi/port num num? any?', inputs + super + port: EventInput port.value! + cc: ValueInput cc + chan: ValueInput chan or Value.num -1 + range: ValueInput range or Value.str 'uni' if not @out\unwrap! - @out\set apply_range @inputs[4], 0 + @out\set apply_range @inputs.range, 0 tick: => - local port, cc, chan - { port, cc, chan } = [i\unwrap! for i in *@inputs] - + { port, cc, chan, range } = @inputs for msg in port\receive! if msg.status == 'control-change' and - (chan == -1 or msg.chan == chan) and - msg.a == cc - @out\set apply_range @inputs[4], msg.b + (chan == -1 or msg.chan == chan!) and + msg.a == cc! + @out\set apply_range range, msg.b { :input diff --git a/lib/time.moon b/lib/time.moon index a726af3..007c4de 100644 --- a/lib/time.moon +++ b/lib/time.moon @@ -1,55 +1,50 @@ -import Registry, Value, Op from require 'core' +import Registry, Value, Result, Op from require 'core' +import ValueInput, EventInput from require 'core.base' +import match from require 'core.pattern' import monotime from require 'system' -delta = do - period = 1 / 60 - - local last - -> - class clock extends Op new: => - super 'num' - @impulses = { Registry.active!.kr } - @out\set 0 + super 'clock', { dt: 0, time: monotime! } - setup: (params) => - super params + setup: => @last = monotime! + super kr: EventInput Registry.active!.kr tick: => time = monotime! dt = time - @last if dt >= 1/60 - @out\set dt + @out\set :dt, :time @last = time class lfo extends Op - @doc: "(lfo clock freq [wave]) - low-frequency oscillator + @doc: "(lfo [clock] freq [wave]) - low-frequency oscillator oscillates between 0 and 1 at the frequency freq. -wave selects the wave shape from the following (default sin): -- sin +wave selects the wave shape from the following: +- sin (default) - saw - tri" - tau = math.pi * 2 new: => super 'num' @phase = 0 - default_wave = Value.str 'sin' - setup: (params) => - super params - - @inputs[3] or= default_wave - @assert_types 'num', 'num', 'str' + default_wave = Result value: Value.str 'sin' + setup: (inputs, scope) => + { clock, freq, wave } = match 'clock? num any?', inputs + super + clock: EventInput clock or scope\get '*clock*' + freq: ValueInput freq + wave: ValueInput wave or default_wave + tau = math.pi * 2 tick: => - -- if clock is dirty - if @inputs[1].dirty - dt, freq, wave = @unwrap_inputs! - @phase += dt * freq + if @inputs.clock\dirty! + { :clock, :freq, :wave } = @unwrap_all! + + @phase += clock.dt * freq @out\set switch wave when 'sin' then .5 + .5 * math.cos @phase * tau when 'saw' then @phase % 1 @@ -57,7 +52,7 @@ wave selects the wave shape from the following (default sin): else error "unknown wave type" class ramp extends Op - @doc: "(ramp clock period [max]) - sawtooth lfo + @doc: "(ramp [clock] period [max]) - sawtooth lfo ramps from 0 to max (default same as ramp) once every period seconds." @@ -65,62 +60,68 @@ ramps from 0 to max (default same as ramp) once every period seconds." super 'num' @phase = 0 - setup: (params) => - super params - assert @inputs[1].type == 'num', "tick requires a clock value" - assert @inputs[2].type == 'num', "tick requires a period value" + setup: (inputs, scope) => + { clock, period, max } = match 'clock? num num?', inputs + super + clock: EventInput clock or scope\get '*clock*' + period: ValueInput period + max: max and ValueInput max tick: => - -- if clock is dirty - if @inputs[1].dirty - dt, period, max = @unwrap_inputs! + clock_dirty = @inputs.clock\dirty! + if clock_dirty + { :clock, :period, :max } = @unwrap_all! max or= period - @phase += dt / period + @phase += clock.dt / period if @phase >= 1 @phase -= 1 + if clock_dirty or (@inputs.max and @inputs.max\dirty!) @out\set @phase * max class tick extends Op - @doc: "(tick clock period) - count ticks + @doc: "(tick [clock] period) - count ticks counts upwards by one every period seconds and returns the number of completed ticks." new: => - super 'num', 0 - @phase = 0 + @phase, @count = 0, 0 + super 'num', @count - setup: (params) => - super params - @assert_types 'num', 'num' + setup: (inputs, scope) => + { clock, period } = match 'clock? num', inputs + super + clock: EventInput clock or scope\get '*clock*' + period: ValueInput period - tick: (first) => - -- if clock is dirty - if first or @inputs[1].dirty - dt, period = @unwrap_inputs! - @phase += dt / period + tick: => + if @inputs.clock\dirty! + { :clock, :period, :max } = @unwrap_all! + @phase += clock.dt / period - next_num = math.floor @phase - if next_num != @.out! - @out\set next_num + if @phase >= 1 + @phase -= 1 + @count += 1 + @out\set @count class every extends Op - @doc: "(every clock period) - trigger every period seconds + @doc: "(every [clock] period) - trigger every period seconds returns true once every period seconds." new: => super 'bang' @phase = 0 - setup: (@period) => - super params - @assert_types 'num', 'num' + setup: (inputs, scope) => + { clock, period } = match 'clock? num', inputs + super + clock: EventInput clock or scope\get '*clock*' + period: ValueInput period - tick: (first) => - -- if clock is dirty - if first or @inputs[1].dirty - dt, period = @unwrap_inputs! - @phase += dt / period + tick: => + if @inputs.clock\dirty! + { :clock, :period, :max } = @unwrap_all! + @phase += clock.dt / period if @phase >= 1 @phase -= 1 |
