-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sequencer.html
206 lines (162 loc) · 5.62 KB
/
Sequencer.html
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
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="https://cdn.jsdelivr.net/npm/webmidi@latest/dist/iife/webmidi.iife.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.49/Tone.js"></script>
</head>
<body>
<button id="play-btn">Start sequence</button>
<button id="stop-btn">Stop sequence</button>
<div style="text-align: center; font-family: Arial;">
<h5>Track 1 Note</h5>
<h2 id="track1-label">-</h2>
<button id="track1play-btn">Track 1 Play</button>
<button id="track1stop-btn">Track 1 Stop</button>
</div>
<div style="text-align: center; font-family: Arial;">
<h5>Track 2 Note</h5>
<h2 id="track2-label">-</h2>
<button id="track2play-btn">Track 2 Play</button>
<button id="track2stop-btn">Track 2 Stop</button>
</div>
<script type="module">
let channelDevice = null;
let track1Data = Array(16).fill({ note: '-', velocity: 0 });
let track2Data = Array(16).fill({ note: '-', velocity: 0 });
let tempo = 0;
let step1 = 0;
let step2 = 0
let time1 = 0;
let time2 = 0;
const deviceName = "Arturia MicroFreak";
const sequencer1Range = "D3:E18";
const sequencer2Range = "F3:G18";
// Track 1 sequence
const seq1 = new Tone.Loop((currentTime) => {
const track1 = track1Data[step1];
time1 = currentTime;
if (step1 === 0) {
const lastStep = track1Data[track1Data.length - 1];
if (lastStep.note !== '-') {
channelDevice.stopNote(lastStep.note);
}
}
if (track1.note !== '-') {
if (step1 > 0) {
channelDevice.stopNote(track1Data[step1 - 1].note);
}
// Show the label
document.getElementById('track1-label').textContent = track1.note;
channelDevice.playNote(track1.note, { attack: track1.velocity });
}
step1++;
// reset the step sequence
if (step1 === 16) {
step1 = 0;
}
}, "8n");
// Track 2 sequence
const seq2 = new Tone.Loop((currentTime) => {
const track2 = track2Data[step2];
time2 = currentTime;
if (step2 === 0) {
const lastStep = track2Data[track2Data.length - 1];
if (lastStep.note !== '-') {
channelDevice.stopNote(lastStep.note);
}
}
if (track2.note !== '-') {
if (step2 > 0) {
channelDevice.stopNote(track2Data[step2 - 1].note);
}
// Show the label
document.getElementById('track2-label').textContent = track2.note;
channelDevice.playNote(track2.note, { attack: track2.velocity });
}
step2++;
// reset the step sequence
if (step2 === 16) {
step2 = 0;
}
}, "2n");
// Play event
const playButton = document.getElementById("play-btn");
playButton.addEventListener("click", async () => {
playButton.textContent = 'Update sequence';
const tempoFromSheet = await new Promise((fn) => {
google.script.run.withSuccessHandler(fn).getTempo();
});
tempo = parseInt(tempoFromSheet);
const track1 = await new Promise((fn) => {
google.script.run.withSuccessHandler(fn).getTrackData(sequencer1Range);
});
const track2 = await new Promise((fn) => {
google.script.run.withSuccessHandler(fn).getTrackData(sequencer2Range);
});
track1Data = [];
for (const item of track1) {
track1Data.push({note: item[0], velocity: item[1] });
}
track2Data = [];
for (const item of track2) {
track2Data.push({note: item[0], velocity: item[1] });
}
Tone.Transport.bpm.value = tempo;
Tone.Transport.start();
});
// Stop event
document.getElementById("stop-btn").addEventListener("click", () => {
track1Data.forEach((data) => {
channelDevice.stopNote(data.note);
});
track2Data.forEach((data) => {
channelDevice.stopNote(data.note);
});
Tone.Transport.stop();
playButton.textContent = "Play sequence";
});
// Track 1 event play
document.getElementById("track1play-btn").addEventListener("click", () => {
seq1.start(time1);
});
// Track 1 event stop
document.getElementById("track1stop-btn").addEventListener("click", () => {
track1Data.forEach((data) => {
channelDevice.stopNote(data.note);
});
seq1.stop(time1);
document.getElementById('track1-label').textContent = '-';
});
// Track 2 event play
document.getElementById("track2play-btn").addEventListener("click", () => {
seq2.start(time2);
});
// Track 2 event stop
document.getElementById("track2stop-btn").addEventListener("click", () => {
track2Data.forEach((data) => {
channelDevice.stopNote(data.note);
});
seq2.stop(time2);
document.getElementById('track2-label').textContent = '-';
});
function onMidiEnabled() {
if (WebMidi.inputs.length < 1) {
console.log('No MIDI device detected');
} else {
const outputDevice = WebMidi.getOutputByName(deviceName);
channelDevice = outputDevice.channels[2];
}
}
function start() {
WebMidi
.enable()
.then(onMidiEnabled)
.catch((err) => {
console.log(err)
});
}
start();
</script>
</body>
</html>