-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
153 lines (111 loc) · 3.21 KB
/
app.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
"use strict"
import { WCTrackPlayer} from './components/track-player.component.js';
const runOnMainThread = false;
// context audio
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
let media = [];
// set up the different audio nodes we will use for the app
const mixer = audioCtx.createGain();
const gain = audioCtx.createGain();
const panner = audioCtx.createStereoPanner();
const analyser = audioCtx.createAnalyser();
analyser.minDecibels = -90;
analyser.maxDecibels = -10;
analyser.smoothingTimeConstant = 0.85;
// ui
let btnNew = document.querySelector('#btn-new');
let btnPlay = document.querySelector('#btn-play');
let btnStop = document.querySelector('#btn-stop');
let btnGainControl = document.querySelector('#gain-control');
let btnPanControl = document.querySelector('#pan-control');
// handlers
btnNew.addEventListener('click', newMedia);
btnPlay.addEventListener('click', play);
btnStop.addEventListener('click', stop);
btnGainControl.addEventListener('input', e => setGain(e.target.value));
btnPanControl.addEventListener('input', e => setPan(e.target.value));
// disabled context menu
document.addEventListener('contextmenu', e => e.preventDefault());
// dropzone
document.querySelector("#main-dropzone").addEventListener('filesdropped', e => load(e.detail));
// init app
init();
function init() {
// input
mixer.connect(panner);
panner.connect(gain);
gain.connect(analyser);
// output
analyser.connect(audioCtx.destination);
newMedia();
}
function newMedia() {
console.log("New media prepared...");
stop();
clear();
media = [];
setStatus('waiting');
}
function load(files) {
let aTracks = [];
let startTime = (new Date).getTime();
// create media tracks
for (let file of files) {
// create track and append to view
const track = new WCTrackPlayer(file, mixer, audioCtx);
track.addEventListener('soloToggled', e => setSolo(e.srcElement, e.detail.active));
render(track);
aTracks.push(track.prepare());
media.push(track);
}
Promise.all(aTracks)
.then(e => {
enablePlayer();
let timeDiff = (new Date).getTime() - startTime; //in ms
console.log(`Time to player enabled: ${timeDiff}ms`);
})
.catch(e => {
setStatus('error');
console.log(e)
});
}
function play() {
media.forEach(track => track.start());
setStatus('playing');
}
function stop() {
if (audioCtx.state != 'suspended') {
media.forEach(track => track.stop());
}
setStatus('stopped');
}
function setSolo(currentTrack, active) {
media
.filter(track => track != currentTrack)
.forEach(track => track.mute(active));
}
function setGain(value) {
gain.gain.value = (value/100);
}
function setPan(value) {
if (Math.abs(value) < 20) {
value = 0;
btnPanControl.value = 0;
}
panner.pan.value = (value/100);
}
// VIEW
function setStatus(status) {
const body = document.querySelector("body");
body.dataset.status = status;
}
function render(track) {
document.querySelector("#wrap-editor").appendChild(track);
}
function clear() {
const wrap = document.querySelector("#wrap-editor");
wrap.textContent = '';
}
function enablePlayer() {
setStatus('prepared');
}