-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript-gun.js
228 lines (193 loc) · 6.03 KB
/
script-gun.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
import { joinRoom, selfId } from "https://cdn.skypack.dev/trystero";
const peers = ['https://gundb-multiserver.glitch.me/1235']
const gun = Gun({peers:peers, localStorage:false, radisk:false});
const roomname = "gun";
var root = gun.get(roomname);
window.gun = gun;
gun.get(roomname).once((data, key)=>{console.log("Initial Connection", key, data)})
const config = { appId: "audiotimeline" };
const room = joinRoom(config, roomname);
var counter = 0;
console.log("IIII AAAMMM", selfId);
var container = document.getElementById("visualization");
// create a Group list
var groups = new vis.DataSet();
groups.add({ id: 1, content: "AUDIO" });
// create a DataSet
var data = new vis.DataSet();
var options = {};
// Create a Timeline
var timeline = new vis.Timeline(container, data, options);
timeline.setGroups(groups);
timeline.moveTo(Date.now(), {
animation: false
});
// Play audio on select
var lastPlay;
timeline.on("select", function(properties) {
var player = document.getElementById("wave" + properties.items);
if (lastPlay) {
lastPlay.pause();
}
if (player && player.play) player.play();
lastPlay = player;
return false;
});
// REC CODE
var gumStream; //stream from getUserMedia()
var rec; //Recorder.js object
var input; //MediaStreamAudioSourceNode we'll be recording
// shim for AudioContext when it's not avb.
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext; //audio context to help us record
var talkButton = document.getElementById("talkButton");
talkButton.addEventListener("click", swapRec);
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.exportWAV(blob => createDownloadLink(blob, time, selfId, counter));
rec.exportWAV(blob => sendGun(blob, time, selfId, counter));
counter++
}
function createDownloadLink(blob, time, remote, count) {
console.log("got data!", blob, time, remote, count);
var auto = true;
if (remote == selfId) auto = false;
console.log('evaluating whether an id exists', !document.getElementById('wave'+remote+count), remote, count)
if(!document.getElementById('wave'+remote+count)){
var url = URL.createObjectURL(blob);
var au = document.createElement("audio");
//add controls to the <audio> element
au.controls = false;
if (auto) au.autoplay = true;
au.src = url;
var player = au;
// render locally
var tsid = remote + count;
player.id = "wave" + tsid;
var pdiv = document.createElement("div");
pdiv.innerHTML = "👋 ➤";
pdiv.appendChild(player);
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 = {};
}
}
function sendGun(blob, time, selfId, counter) {
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var base64data = reader.result;
console.log('buffer',base64data.length);
/*
root
.get("123")
.get("meta")
.put({ name: selfId, time: time });
root
.get("123")
.get("file")
.put({ data: base64data });
*/
gun.get(roomname).get('audio').get(selfId).put({id: selfId, count: counter, time: time, data:base64data})
};
}
// is this right?
root.get('audio').on(audio => shotGun(audio), {change:true})
async function shotGun(data){
console.log('audio in!',data)
var dataArray = await gun.get(roomname).map().promOnce();
for(let data of dataArray) {
console.log(data.key, data.data, "DATAAAAA");
if(data.key != "_"){
let audioObject = await gun.get(data.data._['#']).promOnce();
if(!audioObject.data.time.start) {
let timeObject = await gun.get(audioObject.data.time['#']).promOnce()
audioObject.data.time = timeObject.data;
console.log("direct fetch", audioObject.data, timeObject.data);
}
processData(audioObject.data, data.data._['#'])
}
}
}
function processData(data, soul) {
//console.log('processing data', data)
if (!data.time||!data.data||!data.id) return;
// ignore our own, but his might not be peer id at this point, will check
if (data.id === selfId) return;
fetch(data.data)
.then(res => res.blob())
.then(blob => createDownloadLink(blob,data.time,data.id, data.count))
}