-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
222 lines (191 loc) · 6.71 KB
/
index.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
const pl = document.querySelector('video');
// const audioCtx = new AudioContext();
// const source = audioCtx.createMediaElementSource(pl);
// const biquadFilter = audioCtx.createBiquadFilter();
// const destination = audioCtx.createMediaStreamDestination();
// const outputAudio = new Audio();
// outputAudio.srcObject = destination.stream;
// outputAudio.play();
let cuePoint = 0;
// Connect audio nodes
// source.connect(biquadFilter);
// biquadFilter.connect(destination);
// Set filter type and initial value
// biquadFilter.type = 'highpass';
// biquadFilter.frequency.value = 0;
// Disable pitch preservation for natural effect
pl.preservesPitch = false;
// Create GUI
addComponent('style', document.querySelector('head'), getStyles());
addComponent('div', document.querySelector('#primary #player'), getHTML());
setOutputOptions();
addKeyBindings();
async function addKeyBindings () {
const $pitchInput = document.getElementById('pitch-control');
window.addEventListener('keypress', ({ key }) => {
switch (key) {
case '-':
$pitchInput.valueAsNumber = $pitchInput.valueAsNumber - .5;
$pitchInput.oninput();
break;
case '=':
$pitchInput.valueAsNumber = $pitchInput.valueAsNumber + .5;
$pitchInput.oninput();
break;
}
})
const midi = await navigator.requestMIDIAccess();
const onMidiMessage = ({ data }) => {
const codes = [...data].map(char => `0x${char.toString(16)}`);
const [control, type, value] = [...data];
const id = `${control}-${type}`;
switch (id) {
case ('177-42'): // Pitch fader
$pitchInput.valueAsNumber = 100 * ((127 - value) / 127);
$pitchInput.oninput();
break;
case ('145-1'): // Play/pause (& crossfader?)
if (value) {
(pl.paused ? pl.play() : pl.pause());
}
break;
case ('145-2'): // Cue
if (value) cue();
break;
case ('145-3'): // Hotcue
setCue();
break;
case ('177-30'): // Jog wheel
pl.currentTime = pl.currentTime + (value > 64 ? .5 : -.5);
break;
}
console.log(codes, data);
}
for (const entry of midi.inputs) {
const input = entry[1];
input.addEventListener('midimessage', onMidiMessage);
}
}
async function setOutputOptions () {
await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
const devices = await navigator.mediaDevices.enumerateDevices();
const audioDevices = devices.filter(({kind}) => kind === 'audiooutput');
const $select = document.querySelector('select#audio-outputs');
for (const device of audioDevices) {
const $option = document.createElement('option')
$option.textContent = device.label;
$option.value = device.deviceId;
$select.append($option);
}
$select.addEventListener('change', (e) => {
pl.setSinkId(e.target.value)
// outputAudio.setSinkId(e.value)
})
}
function cue(){
pl.currentTime = cuePoint;
pl.play();
}
function setCue(){
cuePoint = pl.currentTime;
document.querySelector('.ytdj-dash .cue-point').innerHTML = cuePoint.toFixed(2);
}
function onPitchSliderInput(val){
const range = .12;
const d = ((val - 50) / 50);
pl.playbackRate = 1 + (d * range);
}
function onFilterSliderInput (val) {
// biquadFilter.frequency.value = 5000 * (val/100);
}
function resetPitch(){
const $input = document.getElementById('pitch-control');
pl.playbackRate = 1;
$input.value = $input.getAttribute('value');
}
function resetHighPassFilter(){
const $input = document.getElementById('high-pass-filter-control');
// biquadFilter.frequency.value = 0;
$input.value = $input.getAttribute('value');
}
function addComponent(tag, target, html){
const el = document.createElement(tag || 'div');
el.innerHTML = html;
target.append(el);
console.log('component added', {tag, target, html, el})
}
function getHTML () { return `
<div class="ytdj-dash">
<div class="ytdj-dash__header"><span>youtube dj</span></div>
<div class="ytdj-dash__main">
<div class="ytdj-dash__row">
<label>Output</label>
<select id="audio-outputs"></select>
</div>
<div class="ytdj-dash__row">
<div class="ytdj-dash__sliderControl">
<label>Pitch</label>
<input
id="pitch-control"
type="range"
value="50"
step=".5"
oninput="onPitchSliderInput(this.value)">
<button onclick="resetPitch()">Reset</button>
</div>
</div>
<div class="ytdj-dash__row">
<div class="ytdj-dash__sliderControl">
<label>High-pass filter</label>
<input
id="high-pass-filter-control"
type="range"
value="0"
oninput="onFilterSliderInput(this.value)">
<button onclick="resetHighPassFilter()">Reset</button>
</div>
</div>
<div class="ytdj-dash__row">
<div class="ytdj-dash__cue">
<label>Cue</label>
<button onclick="setCue()">Set cue</button>
<button onclick="cue()">Cue</button>
<span class="cue-point"></span>
</div>
</div>
</div>
<div class="ytdj-dash__footer"></div>
</div>`;
}
function getStyles () { return `
.ytdj-dash {
position:absolute;
top:10px;
left:10px;
width:200px;
background-color:#fff;
border-radius:2px;
font-size:15px;
font-family:monospace
}
.ytdj-dash__header{
padding:10px 20px;
background-color:#233a4d;
color:#fff;
display:flex;
align-items:center
}
.ytdj-dash__footer, .ytdj-dash__main{
padding:10px 20px
}
.ytdj-dash__row{
margin-bottom:15px
}
.ytdj-dash .cue-point{
font-size:13px;
color:#367faa
}
.ytdj-dash select {
width: 100%;
}`;
}