From dadf192aef753357b14107ae44d1a9fd8a9514fd Mon Sep 17 00:00:00 2001 From: iou1name Date: Tue, 19 Nov 2024 22:56:16 -0500 Subject: [PATCH] add analyzer setup --- static/pyrite.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/static/pyrite.js b/static/pyrite.js index 1f3ade1..510b7a5 100644 --- a/static/pyrite.js +++ b/static/pyrite.js @@ -1,8 +1,10 @@ var player; var pos_int; +var analyser; +var audio_data; function init() { - player = document.getElementById('player'); + player = document.querySelector('#player'); player.addEventListener("ended", nextTrack); @@ -13,6 +15,8 @@ function init() { navigator.mediaSession.setActionHandler('nexttrack', nextTrack); pos_int = setInterval(update_position, 300); + + init_analyzer(); } async function get_rand_track() { @@ -77,4 +81,17 @@ function update_position() { playbackRate: player.playbackRate, position: parseInt(player.currentTime) }); + + analyser.getByteTimeDomainData(audio_data); +} + +function init_analyzer() { + let audioCtx = new AudioContext(); + analyser = audioCtx.createAnalyser(); + let source = audioCtx.createMediaElementSource(player); + + source.connect(analyser); + source.connect(audioCtx.destination); + + audio_data = new Uint8Array(analyser.frequencyBinCount); }