-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
318 lines (270 loc) · 9.49 KB
/
index.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
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
<!doctype html>
<html>
<head>
<title>Alda web workspace</title>
<style>
.main {
max-width: 50em;
margin: 1em auto;
font-family: sans-serif;
}
a {
color: blue;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
#clear {
float: right;
}
textarea {
width: 100%;
}
.error {
color: red;
}
</style>
</head>
<body>
<!-- this is from https://github.com/colinbdclark/osc.js : -->
<script src="js/osc.js"></script>
<script src="js/JZZ.js"></script>
<script src="js/JZZ.midi.SMF.js"></script>
<script src="js/JZZ.synth.Tiny.js"></script>
<script src="js/JZZ.gui.Select.js"></script>
<script>
// (c) Severák 2021-2022
// MIT licensed
var AldaPlayer = {};
AldaPlayer.output = JZZ().openMidiOut().or('not opening');
// this takes Open Sound Control bundle generated by ALDA programming language
// converts it to MIDI
AldaPlayer.osc2midi = function (bundle, ignoreProgramChanges=false) {
let view = osc.dataView(bundle, 0, bundle.byteLength || bundle.length);
let data = osc.readBundle(view, {});
let BPM = 0;
for (let command of data.packets) {
if (command.address && command.address=='/system/tempo') {
BPM = command.args[1];
}
}
let midiFile = JZZ.MIDI.SMF();
let trak0 = JZZ.MIDI.SMF.MTrk();
midiFile.push(trak0);
trak0.smfBPM(BPM);
// pulse per milisecond
let PPMS = ((BPM * 96) / 60) / 1000;
let commandPattern = /\/track\/(\d+)\/midi\/(\w+)/;
let trakAdd = function (midiFile) {
let trak = JZZ.MIDI.SMF.MTrk();
midiFile.push(trak);
return trak;
};
let isPercCh = {};
for (let packet of data.packets) {
let fullCmd, ch, command;
if (commandPattern.test(packet.address)) {
[fullCmd, ch, command] = commandPattern.exec(packet.address);
if (isPercCh[ch]) ch = 9; // MIDI percussion channel is always 10
let offset = packet.args[0];
let tik = PPMS * offset;
let trak = midiFile[ch] || trakAdd(midiFile);
if (command == 'note') {
trak.add(tik, JZZ.MIDI.noteOn(ch, packet.args[1], packet.args[4]));
trak.add(tik + (packet.args[2] * PPMS), JZZ.MIDI.noteOff(ch, packet.args[1], packet.args[4]))
if (packet.args[2]==0) {
console.log('pauze');
}
} else if (command == 'patch') {
if (!ignoreProgramChanges) {
// program change
trak.add(tik, JZZ.MIDI.program(ch, packet.args[1]));
trak.add(tik, JZZ.MIDI.smfInstrName('MIDI program #' + packet.args[1]));
}
} else if (command == 'volume') {
//console.log('VOL', ch, packet.args[1]/127);
trak.add(tik, JZZ.MIDI.volumeMSB(ch, packet.args[1]));
} else if (command == 'percussion') {
// we can only play percussion on one channel
isPercCh[ch] = true;
console.log(isPercCh);
}
// TODO - panning
console.log(command, ch, packet.args);
}
if (packet.address=='/system/shutdown') {
for (let trk of midiFile) {
trk.add(packet.args[0] * PPMS, JZZ.MIDI.smfEndOfTrack());
}
}
}
return midiFile;
};
AldaPlayer.playMIDI = function(midiFile) {
if (AldaPlayer.player) {
console.log('Already playing!');
return;
}
let player = midiFile.player();
player.onEnd = function () {
console.log('END!');
// document.getElementById('errormsg').innerText = '(song ended successfully)';
enablePlayButton();
AldaPlayer.player = null;
};
player.connect(AldaPlayer.output);
player.play();
AldaPlayer.player = player;
return player;
};
AldaPlayer.stop = function()
{
if (AldaPlayer.player) {
AldaPlayer.player.stop();
}
AldaPlayer.player = null;
};
AldaPlayer.downloadMIDI = function(midiFile, name) {
var buffer = midiFile.toArrayBuffer();
var blob = new File([buffer], name + '.mid', {type: "audio/midi"});
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
};
window.AldaPlayer = AldaPlayer;
</script>
<!-- this part is WASM loader -->
<script src="js/wasm_exec.js"></script>
<script>
if (!WebAssembly.instantiateStreaming) { // polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const go = new Go();
WebAssembly.instantiateStreaming(
fetch("js/alda.wasm"), go.importObject
).then((result) => {
go.run(result.instance)
console.info(`alda.wasm ${Alda.VERSION} loaded`)
}).catch((err) => {
console.error(err);
});
</script>
<!-- this part is UI -->
<div class="main">
<h1><a href="https://alda.io/"><img src="alda-logo-horizontal.svg" alt="Alda"></a></h1>
<p>musical programming language web workspace <button id="clear">clear workspace</button></p>
<textarea placeholder="write your alda score here..." rows="25" cols="80" id="score">
# Stone in focus
# (that song from video with chilling monkey)
# by Aphex Twin
# as this is ambient piece is has very simple structure
# but what really makes it is used sound
# for me - violin works best
(tempo! 20)
(quant! 85)
motif = [
V2: o4 c+ <d f+/b r
V1: o3 a >d <g r
]
midi-violin:
(ppp) motif
(pp) motif
(p) motif
(mp) motif *4 # <- there you can make it longer
(p) motif
(pp) motif
(ppp) motif
</textarea>
<br/>
<pre id="errormsg" class="error"></pre>
<p>
MIDI OUT: <select id="midiout"></select>
<span id="player_buttons" style="display: none">
<button id="play">PLAY</button>
<button id="stop">STOP</button>
(<label><input type="checkbox" id="one_program"> don't send program changes</label>)
</span>
<span id="player_warning">
(You need to select MIDI OUT first to be able to play it.)
</span>
</p>
<p>
<button id="download">DOWNLOAD MIDI</button>
</p>
<p>Tested in recent versions of Chrome and Firefox. In Firefox you need <a href="https://jazz-soft.net/download/">Jazz Plugin</a> installed.</p>
<p>Head out to <a href="https://alda.io/tutorial/">Alda tutorial</a>, <a href="https://github.com/alda-lang/alda/tree/master/examples">examples in Alda repository</a> or <a href="https://github.com/severak/sheet-music">my sheet-music repo</a> for more score examples.</p>
<p>See <a href="https://alda.io/tutorial">tutorial</a> and <a href="https://alda.io/cheat-sheet">cheat sheet</a> for more info about Alda syntax.</p>
<hr>
<p>Made by Dave Yarwood, Severák and many giants on whose shoulders we are standing. See <a href="https://github.com/severak/alda-js">source code</a>.</p>
</div>
<script>
navigator.requestMIDIAccess().then(function (access) {
console.log('povoleno midi out');
});
JZZ.synth.Tiny.register();
var midiOut = JZZ.gui.SelectMidiOut({at: 'midiout'});
midiOut.onSelect = function(name) {
JZZ().openMidiOut(name).and(function () {
AldaPlayer.output = this;
document.getElementById('player_buttons').style.display = 'inline';
document.getElementById('player_warning').style.display = 'none';
}).or(function () {
console.log('unselect MIDI OUT');
document.getElementById('player_buttons').style.display = 'none';
document.getElementById('player_warning').style.display = 'inline';
});
};
function disablePlayButton() {
document.getElementById('play').setAttribute('disabled', 'disabled');
}
function enablePlayButton() {
document.getElementById('play').removeAttribute('disabled');
}
document.getElementById('clear').addEventListener('click', function (event) {
event.preventDefault();
document.getElementById('score').value = '';
});
document.getElementById('play').addEventListener('click', function (event) {
event.preventDefault();
Alda.toOSCBytes(document.getElementById('score').value).then(function (osdData) {
var midi = AldaPlayer.osc2midi(osdData, document.getElementById('one_program').checked);
try {
disablePlayButton();
AldaPlayer.playMIDI(midi);
document.getElementById('errormsg').innerText = '';
} catch (e) {
document.getElementById('errormsg').innerText = e;
}
}, function (errorMsg) {
document.getElementById('errormsg').innerText = errorMsg;
})
});
document.getElementById('stop').addEventListener('click', function (event) {
event.preventDefault();
AldaPlayer.stop();
for (let ch = 0; ch < 16; ch++) {
AldaPlayer.output.allNotesOff(ch);
}
enablePlayButton();
});
document.getElementById('download').addEventListener('click', function (event) {
event.preventDefault();
Alda.toOSCBytes(document.getElementById('score').value).then(function (osdData) {
var midi = AldaPlayer.osc2midi(osdData);
try {
AldaPlayer.downloadMIDI(midi, '');
document.getElementById('errormsg').innerText = '';
} catch (e) {
document.getElementById('errormsg').innerText = e;
}
}, function (errorMsg) {
document.getElementById('errormsg').innerText = errorMsg;
})
});
</script>
<script src="//million.svita.cz/millions_v1.js"></script>
</body>
</html>