@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/dist/mpv.d.ts CHANGED
@@ -56,36 +56,8 @@ export declare class MpvProcess {
56
56
  private answer;
57
57
  private hear;
58
58
  }
59
- export type EngineState = "idle" | "loading" | "playing" | "paused" | "ended" | "error";
60
- export interface LoadOptions {
61
- start_in_milliseconds?: number | null;
62
- end_in_milliseconds?: number | null;
63
- gain_in_decibels?: number | null;
64
- mpv_options?: Record<string, string>;
65
- }
66
- export interface EngineEvents {
67
- position: {
68
- position_in_milliseconds: number;
69
- duration_in_milliseconds: number | null;
70
- };
71
- state: {
72
- state: EngineState;
73
- error?: string;
74
- };
75
- ended: Record<string, never>;
76
- }
77
- export type EngineListener = <Name extends keyof EngineEvents>(event: Name, payload: EngineEvents[Name]) => void;
78
- export interface PlaybackEngine {
79
- load(uri: string, options?: LoadOptions): Promise<void>;
80
- play(): Promise<void>;
81
- pause(): Promise<void>;
82
- stop(): Promise<void>;
83
- seek(position_in_milliseconds: number): Promise<void>;
84
- set_volume(volume: number): Promise<void>;
85
- preload?(uri: string, options?: LoadOptions): Promise<void>;
86
- subscribe(listener: EngineListener): () => void;
87
- dispose(): Promise<void>;
88
- }
59
+ export { NullEngine, type EngineListener, type EngineEvents, type EngineState, type LoadOptions, type PlaybackEngine } from "./playback.ts";
60
+ import type { EngineListener, LoadOptions, PlaybackEngine } from "./playback.ts";
89
61
  export interface MpvEngineOptions extends MpvProcessOptions {
90
62
  video?: boolean;
91
63
  position_interval_in_milliseconds?: number;
@@ -113,26 +85,3 @@ export declare class MpvEngine implements PlaybackEngine {
113
85
  subscribe(listener: EngineListener): () => void;
114
86
  dispose(): Promise<void>;
115
87
  }
116
- export declare class NullEngine implements PlaybackEngine {
117
- private readonly listeners;
118
- state: EngineState;
119
- uri: string | null;
120
- position_in_milliseconds: number;
121
- volume: number;
122
- readonly loaded: string[];
123
- readonly preloaded: string[];
124
- private emit;
125
- private set_state;
126
- load(uri: string): Promise<void>;
127
- play(): Promise<void>;
128
- pause(): Promise<void>;
129
- stop(): Promise<void>;
130
- seek(position_in_milliseconds: number): Promise<void>;
131
- set_volume(volume: number): Promise<void>;
132
- preload(uri: string): Promise<void>;
133
- subscribe(listener: EngineListener): () => void;
134
- dispose(): Promise<void>;
135
- tick(position_in_milliseconds: number, duration_in_milliseconds?: number | null): void;
136
- finish(): void;
137
- fail(message: string): void;
138
- }
package/dist/mpv.js CHANGED
@@ -332,6 +332,9 @@ export class MpvProcess {
332
332
  listener(event);
333
333
  }
334
334
  }
335
+ // ── The playback engine ──────────────────────────────────────────────────────
336
+ // The engine every player drives lives in playback, with no Node, so a page can play through it too.
337
+ export { NullEngine } from "./playback.js";
335
338
  // mpv's per-file options, in the form loadfile takes them.
336
339
  function file_options(options) {
337
340
  const flags = [];
@@ -468,72 +471,6 @@ export class MpvEngine {
468
471
  await this.mpv.close();
469
472
  }
470
473
  }
471
- // ── An engine for tests ──────────────────────────────────────────────────────
472
- // Plays nothing and remembers what it was asked, for testing an app's player without mpv.
473
- export class NullEngine {
474
- listeners = new Set();
475
- state = "idle";
476
- uri = null;
477
- position_in_milliseconds = 0;
478
- volume = 100;
479
- loaded = [];
480
- preloaded = [];
481
- emit = (event, payload) => {
482
- for (const listener of this.listeners)
483
- listener(event, payload);
484
- };
485
- set_state(state) {
486
- this.state = state;
487
- this.emit("state", { state });
488
- }
489
- async load(uri) {
490
- this.uri = uri;
491
- this.loaded.push(uri);
492
- this.position_in_milliseconds = 0;
493
- this.set_state("loading");
494
- }
495
- async play() {
496
- this.set_state("playing");
497
- }
498
- async pause() {
499
- this.set_state("paused");
500
- }
501
- async stop() {
502
- this.uri = null;
503
- this.set_state("idle");
504
- }
505
- async seek(position_in_milliseconds) {
506
- this.position_in_milliseconds = position_in_milliseconds;
507
- this.emit("position", { position_in_milliseconds, duration_in_milliseconds: null });
508
- }
509
- async set_volume(volume) {
510
- this.volume = volume;
511
- }
512
- async preload(uri) {
513
- this.preloaded.push(uri);
514
- }
515
- subscribe(listener) {
516
- this.listeners.add(listener);
517
- return () => void this.listeners.delete(listener);
518
- }
519
- async dispose() {
520
- this.listeners.clear();
521
- }
522
- // Reports a position, as mpv does while playing.
523
- tick(position_in_milliseconds, duration_in_milliseconds = 200_000) {
524
- this.position_in_milliseconds = position_in_milliseconds;
525
- this.emit("position", { position_in_milliseconds, duration_in_milliseconds });
526
- }
527
- // Plays the file to its end.
528
- finish() {
529
- this.state = "ended";
530
- this.emit("ended", {});
531
- }
532
- fail(message) {
533
- this.state = "error";
534
- this.emit("state", { state: "error", error: message });
535
- }
536
- }
537
474
  // ── Helpers ──────────────────────────────────────────────────────────────────
538
475
  function sleep(milliseconds) {
539
476
  return new Promise((resolve) => setTimeout(resolve, milliseconds).unref());
@@ -0,0 +1,110 @@
1
+ import type { EngineState, PlaybackEngine } from "./playback.ts";
2
+ export type RepeatMode = "off" | "one" | "all";
3
+ export interface QueueTrack {
4
+ id: string;
5
+ length_ms: number | null;
6
+ }
7
+ export interface QueueMedia {
8
+ id: string;
9
+ uri: string;
10
+ start_ms: number | null;
11
+ end_ms: number | null;
12
+ gain_db: number | null;
13
+ }
14
+ type Maybe<Value> = Value | Promise<Value>;
15
+ export interface PlayQueueSource<Track extends QueueTrack> {
16
+ track: (id: string) => Maybe<Track | null>;
17
+ media: (id: string) => Maybe<QueueMedia | null>;
18
+ missing: (id: string) => Maybe<string>;
19
+ played: (id: string, at: number) => Maybe<void>;
20
+ skipped: (id: string, at: number) => Maybe<void>;
21
+ }
22
+ export interface PlayQueueStatus<Track extends QueueTrack> {
23
+ state: EngineState;
24
+ track: Track | null;
25
+ media: QueueMedia | null;
26
+ position_ms: number;
27
+ duration_ms: number | null;
28
+ volume: number;
29
+ repeat: RepeatMode;
30
+ shuffle: boolean;
31
+ queue: string[];
32
+ context_length: number;
33
+ context_index: number;
34
+ error?: string;
35
+ }
36
+ export interface PlayQueueEvents<Track extends QueueTrack> {
37
+ status: PlayQueueStatus<Track>;
38
+ position: {
39
+ position_ms: number;
40
+ duration_ms: number | null;
41
+ };
42
+ }
43
+ export type PlayQueueListener<Track extends QueueTrack> = <Name extends keyof PlayQueueEvents<Track>>(event: Name, payload: PlayQueueEvents<Track>[Name]) => void;
44
+ export declare const PLAY_THRESHOLD_RATIO = 0.5;
45
+ export declare const PLAY_THRESHOLD_MS = 240000;
46
+ export declare function counts_as_play(position_ms: number, duration_ms: number | null): boolean;
47
+ export declare function shuffle_order(length: number, seed: number): number[];
48
+ export interface PlayQueueOptions {
49
+ on_error?: (error: unknown) => void;
50
+ now?: () => number;
51
+ }
52
+ export declare class PlayQueue<Track extends QueueTrack> {
53
+ private readonly listeners;
54
+ private readonly unsubscribe;
55
+ private context;
56
+ private context_index;
57
+ private order;
58
+ private queue;
59
+ private current_track;
60
+ private current_media;
61
+ private state;
62
+ private position_ms;
63
+ private duration_ms;
64
+ private volume;
65
+ private error;
66
+ repeat: RepeatMode;
67
+ shuffle: boolean;
68
+ private furthest_ms;
69
+ private settled;
70
+ private seed;
71
+ private readonly source;
72
+ private readonly engine;
73
+ private readonly options;
74
+ constructor(source: PlayQueueSource<Track>, engine: PlaybackEngine, options?: PlayQueueOptions);
75
+ private now;
76
+ private keep;
77
+ private count_play_when_heard;
78
+ private count_skip;
79
+ private rebuild_order;
80
+ private order_position;
81
+ private context_at;
82
+ private next_from_context;
83
+ set_context(track_ids: readonly string[], start_index?: number): void;
84
+ enqueue(track_ids: readonly string[], position?: "end" | "next"): void;
85
+ dequeue(index: number): void;
86
+ move_in_queue(index: number, to_index: number): void;
87
+ clear_queue(): void;
88
+ reload_track(track_ids: readonly string[]): Promise<void>;
89
+ play_track(track_id: string, context_index?: number): Promise<void>;
90
+ private drive;
91
+ private preload_next;
92
+ peek_next(): string | null;
93
+ next(asked?: boolean): Promise<void>;
94
+ previous(): Promise<void>;
95
+ private on_ended;
96
+ play(): Promise<void>;
97
+ pause(): Promise<void>;
98
+ toggle(): Promise<void>;
99
+ stop(): Promise<void>;
100
+ seek(position_ms: number): Promise<void>;
101
+ set_volume(volume: number): Promise<void>;
102
+ set_repeat(mode: RepeatMode): void;
103
+ set_shuffle(on: boolean): void;
104
+ status(): PlayQueueStatus<Track>;
105
+ subscribe(listener: PlayQueueListener<Track>): () => void;
106
+ private emit;
107
+ private publish;
108
+ dispose(): Promise<void>;
109
+ }
110
+ export {};
@@ -0,0 +1,380 @@
1
+ // What plays and what plays next, for a music or podcast player: the queue drained before the standing list, repeat and
2
+ // shuffle, when a listen counts as a play rather than a skip, and the next file named ahead for a gapless join. It
3
+ // decides and the PlaybackEngine plays, so the same queue runs in a daemon on mpv and in a page on an <audio> element.
4
+ // What a track is, which of its files plays and where a play is counted are the app's, through its PlayQueueSource,
5
+ // whose calls may answer at once or later, as a page's calls to its server do. No `node:` or DOM imports.
6
+ // ── Rules ────────────────────────────────────────────────────────────────────
7
+ // A listen counts as a play once half the track or four minutes were heard, the usual thresholds; less, abandoned past
8
+ // the first few seconds, is a skip.
9
+ export const PLAY_THRESHOLD_RATIO = 0.5;
10
+ export const PLAY_THRESHOLD_MS = 240_000;
11
+ const SKIP_AFTER_MS = 3000;
12
+ // Previous starts the track again rather than stepping back once this far in.
13
+ const RESTART_AFTER_MS = 3000;
14
+ export function counts_as_play(position_ms, duration_ms) {
15
+ if (position_ms >= PLAY_THRESHOLD_MS)
16
+ return true;
17
+ if (duration_ms === null || duration_ms <= 0)
18
+ return false;
19
+ return position_ms / duration_ms >= PLAY_THRESHOLD_RATIO;
20
+ }
21
+ // A shuffled order of indexes from a seed, so an order can be made again and tested.
22
+ export function shuffle_order(length, seed) {
23
+ const order = Array.from({ length }, (_, index) => index);
24
+ let state = seed | 0 || 1;
25
+ for (let index = length - 1; index > 0; index--) {
26
+ state ^= state << 13;
27
+ state |= 0;
28
+ state ^= state >>> 17;
29
+ state ^= state << 5;
30
+ state |= 0;
31
+ const other = Math.abs(state) % (index + 1);
32
+ [order[index], order[other]] = [order[other], order[index]];
33
+ }
34
+ return order;
35
+ }
36
+ const load_options = (media) => ({ start_in_milliseconds: media.start_ms, end_in_milliseconds: media.end_ms, gain_in_decibels: media.gain_db });
37
+ export class PlayQueue {
38
+ listeners = new Set();
39
+ unsubscribe;
40
+ context = [];
41
+ context_index = -1;
42
+ order = null;
43
+ queue = [];
44
+ current_track = null;
45
+ current_media = null;
46
+ state = "idle";
47
+ position_ms = 0;
48
+ duration_ms = null;
49
+ volume = 80;
50
+ error;
51
+ repeat = "off";
52
+ shuffle = false;
53
+ // The furthest point heard in the current track, so seeking back cannot make a play.
54
+ furthest_ms = 0;
55
+ // A track ends as a play, a skip or neither, never both.
56
+ settled = false;
57
+ seed = 1;
58
+ source;
59
+ engine;
60
+ options;
61
+ constructor(source, engine, options = {}) {
62
+ this.source = source;
63
+ this.engine = engine;
64
+ this.options = options;
65
+ this.unsubscribe = engine.subscribe((event, payload) => {
66
+ if (event === "position") {
67
+ const position = payload;
68
+ this.position_ms = position.position_in_milliseconds;
69
+ this.duration_ms = position.duration_in_milliseconds ?? this.duration_ms;
70
+ this.furthest_ms = Math.max(this.furthest_ms, position.position_in_milliseconds);
71
+ this.count_play_when_heard();
72
+ this.emit("position", { position_ms: this.position_ms, duration_ms: this.duration_ms });
73
+ }
74
+ else if (event === "state") {
75
+ const state = payload;
76
+ this.state = state.state;
77
+ this.error = state.error;
78
+ this.publish();
79
+ }
80
+ else if (event === "ended") {
81
+ void this.on_ended();
82
+ }
83
+ });
84
+ }
85
+ now() {
86
+ return this.options.now?.() ?? Date.now();
87
+ }
88
+ // Keeps a counted play or skip, its failure told to on_error rather than stopping the music.
89
+ keep(work) {
90
+ void Promise.resolve()
91
+ .then(work)
92
+ .catch((error) => this.options.on_error?.(error));
93
+ }
94
+ // ── Counting ─────────────────────────────────────────────────────────────
95
+ count_play_when_heard() {
96
+ if (this.settled || !this.current_track)
97
+ return;
98
+ if (!counts_as_play(this.furthest_ms, this.duration_ms ?? this.current_track.length_ms))
99
+ return;
100
+ this.settled = true;
101
+ const id = this.current_track.id;
102
+ const at = this.now();
103
+ this.keep(() => this.source.played(id, at));
104
+ }
105
+ // A track left before it counted as played; one barely begun is neither.
106
+ count_skip() {
107
+ if (this.settled || !this.current_track || this.furthest_ms < SKIP_AFTER_MS)
108
+ return;
109
+ this.settled = true;
110
+ const id = this.current_track.id;
111
+ const at = this.now();
112
+ this.keep(() => this.source.skipped(id, at));
113
+ }
114
+ // ── Order ────────────────────────────────────────────────────────────────
115
+ rebuild_order() {
116
+ this.order = this.shuffle ? shuffle_order(this.context.length, this.seed) : null;
117
+ }
118
+ order_position() {
119
+ return this.order ? this.order.indexOf(this.context_index) : this.context_index;
120
+ }
121
+ context_at(position) {
122
+ if (position < 0 || position >= this.context.length)
123
+ return null;
124
+ const index = this.order ? this.order[position] : position;
125
+ return this.context[index] ?? null;
126
+ }
127
+ next_from_context(step) {
128
+ if (this.context.length === 0)
129
+ return null;
130
+ let next = this.order_position() + step;
131
+ if (next >= this.context.length || next < 0) {
132
+ if (this.repeat !== "all")
133
+ return null;
134
+ next = step === 1 ? 0 : this.context.length - 1;
135
+ // Another pass through a shuffled list takes a new order.
136
+ if (this.shuffle) {
137
+ this.seed = (this.seed * 1103515245 + 12345) | 0;
138
+ this.rebuild_order();
139
+ }
140
+ }
141
+ const track_id = this.context_at(next);
142
+ if (track_id === null)
143
+ return null;
144
+ return { track_id, index: this.order ? this.order[next] : next };
145
+ }
146
+ // ── The list and the queue ───────────────────────────────────────────────
147
+ // The standing list that plays after the queue, such as the tracks a view shows, with the one playing at start_index.
148
+ set_context(track_ids, start_index = -1) {
149
+ this.context = [...track_ids];
150
+ this.context_index = start_index;
151
+ this.rebuild_order();
152
+ this.publish();
153
+ }
154
+ enqueue(track_ids, position = "end") {
155
+ this.queue = position === "next" ? [...track_ids, ...this.queue] : [...this.queue, ...track_ids];
156
+ this.publish();
157
+ }
158
+ dequeue(index) {
159
+ this.queue = this.queue.filter((_, place) => place !== index);
160
+ this.publish();
161
+ }
162
+ // Takes the queued track at index out and puts it at to_index, or at the end when to_index is past it.
163
+ move_in_queue(index, to_index) {
164
+ if (!Number.isInteger(index) || index < 0 || index >= this.queue.length)
165
+ throw new Error(`no queued track at ${index}`);
166
+ const rest = this.queue.filter((_, place) => place !== index);
167
+ const to = Math.min(Math.max(0, Math.trunc(to_index)), rest.length);
168
+ this.queue = [...rest.slice(0, to), this.queue[index], ...rest.slice(to)];
169
+ this.publish();
170
+ }
171
+ clear_queue() {
172
+ this.queue = [];
173
+ this.publish();
174
+ }
175
+ // Reads the playing track again when one of these changed, so its rating and tags show as they are now.
176
+ async reload_track(track_ids) {
177
+ if (!this.current_track || !track_ids.includes(this.current_track.id))
178
+ return;
179
+ this.current_track = (await this.source.track(this.current_track.id)) ?? this.current_track;
180
+ this.publish();
181
+ }
182
+ // ── Playing ──────────────────────────────────────────────────────────────
183
+ async play_track(track_id, context_index = -1) {
184
+ this.count_skip();
185
+ const track = await this.source.track(track_id);
186
+ if (!track)
187
+ throw new Error(`track ${track_id} no longer exists`);
188
+ const media = await this.source.media(track_id);
189
+ if (!media) {
190
+ this.current_track = track;
191
+ this.current_media = null;
192
+ this.state = "error";
193
+ this.error = await this.source.missing(track_id);
194
+ this.publish();
195
+ return;
196
+ }
197
+ this.current_track = track;
198
+ this.current_media = media;
199
+ this.position_ms = 0;
200
+ this.furthest_ms = 0;
201
+ this.settled = false;
202
+ this.duration_ms = track.length_ms;
203
+ this.error = undefined;
204
+ if (context_index >= 0)
205
+ this.context_index = context_index;
206
+ const started = await this.drive(async () => {
207
+ await this.engine.load(media.uri, load_options(media));
208
+ await this.engine.play();
209
+ });
210
+ if (!started)
211
+ return;
212
+ this.publish();
213
+ // A hint that fails only costs the gapless join.
214
+ void this.preload_next().catch(() => undefined);
215
+ }
216
+ // Runs an engine call, its failure becoming the error the listener sees rather than a rejection.
217
+ async drive(work) {
218
+ try {
219
+ await work();
220
+ return true;
221
+ }
222
+ catch (error) {
223
+ this.state = "error";
224
+ this.error = error instanceof Error ? error.message : String(error);
225
+ this.publish();
226
+ return false;
227
+ }
228
+ }
229
+ async preload_next() {
230
+ if (!this.engine.preload)
231
+ return;
232
+ const upcoming = this.peek_next();
233
+ if (upcoming === null)
234
+ return;
235
+ const media = await this.source.media(upcoming);
236
+ if (media)
237
+ await this.engine.preload(media.uri, load_options(media));
238
+ }
239
+ // The track that plays after this one, without moving on.
240
+ peek_next() {
241
+ if (this.queue.length > 0)
242
+ return this.queue[0];
243
+ if (this.repeat === "one" && this.current_track)
244
+ return this.current_track.id;
245
+ const order = this.order;
246
+ const seed = this.seed;
247
+ const next = this.next_from_context(1)?.track_id ?? null;
248
+ // Peeking past the end of a shuffled list must not take the new pass's order.
249
+ this.order = order;
250
+ this.seed = seed;
251
+ return next;
252
+ }
253
+ async next(asked = true) {
254
+ if (asked)
255
+ this.count_skip();
256
+ if (this.queue.length > 0) {
257
+ const [head, ...rest] = this.queue;
258
+ this.queue = rest;
259
+ await this.play_track(head);
260
+ return;
261
+ }
262
+ if (this.repeat === "one" && this.current_track && !asked) {
263
+ await this.play_track(this.current_track.id);
264
+ return;
265
+ }
266
+ const target = this.next_from_context(1);
267
+ if (!target) {
268
+ await this.stop();
269
+ return;
270
+ }
271
+ await this.play_track(target.track_id, target.index);
272
+ }
273
+ async previous() {
274
+ if (this.position_ms > RESTART_AFTER_MS && this.current_track) {
275
+ await this.seek(0);
276
+ return;
277
+ }
278
+ this.count_skip();
279
+ const target = this.next_from_context(-1);
280
+ if (!target) {
281
+ await this.seek(0);
282
+ return;
283
+ }
284
+ await this.play_track(target.track_id, target.index);
285
+ }
286
+ async on_ended() {
287
+ // Reaching the end is a whole listen, even when no position report crossed the threshold.
288
+ if (!this.settled && this.current_track) {
289
+ this.furthest_ms = this.duration_ms ?? this.current_track.length_ms ?? PLAY_THRESHOLD_MS;
290
+ this.count_play_when_heard();
291
+ }
292
+ await this.next(false);
293
+ }
294
+ async play() {
295
+ if (!this.current_track) {
296
+ if ((this.queue[0] ?? this.context_at(0)) == null)
297
+ return;
298
+ await this.next(false);
299
+ return;
300
+ }
301
+ // After a failure, or once the engine stopped by itself, play starts the track again.
302
+ if (this.state === "error" || this.state === "idle") {
303
+ await this.play_track(this.current_track.id);
304
+ return;
305
+ }
306
+ await this.drive(() => this.engine.play());
307
+ }
308
+ async pause() {
309
+ if (this.state === "idle" || this.state === "error")
310
+ return;
311
+ await this.drive(() => this.engine.pause());
312
+ }
313
+ async toggle() {
314
+ if (this.state === "playing")
315
+ await this.pause();
316
+ else
317
+ await this.play();
318
+ }
319
+ async stop() {
320
+ this.count_skip();
321
+ await this.drive(() => this.engine.stop());
322
+ this.current_track = null;
323
+ this.current_media = null;
324
+ this.position_ms = 0;
325
+ this.state = "idle";
326
+ this.publish();
327
+ }
328
+ async seek(position_ms) {
329
+ if (!(await this.drive(() => this.engine.seek(position_ms))))
330
+ return;
331
+ this.position_ms = position_ms;
332
+ }
333
+ async set_volume(volume) {
334
+ this.volume = Math.max(0, Math.min(100, volume));
335
+ await this.drive(() => this.engine.set_volume(this.volume));
336
+ this.publish();
337
+ }
338
+ set_repeat(mode) {
339
+ this.repeat = mode;
340
+ this.publish();
341
+ }
342
+ set_shuffle(on) {
343
+ this.shuffle = on;
344
+ this.rebuild_order();
345
+ this.publish();
346
+ }
347
+ // ── Watching ─────────────────────────────────────────────────────────────
348
+ status() {
349
+ return {
350
+ state: this.state,
351
+ track: this.current_track,
352
+ media: this.current_media,
353
+ position_ms: this.position_ms,
354
+ duration_ms: this.duration_ms,
355
+ volume: this.volume,
356
+ repeat: this.repeat,
357
+ shuffle: this.shuffle,
358
+ queue: [...this.queue],
359
+ context_length: this.context.length,
360
+ context_index: this.context_index,
361
+ ...(this.error === undefined ? {} : { error: this.error }),
362
+ };
363
+ }
364
+ subscribe(listener) {
365
+ this.listeners.add(listener);
366
+ return () => void this.listeners.delete(listener);
367
+ }
368
+ emit = (event, payload) => {
369
+ for (const listener of this.listeners)
370
+ listener(event, payload);
371
+ };
372
+ publish() {
373
+ this.emit("status", this.status());
374
+ }
375
+ async dispose() {
376
+ this.unsubscribe();
377
+ this.listeners.clear();
378
+ await this.engine.dispose();
379
+ }
380
+ }
@@ -0,0 +1,53 @@
1
+ export type EngineState = "idle" | "loading" | "playing" | "paused" | "ended" | "error";
2
+ export interface LoadOptions {
3
+ start_in_milliseconds?: number | null;
4
+ end_in_milliseconds?: number | null;
5
+ gain_in_decibels?: number | null;
6
+ mpv_options?: Record<string, string>;
7
+ }
8
+ export interface EngineEvents {
9
+ position: {
10
+ position_in_milliseconds: number;
11
+ duration_in_milliseconds: number | null;
12
+ };
13
+ state: {
14
+ state: EngineState;
15
+ error?: string;
16
+ };
17
+ ended: Record<string, never>;
18
+ }
19
+ export type EngineListener = <Name extends keyof EngineEvents>(event: Name, payload: EngineEvents[Name]) => void;
20
+ export interface PlaybackEngine {
21
+ load(uri: string, options?: LoadOptions): Promise<void>;
22
+ play(): Promise<void>;
23
+ pause(): Promise<void>;
24
+ stop(): Promise<void>;
25
+ seek(position_in_milliseconds: number): Promise<void>;
26
+ set_volume(volume: number): Promise<void>;
27
+ preload?(uri: string, options?: LoadOptions): Promise<void>;
28
+ subscribe(listener: EngineListener): () => void;
29
+ dispose(): Promise<void>;
30
+ }
31
+ export declare class NullEngine implements PlaybackEngine {
32
+ private readonly listeners;
33
+ state: EngineState;
34
+ uri: string | null;
35
+ position_in_milliseconds: number;
36
+ volume: number;
37
+ readonly loaded: string[];
38
+ readonly preloaded: string[];
39
+ private emit;
40
+ private set_state;
41
+ load(uri: string): Promise<void>;
42
+ play(): Promise<void>;
43
+ pause(): Promise<void>;
44
+ stop(): Promise<void>;
45
+ seek(position_in_milliseconds: number): Promise<void>;
46
+ set_volume(volume: number): Promise<void>;
47
+ preload(uri: string): Promise<void>;
48
+ subscribe(listener: EngineListener): () => void;
49
+ dispose(): Promise<void>;
50
+ tick(position_in_milliseconds: number, duration_in_milliseconds?: number | null): void;
51
+ finish(): void;
52
+ fail(message: string): void;
53
+ }