-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard-play.js
141 lines (128 loc) · 4.02 KB
/
keyboard-play.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
// This deals with playing awsedrfgyhujAWSEDRFGYHUJ.
//
// Converts to MIDI and plays it. Yay.
function QwerToMidi() {
this.duration = function () {
var len = 1;
while (this.tune.length > this.ix) {
ch = this.tune.charAt(this.ix);
if (ch == '/') {
len++;
} else if (ch == '\\') {
len/=2;
} else {
break;
}
++this.ix;
}
return len;
};
this.convert = function (tune) {
this.tune = tune;
this.ix = 0;
var beat = this.duration();
var oct = 0;
trax = [ [ /* meta */ ] ];
voice = [];
while (this.ix < this.tune.length) {
var ch = this.tune.charAt(this.ix++);
var semitones = "qasedrfgyhujikSEDRFGYHUJIKLP:".indexOf(ch);
if (semitones >= 0) {
var midi = semitones + oct * 12 + 60; // = MIDI.pianoKeyOffset;
var t = this.duration() * beat * 64;
voice.push({deltaTime: 0, type: "channel", subtype: 'noteOn', channel: trax.length, noteNumber: midi, velocity:127});
voice.push({deltaTime: t, type: "channel", subtype: 'noteOff', channel: trax.length, noteNumber: midi, velocity:0});
}
else {
var tpos = "-*+".indexOf(ch) - 1;
if (tpos == 0) {
trax.push(voice);
voice = [];
oct = 0;
}
if (tpos >= -1) {
oct += tpos;
}
}
}
trax.push(voice);
return {
header: {
formatType: 1,
trackCount: trax.length,
ticksPerBeat: 64 // per crotchet
},
tracks: trax
};
};
};
qwerToMidi = new QwerToMidi();
function onQwerChanged() {
// Auto-play first tune.
var tune = document.getElementById('tune').value;
var midi = qwerToMidi.convert(tune);
MIDI.Player.setMidiData(midi);
MIDI.Player.resume();
}
function playImmediate(ch) {
var midi = qwerToMidi.convert(ch);
MIDI.Player.setMidiData(midi);
MIDI.Player.resume();
return midi.tracks[1].length > 0 || ("\\/*\n\r\b+-".indexOf(ch) >= 0) ;
}
function playAndOrValidateEvent(event) {
var code = event.charCode;
if (code == 0) {
return true;
}
var ch = String.fromCharCode(code);
return playImmediate(ch);
}
function keyboardClick(e) {
var target = e.target || e.srcElement;
var insert = playImmediate(target.innerHTML);
if (insert) {
var tune = document.getElementById('tune');
var csr = tune.selectionStart;
var old = tune.value;
var next = tune.value.substr(0,csr) + target.innerHTML.substr(0,1) + tune.value.substr(csr, tune.value.length);
tune.value = next;
tune.textContent = tune.value;
tune.selectionStart = csr+1;
tune.selectionEnd = csr+1;
putTuneInHash();
}
}
function putTuneInHash() {
var tune = document.getElementById("tune");
var data = tune.value;
// data = data.replace(/\\/g, "~");
// data = data.replace(/\s/g, "_");
// data = encodeURIComponent(data);
// data = data.replace(/%2F/g, "/");
// data = data.replace(/%5E/g, "_");
// data = data.replace(/%2B/g, "+");
data = LZString.compressToBase64(data);
window.location.hash = "v1" + data;
}
function putHashInTune() {
var data = window.location.hash.slice(1);
if (data.lastIndexOf("v1", 0) >= 0) {
data = LZString.decompressFromBase64(data.slice(2));
}
else {
data = decodeURIComponent(data);
data = data.replace(/~/g, "\\");
data = data.replace(/_/g, "\n");
}
var tune = document.getElementById("tune");
tune.value = data;
tune.textContent = tune.value;
}
function onceLoaded() {
MIDI.programChange(0, 0);
putHashInTune();
var tune = document.getElementById("tune");
tune.disabled = false;
onQwerChanged();
}