From 5553fb4e454a71368aa4a10a1799cdc2f7109ece Mon Sep 17 00:00:00 2001 From: kaotisk Date: Mon, 14 Oct 2024 08:30:05 +0300 Subject: Refactoring --- client/js/radio_emulator.js | 69 ++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 36 deletions(-) (limited to 'client/js') diff --git a/client/js/radio_emulator.js b/client/js/radio_emulator.js index 974e71d..627f057 100644 --- a/client/js/radio_emulator.js +++ b/client/js/radio_emulator.js @@ -33,10 +33,8 @@ const listRequest = `${apiURL}${version}/list` const jsonRequest = `${apiURL}${version}/application/json/` const audioRequest = `${apiURL}${version}/audio/ogg/` -function debugLog(message) -{ - if ( debugMode ) console.log(message); -} +const documentTitle = "Radio Station Emulator"; +const separator = " :: "; var audioElement = document.querySelector('audio'); @@ -65,8 +63,20 @@ var placeholders = { deltaTime: document.querySelector('.delta-time'), } -const documentTitle = "Radio Station Emulator"; -const separator = " :: "; +var values = { + delta_time: 0, + min_times_played: 0, + maxTimesPlayed: 0, + Dt: 0, + now_on_sequence: 0, + seconds_here: 0, + current_time: 0 +} + +function debugLog(message) +{ + if ( debugMode ) console.log(message); +} function setTitleSyncing(){ document.title = "🔃Syncing" + separator + documentTitle; @@ -76,20 +86,18 @@ function setTitleMessage(message){ document.title = message + separator + documentTitle; } -var seconds_here = 0; function increaseSeconds() { - seconds_here = seconds_here+1; - placeholders.youAreHere.innerText = seconds_here; - // debugLog(`${typeof(progressbarValue)} progressbarValue = ${progressbarValue}`) - placeholders.playProgress.value = isNaN(parseInt(placeholders.currentTime.innerText)) ? 0 : parseInt(placeholders.currentTime.innerText) + parseInt(seconds_here); - placeholders.relativeTime.innerText = parseInt(placeholders.currentTime.innerText) + seconds_here; + values.seconds_here = values.seconds_here + 1; + placeholders.youAreHere.innerText = values.seconds_here; + placeholders.playProgress.value = values.current_time + values.seconds_here; + placeholders.relativeTime.innerText = values.current_time + values.seconds_here; placeholders.listeningAt.innerText = Math.floor(audioElement.currentTime); - return seconds_here; + return values.seconds_here; } function getSecondsHere() { - return seconds_here; + return values.seconds_here; } setInterval(increaseSeconds, 1000); @@ -182,8 +190,10 @@ function loadShowCallback(json, params) audioElement.load(); FetchAudio(`${audioRequest}${json.hash}`, sync_radio); audioElement.type = json.mimetype; - placeholders.currentTime.innerText = Math.floor((values.now_on_sequence - listItem.starts_on)/1000); + values.current_time = Math.floor((values.now_on_sequence/1000)); // - listItem.starts_on)/1000); + placeholders.currentTime.innerText = values.current_time; audioElement.addEventListener('ended', function(){ + values.current_time = 0; placeholders.currentTime.value = 0; FetchJSON(`${listRequest}`, hashCallback, [ new Date().getTime() ]); // setTimeout(sync_radio, 10000); @@ -219,28 +229,16 @@ function fadeInAudio() function sync_radio() { - var value = placeholders.currentTime.innerText; - if ( value !== "" ) - { - var new_now = parseFloat(value) + getSecondsHere(); - debugLog("Trying to sync @ "+ value + " + " + getSecondsHere() + " = " + new_now); - audioElement.currentTime = new_now; - audioElement.play(); - audioElement.muted = false; - audioElement.volume = 0; - fadeInAudio(); - return new_now; - } - return 0; + var new_now = values.current_time + getSecondsHere(); + debugLog("Trying to sync @ "+ values.current_time + " + " + getSecondsHere() + " = " + new_now); + audioElement.currentTime = new_now; + audioElement.play(); + audioElement.muted = false; + audioElement.volume = 0; + fadeInAudio(); + return new_now; } -var values = { - delta_time: 0, - min_times_played: 0, - maxTimesPlayed: 0, - Dt: 0, - now_on_sequence: 0 -} function listCallback(json, params) { @@ -263,7 +261,6 @@ function listCallback(json, params) placeholders.maxTimesPlayed.innerText = values.max_times_to_be_played; placeholders.listInfo.innerText = JSON.stringify(json, null, 2); debugLog(`now_on_sequence: ${values.now_on_sequence}, Dt: ${values.Dt}`) - previous = { starts_on: -1 }; if ( json.list.length === 1 ) { FetchJSON(`${jsonRequest}${json.list[0].hash}`, loadShowCallback, [json, values.now_on_sequence, json.list[0], hash_of_list]); -- cgit v1.2.3