-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlooper.js
174 lines (141 loc) · 4.91 KB
/
looper.js
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
const sc = require('./synth_common');
module.exports = function (RED) {
'use strict';
function LooperNode (config) {
RED.nodes.createNode(this, config);
const node = this;
reset();
this.on('input', function (msg) {
switch (msg.payload) {
case 'tick':
tickHandler(msg);
break;
case 'play': // the action
case 'record': // the action
node.count = -1;
setState(msg.payload);
break;
case 'stop':
switch (node.state) {
case 'play':
case 'record':
node.count = -1;
setState('waiting');
}
break;
case 'reset':
reset();
// do not send on message
break;
default:
// do nothing
break;
}
});
function tickHandler (msg) {
switch (node.state) {
case 'wait':
// do nothing
break;
case 'play': // the state
case 'record': // the state
let start = msg.start || [];
if (node.count <= 0) {
if (start.includes(node.start)) {
node.count = node.length;
setState(node.state); // to display status
createSynth(node, msg, node.state);
} else {
// ignore all ticks until the start event
}
} else {
if (start.includes(node.input)) {
node.count--;
}
if (node.count <= 0) {
if (node.loop) {
// whether we are recording or looping we carry on to play
setState('play');
// call the handler immediately in case the looper starts playing immediately it finishes recording
tickHandler(msg);
} else {
setState('wait'); // TODO: unless we are looping
}
}
}
break;
default:
// do nothing
}
}
function reset () {
node.volume = config.volume || 100;
node.input = config.input || 'beat';
node.start = config.start || 'bar'; // sampler won't start playing/recording until this
node.loop = config.loop || false;
node.length = Number(config.length) || 4;
node.tags = [];
node.count = -1; // the number of relevant ticks left to the end of the play/record. -1 indicates it hasn't started yet
setState('wait');
// wait a little while to allow wires to be created
setTimeout(function () {
node.send({ topic: 'looper', nodeID: node.id });
}, 200);
}
function setState (state) {
// state can be:
// 'wait' for a command ('play' or 'record');
// 'play'; although if beatCount is -1 it is not actually play yet
// 'record'; ditto
let shape, colour;
node.state = state;
switch (state) {
case 'record':
colour = 'red';
break;
case 'play':
colour = 'green';
break;
case 'wait':
colour = 'grey';
break;
}
if (node.count === -1) {
shape = 'ring';
} else {
shape = 'dot';
}
node.status({ fill: colour, shape: shape, text: state });
}
}
function createSynth (node, msg, action) {
if (!['play', 'record'].includes(action)) {
node.warn('no synth for action ' + action);
return;
}
const amp = sc.volume2amp(node);
let details = { amp };
const bpm = msg.bpm || node.context().flow.get('bpm') || node.context().global.get('bpm');
let sustain = node.length * 60 / bpm;
const multiple = msg['beats_per_' + node.input];
if (multiple) {
sustain *= multiple;
}
Object.assign(details, { bpm, sustain });
let looperMsg = {
payload: 'tick',
details,
looper: action,
nodeID: node.id
};
if (msg.timeTag) {
let timeTag = msg.timeTag;
if (msg.latency) {
timeTag -= msg.latency / 2;
}
looperMsg.timeTag = timeTag;
}
node.send(looperMsg);
}
RED.nodes.registerType('looper', LooperNode);
};