aboutsummaryrefslogtreecommitdiffstats
path: root/lib/pilot.moon
blob: b85144018e68873103e7a6ed82acd6309d1b1c57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Op, Value, Input, Error, match from require 'core.base'
import udp from require 'socket'

local conn

hex = "0123456789abcdef"
encode = (arg) ->
  switch type arg
    when 'number' then
      i = 1 + math.floor arg
      hex\sub i, i
    when 'string' then arg
    else error "invalid type: #{type arg}"

send = (...) ->
  str = ''
  for i = 1, select '#', ...
    tbl = select i, ...
    str ..= table.concat [encode v for v in *tbl]
  conn or= udp!
  conn\sendto str, '127.0.0.1', 49161

play = Value.meta
  meta:
    name: 'play'
    summary: "Play a note when a bang arrives."
    examples: { '(pilot/play trig ch oct note [vel [len]])' }

  value: class extends Op
    setup: (inputs) =>
      { trig, args } = match 'bang *any', inputs
      assert #args < 6, Error 'argument', "too many arguments!"
      super
        trig: Input.event trig
        args: [Input.cold a for a in *args]

    tick: =>
      { :trig, :args } = @inputs
      if trig\dirty! and trig!
        send [a! for a in *@inputs.args]

play_ = Value.meta
  meta:
    name: 'play!'
    summary: "Play a note when a note arrives."
    examples: { '(pilot/play! ch oct note [vel [len]])' }

  value: class extends Op
    setup: (inputs) =>
      { chan, octv, note, args } = match 'any any any *any', inputs
      assert #args < 3, Error 'argument', "too many arguments!"
      super
        chan: Input.cold chan
        octv: Input.cold octv
        note: Input.event note
        args: [Input.cold a for a in *args]

    tick: =>
      if @inputs.note\dirty!
        { :chan, :oct, :note, :args } = @unwrap_all!
        send { chan, oct, note }, args

effect = Value.meta
  meta:
    name: 'effect'
    summary: "Set effect parameters."
    examples: { '(pilot/effect which a b)' }
    description: "`effect` should be one of 'DIS', 'CHO', 'REV' or 'FEE'"

  value: class extends Op
    setup: (inputs) =>
      { which, a, b } = match 'str num num', inputs
      super {
        Input.cold which
        Input.value a
        Input.value b
      }

    tick: =>
      send @unwrap_all!

{
  :play
  'play!': play_
  :effect
}