diff options
Diffstat (limited to 'bairadio.js')
-rw-r--r-- | bairadio.js | 137 |
1 files changed, 64 insertions, 73 deletions
diff --git a/bairadio.js b/bairadio.js index 2e1eafe..4ee1541 100644 --- a/bairadio.js +++ b/bairadio.js @@ -1,17 +1,24 @@ var radionum = 0; -function updateSelect() { +function update(first) { + var first = first || false; + var req = new XMLHttpRequest(); var url = "https://bienvenidoainternet.org:8443"; req.open("GET", url+"/status-json.xsl", true); req.send(); req.onreadystatechange = function() { if (this.readyState === 4 && this.status === 200) { + if (first) { document.getElementById("status").textContent = ""; } + var resp = JSON.parse(req.response); resp = resp.icestats.source; + if (radionum > resp.length) { radionum = 0; } + var sel = document.getElementById("server_name"); - sel.innerHTML = ""; + sel.textContent = ""; + if (resp.length > 1) { for (var i = -1; ++i < resp.length;) { var o = document.createElement("option"); @@ -31,28 +38,9 @@ function updateSelect() { if (resp.server_description) { o.textContent += ": " + resp.server_description; } + o.defaultSelected = true; sel.appendChild(o); } - } - } -} - -function update(first) { - var first = first || false; - - var req = new XMLHttpRequest(); - var url = "https://bienvenidoainternet.org:8443"; - req.open("GET", url+"/status-json.xsl", true); - req.send(); - req.onreadystatechange = function() { - if (this.readyState === 4 && this.status === 200) { - if (first) { document.getElementById("status").textContent = ""; } - - var resp = JSON.parse(req.response); - resp = resp.icestats.source; - if (resp.length > 1) { - resp = resp[radionum]; - } var audio = document.getElementById("stream"); resp.listenurl = resp.listenurl.replace("http://bienvenidoainternet.org:8000", "https://bienvenidoainternet.org:8443"); @@ -63,53 +51,53 @@ function update(first) { var current = document.getElementById("artist").textContent + document.getElementById("title").textContent; + var artist = document.getElementById("artist"); if (resp.artist) { resp.artist = resp.artist.trim(); - var artist = document.getElementById("artist"); - if (!artist.textContent.startsWith(resp.artist)) { + if (artist.textContent.slice(0,-3) != resp.artist) { artist.textContent = resp.artist + " - "; } } else { - document.getElementById("artist").textContent = ""; + artist.textContent = ""; } + var title = document.getElementById("title"); if (resp.title) { - resp.title = resp.title.trim(); - var title = document.getElementById("title"); - if (title.textContent != resp.title) { - if (title.textContent != "" && title.textContent != resp.title) { - var history = document.getElementById("history"); - history.innerHTML += '<div class="pastsong">' + current + '</div>'; - history.scrollTop = history.scrollHeight; - } - - title.textContent = resp.title; - document.title = resp.title + ((resp.artist) ? (" - "+resp.artist) : "") + " @ Radio B.a.I."; - - checkLong(); - } + resp.title = String(resp.title).trim(); } else { - document.getElementById("title").textContent = "(Sin título)"; - document.title = "(Sin título) @ Radio B.a.I."; + resp.title = "(Sin título)"; } - + if (title.textContent != resp.title) { + var history = document.getElementById("history"); + history.innerHTML += '<div class="pastsong">' + current + '</div>'; + history.scrollTop = history.scrollHeight; + + title.textContent = resp.title; + document.title = resp.title + " @ Radio B.a.I."; + + checkLong(); + } + if (resp.listeners) { var listeners = document.getElementById("listeners"); if (listeners.textContent != resp.listeners) { listeners.textContent = resp.listeners; } } - + if (resp["ice-bitrate"]) { var bitrate = document.getElementById("bitrate"); if (bitrate.textContent != resp["ice-bitrate"]) { bitrate.textContent = resp["ice-bitrate"]; } } - + if (resp.audio_samplerate) { var audio_samplerate = document.getElementById("audio_samplerate"); - audio_samplerate.textContent = Math.round(resp.audio_samplerate / 1000); + var khz = Math.round(resp.audio_samplerate / 1000); + if (audio_samplerate.textContent != khz) { + audio_samplerate.textContent = khz; + } } } } @@ -129,51 +117,55 @@ function startVisualization() { anal.getByteFrequencyData(freq); var vis = document.getElementById("vis"); - vis.innerHTML = ""; + vis.textContent = ""; var div = 3; + var svgns = "http://www.w3.org/2000/svg"; for (var i = 0; i < 900; i += 20) { var h = (freq[i] / div); - vis.innerHTML += '<rect class="bar"' + - 'height="' + h + '" ' + - 'width="1"' + - 'x="' + (i / 10) + '" ' + - 'y="' + ((255 / div) - h) + '" />'; + var rect = document.createElementNS(svgns, "rect"); + rect.setAttributeNS(null, "x", (i / 10)); + rect.setAttributeNS(null, "y", ((255 / div) - h)); + rect.setAttributeNS(null, "height", h); + rect.setAttributeNS(null, "width", "1"); + rect.setAttributeNS(null, "class", "bar"); + vis.appendChild(rect); } } - + renderVis(); } function checkLong() { + var cont = document.getElementById("nowplaying"); var strip = document.getElementById("nowplaying-txt"); var marquee = document.getElementById("marqueeStyle"); if (marquee) marquee.parentElement.removeChild(marquee); - if (strip.clientWidth > 480) { // #nowplaying width = 480px - var travel = strip.clientWidth - 475; + var ws = strip.clientWidth; + var wc = cont.clientWidth + if (ws > wc) { + var travel = ws - (wc-5); var style = document.createElement("style"); style.id = "marqueeStyle"; - style.textContent = "#nowplaying-txt {animation: marquee " + Math.round(travel/10) + "s linear infinite;}"; - style.textContent += "@keyframes marquee {"; - style.textContent += "10% { transform: translateX(0); }"; - style.textContent += "45% { transform: translateX(-" + travel + "px); }"; - style.textContent += "55% { transform: translateX(-" + travel + "px); }"; - style.textContent += "90% { transform: translateX(0); }"; - style.textContent += "}"; + var dur = Math.round(travel/10); + if (dur < 3) { dur++; } + style.textContent = "#nowplaying-txt {\n -webkit-animation: marquee " + dur + "s linear infinite;\n animation: marquee " + dur + "s linear infinite;\n}\n"; + style.textContent += "@-webkit-keyframes marquee {\n 10% { transform: translateX(0); }\n 45% { transform: translateX(-" + travel + "px); }\n 55% { transform: translateX(-" + travel + "px); }\n 90% { transform: translateX(0); }\n}\n"; + style.textContent += "@keyframes marquee {\n 10% { transform: translateX(0); }\n 45% { transform: translateX(-" + travel + "px); }\n 55% { transform: translateX(-" + travel + "px); }\n 90% { transform: translateX(0); }\n}"; document.getElementsByTagName("head")[0].appendChild(style); } } function playPause() { var radio = document.getElementById("stream"); - var vis = document.getElementById("vis"); + var stat = document.getElementById("control"); if (radio.paused) { radio.play(); - vis.removeAttribute("class") + stat.setAttribute("class", "playing"); } else { - vis.setAttribute("class", "show-play-btn"); + stat.setAttribute("class", "paused"); radio.pause(); } } @@ -204,13 +196,15 @@ function changeVolume(e) { function collapseHistory() { var box = document.getElementById("box-history"); - + var cont = document.getElementById("history").parentElement; if (box.className == "outer") { box.classList.add("collapse"); + cont.style.width = (box.clientWidth - document.getElementById("box-history-title").clientWidth - 14) + "px"; } else { + cont.removeAttribute("style"); box.classList.remove("collapse"); } - + box.scrollTop = box.scrollHeight; removeSel(); } @@ -238,11 +232,10 @@ function removeSel() { var chatConnected = false; var antiFlood = false; -const chatMaxChar = 80; const chatFloodTime = 1100; function connect() { - sock = new WebSocket("wss://tv.bienvenidoainternet.org/daemon/"); + sock = new WebSocket("wss://radio.bienvenidoainternet.org/daemon/"); sock.addEventListener("open", function(e) { addMsg("Conectado al chat."); chatConnected = true; @@ -283,7 +276,7 @@ function checkSend(e) { if (antiFlood) return; var msgField = document.getElementById("msg_type"); - var msg = msgField.value.trim().substr(0, chatMaxChar); + var msg = msgField.value.trim(); if (!msg) return; if (chatConnected) { @@ -327,16 +320,14 @@ function addMsg(msg) { } document.addEventListener("DOMContentLoaded", function() { - document.getElementById("vis").addEventListener("click", playPause); + document.getElementById("control").addEventListener("click", playPause); document.getElementById("volume").addEventListener("mousedown", changeVolume); document.getElementById("server_name").addEventListener("change", function(e) { radionum = this.value; }); document.getElementById("box-history-title").addEventListener("dblclick", collapseHistory); document.getElementById("resize").addEventListener("mousedown", resizeChat); update(true); - updateSelect(); startVisualization(); - setInterval(update, 1000); - setInterval(updateSelect, 10000); + setInterval(update, 1500); connect(); }); |