aboutsummaryrefslogtreecommitdiffstats
path: root/lib/pilot.moon
blob: 7251f555054051d32d6c4e3383054fd5cf0f50c7 (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
import Op, EventInput, ValueInput, ColdInput, match from require 'core.base'
import udp from require 'socket'

conn = udp!
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\sendto str, '127.0.0.1', 49161

class play extends Op
  @doc: "(play trig ch oct note [vel [len]]) - play a note when trig is live"

  setup: (inputs) =>
    { trig, args } = match 'bang *any', inputs
    assert #args < 6, "too many arguments!"
    super
      trig: EventInput trig
      args: [ColdInput a for a in *args]

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

class play_ extends Op
  @doc: "(play! ch oct note [vel [len]]) - play a note when note is live"

  setup: (inputs) =>
    { chan, octv, note, args } = match 'any any any *any', inputs
    assert #args < 3, "too many arguments!"
    super
      chan: ColdInput chan
      octv: ColdInput octv
      note: EventInput note
      args: [ColdInput a for a in *args]

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

class effect extends Op
  @doc: "(effect which a b) - set an effect

which is one of 'DIS', 'CHO', 'REV' or 'FEE'"

  setup: (inputs) =>
    { which, a, b } = match 'str num num', inputs
    super {
      ColdInput which
      ValueInput a
      ValueInput b
    }

  tick: =>
    send @unwrap_all!
{
  :play
  'play!': play_
  :effect
}