-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathoverlay.js
More file actions
166 lines (150 loc) · 3.93 KB
/
overlay.js
File metadata and controls
166 lines (150 loc) · 3.93 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
/**
* audioMotion-analyzer Overlay demo
*
* https://github.com/hvianna/audioMotion-analyzer
*/
import AudioMotionAnalyzer from '../src/audioMotion-analyzer.js';
const videoEl = document.getElementById('video'),
container = document.getElementById('container'),
presetSelection = document.getElementById('presets');
// Visualization presets
const presets = [
{
name: 'Defaults',
options: undefined
},
{
name: 'Classic LEDs',
options: {
mode: 3,
barSpace: .5,
bgAlpha: .7,
colorMode: 'gradient',
gradient: 'classic',
ledBars: true,
lumiBars: false,
maxFreq: 16000,
radial: false,
reflexRatio: 0,
showBgColor: true,
showPeaks: true,
overlay: true
}
},
{
name: 'Mirror wave',
options: {
mode: 10,
bgAlpha: .7,
fillAlpha: .6,
gradient: 'rainbow',
lineWidth: 2,
lumiBars: false,
maxFreq: 16000,
radial: false,
reflexAlpha: 1,
reflexBright: 1,
reflexRatio: .5,
showBgColor: false,
showPeaks: false,
overlay: true
}
},
{
name: 'Radial inverse',
options: {
mode: 3,
barSpace: .25,
bgAlpha: .5,
fillAlpha: .5,
gradient: 'prism',
ledBars: false,
linearAmplitude: true,
linearBoost: 1.8,
lineWidth: 1.5,
maxDecibels: -30,
maxFreq: 16000,
radial: true,
radialInvert: true,
showBgColor: true,
showPeaks: true,
spinSpeed: 2,
outlineBars: true,
overlay: true,
weightingFilter: 'D'
}
},
{
name: 'Reflex Bars',
options: {
mode: 5,
barSpace: .25,
bgAlpha: .5,
colorMode: 'bar-level',
gradient: 'prism',
ledBars: false,
lumiBars: false,
maxFreq: 16000,
radial: false,
reflexAlpha: .5,
reflexFit: true,
reflexRatio: .3,
showBgColor: false,
showPeaks: true,
overlay: true,
outlineBars: false
}
}
];
// Create audioMotion-analyzer object
try {
var audioMotion = new AudioMotionAnalyzer( container, {
source: videoEl,
fsElement: container
});
}
catch( err ) {
container.innerHTML = `<p>audioMotion-analyzer failed with error: ${ err.code ? '<strong>' + err.code + '</strong>' : '' } <em>${ err.code ? err.message : err }</em></p>`;
}
// Display package version at the footer
document.getElementById('version').innerText = AudioMotionAnalyzer.version;
// Event listeners for UI controls
document.querySelectorAll('button[data-prop]').forEach( el => {
el.addEventListener( 'click', () => {
if ( el.dataset.func )
audioMotion[ el.dataset.func ]();
else
audioMotion[ el.dataset.prop ] = ! audioMotion[ el.dataset.prop ];
el.classList.toggle( 'active', audioMotion[ el.dataset.prop ] );
});
});
document.querySelectorAll('[data-setting]').forEach( el => {
el.addEventListener( 'change', () => audioMotion[ el.dataset.setting ] = el.value );
});
presetSelection.addEventListener( 'change', () => {
audioMotion.setOptions( presets[ presetSelection.value ].options );
updateUI();
});
// Display value of ranged input elements
document.querySelectorAll('input[type="range"]').forEach( el => el.addEventListener( 'change', () => updateRangeElement( el ) ) );
// Populate the UI presets select element
presets.forEach( ( preset, index ) => {
const option = new Option( preset.name, index );
presetSelection.append( option );
});
// Initialize settings with options from a preset
presetSelection.value = 3;
audioMotion.setOptions( presets[ presetSelection.value ].options );
updateUI();
// Update value div of range input elements
function updateRangeElement( el ) {
const s = el.nextElementSibling;
if ( s && s.className == 'value' )
s.innerText = el.value;
}
// Update UI elements to reflect the analyzer's current settings
function updateUI() {
document.querySelectorAll('[data-setting]').forEach( el => el.value = audioMotion[ el.dataset.setting ] );
document.querySelectorAll('input[type="range"]').forEach( el => updateRangeElement( el ) );
document.querySelectorAll('button[data-prop]').forEach( el => el.classList.toggle( 'active', audioMotion[ el.dataset.prop ] ) );
}