-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
255 lines (220 loc) Β· 6.46 KB
/
script.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
import { joinRoom, selfId } from "https://cdn.skypack.dev/trystero";
var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
var peer = new Peer(selfId);
peer.on("open", function(id) {
console.log("My peer ID is: " + id, selfId);
});
peer.on("connection", handlePeerConnect);
const config = { appId: "audiotimeline" };
const room = joinRoom(config, "lobby");
window.room = room;
var container = document.getElementById("visualization");
var groups = new vis.DataSet();
groups.add({ id: 1, content: "AUDIO" });
var data = new vis.DataSet();
var options = {};
var timeline = new vis.Timeline(container, data, options);
timeline.setGroups(groups);
timeline.moveTo(Date.now(), {
animation: false
});
var lastPlay;
timeline.on("select", function(properties) {
console.log("click", properties);
var player = document.getElementById("wave" + properties.items);
var text = document.getElementById("text" + properties.items);
if (player && player.play) {
if (lastPlay) {
lastPlay.pause();
}
player.play();
lastPlay = player;
} else if (text && spoken) {
console.log("speaking", text);
spoken.say(text.textContent);
}
return false;
});
// REC CODE
var gumStream; //stream from getUserMedia()
var rec; //Recorder.js object
var input; //MediaStreamAudioSourceNode we'll be recording
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext; //audio context to help us record
var talkButton = document.getElementById("talkButton");
talkButton.addEventListener("click", swapRec);
var speakButton = document.getElementById("speakButton");
speakButton.addEventListener("click", speakUp);
if (!spoken) speakButton.disabled = true;
if (!isChrome) speakButton.style.visibility = "hidden";
var time = {};
var active = false;
function swapRec() {
talkButton.innerHTML = active ? "π€ Talk" : "β Stop";
if (!active) {
startRecording();
} else {
stopRecording();
}
active = !active;
}
function startRecording() {
console.log("recordButton clicked");
time.start = Date.now();
var constraints = {
audio: true,
video: false
};
navigator.mediaDevices
.getUserMedia(constraints)
.then(function(stream) {
audioContext = new AudioContext();
//update the format
document.getElementById("formats").innerHTML =
"Recording: 1 channel pcm @ " + audioContext.sampleRate / 1000 + "kHz";
/* assign to gumStream for later use */
gumStream = stream;
/* use the stream */
input = audioContext.createMediaStreamSource(stream);
/*
Create the Recorder object and configure to record mono sound (1 channel)
Recording 2 channels will double the file size
*/
rec = new Recorder(input, {
numChannels: 1
});
//start the recording process
rec.record();
console.log("Recording started");
})
.catch(function(err) {
console.log(err);
});
}
function pauseRecording() {
console.log("pauseButton clicked rec.recording=", rec.recording);
if (rec.recording) {
rec.stop();
} else {
rec.record();
}
}
function stopRecording() {
console.log("stopButton clicked");
time.stop = Date.now();
rec.stop();
gumStream.getAudioTracks()[0].stop();
document.getElementById("formats").innerHTML = "";
//create the wav blob and pass it on to createDownloadLink
rec.getBuffer(console.log);
rec.exportWAV(blob => createDownloadLink(blob, time));
//rec.exportWAV(blob => sendAudio(blob));
rec.exportWAV(blob => sendPeers(blob, time, selfId, "audio"));
}
async function createDownloadLink(blob, time, remote) {
console.log("got data!", blob);
var url = await URL.createObjectURL(blob);
var au = document.createElement("audio");
//add controls to the <audio> element
au.controls = false;
if (remote) au.autoplay = true;
au.src = url;
var player = au;
// render locally
// insertElement(player,time,remote)
var tsid = Date.now();
player.id = "wave" + tsid;
var pdiv = document.createElement("div");
pdiv.innerHTML = "π ➤";
pdiv.appendChild(player);
var items = [
{
id: tsid,
content: pdiv,
group: 1,
start: time.start || Date.now(),
end: time.stop || undefined
}
];
timeline.moveTo(time.start || Date.now(), {
animation: false
});
data.add(items);
timeline.setGroups(groups);
timeline.fit();
time = {};
}
// ROOM EVENTS
const [sendAudio, getAudio] = room.makeAction("audio");
getAudio((data, id, meta) => processAudio(data, id, meta));
async function processAudio(data, id, meta) {
var blob = new Blob([data], { type: "audio/wav" });
//var blob = data;
console.log(blob, id, meta);
createDownloadLink(blob, true);
}
function sendPeers(data, time, id, type) {
var send = function(p) {
//console.log('sending to peer',p)
var conn = peer.connect(p);
conn.on("open", function() {
return new Promise(function(resolve, reject) {
conn.send({ data, time, id, type });
}).then(console.log("sent")); //conn.close();
});
};
everyone(send);
}
function handlePeerConnect(conn) {
console.log("got connection", conn);
conn.on("data", handlePeerMessage);
}
function handlePeerMessage(msg) {
console.log("peer msg", msg);
if (msg.type == "audio") createDownloadLink(new Blob([msg.data]), msg.time, msg.id)
else insertElement(msg.data,msg.time, msg.id, msg.type)
}
function everyone(cb) {
var peers = room.getPeers();
console.log(peers);
peers.forEach(peer => cb(peer));
}
function speakUp() {
console.log("start spoken!");
spoken.say("ready?").then(e => {
speakButton.disabled = true;
speakButton.innerHTML = "π ...";
spoken
.listen()
.then(transcript => insertElement(transcript))
.then(function(transcript) {
speakButton.innerHTML = "π Text";
speakButton.disabled = false;
return transcript;
})
.then(transcript => sendPeers(transcript, false, selfId, "text"))
.catch(e => console.warn(e.message));
});
}
function insertElement(content, time, id, type) {
var tsid = Date.now();
var pdiv = document.createElement("div");
pdiv.id = "text" + tsid;
pdiv.innerHTML = content;
var items = [
{
id: tsid,
content: pdiv,
group: 1,
start: time ? time.start : tsid,
end: time ? time.stop : undefined
}
];
timeline.moveTo(time ? time.start : tsid, {
animation: false
});
data.add(items);
timeline.setGroups(groups);
timeline.fit();
return content;
}