-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
228 lines (205 loc) · 6.68 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Midi playground</title>
<style>
*{
box-sizing: border-box;
}
html,body{
margin:0;
padding:0;
}
.container {
max-width: 350px;
margin: 0 auto;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
fieldset {
width: 350px;
height: 70px;
text-align: left;
}
select,input,button{
margin-top:10px;
}
.arrows{
cursor: pointer;
}
a {
display: inline;
text-decoration: none;
}
.author{
text-align: right;
width: 100%;
margin-top: 3px;
margin-right: 15px;
}
</style>
</head>
<body>
<div class="container">
<fieldset>
<label for="timbre"><strong>Select instrument</strong></label><br>
<span class="arrows left" onclick="changeInstrument(-1)"><</span>
<select id="timbre" onchange="changeInstrument(this.value)"></select>
<span class="arrows right" onclick="changeInstrument(128)">></span>
</fieldset>
<fieldset>
<label for="midiFile"><strong>Load Midi File:</strong></label><br>
<input type="file" id="midiFile" onchange="loadMidi(this.files)">
</fieldset>
<fieldset>
<button onclick="playMidi()">PLAY</button>
<button onclick="stopMidi()">STOP</button>
</fieldset>
<fieldset>
<label for="volume"><strong>Volume:</strong></label><br>
<input id="volume" type="range" value="50" min="0" max="100" oninput="setVolume(this.value)">
</fieldset>
<fieldset style="height:auto;">
<p>You can use your keyboard to play notes or connect a MIDI keyboard to play full range.</p>
<p>Feel free to change the instrument or just upload a MIDI file and enjoy!</p>
</fieldset>
<p class="author">
made by <a href="https://github.com/sujumayas">@sujumayas</a>
</p>
</div>
<script src='https://g200kg.github.io/webaudio-tinysynth/webaudio-tinysynth.js'></script>
<script>
let tinySynth;
let timbre = 1;
// Init synth & start midi
function init(){
tinySynth=new WebAudioTinySynth({voices:64});
for(var i=0;i<128;++i){
var o=document.createElement("option");
o.innerHTML=tinySynth.getTimbreName(0,i);
o.value=i;
document.getElementById("timbre").appendChild(o);
}
//setInterval(function(){
// var st=tinySynth.getPlayStatus();
// document.getElementById("status").innerHTML="Play:"+st.play+" Pos:"+st.curTick+"/"+st.maxTick;
//},100);
//Set main Event Handlers:
document.addEventListener("keydown", handleKeyDown)
document.addEventListener("keyup", handleKeyUp)
}
// Controle Volume
function setVolume(value){
tinySynth.setMasterVol(value/100)
}
// load midi files
function loadMidi(files){
var reader = new FileReader();
reader.onload=(e)=>{
tinySynth.loadMIDI(reader.result);
}
reader.readAsArrayBuffer(files[0]);
}
// play midi file
function playMidi(){
tinySynth.playMIDI();
}
// stop midi file
function stopMidi(){
tinySynth.stopMIDI();
}
// On Midi input handler
function onMidiMessage(message){
data = message.data,
cmd = data[0] >> 4,
channel = data[0] & 0xf,
type = data[0] & 0xf0, // channel agnostic message type. Thanks, Phil Burk.
note = data[1],
velocity = data[2];
// with pressure and tilt off
// note off: 128, cmd: 8
// note on: 144, cmd: 9
// pressure / tilt on
// pressure: 176, cmd 11:
// bend: 224, cmd: 14
if(note && velocity > 0){
processSound([0x90,data[1],data[0]])
}
if(velocity == 0){
processSound([0x80,data[1],0])
}
//console.log('data', data, 'cmd', cmd, 'channel', channel);
}
// Just a not-yet-implemented-filter function
function processSound(midiMessage){
tinySynth.send(midiMessage);
}
// on Change instrument handler
function changeInstrument(number){
if(number == 128 && timbre < 127){
timbre++
changeSelect(timbre)
}
if(number == -1 && timbre > 1){
timbre--
changeSelect(timbre)
}
if(number > 0 && number < 127){
timbre = number
}
tinySynth.send([0xc0, timbre]);
}
//Process keyboard input on
function handleKeyDown(event){
let key = event.key
processSound([0x90,keyboardToMidi[key],100])
}
//Process keyboard input off
function handleKeyUp(event){
let key = event.key
processSound([0x80,keyboardToMidi[key],0])
}
init()
changeInstrument(1)
// Not sure if this can go inside init function
if ("requestMIDIAccess" in navigator) {
// The Web MIDI API is available to us!
navigator.requestMIDIAccess().then(access=> {
const devices = access.inputs.values();
for (let device of devices){
device.onmidimessage = onMidiMessage
}
}).catch(console.error)
}
// Helpers
function changeSelect(number){
document.getElementById("timbre").value = number
}
//Datamappers
let keyboardToMidi = {
"a":61,
"w":62,
"s":63,
"e":64,
"d":65,
"f":66,
"t":67,
"g":68,
"y":69,
"h":70,
"u":71,
"j":72,
"k":73,
"o":74,
"l":75,
"p":76,
"ñ":77,
}
</script>
</body>
</html>