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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
// Radio Emulator
// Kaotisk Hund 2024
//
// A simple co-mechanism to pretend playing a live radio as it would happen for
// a radio station that mostly plays prerecoded shows.
//
// Client side implementation
// Let's remind here the structures we are waiting for:
// 1. Hash
// 2. 0
// 3. audio/ogg file
// 4. application/json file
//
// 1. Hash
// Can be an SHA512sum or SHA256sum, we don't do checks, we only ask the hash
// accompanied by what do we think it is.
//
// 2. 0
// When nothing exists on the radio station we are visiting.
//
// 3. audio/ogg file
// An audio file to play.
//
// 4. application/json file
// Could be one of the following:
// - list
// - show_info
//
var debugMode = true;
const apiURL = "http://z.kaotisk-hund.com:8010/";
const version = "v0";
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);
}
var audioElement = document.querySelector('audio');
// var sourceElement = document.querySelector('source');
var currentTimeP = document.querySelector('.current-time');
var listStartedP = document.querySelector('.started-on');
var currentShowHash = document.querySelector('.current-show-hash');
var listHash = document.querySelector('.list-hash');
var artistP = document.querySelector('.artist');
var titleP = document.querySelector('.title');
var radioPlayerDiv = document.querySelector('.radio-player');
var youAreHere = document.querySelector('.you-are-here');
var ProgressBar = document.querySelector('progress');
var startsOnP = document.querySelector('.starts-on');
var showDurationP = document.querySelector('.show-duration');
var relativeTime = document.querySelector('.relative-time');
var listeningAt = document.querySelector('.listening-at');
const documentTitle = "Radio Station Emulator";
const separator = " :: ";
function setTitleSyncing(){
document.title = "🔃Syncing" + separator + documentTitle;
}
function setTitleMessage(message){
document.title = message + separator + documentTitle;
}
var seconds_here = 0;
function increaseSeconds()
{
seconds_here = seconds_here+1;
youAreHere.innerText = seconds_here;
var progressbarValue = isNaN(parseInt(currentTimeP.innerText)) ? 0 : parseInt(currentTimeP.innerText) + parseInt(seconds_here);
debugLog(`${typeof(progressbarValue)} progressbarValue = ${progressbarValue}`)
ProgressBar.value = progressbarValue;
relativeTime.innerText = parseInt(currentTimeP.innerText) + seconds_here;
listeningAt.innerText = Math.floor(audioElement.currentTime);
return seconds_here;
}
function getSecondsHere()
{
return seconds_here;
}
setInterval(increaseSeconds, 1000);
function FetchJSON( url, callback, params )
{
setTitleSyncing();
const request = new XMLHttpRequest();
request.addEventListener("load", ()=>{
var json = JSON.parse(request.response);
if(request.status !== 404){
callback(json, params);
} else {
debugLog(`ERROR ${request.status} while loading ${url}`);
}
});
request.addEventListener("error", ()=>{
debugLog("An error occured. While loading: "+url+" for "+callback+".");
});
request.addEventListener("abort", ()=>{
debugLog("Request aborted: "+url+" for "+callback+".");
});
request.open("GET", url);
request.send();
}
function FetchAudio(url)
{
const request = new XMLHttpRequest();
request.responseType = 'blob';
request.addEventListener("load", ()=>{
if(request.status === 200){
debugLog("Got it... trying!")
audioElement.src = URL.createObjectURL(request.response);
audioElement.play();
debugLog("Tried... did it work?");
} else {
debugLog(`ERROR ${request.status} while loading ${url}`);
}
});
request.addEventListener("error", ()=>{
debugLog("An error occured. While loading: "+url+" for "+callback+".");
});
request.addEventListener("abort", ()=>{
debugLog("Request aborted: "+url+" for "+callback+".");
});
request.open("GET", url);
request.send();
}
function genericCallback(json, params)
{
debugLog('genericCallback');
debugLog(json);
debugLog(params);
}
var calledLoadShowCallback = 0;
function stopHereAndReflect()
{
return 0;
}
function loadShowCallback(json, params)
{
const [ list, now_on_sequence, element, hash_of_list ] = params;
debugLog('loadShowCallback');
debugLog(json);
debugLog(element);
debugLog(params);
listStartedP.innerText = list.started_on;
listHash.innerText = hash_of_list;
currentShowHash.innerText = json.hash;
artistP.innerText = json.artist;
titleP.innerText = json.title;
setTitleMessage ( "▶️ " + json.artist + " - " + json.title );
showDurationP.innerText = Math.floor(element.duration/1000);
startsOnP.innerText = element.starts_on;
ProgressBar.max = Math.floor(element.duration/1000);
// sourceElement.src = "http://z.kaotisk-hund.com:8010/v0/audio/ogg/" + json.hash + "#t=" + Math.floor((now_on_sequence - element.starts_on)/1000);
audioElement.load();
FetchAudio(`${audioRequest}${json.hash}`);
audioElement.type = json.mimetype;
// debugLog('plays here: '+(now_on_sequence - element.starts_on)/1000);
// audioElement.addEventListener('canplaythrough', function(){
// debugLog('CAN PLAY THROUGH');
// if ( calledLoadShowCallback < 100 )
// {
// calledLoadShowCallback++;
currentTimeP.innerText = ((now_on_sequence - element.starts_on)/1000);
// audioElement.currentTime = ((now_on_sequence - element.starts_on)/1000);
// audioElement.play();
// }
// }, {passive: true});
// audioElement.addEventListener('ended', function(){
// location.reload();
// });
// audioElement.addEventListener('complete', ()=>{
// debugLog("Download completed!")
// var synced_at = sync_radio();
// debugLog("Synced initially at : "+synced_at);
// if ( synced_at === 0 )
// {
// if ( synced_at > audioElement.currentTime )
// {
// debugLog('Good sync!');
// }
// else
// {
setTimeout(sync_radio, 10000);
// }
// }
// }, {passive: true});
}
function sync_radio()
{
var value = currentTimeP.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;
for ( av = 0.1; av === 1; av = av+0.1 )
audioElement.volume = 0.1;
return new_now;
}
return 0;
}
function listCallback(json, params)
{
debugLog('listCallback');
var [ now, hash_of_list ] = params;
var delta_time = now - json.started_on;
var min_times_played = Math.floor( delta_time / json.duration );
var max_times_to_be_played = delta_time / json.duration;
var Dt = max_times_to_be_played - min_times_played;
var now_on_sequence = Dt * json.duration;
debugLog(`now_on_sequence: ${now_on_sequence}, Dt: ${Dt}`)
previous = { starts_on: 0 };
json.list.forEach((element)=>{
if ( now_on_sequence < element.starts_on && now_on_sequence > previous.starts_on){
} else {
now_on_sequence = now_on_sequence - previous.starts_on;
debugLog(now_on_sequence);
previous = element;
debugLog(element);
FetchJSON(`${jsonRequest}${element.hash}`, loadShowCallback, [json, now_on_sequence, element, hash_of_list]);
}
});
}
function hashCallback(json, params)
{
var [ now ] = params;
debugLog('hashCallback');
FetchJSON(`${jsonRequest}${json.latest_list}`, listCallback, [now, json.latest_list]);
}
FetchJSON(`${listRequest}`, hashCallback, [ new Date().getTime() ]);
|