visualizer controls
This commit is contained in:
parent
eabec44490
commit
aa02cf9fa4
|
@ -1,14 +1,15 @@
|
||||||
var player;
|
var player;
|
||||||
var canvasCtx;
|
var audio_ctx;
|
||||||
var audioCtx;
|
|
||||||
var analyser;
|
var analyser;
|
||||||
var audio_data;
|
var audio_data;
|
||||||
|
var canvas_ctx;
|
||||||
var draw_loop;
|
var draw_loop;
|
||||||
var vol_prev;
|
var vol_prev;
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
player = document.querySelector('#player');
|
player = document.querySelector('#player');
|
||||||
canvasCtx = document.querySelector('#visualizer').getContext('2d');
|
init_analyzer();
|
||||||
|
init_canvas();
|
||||||
|
|
||||||
player.addEventListener('ended', nextTrack);
|
player.addEventListener('ended', nextTrack);
|
||||||
player.addEventListener('loadedmetadata', display_duration);
|
player.addEventListener('loadedmetadata', display_duration);
|
||||||
|
@ -37,8 +38,6 @@ function init() {
|
||||||
document.querySelector('#vol-off-btn').style.display = 'none';
|
document.querySelector('#vol-off-btn').style.display = 'none';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//init_analyzer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function get_rand_track() {
|
async function get_rand_track() {
|
||||||
|
@ -65,8 +64,8 @@ async function playTrack() {
|
||||||
player.play();
|
player.play();
|
||||||
navigator.mediaSession.playbackState = "playing";
|
navigator.mediaSession.playbackState = "playing";
|
||||||
|
|
||||||
if (!audioCtx) {
|
if (document.querySelector("#visualizer-select").value === 'bar_graph') {
|
||||||
init_analyzer();
|
bar_graph();
|
||||||
}
|
}
|
||||||
|
|
||||||
document.querySelector("#play-btn").style.display = "none";
|
document.querySelector("#play-btn").style.display = "none";
|
||||||
|
@ -116,8 +115,6 @@ function update_position() {
|
||||||
position: Math.floor(player.currentTime)
|
position: Math.floor(player.currentTime)
|
||||||
});
|
});
|
||||||
|
|
||||||
//analyser.getByteTimeDomainData(audio_data);
|
|
||||||
|
|
||||||
document.querySelector('#seek-slider').value = Math.floor(player.currentTime);
|
document.querySelector('#seek-slider').value = Math.floor(player.currentTime);
|
||||||
display_current_time();
|
display_current_time();
|
||||||
}
|
}
|
||||||
|
@ -160,22 +157,38 @@ function volume_restore() {
|
||||||
document.querySelector('#vol-off-btn').style.display = 'none';
|
document.querySelector('#vol-off-btn').style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function select_visualizer(event) {
|
||||||
|
if (event.target.value == 'bar_graph') {
|
||||||
|
bar_graph();
|
||||||
|
} else if (event.target.value == 'disabled') {
|
||||||
|
cancelAnimationFrame(draw_loop);
|
||||||
|
let canvas = document.querySelector('#visualizer');
|
||||||
|
canvas_ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function init_analyzer() {
|
function init_analyzer() {
|
||||||
audioCtx = new AudioContext();
|
audio_ctx = new AudioContext();
|
||||||
analyser = audioCtx.createAnalyser();
|
analyser = audio_ctx.createAnalyser();
|
||||||
let source = audioCtx.createMediaElementSource(player);
|
let source = audio_ctx.createMediaElementSource(player);
|
||||||
|
|
||||||
source.connect(analyser);
|
source.connect(analyser);
|
||||||
source.connect(audioCtx.destination);
|
source.connect(audio_ctx.destination);
|
||||||
|
}
|
||||||
|
|
||||||
|
function init_canvas() {
|
||||||
|
let canvas = document.querySelector('#visualizer');
|
||||||
|
canvas_ctx = canvas.getContext('2d');
|
||||||
|
canvas_ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
function bar_graph() {
|
||||||
analyser.fftSize = 256;
|
analyser.fftSize = 256;
|
||||||
analyser.minDecibels = -90;
|
analyser.minDecibels = -90;
|
||||||
analyser.maxDecibels = -10;
|
analyser.maxDecibels = -10;
|
||||||
analyser.smoothingTimeConstant = 0.85;
|
analyser.smoothingTimeConstant = 0.85;
|
||||||
audio_data = new Uint8Array(analyser.frequencyBinCount);
|
audio_data = new Uint8Array(analyser.frequencyBinCount);
|
||||||
|
|
||||||
let canvas = document.querySelector('#visualizer');
|
|
||||||
canvasCtx.clearRect(0, 0, canvas.width, canvas.height);
|
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,8 +198,8 @@ function draw() {
|
||||||
analyser.getByteTimeDomainData(audio_data);
|
analyser.getByteTimeDomainData(audio_data);
|
||||||
let canvas = document.querySelector('#visualizer');
|
let canvas = document.querySelector('#visualizer');
|
||||||
|
|
||||||
canvasCtx.fillStyle = "rgb(0 0 0)";
|
canvas_ctx.fillStyle = "rgb(0 0 0)";
|
||||||
canvasCtx.fillRect(0, 0, canvas.width, canvas.height);
|
canvas_ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||||
|
|
||||||
const barWidth = (canvas.width / analyser.frequencyBinCount) * 2.5;
|
const barWidth = (canvas.width / analyser.frequencyBinCount) * 2.5;
|
||||||
let barHeight;
|
let barHeight;
|
||||||
|
@ -195,8 +208,8 @@ function draw() {
|
||||||
for (let i = 0; i < analyser.frequencyBinCount; i++) {
|
for (let i = 0; i < analyser.frequencyBinCount; i++) {
|
||||||
barHeight = audio_data[i];
|
barHeight = audio_data[i];
|
||||||
|
|
||||||
canvasCtx.fillStyle = `rgb(${barHeight + 100} 50 50)`;
|
canvas_ctx.fillStyle = `rgb(${barHeight + 100} 50 50)`;
|
||||||
canvasCtx.fillRect(x, canvas.height - barHeight, barWidth, barHeight);
|
canvas_ctx.fillRect(x, canvas.height - barHeight, barWidth, barHeight);
|
||||||
|
|
||||||
x += barWidth + 1;
|
x += barWidth + 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="visualizer-select">Visualizer:</label>
|
<label for="visualizer-select">Visualizer:</label>
|
||||||
<select id="visualizer-select">
|
<select id="visualizer-select" onchange="select_visualizer(event)">
|
||||||
<option value="disabled" selected>Disabled</option>
|
<option value="disabled" selected>Disabled</option>
|
||||||
<option value="bar_graph">Bar Graph</option>
|
<option value="bar_graph">Bar Graph</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user