-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi.js
263 lines (224 loc) · 6.91 KB
/
midi.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
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
/* Controls MIDI initialization and
* key interpretation functions.
* */
InitMidi = () => {
if (navigator.requestMIDIAccess) {
navigator.requestMIDIAccess().then(
(access) => {
setTimeout(() => {
//List of midi inputs
const it=access.inputs;
//Append midi input options to dropdown menu.
it.forEach((input) => {
let e=document.createElement("option");
e.innerHTML=input.name;
document.getElementById("midiport").appendChild(e);
//Append input to midiPort array
midiPort.push(input);
})
//If there are any available midi inputs, select that. Default to first one.
if(midiPort.length>0)
SelectMidi(0);
}, 10);
},
() => {
alert("MIDI is not available.");
}
);}
};
//Selects device through Midi Port array
function SelectMidi(n){
// console.log("Select Port:"+n+":"+(n>=0?midiPort[n].name:"none"));
document.getElementById("midiport").selectedIndex=n+1;
if(currentPort>=0)
midiPort[currentPort].removeEventListener("midimessage",MidiIn);
currentPort=n;
if(currentPort>=0){
//Run MidiIn function whenever a key is pressed on Midi Keyboard.
midiPort[currentPort].addEventListener("midimessage", MidiIn);
}
}
//Parse MIDI keyboard inputs and
//play appropriate note based on keyboard
//key pressed.
function MidiIn(e){
if(synth){
switch(e.data[0]&0xf0){
case 0x90:
kb.setNote(e.data[2]?1:0,e.data[1]);
break;
case 0x80:
kb.setNote(0,e.data[1]);
}
e.data[1]=e.data[1]+curOct*12;
synth.send(e.data,0);
}
}
//Create dialog box for midi files
openMidiFile = () => {
let input = document.createElement('input');
input.type = "file";
input.onchange = e => {
const file = e.target.files[0];
if(file["type"] == "audio/midi") {
utter("Select a MIDI file.");
loadMidi([file]);
} else {
utter("Incorrect file type. Please select a MIDI file.");
}
}
input.click();
}
//Load midi files from the browser.
function loadMidi(files){
let reader = new FileReader();
reader.onload=function(e){
synth.loadMIDI(reader.result);
}
reader.readAsArrayBuffer(files[0]);
}
//Add note to the record.
addNote = (note) => {
noteRecord.push(MidiEvent.noteOn(note));
activeNotes[note] = Date.now();
}
//End note and calculate duration, adding that to the record.
endNote = (note) => {
noteRecord.push(MidiEvent.noteOff(note, convertToTicks(activeNotes[note])))
}
//Convert start time of note to number of MIDI ticks the note was sustained.
convertToTicks = (startTime) => {
return Math.floor((((Date.now()-startTime)/1000)/0.67)*128);
}
//Pull current contents of the record and create track.
createTrack = () => {
tracks.push(new MidiTrack({ events:noteRecord }));
utter(`Track created. ${tracks.length} tracks total.`, voiceIndex);
noteRecord=[];
if (tracks.length-1) curTrackIndex+=1;
}
delTrack = (trackIndex) => {
utter(`Track ${curTrackIndex} deleted.`, voiceIndex)
if(trackIndex) {
curTrackIndex -= 1;
return tracks.filter((track) => {
return track != tracks[trackIndex];
});
} else {
tracks.pop();
}
}
clearNoteQueue = () => {
noteRecord = [];
}
changeTracks = (index) => {
curTrackIndex = index;
}
changeTrackUp = () => {
if(curTrackIndex < (tracks.length-1)) {
utter("Next Track");
curTrackIndex += 1;
} else {
utter("Last Track Already Reached.", voiceIndex);
}
}
changeTrackDown = () => {
if(curTrackIndex) {
utter("Previous Track");
curTrackIndex -= 1;
} else {
utter("First Track Already Reached.", voiceIndex)
}
}
exportSong = () => {
MidiWriter({ tracks: tracks }).save();
}
reloadRecorder = () => {
if (navigator.mediaDevices.getUserMedia) {
console.log('getUserMedia supported.');
const constraints = { audio: true };
let chunks = [];
let onSuccess = function(stream) {
//setting up recorder to record streams from both microphone and instruments
const audioMixer = new MultiStreamsMixer([stream, dest.stream]);
let mixedstream = audioMixer.getMixedStream();
visualize(mixedstream);
const mediaRecorder = useMic ? new MediaRecorder(mixedstream) : new MediaRecorder(dest.stream);
record.onclick = function() {
mediaRecorder.start();
console.log(mediaRecorder.state);
console.log("recorder started");
record.style.background = "red";
stop.disabled = false;
record.disabled = true;
}
stop.onclick = function() {
mediaRecorder.stop();
console.log(mediaRecorder.state);
console.log("recorder stopped");
record.style.background = "";
record.style.color = "";
// mediaRecorder.requestData();
stop.disabled = true;
record.disabled = false;
}
mediaRecorder.onstop = function() {
console.log("data available after MediaRecorder.stop() called.");
const clipName = prompt('Enter a name for your sound clip?','My unnamed clip');
const clipContainer = document.createElement('tr');
const clipLabel = document.createElement('p');
const audio = document.createElement('audio');
const deleteButton = document.createElement('button');
clipContainer.classList.add('clip');
audio.setAttribute('controls', '');
deleteButton.textContent = 'Delete';
deleteButton.className = 'delete';
if(clipName === null) {
clipLabel.textContent = 'My unnamed clip';
} else {
clipLabel.textContent = clipName;
}
clipContainer.appendChild(document.createElement('td')).appendChild(audio);
clipContainer.appendChild(document.createElement('td')).appendChild(clipLabel);
clipContainer.appendChild(document.createElement('td')).appendChild(deleteButton);
soundClips.appendChild(clipContainer);
audio.controls = true;
const blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' });
chunks = [];
const audioURL = window.URL.createObjectURL(blob);
audio.src = audioURL;
console.log("recorder stopped");
deleteButton.onclick = function(e) {
let evtTgt = e.target;
let trackIndex = curTrackIndex;
for (let i=0; i < evtTgt.parentNode.parentNode.parentNode.children.length; i++) {
if(evtTgt.parentNode.parentNode.parentNode.children[i] == evtTgt.parentNode.parentNode) {
let trackIndex = i;
break;
}
}
delTrack(trackIndex);
evtTgt.parentNode.parentNode.parentNode.removeChild(evtTgt.parentNode.parentNode);
}
clipLabel.onclick = function() {
const existingName = clipLabel.textContent;
const newClipName = prompt('Enter a new name for your sound clip?');
if(newClipName === null) {
clipLabel.textContent = existingName;
} else {
clipLabel.textContent = newClipName;
}
}
}
mediaRecorder.ondataavailable = function(e) {
chunks.push(e.data);
}
}
let onError = function(err) {
console.log('The following error occured: ' + err);
}
navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError);
} else {
console.log('getUserMedia not supported on your browser!');
}
}