forked from mbarton/sonic-quiche
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.rb
493 lines (407 loc) · 11.3 KB
/
api.rb
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# Aggregated and modified Sonic Pi API. Original license stated below.
#--
# This file is part of Sonic Pi: http://sonic-pi.net
# Full project source: https://github.com/samaaron/sonic-pi
# License: https://github.com/samaaron/sonic-pi/blob/master/LICENSE.md
#
# Copyright 2013, 2014 by Sam Aaron (http://sam.aaron.name).
# All rights reserved.
#
# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as this
# notice is included.
#++
%x{
var _reserved = {
thread_id: 0,
current_thread: 0,
next_node_id: 0,
sleep_mul: 0.5, // 120 BPM,
transpose: 0,
synth: "square",
send: function(action, data) {
if(action == "complete" || _reserved.current_thread == _reserved.thread_id) {
postMessage({action: action, data: data, thread_id: _reserved.thread_id});
}
}
};
}
def resolve_synth_opts_hash_or_array(opts)
case opts
when Hash
return opts
when Array
s = opts.size
return Hash[*opts] if s.even? && s > 1
case s
when 1
case opts[0]
when Hash
return opts[0]
else
raise "Invalid options. Options should either be an even list of key value pairs, a single Hash or nil. Got a single value array where the value isn't a Hash"
end
when 0
return {}
end
when NilClass
return {}
else
raise "Invalid options. Options should either be an even list of key value pairs, a single Hash or nil. Got something completely different"
end
end
def print(output)
%x{
_reserved.send("log", output);
}
end
def puts(output)
print(output)
end
def rrand_i(min, max)
%x{
return Math.floor(Math.random() * (max - min + 1) + min);
}
end
def dice(num_sides=6)
rrand_i(1, num_sides)
end
def one_in(num)
rrand_i(1, num) == 1
end
def rrand(min, max)
%x{
return Math.random() * (max - min + 1) + min;
}
end
def rand(max=1)
rrand(0, max)
end
def rand_i(max=2)
rrand_i(0, max)
end
def choose(list)
list[rand_i(list.length)]
end
def use_random_seed(seed, &block)
raise "use_random_seed does not work with a block. Perhaps you meant with_random_seed" if block
%x{
_reserved.send("warn", "Random number seeding is not supported in web browsers");
}
end
def with_random_seed(seed, &block)
raise "with_random_seed requires a block. Perhaps you meant use_random_seed" unless block
use_random_seed(seed)
block.call
end
def use_bpm(bpm, &block)
raise "use_bpm does not work with a block. Perhaps you meant with_bpm" if block
%x{
_reserved.sleep_mul = 60.0 / bpm;
}
end
def with_bpm(bpm, &block)
raise "with_bpm must be called with a block. Perhaps you meant use_bpm" unless block
old_bpm = current_bpm
use_bpm(bpm)
block.call
use_bpm(old_bpm)
end
def current_bpm
%x{
return 60.0 / _reserved.sleep_mul;
}
end
def rt(t)
%x{
return t / _reserved.sleep_mul;
}
end
def sleep(seconds)
%x{
_reserved.send("cmd", {type: "sleep", length: (seconds * _reserved.sleep_mul)});
}
end
def sync(cue_id)
%x{
_reserved.send("sync", cue_id);
}
end
def cue(cue_id)
%x{
_reserved.send("cue", cue_id);
}
end
def wait(time)
if time.is_a? Symbol
sync(time)
else
sleep(time)
end
end
def in_thread(*opts, &block)
%x{
if(_reserved.current_thread + 1 == _reserved.thread_id) {
_reserved.current_thread++;
block.$call();
_reserved.current_thread--;
} else {
_reserved.send("worker", _reserved.current_thread + 1);
}
}
end
def use_arg_bpm_scaling(bool, &block)
raise "use_arg_bpm_scaling does not work with a block. Perhaps you meant with_arg_bpm_scaling" if block
%x{
_reserved.arg_bpm_scaling = bool;
}
end
def with_arg_bpm_scaling(bool, &block)
raise "with_arg_bpm_scaling must be called with a block. Perhaps you meant use_arg_bpm_scaling" unless block
%x{
var oldValue = _reserved.arg_bpm_scaling;
_reserved.arg_bpm_scaling = bool;
block.$call();
_reserved.arg_bpm_scaling = oldValue;
}
end
def midi_to_hz(n)
n = note(n) unless n.is_a? Numeric
440.0 * (2 ** ((n - 69) / 12.0))
end
def hz_to_midi(freq)
(12 * (Math.log(freq * 0.0022727272727) / Math.log(2))) + 69
end
def note(n, *args)
return nil if (n.nil? || n == :r || n == :rest)
return Note.resolve_midi_note_without_octave(n) if args.empty?
args_h = resolve_synth_opts_hash_or_array(args)
octave = args_h[:octave]
if octave
Note.resolve_midi_note(n, octave)
else
Note.resolve_midi_note_without_octave(n)
end
end
def note_info(n, *args)
args_h = resolve_synth_opts_hash_or_array(args)
octave = args_h[:octave]
Note.new(n, octave)
end
def degree(degree, tonic, scale)
Scale.resolve_degree(degree, tonic, scale)
end
def scale(tonic, name, *opts)
opts = resolve_synth_opts_hash_or_array(opts)
opts = {:num_octaves => 1}.merge(opts)
Scale.new(tonic, name, opts[:num_octaves])
end
def chord(tonic, name=:major, *opts)
if tonic.is_a? Array
raise "List passed as parameter to chord needs two elements i.e. chord([:e3, :minor]), you passed: #{tonic.inspect}" unless tonic.size == 2
Chord.new(tonic[0], tonic[1]).to_a
else
Chord.new(tonic, name).to_a
end
end
def set_sched_ahead_time!(t)
sleep(t)
end
def current_transpose
%x{
return _reserved.transpose;
}
end
def use_transpose(shift, &block)
raise "use_transpose does not work with a do/end block. Perhaps you meant with_transpose" if block
raise "Transpose value must be a number, got #{shift.inspect}" unless shift.is_a?(Numeric)
%x{
_reserved.transpose = shift;
}
end
def with_transpose(shift, &block)
raise "with_transpose requires a do/end block. Perhaps you meant use_transpose" unless block
raise "Transpose value must be a number, got #{shift.inspect}" unless shift.is_a?(Numeric)
%x{
var oldValue = _reserved.transpose;
_reserved.transpose = shift;
block.$call();
_reserved.transpose = oldValue;
}
end
def current_volume
%x{
return _reserved.volume;
}
end
def set_volume!(vol)
max_vol = 5
if (vol > max_vol)
new_vol = max_vol
elsif (vol < 0)
new_vol = 0
else
new_vol = vol
end
%x{
_reserved.volume = new_vol;
}
end
def current_synth
%x{
return _reserved.synth;
}
end
def use_synth(synth, &block)
raise "use_synth does not work with a do/end block. Perhaps you meant with_synth" if block
%x{
_reserved.synth = synth;
}
end
def with_synth(synth, &block)
raise "with_synth must be called with a do/end block. Perhaps you meant use_synth" unless block
%x{
var oldValue = _reserved.synth;
_reserved.synth = synth;
block.$call();
_reserved.synth = oldValue;
}
end
def current_synth_defaults
%x{
return _reserved.synth_defaults;
}
end
def use_synth_defaults(*args, &block)
%x{
_reserved.synth_defaults = args;
}
end
def use_merged_synth_defaults(*args, &block)
raise "use_merged_synth_defaults does not work with a block. Perhaps you meant with_merged_synth_defaults" if block
current_defs = current_synth_defaults
args_h = resolve_synth_opts_hash_or_array(args)
merged_defs = (current_defs || {}).merge(args_h)
use_synth_defaults(merged_defs)
end
def with_synth_defaults(*args, &block)
raise "with_synth_defaults must be called with a block" unless block
current_defs = current_synth_defaults
args_h = resolve_synth_opts_hash_or_array(args)
use_synth_defaults(args_h)
block.call
use_synth_defaults(current_dfs)
end
def with_merged_synth_defaults(*args, &block)
raise "with_merged_synth_defaults must be called with a block" unless block
current_defs = current_synth_defaults
args_h = resolve_synth_opts_hash_or_array(args)
merged_defs = (current_defs || {}).merge(args_h)
with_synth_defaults(merged_defs, block)
end
def recording_start
%x{
_reserved.send("warn", "Sonic Quiche does not yet support recodings. Sad soup.");
}
end
def recording_stop
%x{
_reserved.send("warn", "Sonic Quiche does not yet support recodings. Sad soup.");
}
end
def recording_save
%x{
_reserved.send("warn", "Sonic Quiche does not yet support recodings. Sad soup.");
}
end
def recording_delete
%x{
_reserved.send("warn", "Sonic Quiche does not yet support recodings. Sad soup.");
}
end
def synth(synth, *args)
args_h = resolve_synth_opts_hash_or_array(args)
if args.has_key? :note
n = args_h[:note]
n = n.call if n.is_a? Proc
n = note(n) unless n.is_a? Numeric
n += current_transpose
args_h[:note] = n
end
_reserved_trigger_inst synth, args_h
end
def play(n, *args)
return play_chord(n, *args) if n.is_a?(Array)
n = n.call if n.is_a? Proc
return nil if (n.nil? || n == :r || n == :rest)
init_args_h = {}
args_h = resolve_synth_opts_hash_or_array(args)
if n.is_a? Numeric
# Do nothing
elsif n.is_a? Hash
init_args_h = resolve_synth_opts_hash_or_array(n)
n = note(init_args_h[:note])
return nil if n.nil?
n
else
n = note(n)
end
n += current_transpose
args_h[:note] = n
synth = current_synth
final_args = init_args_h.merge(args_h)
%x{
_reserved.send("cmd", {type: "note", synth: synth, note: n, noteLength: _reserved.sleep_mul, args: final_args.map});
}
end
def play_pattern(notes, *args)
notes.each{|note| play(note, *args) ; sleep 1 }
end
def play_pattern_timed(notes, times, *args)
if times.is_a? Array
notes.each_with_index{|note, idx| play(note, *args) ; sleep(times[idx % times.size])}
else
notes.each_with_index{|note, idx| play(note, *args) ; sleep times}
end
end
def play_chord(notes, *args)
shift = current_transpose
shifted_notes = notes.map do |n|
n = note(n) unless n.is_a? Numeric
n + shift
end
synth_name = current_synth
trigger_chord(synth_name, shifted_notes, args)
end
def sample(name, *args_a_or_h)
args_h = resolve_synth_opts_hash_or_array(args_a_or_h)
%x{
var _reserved_node_id = "" + _reserved.thread_id + "-" + _reserved.next_node_id;
_reserved.next_node_id++;
_reserved.send("cmd", {type: "sample", node_id: _reserved_node_id, sample: name, args: args_h.map});
return _reserved_node_id;
}
end
def control(node, *args)
args_h = resolve_synth_opts_hash_or_array(args)
n = args_h[:note]
args_h[:note] = note(n) if n
_reserved_control node, args_h
end
def stop(node)
_reserved_stop node
end
def _reserved_body
[[code_body]]
end
%x{
onmessage = function(initEvent) {
_reserved.thread_id = initEvent.data.thread_id;
try {
$opal.Object._proto.$_reserved_body();
} catch(err) {
_reserved.send("error", err.toString());
}
_reserved.send("complete", {});
}
}