forked from dtinth/midi-rtc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
287 lines (234 loc) · 6.38 KB
/
script.js
File metadata and controls
287 lines (234 loc) · 6.38 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* global p2pkit, Vue */
function wmwrtctr_copy_url_to_clipboard_on_click()
{
const dom_element = document.getElementById( 'connection-info' );
const url = dom_element.innerHTML;
navigator.clipboard.writeText( url );
dom_element.setAttribute( "title", "copied to clipboard" );
dom_element.style.color = 'green';
//console.log( 'URL copied to clipboard:', url );
}
function wmwrtctr_open_close_instructions()
{
const dom_element = document.getElementById( 'wmwrtctr_instructions' );
if ( dom_element.style.display === 'none' )
{
dom_element.style.display = 'block';
}
else
{
dom_element.style.display = 'none';
}
}
Vue.component('port-selector', {
template: '#port-selector',
props: ['available', 'selected'],
computed: {
options() {
return [
{ key: null, text: 'None selected', selected: this.selected === null },
...this.available.map(item => ({
key: item.key,
text: item.name,
selected: this.selected === item.key
}))
]
}
},
methods: {
select(option) {
this.$emit('select', option.key)
}
}
})
const trackersAnnounceURLs = [
//'wss://tracker.btorrent.xyz',
'wss://tracker.openwebtorrent.com',
//'wss://tracker.fastcast.nz',
//'wss://tracker.sloppyta.co:443/',
'wss://tracker.novage.com.ua:443/',
//'wss://spacetradersapi-chatbox.herokuapp.com:443/announce',
//'wss://tracker.files.fm:7073/announce',
]
const peers = new Set()
let roomId
const roomIdMatch = location.hash.match(/^\#?([a-zA-Z0-9-]+)/)
if (roomIdMatch) {
roomId = roomIdMatch[1]
} else {
roomId = crypto.randomUUID()
location.replace('#' + roomId)
}
const p2pt = new p2pkit.P2PT(trackersAnnounceURLs, roomId)
const app = new Vue({
el: '#app',
data: {
baseUrl: location.href.replace(/[\?#].*/, ''),
error: null,
selectedInput: null,
selectedOutput: null,
availableInputs: [],
availableOutputs: [],
currentInputPort: null,
currentOutputPort: null,
hardCodedNameInput: "WebRTC-Outgoing", // A MIDI PORT THAT IS CONSIDERED OUTGOING FROM THIS DEVICE PERSPECTIVE
hardCodedNameOutput: "WebRTC-Incoming", // A MIDI PORT THAT IS CONSIDERED INCOMING FROM THIS DEVICE PERSPECTIVE
midiInputPortName: "",
midiOutputPortName: "",
received: 0,
sent: 0,
activeConnections: 0,
},
methods:
{
pauseMIDIEngine: function()
{
window.location.href = "./paused.html";
}
},
computed: {
url() {
return this.baseUrl + '#' + roomId
},
currentInput() {
return this.selectedInput
},
currentOutput() {
return this.selectedOutput
},
},
watch: {
selectedInput: {
handler(nextValue, currentValue) {
this.currentInputPort = nextValue != null ? this.midiAccess.inputs.get(nextValue) : null
console.log('currentInputPort =>', this.currentInputPort);
if ( this.currentInputPort )
{
this.currentInputPort.open(); console.log( 'currentInputPort open' );
}
},
immediate: true
},
selectedOutput: {
handler(nextValue, currentValue) {
this.currentOutputPort = nextValue != null ? this.midiAccess.outputs.get(nextValue) : null
console.log('currentOutputPort =>', this.currentOutputPort)
},
immediate: true
},
currentInputPort: {
handler(nextValue, currentValue) {
if (currentValue) {
currentValue.onmidimessage = () => {}
}
if (nextValue) {
nextValue.onmidimessage = (e) => {
this.sent ++
peers.forEach(peer => {
p2pt.send(peer, { data: [...e.data] })
})
}
}
},
immediate: true
},
},
async mounted() {
const updateStats = () => {
this.activeConnections = peers.size
}
p2pt.on('trackerwarning', (error, stats) => {
console.warn('trackerwarning =>', error, stats)
})
p2pt.on('trackerconnect', (tracker, stats) => {
//console.log('trackerconnect =>', tracker, stats)
})
p2pt.on('peerconnect', (peer) => {
console.log('peerconnect =>', peer)
peers.add(peer)
updateStats()
})
p2pt.on('peerclose', (peer) => {
console.log('peerclose =>', peer)
peers.delete(peer)
updateStats()
})
p2pt.on
(
'msg', ( peer, message ) =>
{
// console.log( 'received from p2p <= ' + JSON.stringify( peer ) + ", data: " + message.data );
this.received++;
if ( this.currentOutputPort )
{
this.currentOutputPort.send( message.data );
}
}
);
p2pt.start()
window.p2pt = this.p2pt = p2pt
try
{
const access = this.midiAccess = await navigator.requestMIDIAccess( { sysex: false, software: true } );
const refreshPorts = () =>
{
this.availableInputs = getKeys( access.inputs ).map(key => ({
key,
name: access.inputs.get(key).name
}))
this.availableOutputs = getKeys( access.outputs ).map(key => ({
key,
name: access.outputs.get(key).name
}))
};
access.onstatechange = refreshPorts;
refreshPorts();
const bindToPorts = () =>
{
this.currentInputPort = matchName( access.inputs, this.hardCodedNameInput );
this.currentOutputPort = matchName( access.outputs, this.hardCodedNameOutput );
console.log( 'bindToPorts(): currentInputPort =>', this.currentInputPort );
console.log( 'bindToPorts(): currentOutputPort =>', this.currentOutputPort );
this.midiInputPortName = this.currentInputPort.name;
this.midiOutputPortName = this.currentOutputPort.name;
};
bindToPorts();
}
catch (e)
{
console.log( 'Failed to request MIDI access! ' + e )
this.error = ( 'Failed to request MIDI access! ' + e )
}
}
})
function getKeys( portMap )
{
const keys = [];
const iterator = portMap.keys();
for (;;)
{
const { done, value: key } = iterator.next();
if ( done ) break;
keys.push( key );
}
return keys;
}
function matchName( portMap, toMatch )
{
var port_key = null;
var port = null;
const iterator = portMap.keys();
for (;;)
{
const { done, value: key } = iterator.next();
if ( done ) break;
port = portMap.get( key );
//console.log( "matchName(): port: " + port.name );
if ( port.name === toMatch )
{
port_key = key;
break;
}
}
return port;
}