-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
265 lines (218 loc) · 10.2 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
<!DOCTYPE html>
<html>
<head>
<title>My Offline Piano</title>
<style>
label{display:inline-block;width:120px;height:25px;}
select{display:inline-block;width:120px;height:25px;}
</style>
<script src="audiounit.js"></script>
<script type="text/javascript">
let base_freq = 440;
let keyboard_mode = 1; // 0: alphabetic order, 1: qwerty layout, 2: qwerty angular (qazwsx...)
let shift_mode = 47; // 470 => Major-R
let last_focus_window = null;
let last_mouse_note = 0;
let mouse_is_down = 0;
const alphabetic_str = "abcdefghijklmnopqrstuvwxyz,<.>/?;:'\"\\\[{]}";
const qwerty_str = "zxcvbnm,./-=asdfghjkl;'\\qwertyuiop[]";
const qwerty_str_zaq = "zaqxswcdevfrbgtnhymju,ki.lo/;p'[-\\]=";
let live_down_keys = [];
function on_mouse_event(event, event_type){ //0: move, 1: down, 2: up
if ( event_type == 2 ){
mouse_is_down = 0;
return;
}
if ( event_type == 1 )mouse_is_down = 1;
if ( mouse_is_down != 1 )return;
const r2 = document.getElementById('my_canvas').getBoundingClientRect();
const x2 = ( event.clientX - r2.left), y2 = ( event.clientY - r2.top );
let note_no = Math.floor((y2 * 4)/(r2.bottom - r2.top)) * 12 + Math.floor(( x2 * 12 )/(r2.right - r2.left));
note_no -= 12;
if ( note_no == last_mouse_note )return;
audiounit_play_notes([parseInt(440 * Math.pow(2.0, note_no/12.0))], [0]);
last_mouse_note = note_no;
}
function char_to_frequencies(c_asci){
let c_low = String.fromCharCode((c_asci >= 65 && c_asci <= 90) ? (c_asci + 32) : c_asci );
let note_no = 0, note_mode = ( ( c_asci >= 65 && c_asci <= 90 ) ? shift_mode : 0 );
if ( keyboard_mode == 1 )note_no = qwerty_str.indexOf(c_low);
else if ( keyboard_mode == 2 )note_no = qwerty_str_zaq.indexOf(c_low);
else note_no = alphabetic_str.indexOf(c_low);
note_no = parseInt(note_no - 24);
let ff = [];
ff[ff.length] = parseInt(base_freq * Math.pow(2.0, note_no/12.0));
if ( note_mode <= 4 )return ff;
for ( var i = 0; i < 4 && note_mode > 0; i++, note_mode = parseInt(note_mode/10) ){
let nm = parseInt(note_mode % 10);
if(nm == 0)ff[ff.length] = parseInt(base_freq * Math.pow(2.0, parseFloat( note_no - 8 )/12.0));
else if(nm == 8)ff[ff.length] = parseInt(base_freq * Math.pow(2.0, parseFloat( note_no + 12)/12.0));
else ff[ff.length] = parseInt(base_freq * Math.pow(2.0, parseFloat(note_no + nm)/12.0));
}
return ff;
}
function on_key_event(event, is_up){
let str = event.key;
if ( str.length > 1 )return;
let c_asci = str.charCodeAt(0);
if ( c_asci >= 48 && c_asci <= 57 ){
if ( is_up != 0 )return;
c_asci = parseInt(c_asci - 48);
if ( c_asci == 0 ){
document.getElementById('id_harmonics').selectedIndex++;
handle_menu_event(6);
}else{
document.getElementById('id_shift_mode').selectedIndex = c_asci;
handle_menu_event(4);
}
return;
}
let continuous_tone = audiounit_is_continuous_tone_enabled();
if ( continuous_tone == 0 ){
if ( is_up != 0 )return;
let ff = char_to_frequencies(c_asci);
let opt2 = [0,0,0,0,0];
if ( c_asci >= 65 && c_asci <= 92 && shift_mode > 0 && shift_mode <= 4 )opt2 = [shift_mode];
audiounit_play_notes(ff,opt2);
return;
}
let c_asci2 = c_asci;
if ( c_asci >= 65 && c_asci <= 90 )c_asci2 = parseInt(c_asci + 32);
else if ( c_asci >= 97 && c_asci <= 122 )c_asci2 = parseInt(c_asci - 32);
let p = live_down_keys.indexOf(c_asci);
if ( p >= 0 && is_up == 0 )return; //continuous down event
if ( p < 0 )p = live_down_keys.indexOf(c_asci2);
if ( p < 0 && is_up == 1 )return;
if ( p >= 0 )live_down_keys.splice(p,1);
else if ( c_asci == 32 )live_down_keys = [];
else if ( is_up == 0 )live_down_keys[live_down_keys.length] = c_asci;
let ff = [];
for ( var i = 0; i < live_down_keys.length; i++ ){
let f = char_to_frequencies(live_down_keys[i]);
for ( var j = 0; j < f.length; j++ )ff[ff.length] = f[j];
}
audiounit_update_continuous_notes(ff);
}
window.onload = function() {
document.addEventListener('mousedown',function(event)
{
if ( last_focus_window == document.getElementById('my_canvas')){
on_mouse_event(event, 1);
}
last_focus_window = event.target;
}, false);
document.addEventListener('mousemove', function(event){
on_mouse_event(event, 0);
}, false);
document.addEventListener('mouseup', function(event){
on_mouse_event(event, 2);
}, false);
document.addEventListener('keyup', function(event){
// if(last_focus_window == document.getElementById('my_canvas'));
// else return;
on_key_event(event, 1);
}, false);
document.addEventListener('keydown', function(event)
{
// if(last_focus_window == document.getElementById('my_canvas'));
// else return;
on_key_event(event, 0);
}, false);
}
function handle_menu_event(n){
if ( n == 0 ){ // Start button
audiounit_init();
return;
}
if ( n == 1 ){ // Play mode
let s = document.getElementById('id_play_mode').value.toLowerCase();
if ( s.indexOf('timer') >= 0 )audiounit_set_play_mode(1);
else if ( s.indexOf('event') >= 0)audiounit_set_play_mode(2);
else audiounit_set_play_mode(0);
return;
}
if ( n == 2 ){
let s1 = document.getElementById('id_kpm').value.toLowerCase().trim();
let out_mode = document.getElementById('id_output_mode').selectedIndex;
if ( s1.indexOf('flute') >= 0 || s1.indexOf('organ') >= 0 || parseInt(s1) <= 0 ){
audiounit_set_output_settings(out_mode, 0);
}else audiounit_set_output_settings(out_mode, 60000/parseInt(s1));
return;
}
if ( n == 3 ){
let s = document.getElementById('id_keyboard_mode').value.toLowerCase();
if ( s.startsWith('qwerty zaq'))keyboard_mode = 2;
else if ( s.startsWith('qwerty'))keyboard_mode = 1;
else keyboard_mode = 0;
return;
}
if ( n == 4 ){
let s = document.getElementById('id_shift_mode').value.toLowerCase();
if ( s.startsWith("half n"))shift_mode = 1;
else if ( s.startsWith("quarter n"))shift_mode = 2;
else if ( s.startsWith("half vol"))shift_mode = 3;
else if ( s.startsWith("double vol"))shift_mode = 4;
else if ( s.startsWith("major-r"))shift_mode = 470;
else if ( s.startsWith("major"))shift_mode = 47;
else if ( s.startsWith("minor-r"))shift_mode = 370;
else if ( s.startsWith("minor"))shift_mode = 37;
else if ( s.startsWith("r+r"))shift_mode = 8;
else if ( s.startsWith("r+7"))shift_mode = 7;
else shift_mode = 0;
return;
}
if ( n == 5 ){
let sample_rate = parseInt(document.getElementById('id_sample_rate').value);
audiounit_set_sample_rate(sample_rate);
return;
}
if ( n == 6 ){
audiounit_set_harmonic_amps(document.getElementById('id_harmonics').value.split(','));
return;
}
if ( n == 7 ){
base_freq = parseInt(document.getElementById('id_basefreq').value);
return;
}
if ( n == 8 ){
audiounit_set_volume(parseFloat(document.getElementById('id_volume').value/100.0));
return;
}
}
</script>
</head>
<body>
<canvas id="my_canvas" width="640px" height="160px" onkeydown="on_canvas_key(event);"></canvas><br/>
<label>Sample rate</label><select id='id_sample_rate' onchange="handle_menu_event(5);"><option>16000/s</option><option>44000/s</option><option>48000/s</option><option>8000/s</option></select>
<input type="submit" value="Start" onclick="handle_menu_event(0);"/><br/>
<label>Base frequency</label><select id='id_basefreq' onchange="handle_menu_event(7);"><option>420</option><option selected="selected">440</option><option>220</option><option>880</option><option>660</option></select><br/>
<label>Harmonic amps</label><select id='id_harmonics' onchange="handle_menu_event(6);"><option>None</option><option>0.64,0.125,0.11,0.11</option><option>0.125,0.5,0.11,0.11</option><option>0.25,0.125,0.64,0</option><option selected="selected">0.125,0.25,0.1,0.64</option></select><br/><br/>
<b><u>Piano settings</u></b><br/>
<label>Note duration</label><select id='id_kpm' onchange="handle_menu_event(2);"><option>Flute/organ mode</option><option>30 npm</option><option selected="selected">60 npm</option><option>90 npm</option><option>120 npm</option></select><br/>
<label>Play mode</label><select id='id_play_mode' onchange="handle_menu_event(1);"><option>Play as you type</option><option selected="selected">Use timer</option><option>Use end event</option></select><br/>
<label>Output mode</label><select id='id_output_mode' onchange="handle_menu_event(2);"><option>Mono</option><option>Stereo LR Equal</option><option selected="selected">Stereo Type 2 </option><option>Stereo Type 3</option><option>Stereo Type 4</option><option>Stereo Type 5</option></select><br/>
<label>Shift + Key</label><select id='id_shift_mode' onchange="handle_menu_event(4);"><option>None</option><option>Half note</option><option>Quarter note</option><option>Half volume</option><option>Double volume</option><option selected="selected">Major</option><option>Minor</option><option>Major-R</option><option>Minor-R</option><option>R+R</option><option>R+7</option></select><br/>
<label>Key2Note</label><select id='id_keyboard_mode' onchange='handle_menu_event(3);'><option selected="selected">Qwerty</option><option>Qwerty ZAQ</option><option>Alphabetic</option></select><br/>
<label>Volume</label><input id='id_volume' type="range" onchange='handle_menu_event(8);' min="0" max="100" value="30">
<br/><br/>
<label>How to play</label>Click start, and type on your keyboard<br/>
<label>Custom version</label>Contact with [email protected]<br/>
<script type="text/javascript">
let hrm_select = document.getElementById('id_harmonics');
for ( var i = 0; i < 81; i++ ){
let tmp = i;
let s = '';
for ( var j = 0; j < 4; j++ ){
let tmp2 = parseInt(tmp % 3);
tmp = parseInt(tmp/3);
if ( j > 0 )s = ',' + s;
if ( tmp2 == 0 )s = '0' + s;
else if ( tmp2 == 1 )s = '0.25' + s;
else s = '0.5' + s;
}
hrm_select.options[hrm_select.options.length] = new Option(s, s);
}
for ( var i = 1; i <= 8; i++ )handle_menu_event(i);
</script>
</body>
</html>