-
Notifications
You must be signed in to change notification settings - Fork 0
/
recordinstruments.js
134 lines (103 loc) · 3.95 KB
/
recordinstruments.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
const record2 = document.querySelector('.record2');
const stop2 = document.querySelector('.stop2');
const soundClips2 = document.querySelector('#track_list');
const mainSection2 = document.querySelector('.main-controls2');
// tinysynth element is selected, and the audio outputted from it is fed to a gain node
// that then feeds sound to both a media recorder receiving input from
//the mediastream destination node and to the speakers
let synth = document.querySelector('.tinysynth');
let context = synth.getAudioContext();
let speaker = context.destination;
let gain = context.createGain();
synth.setAudioContext(context, gain);
let dest = context.createMediaStreamDestination();
let mediaRecorder2 = new MediaRecorder(dest.stream);
gain.connect(dest);
gain.connect(speaker);
// disable stop button while not recording
stop2.disabled = true;
//main block for doing the audio recording
if (navigator.mediaDevices.getUserMedia) {
console.log('getUserMedia supported.');
const constraints = { audio: true };
let chunks2 = [];
let onSuccess = function(stream) {
record2.onclick = function() {
mediaRecorder2.start();
console.log(mediaRecorder2.state);
console.log("recorder started");
record2.style.background = "red";
stop2.disabled = false;
record2.disabled = true;
}
stop2.onclick = function() {
mediaRecorder2.stop();
console.log(mediaRecorder2.state);
console.log("recorder stopped");
record2.style.background = "";
record2.style.color = "";
// mediaRecorder.requestData();
stop2.disabled = true;
record2.disabled = false;
}
mediaRecorder2.onstop = function(e) {
console.log("data available after MediaRecorder.stop() called.");
const clipName2 = prompt('Enter a name for your sound clip?','My unnamed clip');
const clipContainer2 = document.createElement('article');
const clipLabel2 = document.createElement('p');
const audio2 = document.createElement('audio');
const deleteButton2 = document.createElement('button');
clipContainer2.classList.add('clip');
audio2.setAttribute('controls', '');
deleteButton2.textContent = 'Delete';
deleteButton2.className = 'delete';
if(clipName2 === null) {
clipLabel2.textContent = 'My unnamed clip';
} else {
clipLabel2.textContent = clipName2;
}
let clipRow = document.createElement("tr");
[audio2, clipLabel2, deleteButton2].forEach((item, i) => {
let table_data = document.createElement("td");
if(i) table_data.classList.add("td_align_right");
table_data.appendChild(item);
clipRow.appendChild(table_data);
});
soundClips2.appendChild(clipRow);
//clipRow.appendChild(clipContainer2);
audio2.controls = true;
const blob2 = new Blob(chunks2, { 'type' : 'audio/ogg; codecs=opus' });
chunks2 = [];
const audioURL2 = window.URL.createObjectURL(blob2);
audio2.src = audioURL2;
console.log("recorder stopped");
deleteButton2.onclick = function(e) {
let evtTgt = e.target;
console.log(evtTgt.parentNode);
evtTgt.parentNode.parentNode.parentNode.removeChild(evtTgt.parentNode.parentNode);
}
clipLabel2.onclick = function() {
const existingName2 = clipLabel2.textContent;
const newClipName2 = prompt('Enter a new name for your sound clip?');
if(newClipName2 === null) {
clipLabel2.textContent = existingName2;
} else {
clipLabel2.textContent = newClipName2;
}
}
}
mediaRecorder2.ondataavailable = function(e) {
chunks2.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!');
}
window.onresize = function() {
canvas.width = mainSection.offsetWidth;
}
window.onresize();