@x-tech-laboratories/x-app 0.134.0 → 0.136.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -5
- package/dist/app/desktop.d.ts +8 -2
- package/dist/app/desktop.js +25 -0
- package/dist/app/protocol.d.ts +19 -1
- package/dist/app/protocol.js +1 -1
- package/dist/app/server.js +5 -0
- package/dist/app/services.d.ts +1 -0
- package/dist/app/web/app_page.d.ts +4 -1
- package/dist/app/web/app_page.js +14 -5
- package/dist/app/web/density.d.ts +6 -0
- package/dist/app/web/density.js +40 -0
- package/dist/app/web/hosts.js +2 -0
- package/dist/app/web/page_dashboards.d.ts +7 -1
- package/dist/app/web/page_dashboards.js +12 -1
- package/dist/app/web/portal.js +8 -0
- package/dist/app/web/settings_page.js +5 -1
- package/dist/derived_ids.d.ts +1 -0
- package/dist/derived_ids.js +35 -0
- package/dist/mpv.d.ts +2 -53
- package/dist/mpv.js +3 -66
- package/dist/play_queue.d.ts +110 -0
- package/dist/play_queue.js +380 -0
- package/dist/playback.d.ts +53 -0
- package/dist/playback.js +69 -0
- package/dist/playlist_files.d.ts +17 -0
- package/dist/playlist_files.js +133 -0
- package/dist/server/account_sign_in.d.ts +2 -0
- package/dist/server/account_sign_in.js +27 -0
- package/dist/server/file_ranges.d.ts +11 -0
- package/dist/server/file_ranges.js +80 -0
- package/dist/server/media_links.d.ts +12 -0
- package/dist/server/media_links.js +33 -0
- package/dist/web/audio_engine.d.ts +54 -0
- package/dist/web/audio_engine.js +197 -0
- package/package.json +2 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// A PlaybackEngine on the page's own <audio> element, for a page that plays what its server streams, such as a music
|
|
2
|
+
// app in a browser or on a phone, where there is no mpv: the same PlayQueue then runs in the page. A part of a file
|
|
3
|
+
// plays from its start to its end with positions counted from that start, a gain becomes a lower volume (an element
|
|
4
|
+
// cannot play louder than its file), and the next file is fetched ahead on a second element, which takes over when it
|
|
5
|
+
// is loaded, so the join is as short as the browser allows.
|
|
6
|
+
// What MediaError's codes mean, in words for the listener.
|
|
7
|
+
const media_errors = {
|
|
8
|
+
1: "Playing was stopped before the file loaded.",
|
|
9
|
+
2: "The file could not be fetched; check the connection to the server.",
|
|
10
|
+
3: "The file could not be decoded.",
|
|
11
|
+
4: "This file's format cannot be played here.",
|
|
12
|
+
};
|
|
13
|
+
export class AudioEngine {
|
|
14
|
+
listeners = new Set();
|
|
15
|
+
options;
|
|
16
|
+
audio;
|
|
17
|
+
next = null;
|
|
18
|
+
loaded = null;
|
|
19
|
+
volume = 100;
|
|
20
|
+
state = "idle";
|
|
21
|
+
last_report = -Infinity;
|
|
22
|
+
detach = () => undefined;
|
|
23
|
+
constructor(options = {}) {
|
|
24
|
+
this.options = options;
|
|
25
|
+
this.audio = this.make();
|
|
26
|
+
}
|
|
27
|
+
make() {
|
|
28
|
+
const audio = this.options.make_audio?.() ?? new Audio();
|
|
29
|
+
audio.preload = "auto";
|
|
30
|
+
return audio;
|
|
31
|
+
}
|
|
32
|
+
emit = (event, payload) => {
|
|
33
|
+
for (const listener of this.listeners)
|
|
34
|
+
listener(event, payload);
|
|
35
|
+
};
|
|
36
|
+
set_state(state, error) {
|
|
37
|
+
this.state = state;
|
|
38
|
+
this.emit("state", error === undefined ? { state } : { state, error });
|
|
39
|
+
}
|
|
40
|
+
duration_ms() {
|
|
41
|
+
if (!this.loaded)
|
|
42
|
+
return null;
|
|
43
|
+
if (this.loaded.end_ms !== null)
|
|
44
|
+
return this.loaded.end_ms - this.loaded.start_ms;
|
|
45
|
+
const duration = this.audio.duration;
|
|
46
|
+
return Number.isFinite(duration) && duration > 0 ? Math.round(duration * 1000) - this.loaded.start_ms : null;
|
|
47
|
+
}
|
|
48
|
+
apply_volume() {
|
|
49
|
+
this.audio.volume = Math.max(0, Math.min(1, (this.volume / 100) * (this.loaded?.gain ?? 1)));
|
|
50
|
+
}
|
|
51
|
+
// Listens to the element now playing, and stops listening to the one before.
|
|
52
|
+
attach(audio) {
|
|
53
|
+
this.detach();
|
|
54
|
+
const on = {
|
|
55
|
+
playing: () => this.set_state("playing"),
|
|
56
|
+
pause: () => {
|
|
57
|
+
if (this.state === "playing")
|
|
58
|
+
this.set_state("paused");
|
|
59
|
+
},
|
|
60
|
+
timeupdate: () => this.on_time(),
|
|
61
|
+
ended: () => this.finish(),
|
|
62
|
+
error: () => {
|
|
63
|
+
if (!this.loaded)
|
|
64
|
+
return;
|
|
65
|
+
const error = audio.error;
|
|
66
|
+
this.set_state("error", (error && media_errors[error.code]) ?? error?.message ?? "The file could not be played.");
|
|
67
|
+
},
|
|
68
|
+
loadedmetadata: () => {
|
|
69
|
+
if (this.loaded && this.loaded.start_ms > 0 && audio.currentTime * 1000 < this.loaded.start_ms)
|
|
70
|
+
audio.currentTime = this.loaded.start_ms / 1000;
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
for (const [type, listener] of Object.entries(on))
|
|
74
|
+
audio.addEventListener(type, listener);
|
|
75
|
+
this.detach = () => {
|
|
76
|
+
for (const [type, listener] of Object.entries(on))
|
|
77
|
+
audio.removeEventListener(type, listener);
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
on_time() {
|
|
81
|
+
if (!this.loaded)
|
|
82
|
+
return;
|
|
83
|
+
const absolute = this.audio.currentTime * 1000;
|
|
84
|
+
if (this.loaded.end_ms !== null && absolute >= this.loaded.end_ms) {
|
|
85
|
+
this.finish();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const position = Math.max(0, Math.round(absolute - this.loaded.start_ms));
|
|
89
|
+
const interval = this.options.position_interval_in_milliseconds ?? 250;
|
|
90
|
+
if (Math.abs(position - this.last_report) < interval)
|
|
91
|
+
return;
|
|
92
|
+
this.last_report = position;
|
|
93
|
+
this.emit("position", { position_in_milliseconds: position, duration_in_milliseconds: this.duration_ms() });
|
|
94
|
+
}
|
|
95
|
+
finish() {
|
|
96
|
+
if (this.state === "ended" || !this.loaded)
|
|
97
|
+
return;
|
|
98
|
+
this.audio.pause();
|
|
99
|
+
this.state = "ended";
|
|
100
|
+
this.emit("ended", {});
|
|
101
|
+
}
|
|
102
|
+
async describe(uri, options) {
|
|
103
|
+
return {
|
|
104
|
+
uri: await (this.options.resolve?.(uri) ?? uri),
|
|
105
|
+
start_ms: options?.start_in_milliseconds ?? 0,
|
|
106
|
+
end_ms: options?.end_in_milliseconds ?? null,
|
|
107
|
+
gain: options?.gain_in_decibels == null ? 1 : 10 ** (options.gain_in_decibels / 20),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
async load(uri, options) {
|
|
111
|
+
const loaded = await this.describe(uri, options);
|
|
112
|
+
const ahead = this.next;
|
|
113
|
+
this.next = null;
|
|
114
|
+
// The file before stops without telling the listener it paused.
|
|
115
|
+
this.detach();
|
|
116
|
+
this.audio.pause();
|
|
117
|
+
if (ahead && ahead.loaded.uri === loaded.uri && ahead.loaded.start_ms === loaded.start_ms) {
|
|
118
|
+
// The file fetched ahead takes over.
|
|
119
|
+
this.drop(this.audio);
|
|
120
|
+
this.audio = ahead.audio;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
if (ahead)
|
|
124
|
+
this.drop(ahead.audio);
|
|
125
|
+
this.audio.src = loaded.uri;
|
|
126
|
+
this.audio.load();
|
|
127
|
+
}
|
|
128
|
+
this.loaded = loaded;
|
|
129
|
+
this.last_report = -Infinity;
|
|
130
|
+
this.attach(this.audio);
|
|
131
|
+
if (loaded.start_ms > 0)
|
|
132
|
+
this.audio.currentTime = loaded.start_ms / 1000;
|
|
133
|
+
this.apply_volume();
|
|
134
|
+
this.set_state("loading");
|
|
135
|
+
}
|
|
136
|
+
async preload(uri, options) {
|
|
137
|
+
const loaded = await this.describe(uri, options);
|
|
138
|
+
if (this.next)
|
|
139
|
+
this.drop(this.next.audio);
|
|
140
|
+
const audio = this.make();
|
|
141
|
+
audio.src = loaded.uri;
|
|
142
|
+
audio.load();
|
|
143
|
+
if (loaded.start_ms > 0)
|
|
144
|
+
audio.currentTime = loaded.start_ms / 1000;
|
|
145
|
+
this.next = { audio, loaded };
|
|
146
|
+
}
|
|
147
|
+
async play() {
|
|
148
|
+
if (!this.loaded)
|
|
149
|
+
return;
|
|
150
|
+
await this.audio.play();
|
|
151
|
+
if (this.state !== "playing")
|
|
152
|
+
this.set_state("playing");
|
|
153
|
+
}
|
|
154
|
+
async pause() {
|
|
155
|
+
if (!this.loaded)
|
|
156
|
+
return;
|
|
157
|
+
this.audio.pause();
|
|
158
|
+
this.set_state("paused");
|
|
159
|
+
}
|
|
160
|
+
async stop() {
|
|
161
|
+
this.loaded = null;
|
|
162
|
+
this.drop(this.audio);
|
|
163
|
+
this.audio = this.make();
|
|
164
|
+
this.set_state("idle");
|
|
165
|
+
}
|
|
166
|
+
async seek(position_in_milliseconds) {
|
|
167
|
+
if (!this.loaded)
|
|
168
|
+
return;
|
|
169
|
+
this.audio.currentTime = (this.loaded.start_ms + Math.max(0, position_in_milliseconds)) / 1000;
|
|
170
|
+
this.last_report = -Infinity;
|
|
171
|
+
this.emit("position", { position_in_milliseconds, duration_in_milliseconds: this.duration_ms() });
|
|
172
|
+
}
|
|
173
|
+
async set_volume(volume) {
|
|
174
|
+
this.volume = Math.max(0, Math.min(100, volume));
|
|
175
|
+
this.apply_volume();
|
|
176
|
+
}
|
|
177
|
+
subscribe(listener) {
|
|
178
|
+
this.listeners.add(listener);
|
|
179
|
+
return () => void this.listeners.delete(listener);
|
|
180
|
+
}
|
|
181
|
+
// Lets go of an element, so it stops fetching.
|
|
182
|
+
drop(audio) {
|
|
183
|
+
if (audio === this.audio)
|
|
184
|
+
this.detach();
|
|
185
|
+
audio.pause();
|
|
186
|
+
audio.removeAttribute("src");
|
|
187
|
+
audio.load();
|
|
188
|
+
}
|
|
189
|
+
async dispose() {
|
|
190
|
+
if (this.next)
|
|
191
|
+
this.drop(this.next.audio);
|
|
192
|
+
this.next = null;
|
|
193
|
+
this.loaded = null;
|
|
194
|
+
this.drop(this.audio);
|
|
195
|
+
this.listeners.clear();
|
|
196
|
+
}
|
|
197
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-tech-laboratories/x-app",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.136.0",
|
|
4
4
|
"description": "Code the X-apps share: agents and their long runs, passwords and sign-in, the Fastify server pieces, durations, and the web page's helpers and widgets",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"repository": {
|
|
@@ -252,6 +252,7 @@
|
|
|
252
252
|
"emojibase": "^18.0.0",
|
|
253
253
|
"emojibase-data": "^18.0.0",
|
|
254
254
|
"es-module-lexer": "^3.0.4",
|
|
255
|
+
"fast-xml-parser": "^5.12.1",
|
|
255
256
|
"fflate": "^0.8.3",
|
|
256
257
|
"force-graph": "^1.52.0",
|
|
257
258
|
"frimousse": "^0.4.0",
|