-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
165 lines (145 loc) · 4.7 KB
/
run.js
File metadata and controls
165 lines (145 loc) · 4.7 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
"use strict";
import objectDeepAssign from './utils/objectDeepAssign.js';
const ctx = new AudioContext();
const audioIn = ctx.createGain();
const audioInSplit = ctx.createChannelSplitter( 2 );
const audioOutSplit = ctx.createChannelSplitter( 2 );
const audioBuffers = new Map();
const audioBSN = [];
let launched = false;
const fftSize = 512;
const audioInAnalyser = ctx.createAnalyser();
const audioInAnalyserL = ctx.createAnalyser();
const audioInAnalyserR = ctx.createAnalyser();
const audioOutAnalyser = ctx.createAnalyser();
const audioOutAnalyserL = ctx.createAnalyser();
const audioOutAnalyserR = ctx.createAnalyser();
const audioInData = new Uint8Array( fftSize / 2 );
const audioInDataL = new Uint8Array( fftSize / 2 );
const audioInDataR = new Uint8Array( fftSize / 2 );
const audioOutData = new Uint8Array( fftSize / 2 );
const audioOutDataL = new Uint8Array( fftSize / 2 );
const audioOutDataR = new Uint8Array( fftSize / 2 );
const spectrumIn = new gsuiSpectrum();
const spectrumOut = new gsuiSpectrum();
const analyserIn = new gsuiAnalyser();
const analyserOut = new gsuiAnalyser();
const fx = new gswaFxEcho();
const pluginWrap = document.querySelector( "#pluginWrap" );
Promise.all( [
fetch( "plugin/echo/gsuiFxEcho.html" ).then( res => res.text() ),
fetch( "plugin/echo/gsuiFxEcho.js" ).then( res => res.text() )
] ).then( arr => {
const div = document.createElement( "div" );
const script = document.createElement( "script" );
div.innerHTML = arr[ 0 ];
script.textContent = arr[ 1 ];
document.body.append( div.firstElementChild, script );
setup();
document.querySelector( "#bufferBtns" ).onclick = onclickButtons;
// pluginWrap.onmouseup = e => {
// lg(e.target, 564)
// };
window.uiFx.data.beats = 4;
} );
function setup() {
const uiFx = new gsuiFxEcho();
window.uiFx = uiFx;
fx.setContext( ctx );
fx.setBPM( 120 );
pluginWrap.append( uiFx.rootElement );
uiFx.attached();
uiFx.oninput = ( param, val ) => {
lg( "gsuiFxEcho.oninput", param, val );
if ( param === "gain" ) {
audioIn.gain.setValueAtTime( val, ctx.currentTime );
}
};
uiFx.onchange = obj => {
lg( "gsuiFxEcho.onchange", obj );
objectDeepAssign(fx.data, obj);
};
audioInAnalyser.fftSize =
audioInAnalyserL.fftSize =
audioInAnalyserR.fftSize =
audioOutAnalyser.fftSize =
audioOutAnalyserL.fftSize =
audioOutAnalyserR.fftSize = fftSize;
audioInAnalyser.smoothingTimeConstant =
audioInAnalyserL.smoothingTimeConstant =
audioInAnalyserR.smoothingTimeConstant =
audioOutAnalyser.smoothingTimeConstant =
audioOutAnalyserL.smoothingTimeConstant =
audioOutAnalyserR.smoothingTimeConstant = 0;
audioInSplit.connect( audioInAnalyserL, 0 );
audioInSplit.connect( audioInAnalyserR, 1 );
audioOutSplit.connect( audioOutAnalyserL, 0 );
audioOutSplit.connect( audioOutAnalyserR, 1 );
audioIn.connect( audioInAnalyser );
audioIn.connect( audioInSplit );
audioIn.connect( fx.input );
fx.output.connect( ctx.destination );
fx.output.connect( audioOutAnalyser );
fx.output.connect( audioOutSplit );
spectrumIn.setCanvas( document.querySelector( "#spectrumIn" ) );
spectrumOut.setCanvas( document.querySelector( "#spectrumOut" ) );
analyserIn.setCanvas( document.querySelector( "#analyserIn" ) );
analyserOut.setCanvas( document.querySelector( "#analyserOut" ) );
}
function startFrame() {
launched = true;
frame();
}
function frame() {
audioInAnalyser.getByteFrequencyData( audioInData );
audioInAnalyserL.getByteFrequencyData( audioInDataL );
audioInAnalyserR.getByteFrequencyData( audioInDataR );
audioOutAnalyser.getByteFrequencyData( audioOutData );
audioOutAnalyserL.getByteFrequencyData( audioOutDataL );
audioOutAnalyserR.getByteFrequencyData( audioOutDataR );
spectrumIn.draw( audioInData );
spectrumOut.draw( audioOutData );
analyserIn.draw( audioInDataL, audioInDataR );
analyserOut.draw( audioOutDataL, audioOutDataR );
requestAnimationFrame( frame );
}
function stopAllBuffers() {
audioBSN.forEach( absn => absn.stop() );
}
function loadPlaySample( smp ) {
const buf = audioBuffers.get( smp );
if ( buf ) {
playBuffer( buf );
} else {
fetch( smp )
.then( res => res.arrayBuffer() )
.then( arr => ctx.decodeAudioData( arr ) )
.then( buf => {
audioBuffers.set( smp, buf );
playBuffer( buf );
} );
}
}
function playBuffer( buf ) {
const absn = ctx.createBufferSource();
absn.buffer = buf;
absn.connect( audioIn );
absn.start();
audioBSN.push( absn );
}
function onclickButtons( e ) {
const name = e.target.dataset.name;
if ( e.target.nodeName === "BUTTON" ) {
stopAllBuffers();
if ( name ) {
if ( launched ) {
loadPlaySample( name );
} else {
ctx.resume().then( () => {
startFrame();
loadPlaySample( name );
} );
}
}
}
}