Module: Audulus

Included in:
WavetablePatch
Defined in:
lib/audulus.rb

Constant Summary collapse

XMUX_NODE =
O2HZ_NODE =
INIT_PATCH =
YAML.load <<-YAML
---
version: 1
patch:
id: 2aedc73c-4095-4d1b-ab1b-2121ea9ac19d
pan:
  x: 0.0
  y: 0.0
zoom: 1.0
nodes: []
wires: []
YAML
LIGHT_NODE =
YAML.load <<-YAML
---
type: Light
id: b264602f-365b-48a2-9d25-9b95055a2c34
position:
x: 0.0
y: 0.0
YAML
TRIGGER_NODE =
JSON.parse <<-JSON
{
"type": "Trigger",
"id": "3e8e612d-3b7a-4c6d-a2ea-2e5f1a00161d",
"position": {
  "x": 0,
  "y": 0
},
"toggle": false,
"state": false
}
JSON
INPUT_NODE =
JSON.parse <<-JSON
{
"type": "Input",
"id": "3e8e612d-3b7a-4c6d-a2ea-2e5f1a00161d",
"position": {
  "x": 0,
  "y": 0
},
"exposedPosition": {
  "x": 0,
  "y": 0
},
"name": "input"
}
JSON
SUBPATCH_NODE =
JSON.parse <<-JSON
{
"type": "Patch",
"id": "0fe72e0e-2616-4366-8036-f852398d1c73",
"position": {
  "x": -33.04297,
  "y": -44.77734
},
"subPatch": {
  "id": "0e096166-2c2d-4c0e-bce3-f9c5f42ce5c5",
  "pan": {
    "x": 0,
    "y": 0
  },
  "zoom": 1,
  "nodes": [],
  "wires": []
}
}
JSON
CLOCK_NODE =
JSON.parse(File.read(File.join(File.dirname(__FILE__), 'clock.json')))
VIA_NODE =

Class Method Summary collapse

Class Method Details

.add_node(patch, node) ⇒ Object



62
63
64
65
# File 'lib/audulus.rb', line 62

def add_node(patch, node)
  patch['nodes'] << node
  patch
end

.add_nodes(patch, nodes) ⇒ Object



67
68
69
70
71
# File 'lib/audulus.rb', line 67

def add_nodes(patch, nodes)
  nodes.each do |node|
    add_node(patch, node)
  end
end

.build_clock_nodeObject

gate output is output 0



126
127
128
# File 'lib/audulus.rb', line 126

def build_clock_node
  clone_node(CLOCK_NODE)
end

.build_demux_nodeObject



163
164
165
# File 'lib/audulus.rb', line 163

def build_demux_node
  build_simple_node("Demux8")
end

.build_expr_node(expr) ⇒ Object



167
168
169
170
171
# File 'lib/audulus.rb', line 167

def build_expr_node(expr)
  result = build_simple_node('Expr')
  result['expr'] = expr
  result
end

.build_init_docObject



98
99
100
101
# File 'lib/audulus.rb', line 98

def build_init_doc
  result = clone_node(INIT_PATCH)
  result
end

.build_input_nodeObject



134
135
136
# File 'lib/audulus.rb', line 134

def build_input_node
  clone_node(INPUT_NODE)
end

.build_knob_nodeObject



144
145
146
147
148
149
150
151
152
153
# File 'lib/audulus.rb', line 144

def build_knob_node
  result = build_simple_node("Knob")
  result['knob'] = {
    'value' => 0.5,
    'min' => 0.0,
    'max' => 1.0,
  }
  expose_node(result, 0, 0)
  result
end

.build_light_nodeObject



113
114
115
# File 'lib/audulus.rb', line 113

def build_light_node
  clone_node(LIGHT_NODE)
end

.build_mux_nodeObject



159
160
161
# File 'lib/audulus.rb', line 159

def build_mux_node
  build_simple_node("Mux8")
end

.build_o2hz_nodeObject



196
197
198
# File 'lib/audulus.rb', line 196

def build_o2hz_node
  clone_node(O2HZ_NODE)
end

.build_output_nodeObject



138
139
140
141
142
# File 'lib/audulus.rb', line 138

def build_output_node
  result = build_simple_node("Output")
  result['name'] = "Output"
  result
end

.build_sample_and_hold_nodeObject



155
156
157
# File 'lib/audulus.rb', line 155

def build_sample_and_hold_node
  build_simple_node("Sample & Hold")
end

.build_simple_node(type) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/audulus.rb', line 179

def build_simple_node(type)
  clone_node({
    "type" => type,
    "id" => "7e5486fc-994c-44f0-ae83-5ebba54d7e3b",
    "position" => {
      "x" => 0,
      "y" => 0
    }
  })
end

.build_subpatch_nodeObject



121
122
123
# File 'lib/audulus.rb', line 121

def build_subpatch_node
  clone_node(SUBPATCH_NODE)
end

.build_text_node(text) ⇒ Object



173
174
175
176
177
# File 'lib/audulus.rb', line 173

def build_text_node(text)
  result = build_simple_node("Text")
  result['text'] = text
  result
end

.build_trigger_nodeObject



117
118
119
# File 'lib/audulus.rb', line 117

def build_trigger_node
  clone_node(TRIGGER_NODE)
end

.build_uuid_map(node) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/audulus.rb', line 30

def build_uuid_map(node)
  existing_uuids = scan_uuids(node)
  existing_uuids.reduce({}) {|h, uuid|
    h[uuid] = SecureRandom.uuid()
    h
  }
end

.build_via_nodeObject



130
131
132
# File 'lib/audulus.rb', line 130

def build_via_node
  clone_node(VIA_NODE)
end

.build_xmux_nodeObject



191
192
193
# File 'lib/audulus.rb', line 191

def build_xmux_node
  clone_node(XMUX_NODE)
end

.clone_node(node) ⇒ Object



38
39
40
41
# File 'lib/audulus.rb', line 38

def clone_node(node)
  uuid_map = build_uuid_map(node)
  clone_node_helper(node, uuid_map)
end

.clone_node_helper(node, uuid_map) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/audulus.rb', line 43

def clone_node_helper(node, uuid_map)
  case node
  when Hash
    Hash[node.map {|key, elem|
      if uuid?(elem)
        [key, uuid_map[elem]]
      else
        [key, clone_node_helper(elem, uuid_map)]
      end
    }]
  when Array
    node.map {|elem|
      clone_node_helper(elem, uuid_map)
    }
  else
    node
  end
end

.expose_node(node, x, y) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/audulus.rb', line 81

def expose_node(node, x, y)
  node['exposedPosition'] = {
    'x' => x,
    'y' => y,
  }
  node
end

.make_subpatch(subpatch) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/audulus.rb', line 103

def make_subpatch(subpatch)
  doc = build_init_doc
  patch = doc['patch']

  subpatch_node = build_subpatch_node
  subpatch_node['subPatch'] = subpatch
  add_node(patch, subpatch_node)
  doc
end

.move_node(node, x, y) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/audulus.rb', line 73

def move_node(node, x, y)
  node['position'] = {
    'x' => x,
    'y' => y,
  }
  node
end

.scan_uuids(node) ⇒ Object

Node -> [ UUID ]



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/audulus.rb', line 13

def scan_uuids(node)
  case node
  when Hash
    node.map {|key, elem|
      if uuid?(elem)
        elem
      else
        scan_uuids(elem)
      end
    }.flatten
  when Array
    node.map {|elem| scan_uuids(elem)}.flatten
  else
    []
  end
end

.uuid?(string) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/audulus.rb', line 8

def uuid?(string)
  string.kind_of?(String) && /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ =~ string
end

.wire_output_to_input(patch, source_node, source_output, destination_node, destination_input) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/audulus.rb', line 89

def wire_output_to_input(patch, source_node, source_output, destination_node, destination_input)
  patch['wires'] << {
    "from" => source_node['id'],
    "output" => source_output,
    "to" => destination_node['id'],
    "input": destination_input
  }
end