tangled
alpha
login
or
join now
tokono.ma
/
diffuse-applets
0
fork
atom
Experiment to rebuild Diffuse using web applets.
0
fork
atom
overview
issues
pulls
pipelines
feat: Implement a full-fledged audio engine
Steven Vandevelde
1 year ago
91f86ac8
7c8b431f
+481
-80
9 changed files
expand all
collapse all
unified
split
deno.json
deno.lock
src
applets
engine
audio
applet.astro
manifest.json
types.ts
themes
pilot
ui
audio
applet.astro
types.ts
pages
[...applet].astro
scripts
themes
pilot
index.ts
+2
-1
deno.json
reviewed
···
3
3
"@picocss/pico": "npm:@picocss/pico@^2.0.6",
4
4
"@web-applets/sdk": "npm:@web-applets/sdk@^0.2.6",
5
5
"astro": "npm:astro@^5.4.1",
6
6
-
"spellcaster": "npm:spellcaster@^5.0.2"
6
6
+
"spellcaster": "npm:spellcaster@^5.0.2",
7
7
+
"throttle-debounce": "npm:throttle-debounce@^5.0.2"
7
8
},
8
9
"tasks": {
9
10
"astro": "astro",
+7
-2
deno.lock
reviewed
···
8
8
"npm:astro@5.4.2": "5.4.2_vite@6.2.1_zod@3.24.2",
9
9
"npm:astro@^5.4.1": "5.4.2_vite@6.2.1_zod@3.24.2",
10
10
"npm:create-astro@latest": "4.11.1",
11
11
-
"npm:spellcaster@^5.0.2": "5.0.2"
11
11
+
"npm:spellcaster@^5.0.2": "5.0.2",
12
12
+
"npm:throttle-debounce@^5.0.2": "5.0.2"
12
13
},
13
14
"npm": {
14
15
"@astrojs/cli-kit@0.4.1": {
···
2078
2079
"yallist"
2079
2080
]
2080
2081
},
2082
2082
+
"throttle-debounce@5.0.2": {
2083
2083
+
"integrity": "sha512-B71/4oyj61iNH0KeCamLuE2rmKuTO5byTOSVwECM5FA7TiAiAW+UqTKZ9ERueC4qvgSttUhdmq1mXC3kJqGX7A=="
2084
2084
+
},
2081
2085
"tinyexec@0.3.2": {
2082
2086
"integrity": "sha512-KQQR9yN7R5+OSwaK0XQoj22pwHoTlgYqmUscPYoknOoWCWfj/5/ABTMRi69FrKU5ffPVh5QcFikpWJI/P1ocHA=="
2083
2087
},
···
2325
2329
"npm:@picocss/pico@^2.0.6",
2326
2330
"npm:@web-applets/sdk@~0.2.6",
2327
2331
"npm:astro@^5.4.1",
2328
2328
-
"npm:spellcaster@^5.0.2"
2332
2332
+
"npm:spellcaster@^5.0.2",
2333
2333
+
"npm:throttle-debounce@^5.0.2"
2329
2334
]
2330
2335
}
2331
2336
}
+279
-46
src/applets/engine/audio/applet.astro
reviewed
···
1
1
-
<div id="container">
2
2
-
<audio
3
3
-
crossorigin="anonymous"
4
4
-
src="https://archive.org/download/lp_moonlight-sonata_ludwig-van-beethoven-frdric-chopin-alexand/disc1%2F01.02.%20Moonlight%20Sonata%20Op.%2027%2C%20No.%202%20In%20C%20Sharp%20Minor%3A%20Allegretto.mp3?tunnel=1"
5
5
-
preload="auto"></audio>
6
6
-
</div>
1
1
+
<div id="container"></div>
7
2
8
3
<script>
9
4
import { applets } from "@web-applets/sdk";
5
5
+
import { State, Track, TrackState } from "./types";
10
6
11
11
-
interface State {
12
12
-
isPlaying: boolean;
13
13
-
progress: number;
14
14
-
}
7
7
+
////////////////////////////////////////////
8
8
+
// CONSTANTS
9
9
+
////////////////////////////////////////////
10
10
+
const SILENT_MP3 =
11
11
+
"data:audio/mp3;base64,SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU2LjM2LjEwMAAAAAAAAAAAAAAA//OEAAAAAAAAAAAAAAAAAAAAAAAASW5mbwAAAA8AAAAEAAABIADAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDV1dXV1dXV1dXV1dXV1dXV1dXV1dXV1dXV6urq6urq6urq6urq6urq6urq6urq6urq6v////////////////////////////////8AAAAATGF2YzU2LjQxAAAAAAAAAAAAAAAAJAAAAAAAAAAAASDs90hvAAAAAAAAAAAAAAAAAAAA//MUZAAAAAGkAAAAAAAAA0gAAAAATEFN//MUZAMAAAGkAAAAAAAAA0gAAAAARTMu//MUZAYAAAGkAAAAAAAAA0gAAAAAOTku//MUZAkAAAGkAAAAAAAAA0gAAAAANVVV";
15
12
13
13
+
////////////////////////////////////////////
14
14
+
// SETUP
15
15
+
////////////////////////////////////////////
16
16
const context = applets.register<State>();
17
17
const container = document.querySelector("#container");
18
18
-
const audio = document.querySelector("audio");
19
18
20
20
-
////////////////////////////////////////////
21
19
// Initial state
22
22
-
////////////////////////////////////////////
23
20
context.data = {
24
24
-
isPlaying: false,
25
25
-
progress: 0,
21
21
+
nowPlaying: {},
26
22
};
23
23
+
24
24
+
// State helpers
25
25
+
function update(partial: Partial<State>): void {
26
26
+
context.data = { ...context.data, ...partial };
27
27
+
}
28
28
+
29
29
+
function updateNowPlaying(trackId: string, partial: Partial<TrackState>): void {
30
30
+
update({
31
31
+
...context.data,
32
32
+
nowPlaying: {
33
33
+
...context.data.nowPlaying,
34
34
+
[trackId]: { ...context.data.nowPlaying[trackId], ...partial },
35
35
+
},
36
36
+
});
37
37
+
}
27
38
28
39
////////////////////////////////////////////
29
29
-
// Audio events
40
40
+
// ACTIONS
41
41
+
////////////////////////////////////////////
42
42
+
context.setActionHandler(
43
43
+
"render",
44
44
+
async (args: { play?: { trackId: string; volume: number }; tracks: Track[] }) => {
45
45
+
await render(args.tracks);
46
46
+
if (args.play) play({ trackId: args.play.trackId, volume: args.play.volume });
47
47
+
},
48
48
+
);
49
49
+
50
50
+
context.setActionHandler("pause", pause);
51
51
+
context.setActionHandler("play", play);
52
52
+
context.setActionHandler("reload", reload);
53
53
+
context.setActionHandler("seek", seek);
54
54
+
context.setActionHandler("volume", volume);
55
55
+
56
56
+
function pause({ trackId }: { trackId: string }) {
57
57
+
withAudioNode(trackId, (audio) => audio.pause());
58
58
+
}
59
59
+
60
60
+
function play({ trackId, volume }: { trackId: string; volume: number }) {
61
61
+
withAudioNode(trackId, (audio) => {
62
62
+
audio.volume = volume;
63
63
+
audio.muted = false;
64
64
+
65
65
+
if (audio.readyState === 0) audio.load();
66
66
+
if (!audio.isConnected) return;
67
67
+
68
68
+
const promise = audio.play() || Promise.resolve();
69
69
+
const didPreload = audio.getAttribute("data-did-preload") === "true";
70
70
+
const isPreload = audio.getAttribute("data-is-preload") === "true";
71
71
+
72
72
+
if (didPreload && !isPreload) {
73
73
+
audio.removeAttribute("data-did-preload");
74
74
+
}
75
75
+
76
76
+
updateNowPlaying(audio.id, { isPlaying: true });
77
77
+
78
78
+
promise.catch((e) => {
79
79
+
if (!audio.isConnected)
80
80
+
return; /* The node was removed from the DOM, we can ignore this error */
81
81
+
const err = "Couldn't play audio automatically. Please resume playback manually.";
82
82
+
console.error(err, e);
83
83
+
updateNowPlaying(trackId, { isPlaying: false });
84
84
+
});
85
85
+
});
86
86
+
}
87
87
+
88
88
+
function reload(args: { play: boolean; progress?: number; trackId: string }) {
89
89
+
withAudioNode(args.trackId, (audio) => {
90
90
+
if (audio.readyState === 0 || audio.error?.code === 2) {
91
91
+
audio.load();
92
92
+
93
93
+
if (args.progress !== undefined) {
94
94
+
audio.setAttribute("data-initial-progress", JSON.stringify(args.progress));
95
95
+
}
96
96
+
97
97
+
if (args.play) {
98
98
+
play({ trackId: args.trackId, volume: audio.volume });
99
99
+
}
100
100
+
}
101
101
+
});
102
102
+
}
103
103
+
104
104
+
function seek({ percentage, trackId }: { percentage: number; trackId: string }) {
105
105
+
withAudioNode(trackId, (audio) => {
106
106
+
if (!isNaN(audio.duration)) {
107
107
+
audio.currentTime = audio.duration * percentage;
108
108
+
}
109
109
+
});
110
110
+
}
111
111
+
112
112
+
function volume(args: { trackId?: string; volume: number }) {
113
113
+
Array.from(container.querySelectorAll('audio[data-is-preload="false"]')).forEach((node) => {
114
114
+
const audio = node as HTMLAudioElement;
115
115
+
if (args.trackId === undefined || args.trackId === audio.id) {
116
116
+
audio.volume = args.volume;
117
117
+
}
118
118
+
});
119
119
+
}
120
120
+
121
121
+
////////////////////////////////////////////
122
122
+
// RENDER
123
123
+
////////////////////////////////////////////
124
124
+
async function render(tracks: Array<Track>) {
125
125
+
const ids = tracks.map((e) => e.id);
126
126
+
const existingNodes = {};
127
127
+
128
128
+
// Manage existing nodes
129
129
+
Array.from(container.querySelectorAll("audio")).map((node: HTMLAudioElement) => {
130
130
+
if (ids.includes(node.id)) {
131
131
+
existingNodes[node.id] = node;
132
132
+
} else {
133
133
+
node.src = SILENT_MP3;
134
134
+
container?.removeChild(node);
135
135
+
}
136
136
+
});
137
137
+
138
138
+
// Adjust existing and add new
139
139
+
await tracks.reduce(async (acc: Promise<void>, track: Track) => {
140
140
+
await acc;
141
141
+
142
142
+
const existingNode = existingNodes[track.id];
143
143
+
144
144
+
if (existingNode) {
145
145
+
const isPreload = existingNode.getAttribute("data-is-preload");
146
146
+
if (isPreload === "true") existingNode.setAttribute("data-did-preload", "true");
147
147
+
148
148
+
existingNode.setAttribute("data-is-preload", track.isPreload ? "true" : "false");
149
149
+
} else {
150
150
+
await createElement(track);
151
151
+
}
152
152
+
}, Promise.resolve());
153
153
+
154
154
+
// Now playing state
155
155
+
const nowPlaying = tracks.reduce((acc, track) => {
156
156
+
return {
157
157
+
...acc,
158
158
+
[track.id]: context.data.nowPlaying[track.id] || {
159
159
+
duration: 0,
160
160
+
id: track.id,
161
161
+
loadingState: "loading",
162
162
+
isPlaying: true,
163
163
+
isPreload: track.isPreload,
164
164
+
progress: track.progress ?? 0,
165
165
+
},
166
166
+
};
167
167
+
}, {});
168
168
+
169
169
+
update({ nowPlaying });
170
170
+
}
171
171
+
172
172
+
export async function createElement(track: Track) {
173
173
+
const source = document.createElement("source");
174
174
+
if (track.mimeType) source.setAttribute("type", track.mimeType);
175
175
+
source.setAttribute("src", track.url);
176
176
+
177
177
+
// Audio node
178
178
+
const audio = new Audio();
179
179
+
audio.setAttribute("id", track.id);
180
180
+
audio.setAttribute("crossorigin", "anonymous");
181
181
+
audio.setAttribute("data-is-preload", track.isPreload ? "true" : "false");
182
182
+
audio.setAttribute("muted", "true");
183
183
+
audio.setAttribute("preload", "auto");
184
184
+
185
185
+
if (track.progress !== undefined) {
186
186
+
audio.setAttribute("data-initial-progress", JSON.stringify(track.progress));
187
187
+
}
188
188
+
189
189
+
audio.appendChild(source);
190
190
+
191
191
+
audio.addEventListener("canplay", canplayEvent);
192
192
+
audio.addEventListener("durationchange", durationchangeEvent);
193
193
+
audio.addEventListener("ended", endedEvent);
194
194
+
audio.addEventListener("error", errorEvent);
195
195
+
audio.addEventListener("pause", pauseEvent);
196
196
+
audio.addEventListener("play", playEvent);
197
197
+
audio.addEventListener("suspend", suspendEvent);
198
198
+
audio.addEventListener("timeupdate", timeupdateEvent);
199
199
+
audio.addEventListener("waiting", waitingEvent);
200
200
+
201
201
+
container?.appendChild(audio);
202
202
+
}
203
203
+
30
204
////////////////////////////////////////////
31
31
-
audio.ontimeupdate = (event) => {
32
32
-
const progress =
33
33
-
isNaN(audio.duration) || audio.duration === 0 ? 0 : audio.currentTime / audio.duration;
34
34
-
context.data = { ...context.data, progress };
35
35
-
};
205
205
+
// AUDIO EVENTS
206
206
+
////////////////////////////////////////////
36
207
37
37
-
audio.onpause = () => (context.data = { ...context.data, isPlaying: false });
38
38
-
audio.onplay = () => (context.data = { ...context.data, isPlaying: true });
208
208
+
function canplayEvent(event: Event) {
209
209
+
const target = event.target as HTMLAudioElement;
39
210
40
40
-
// TODO:
41
41
-
// audio.oncanplay = (event) => console.log("canplay", event);
42
42
-
// audio.onemptied = (event) => console.log("emptied", event);
43
43
-
// audio.onended = (event) => console.log("ended", event);
44
44
-
// audio.onloadeddata = (event) => console.log("loadeddata", event);
45
45
-
// audio.onloadedmetadata = (event) => console.log("loadedmetadata", event);
46
46
-
// audio.onloadstart = (event) => console.log("loadstart", event);
47
47
-
// audio.onplaying = (event) => console.log("playing", event);
48
48
-
// audio.onstalled = (event) => console.log("stalled", event);
49
49
-
// audio.onsuspend = (event) => console.log("suspend", event);
50
50
-
// audio.onwaiting = (event) => console.log("waiting", event);
211
211
+
if (
212
212
+
target.hasAttribute("data-initial-progress") &&
213
213
+
target.duration &&
214
214
+
!isNaN(target.duration)
215
215
+
) {
216
216
+
const progress = JSON.parse(target.getAttribute("data-initial-progress") as string);
217
217
+
target.currentTime = target.duration * progress;
218
218
+
target.removeAttribute("data-initial-progress");
219
219
+
}
51
220
52
52
-
audio.onerror = (err) => console.error(err);
221
221
+
finishedLoading(event);
222
222
+
}
223
223
+
224
224
+
function durationchangeEvent(event: Event) {
225
225
+
const audio = event.target as HTMLAudioElement;
226
226
+
227
227
+
if (!isNaN(audio.duration)) {
228
228
+
updateNowPlaying(audio.id, { duration: audio.duration });
229
229
+
}
230
230
+
}
231
231
+
232
232
+
function endedEvent(event: Event) {
233
233
+
const audio = event.target as HTMLAudioElement;
234
234
+
audio.currentTime = 0;
235
235
+
// TODO
236
236
+
}
237
237
+
238
238
+
function errorEvent(event: Event) {
239
239
+
const audio = event.target as HTMLAudioElement;
240
240
+
const code = audio.error?.code || 0;
241
241
+
updateNowPlaying(audio.id, { loadingState: { error: { code } } });
242
242
+
}
243
243
+
244
244
+
function pauseEvent(event: Event) {
245
245
+
const audio = event.target as HTMLAudioElement;
246
246
+
updateNowPlaying(audio.id, { isPlaying: false });
247
247
+
}
248
248
+
249
249
+
function playEvent(event: Event) {
250
250
+
const audio = event.target as HTMLAudioElement;
251
251
+
updateNowPlaying(audio.id, { isPlaying: true });
252
252
+
253
253
+
// In case audio was preloaded:
254
254
+
if (audio.readyState === 4) finishedLoading(event);
255
255
+
}
256
256
+
257
257
+
function suspendEvent(event: Event) {
258
258
+
finishedLoading(event);
259
259
+
}
260
260
+
261
261
+
function timeupdateEvent(event: Event) {
262
262
+
const audio = event.target as HTMLAudioElement;
263
263
+
264
264
+
updateNowPlaying(audio.id, {
265
265
+
progress:
266
266
+
isNaN(audio.duration) || audio.duration === 0 ? 0 : audio.currentTime / audio.duration,
267
267
+
});
268
268
+
}
269
269
+
270
270
+
function waitingEvent(event: Event) {
271
271
+
initiateLoading(event);
272
272
+
}
53
273
54
274
////////////////////////////////////////////
55
55
-
// Actions
275
275
+
// 🛠️
56
276
////////////////////////////////////////////
57
57
-
context.setActionHandler("load", (src: string) => {
58
58
-
audio.src = src;
59
59
-
audio.load();
60
60
-
});
61
277
62
62
-
context.setActionHandler("play", () => {
63
63
-
audio.play();
64
64
-
});
278
278
+
function finishedLoading(event: Event) {
279
279
+
const audio = event.target as HTMLAudioElement;
280
280
+
updateNowPlaying(audio.id, { loadingState: "loaded" });
281
281
+
}
65
282
66
66
-
context.setActionHandler("pause", () => {
67
67
-
audio.pause();
68
68
-
});
283
283
+
function initiateLoading(event: Event) {
284
284
+
const audio = event.target as HTMLAudioElement;
285
285
+
if (audio.readyState < 4) updateNowPlaying(audio.id, { loadingState: "loaded" });
286
286
+
}
287
287
+
288
288
+
function withActiveAudioNode(fn: (node: HTMLAudioElement) => void): void {
289
289
+
const nonPreloadNodes: HTMLAudioElement[] = Array.from(
290
290
+
container.querySelectorAll(`audio[data-is-preload="false"]`),
291
291
+
);
292
292
+
293
293
+
const playingNodes = nonPreloadNodes.filter((n) => n.paused === false);
294
294
+
const node = playingNodes.length ? playingNodes[0] : nonPreloadNodes[0];
295
295
+
if (node) fn(node);
296
296
+
}
297
297
+
298
298
+
function withAudioNode(trackId: string, fn: (node: HTMLAudioElement) => void): void {
299
299
+
const node = container.querySelector(`audio[id="${trackId}"][data-is-preload="false"]`);
300
300
+
if (node) fn(node as HTMLAudioElement);
301
301
+
}
69
302
</script>
+104
-12
src/applets/engine/audio/manifest.json
reviewed
···
1
1
{
2
2
"name": "diffuse/engine/audio",
3
3
+
"title": "Diffuse Audio",
3
4
"entrypoint": "index.html",
4
5
"actions": {
5
5
-
"load": {
6
6
-
"title": "Load",
7
7
-
"description": "Load a given audio src",
8
8
-
"params_schema": {
9
9
-
"type": "string",
10
10
-
"description": "String to be used as the audio `src`"
11
11
-
}
12
12
-
},
13
6
"pause": {
14
7
"title": "Pause",
15
15
-
"description": "Indicate the active audio should be paused",
8
8
+
"description": "Pause a track",
16
9
"params_schema": {
17
17
-
"type": "null"
10
10
+
"type": "object",
11
11
+
"properties": {
12
12
+
"trackId": {
13
13
+
"type": "string"
14
14
+
}
15
15
+
},
16
16
+
"required": ["trackId"]
18
17
}
19
18
},
20
19
"play": {
21
20
"title": "Play",
22
22
-
"description": "Indicate the active audio should be playing",
21
21
+
"description": "Play a track",
23
22
"params_schema": {
24
24
-
"type": "null"
23
23
+
"type": "object",
24
24
+
"properties": {
25
25
+
"trackId": {
26
26
+
"type": "string"
27
27
+
},
28
28
+
"volume": {
29
29
+
"type": "number"
30
30
+
}
31
31
+
},
32
32
+
"required": ["trackId", "volume"]
33
33
+
}
34
34
+
},
35
35
+
"render": {
36
36
+
"title": "Render",
37
37
+
"description": "Determine the active set of audio elements",
38
38
+
"params_schema": {
39
39
+
"type": "object",
40
40
+
"properties": {
41
41
+
"play": {
42
42
+
"type": "object"
43
43
+
},
44
44
+
"tracks": {
45
45
+
"type": "array",
46
46
+
"items": {
47
47
+
"anyOf": [
48
48
+
{
49
49
+
"type": "object",
50
50
+
"properties": {
51
51
+
"id": { "type": "string" },
52
52
+
"isPreload": { "type": "boolean" },
53
53
+
"mimeType": { "type": "string" },
54
54
+
"progress": { "type": "number" },
55
55
+
"url": { "type": "string" }
56
56
+
},
57
57
+
"required": ["id", "isPreload", "url"]
58
58
+
}
59
59
+
]
60
60
+
}
61
61
+
}
62
62
+
},
63
63
+
"required": ["tracks"]
64
64
+
}
65
65
+
},
66
66
+
"reload": {
67
67
+
"title": "Reload",
68
68
+
"description": "Make sure the audio node with the given track id is loading properly. This should be used when for example, the internet connection comes back and the loading of the track depended on said internet connection.",
69
69
+
"params_schema": {
70
70
+
"type": "object",
71
71
+
"properties": {
72
72
+
"play": {
73
73
+
"type": "boolean"
74
74
+
},
75
75
+
"progress": {
76
76
+
"type": "number"
77
77
+
},
78
78
+
"trackId": {
79
79
+
"type": "string"
80
80
+
}
81
81
+
},
82
82
+
"required": ["percentage", "trackId"]
83
83
+
}
84
84
+
},
85
85
+
"seek": {
86
86
+
"title": "Seek",
87
87
+
"description": "Seek a track to a given position",
88
88
+
"params_schema": {
89
89
+
"type": "object",
90
90
+
"properties": {
91
91
+
"percentage": {
92
92
+
"type": "number",
93
93
+
"description": "A number between 0 and 1 that determines the new current position in the audio"
94
94
+
},
95
95
+
"trackId": {
96
96
+
"type": "string"
97
97
+
}
98
98
+
},
99
99
+
"required": ["percentage", "trackId"]
100
100
+
}
101
101
+
},
102
102
+
"volume": {
103
103
+
"title": "Volume",
104
104
+
"description": "Set the volume of all tracks, or a specific track.",
105
105
+
"params_schema": {
106
106
+
"type": "object",
107
107
+
"properties": {
108
108
+
"trackId": {
109
109
+
"type": "string"
110
110
+
},
111
111
+
"volume": {
112
112
+
"type": "number",
113
113
+
"description": "A number between 0 and 1 that determines the new volume of all audio elements"
114
114
+
}
115
115
+
},
116
116
+
"required": ["volume"]
25
117
}
26
118
}
27
119
}
+22
src/applets/engine/audio/types.ts
reviewed
···
1
1
+
export interface State {
2
2
+
nowPlaying: Record<string, TrackState>;
3
3
+
}
4
4
+
5
5
+
export interface Track {
6
6
+
id: string;
7
7
+
isPreload: boolean;
8
8
+
mimeType?: string;
9
9
+
progress?: number;
10
10
+
url: string;
11
11
+
}
12
12
+
13
13
+
export interface TrackState {
14
14
+
duration: number;
15
15
+
id: string;
16
16
+
loadingState: "initialisation" | "loading" | "loaded" | {
17
17
+
error: { code: number };
18
18
+
};
19
19
+
isPlaying: boolean;
20
20
+
isPreload: boolean;
21
21
+
progress: number;
22
22
+
}
+11
-7
src/applets/themes/pilot/ui/audio/applet.astro
reviewed
···
48
48
flex: 1;
49
49
flex-direction: column;
50
50
max-width: var(--container-lg);
51
51
-
padding: var(--space-sm) var(--space-md);
51
51
+
margin-top: var(--space-2xs);
52
52
+
padding: var(--space-2xs) var(--space-md);
52
53
}
53
54
54
55
.controls {
55
56
align-items: center;
56
57
display: flex;
57
58
justify-content: center;
58
58
-
margin-bottom: var(--space-2xs);
59
59
60
60
& .controls__playpause {
61
61
font-size: var(--fs-lg);
···
65
65
.time {
66
66
align-self: stretch;
67
67
display: flex;
68
68
+
padding: var(--space-2xs) 0;
68
69
}
69
70
70
71
progress {
···
91
92
92
93
<script>
93
94
import { applets } from "@web-applets/sdk";
94
94
-
95
95
-
interface State {
96
96
-
isPlaying: boolean;
97
97
-
}
95
95
+
import { State } from "./types";
98
96
99
97
const context = applets.register<State>();
100
98
···
122
120
// DOM
123
121
////////////////////////////////////////////
124
122
document.body.querySelector(".controls__playpause").addEventListener("click", () => {
125
125
-
context.data = { isPlaying: !(context.data?.isPlaying ?? false) };
123
123
+
context.data = { ...context.data, isPlaying: !(context.data?.isPlaying ?? false) };
124
124
+
});
125
125
+
126
126
+
document.body.querySelector(".time").addEventListener("click", (event: Event) => {
127
127
+
const mouseEvent = event as MouseEvent;
128
128
+
const seekPosition = mouseEvent.offsetX / (event.target as HTMLProgressElement).clientWidth;
129
129
+
context.data = { ...context.data, seekPosition };
126
130
});
127
131
128
132
function render() {
+4
src/applets/themes/pilot/ui/audio/types.ts
reviewed
···
1
1
+
export interface State {
2
2
+
isPlaying: boolean;
3
3
+
seekPosition?: number;
4
4
+
}
+1
-1
src/pages/[...applet].astro
reviewed
···
30
30
31
31
return {
32
32
applet: path.join("/"),
33
33
-
title: manifest.default.name,
33
33
+
title: manifest.default.title || manifest.default.name,
34
34
Component: Applet.default,
35
35
};
36
36
}
+51
-11
src/scripts/themes/pilot/index.ts
reviewed
···
1
1
import { type Applet, type AppletEvent, applets } from "@web-applets/sdk";
2
2
import { effect, signal } from "spellcaster/spellcaster.js";
3
3
4
4
+
import type * as AudioEngine from "../../../applets/engine/audio/types.ts";
5
5
+
import type * as AudioUI from "../../../applets/themes/pilot/ui/audio/types.ts";
6
6
+
4
7
import "../../../styles/themes/pilot/index.css";
5
8
6
9
////////////////////////////////////////////
7
10
// 🗂️ Applets
8
11
////////////////////////////////////////////
9
12
const engine = {
10
10
-
audio: await applet("../../engine/audio"),
13
13
+
audio: await applet<AudioEngine.State>("../../engine/audio"),
11
14
};
12
15
13
16
const ui = {
···
19
22
////////////////////////////////////////////
20
23
reactive(
21
24
ui.audio,
22
22
-
"isPlaying",
23
23
-
(isPlaying: boolean) =>
24
24
-
engine.audio.sendAction(isPlaying ? "play" : "pause", null),
25
25
+
(data: AudioUI.State) => data.isPlaying,
26
26
+
(isPlaying: boolean) => {
27
27
+
if (isPlaying) {
28
28
+
// TODO: Replace with an actual queue system (shift queue)
29
29
+
engine.audio.sendAction("render", {
30
30
+
tracks: [{
31
31
+
id: "TODO",
32
32
+
isPreload: false,
33
33
+
url:
34
34
+
"https://archive.org/download/78_lollipop_the-chordettes-j-dixon-b-ross-archie-bleyer_gbia0068558a/Lollipop%20-%20The%20Chordettes%20-%20J.%20Dixon%20-%20B.%20Ross.mp3",
35
35
+
}],
36
36
+
play: {
37
37
+
trackId: "TODO",
38
38
+
volume: 0.5,
39
39
+
},
40
40
+
});
41
41
+
} else {
42
42
+
engine.audio.sendAction("pause", { trackId: "TODO" });
43
43
+
}
44
44
+
},
45
45
+
);
46
46
+
47
47
+
reactive(
48
48
+
ui.audio,
49
49
+
(data: AudioUI.State) => data.seekPosition,
50
50
+
(seekPosition: undefined | number) => {
51
51
+
if (seekPosition) {
52
52
+
engine.audio.sendAction("seek", {
53
53
+
percentage: seekPosition,
54
54
+
trackId: "TODO",
55
55
+
});
56
56
+
}
57
57
+
},
25
58
);
26
59
27
60
reactive(
28
61
engine.audio,
29
29
-
"isPlaying",
62
62
+
(data: AudioEngine.State) =>
63
63
+
// TODO: Simplify using queue engine
64
64
+
Object.values(data.nowPlaying).some((s) => s.isPlaying),
30
65
(isPlaying: boolean) => ui.audio.sendAction("set_is_playing", isPlaying),
31
66
);
32
67
33
68
reactive(
34
69
engine.audio,
35
35
-
"progress",
70
70
+
(data: AudioEngine.State) => {
71
71
+
// TODO: Simplify using queue engine
72
72
+
return Object.values(data.nowPlaying).filter((s) => !s.isPreload)[0]
73
73
+
?.progress ??
74
74
+
0;
75
75
+
},
36
76
(progress: number) => ui.audio.sendAction("set_progress", progress),
37
77
);
38
78
39
79
////////////////////////////////////////////
40
80
// 🪟 Applet initialiser
41
81
////////////////////////////////////////////
42
42
-
async function applet(src: string, opts: { setHeight?: boolean } = {}) {
82
82
+
async function applet<T>(src: string, opts: { setHeight?: boolean } = {}) {
43
83
const frame: HTMLIFrameElement | null = document.querySelector(
44
84
`[src="${src}${src.endsWith("/") ? "" : "/"}"]`,
45
85
);
···
49
89
throw new Error("iframe does not have a contentWindow");
50
90
}
51
91
52
52
-
const applet = await applets.connect(frame.contentWindow);
92
92
+
const applet: Applet = await applets.connect<T>(frame.contentWindow);
53
93
54
94
if (opts.setHeight) {
55
95
applet.onresize = () => {
···
75
115
// TODO: Applet should have a subtype
76
116
function reactive<T>(
77
117
applet: Applet,
78
78
-
property: string,
118
118
+
dataFn: (data: any /* TODO: Proper types pls */) => T,
79
119
effectFn: (t: T) => void,
80
120
) {
81
121
const [getter, setter] = signal(
82
82
-
(applet.data as any)[property] as T,
122
122
+
dataFn(applet.data as any),
83
123
);
84
124
85
125
effect(() => effectFn(getter()));
86
126
87
127
applet.addEventListener("data", (event: AppletEvent) => {
88
88
-
setter(event.data[property]);
128
128
+
setter(dataFn(event.data));
89
129
});
90
130
}