taglib-wasm 0.3.3 → 0.3.9
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/CONTRIBUTING.md +293 -0
- package/NOTICE +34 -0
- package/README.md +122 -511
- package/dist/index.d.ts +132 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +137 -0
- package/dist/index.ts +220 -0
- package/dist/src/constants.d.ts +201 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.ts +227 -0
- package/dist/src/errors.d.ts +89 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.ts +237 -0
- package/dist/src/file-utils.d.ts +205 -0
- package/dist/src/file-utils.d.ts.map +1 -0
- package/dist/src/file-utils.ts +467 -0
- package/dist/src/file.js +47 -0
- package/dist/src/global.d.ts +10 -0
- package/dist/src/mod.d.ts +9 -0
- package/dist/src/mod.d.ts.map +1 -0
- package/dist/src/mod.ts +19 -0
- package/dist/src/simple.d.ts +347 -0
- package/dist/src/simple.d.ts.map +1 -0
- package/dist/src/simple.ts +659 -0
- package/dist/src/taglib.d.ts +502 -0
- package/dist/src/taglib.d.ts.map +1 -0
- package/dist/src/taglib.ts +959 -0
- package/dist/src/types.d.ts +323 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.ts +538 -0
- package/dist/src/utils/file.d.ts +15 -0
- package/dist/src/utils/file.d.ts.map +1 -0
- package/dist/src/utils/file.ts +82 -0
- package/dist/src/utils/write.d.ts +15 -0
- package/dist/src/utils/write.d.ts.map +1 -0
- package/dist/src/utils/write.ts +61 -0
- package/dist/src/wasm-workers.d.ts +33 -0
- package/dist/src/wasm-workers.d.ts.map +1 -0
- package/dist/src/wasm-workers.ts +176 -0
- package/dist/src/wasm.d.ts +97 -0
- package/dist/src/wasm.d.ts.map +1 -0
- package/dist/src/wasm.ts +133 -0
- package/dist/src/web-utils.d.ts +180 -0
- package/dist/src/web-utils.d.ts.map +1 -0
- package/dist/src/web-utils.ts +347 -0
- package/dist/src/workers.d.ts +219 -0
- package/dist/src/workers.d.ts.map +1 -0
- package/dist/src/workers.ts +465 -0
- package/dist/src/write.js +33 -0
- package/dist/taglib-wrapper.d.ts +5 -0
- package/dist/taglib-wrapper.js +14 -0
- package/dist/taglib.wasm +0 -0
- package/index.ts +100 -7
- package/package.json +40 -16
- package/src/errors.ts +237 -0
- package/src/file-utils.ts +467 -0
- package/src/global.d.ts +10 -0
- package/src/simple.ts +399 -84
- package/src/taglib.ts +522 -28
- package/src/types.ts +1 -1
- package/src/utils/file.ts +82 -0
- package/src/utils/write.ts +61 -0
- package/src/wasm-workers.ts +13 -4
- package/src/wasm.ts +1 -1
- package/src/web-utils.ts +347 -0
- package/src/workers.ts +32 -13
- package/build/taglib.js +0 -2407
- package/build/taglib.wasm +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-utils.d.ts","sourceRoot":"","sources":["../../src/web-utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGzC;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAIzD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,MAAM,EACf,IAAI,GAAE,WAAoC,EAC1C,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAsBT;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,qBAAqB,CACzC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,IAAI,EAC9C,MAAM,EAAE,iBAAiB,EACzB,OAAO,GAAE;IACP,MAAM,CAAC,EAAE,YAAY,GAAG,WAAW,GAAG,YAAY,CAAC;IACnD,OAAO,CAAC,EA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 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Web browser utilities for working with cover art in taglib-wasm
|
|
3
|
+
*
|
|
4
|
+
* This module provides browser-specific helpers for integrating taglib-wasm
|
|
5
|
+
* with web applications, including canvas integration and data URL support.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { pictureToDataURL, setCoverArtFromCanvas } from "taglib-wasm/web-utils";
|
|
10
|
+
*
|
|
11
|
+
* // Display cover art in an <img> element
|
|
12
|
+
* const pictures = await readPictures("song.mp3");
|
|
13
|
+
* if (pictures.length > 0) {
|
|
14
|
+
* const dataURL = pictureToDataURL(pictures[0]);
|
|
15
|
+
* document.getElementById('cover').src = dataURL;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* // Set cover art from a canvas
|
|
19
|
+
* const canvas = document.getElementById('myCanvas') as HTMLCanvasElement;
|
|
20
|
+
* const modifiedBuffer = await setCoverArtFromCanvas("song.mp3", canvas);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import type { Picture } from "./types.ts";
|
|
25
|
+
import { PictureType } from "./types.ts";
|
|
26
|
+
import { applyPictures, readPictures } from "./simple.ts";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Convert a Picture object to a data URL for display in web browsers
|
|
30
|
+
*
|
|
31
|
+
* @param picture - Picture object from taglib-wasm
|
|
32
|
+
* @returns Data URL string that can be used as src for <img> elements
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const pictures = await readPictures("song.mp3");
|
|
37
|
+
* const imgElement = document.getElementById('coverArt') as HTMLImageElement;
|
|
38
|
+
* imgElement.src = pictureToDataURL(pictures[0]);
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export function pictureToDataURL(picture: Picture): string {
|
|
42
|
+
// Convert Uint8Array to base64
|
|
43
|
+
const base64 = btoa(String.fromCharCode(...picture.data));
|
|
44
|
+
return `data:${picture.mimeType};base64,${base64}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Convert a data URL to a Picture object
|
|
49
|
+
*
|
|
50
|
+
* @param dataURL - Data URL string (e.g., "data:image/jpeg;base64,...")
|
|
51
|
+
* @param type - Picture type (defaults to FrontCover)
|
|
52
|
+
* @param description - Optional description
|
|
53
|
+
* @returns Picture object
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const dataURL = canvas.toDataURL('image/jpeg');
|
|
58
|
+
* const picture = dataURLToPicture(dataURL, PictureType.FrontCover);
|
|
59
|
+
* const modifiedBuffer = await applyPictures("song.mp3", [picture]);
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function dataURLToPicture(
|
|
63
|
+
dataURL: string,
|
|
64
|
+
type: PictureType = PictureType.FrontCover,
|
|
65
|
+
description?: string,
|
|
66
|
+
): Picture {
|
|
67
|
+
// Parse data URL
|
|
68
|
+
const matches = dataURL.match(/^data:([^;]+);base64,(.+)$/);
|
|
69
|
+
if (!matches) {
|
|
70
|
+
throw new Error("Invalid data URL format");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const [, mimeType, base64] = matches;
|
|
74
|
+
|
|
75
|
+
// Convert base64 to Uint8Array
|
|
76
|
+
const binaryString = atob(base64);
|
|
77
|
+
const data = new Uint8Array(binaryString.length);
|
|
78
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
79
|
+
data[i] = binaryString.charCodeAt(i);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
mimeType,
|
|
84
|
+
data,
|
|
85
|
+
type,
|
|
86
|
+
description,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Set cover art from an HTML canvas element
|
|
92
|
+
*
|
|
93
|
+
* @param file - File path, Uint8Array buffer, ArrayBuffer, or File object
|
|
94
|
+
* @param canvas - HTMLCanvasElement containing the image
|
|
95
|
+
* @param options - Options for image format and quality
|
|
96
|
+
* @returns Modified file buffer with cover art from canvas
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const canvas = document.getElementById('albumArt') as HTMLCanvasElement;
|
|
101
|
+
* const modifiedBuffer = await setCoverArtFromCanvas("song.mp3", canvas, {
|
|
102
|
+
* format: 'image/jpeg',
|
|
103
|
+
* quality: 0.9
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export async function setCoverArtFromCanvas(
|
|
108
|
+
file: string | Uint8Array | ArrayBuffer | File,
|
|
109
|
+
canvas: HTMLCanvasElement,
|
|
110
|
+
options: {
|
|
111
|
+
format?: 'image/jpeg' | 'image/png' | 'image/webp';
|
|
112
|
+
quality?: number;
|
|
113
|
+
type?: PictureType;
|
|
114
|
+
description?: string;
|
|
115
|
+
} = {},
|
|
116
|
+
): Promise<Uint8Array> {
|
|
117
|
+
const {
|
|
118
|
+
format = 'image/jpeg',
|
|
119
|
+
quality = 0.92,
|
|
120
|
+
type = PictureType.FrontCover,
|
|
121
|
+
description = "Front Cover",
|
|
122
|
+
} = options;
|
|
123
|
+
|
|
124
|
+
// Convert canvas to data URL
|
|
125
|
+
const dataURL = canvas.toDataURL(format, quality);
|
|
126
|
+
|
|
127
|
+
// Convert to Picture object
|
|
128
|
+
const picture = dataURLToPicture(dataURL, type, description);
|
|
129
|
+
|
|
130
|
+
// Apply to file
|
|
131
|
+
return applyPictures(file, [picture]);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Convert canvas to Picture object using blob for better performance
|
|
136
|
+
*
|
|
137
|
+
* This is more efficient than toDataURL for large images as it avoids
|
|
138
|
+
* the base64 encoding/decoding step.
|
|
139
|
+
*
|
|
140
|
+
* @param canvas - HTMLCanvasElement containing the image
|
|
141
|
+
* @param options - Options for image format and quality
|
|
142
|
+
* @returns Promise resolving to Picture object
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const canvas = document.getElementById('albumArt') as HTMLCanvasElement;
|
|
147
|
+
* const picture = await canvasToPicture(canvas, {
|
|
148
|
+
* format: 'image/png',
|
|
149
|
+
* type: PictureType.BackCover
|
|
150
|
+
* });
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export async function canvasToPicture(
|
|
154
|
+
canvas: HTMLCanvasElement,
|
|
155
|
+
options: {
|
|
156
|
+
format?: 'image/jpeg' | 'image/png' | 'image/webp';
|
|
157
|
+
quality?: number;
|
|
158
|
+
type?: PictureType;
|
|
159
|
+
description?: string;
|
|
160
|
+
} = {},
|
|
161
|
+
): Promise<Picture> {
|
|
162
|
+
const {
|
|
163
|
+
format = 'image/jpeg',
|
|
164
|
+
quality = 0.92,
|
|
165
|
+
type = PictureType.FrontCover,
|
|
166
|
+
description,
|
|
167
|
+
} = options;
|
|
168
|
+
|
|
169
|
+
return new Promise((resolve, reject) => {
|
|
170
|
+
canvas.toBlob(
|
|
171
|
+
async (blob) => {
|
|
172
|
+
if (!blob) {
|
|
173
|
+
reject(new Error("Failed to convert canvas to blob"));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Convert blob to Uint8Array
|
|
178
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
179
|
+
const data = new Uint8Array(arrayBuffer);
|
|
180
|
+
|
|
181
|
+
resolve({
|
|
182
|
+
mimeType: format,
|
|
183
|
+
data,
|
|
184
|
+
type,
|
|
185
|
+
description,
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
format,
|
|
189
|
+
quality,
|
|
190
|
+
);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Load an image file into a Picture object
|
|
196
|
+
*
|
|
197
|
+
* @param file - File object from <input type="file"> or drag-and-drop
|
|
198
|
+
* @param type - Picture type (defaults to FrontCover)
|
|
199
|
+
* @param description - Optional description
|
|
200
|
+
* @returns Promise resolving to Picture object
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* // From file input
|
|
205
|
+
* const input = document.getElementById('fileInput') as HTMLInputElement;
|
|
206
|
+
* input.addEventListener('change', async (e) => {
|
|
207
|
+
* const file = e.target.files[0];
|
|
208
|
+
* const picture = await imageFileToPicture(file);
|
|
209
|
+
* const modifiedBuffer = await applyPictures("song.mp3", [picture]);
|
|
210
|
+
* });
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
export async function imageFileToPicture(
|
|
214
|
+
file: File,
|
|
215
|
+
type: PictureType = PictureType.FrontCover,
|
|
216
|
+
description?: string,
|
|
217
|
+
): Promise<Picture> {
|
|
218
|
+
const arrayBuffer = await file.arrayBuffer();
|
|
219
|
+
const data = new Uint8Array(arrayBuffer);
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
mimeType: file.type,
|
|
223
|
+
data,
|
|
224
|
+
type,
|
|
225
|
+
description: description || file.name,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Display a Picture in an HTML img element
|
|
231
|
+
*
|
|
232
|
+
* @param picture - Picture object from taglib-wasm
|
|
233
|
+
* @param imgElement - HTMLImageElement to display the picture in
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```typescript
|
|
237
|
+
* const pictures = await readPictures("song.mp3");
|
|
238
|
+
* const img = document.getElementById('coverArt') as HTMLImageElement;
|
|
239
|
+
* displayPicture(pictures[0], img);
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
export function displayPicture(
|
|
243
|
+
picture: Picture,
|
|
244
|
+
imgElement: HTMLImageElement,
|
|
245
|
+
): void {
|
|
246
|
+
// Clean up previous object URL if any
|
|
247
|
+
if (imgElement.src.startsWith('blob:')) {
|
|
248
|
+
URL.revokeObjectURL(imgElement.src);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Create blob and object URL
|
|
252
|
+
const blob = new Blob([picture.data], { type: picture.mimeType });
|
|
253
|
+
const objectURL = URL.createObjectURL(blob);
|
|
254
|
+
|
|
255
|
+
// Set the src
|
|
256
|
+
imgElement.src = objectURL;
|
|
257
|
+
|
|
258
|
+
// Clean up object URL when image is no longer needed
|
|
259
|
+
imgElement.addEventListener('load', () => {
|
|
260
|
+
// Keep the URL alive for a bit to ensure image is rendered
|
|
261
|
+
setTimeout(() => URL.revokeObjectURL(objectURL), 100);
|
|
262
|
+
}, { once: true });
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Create a download link for a Picture
|
|
267
|
+
*
|
|
268
|
+
* @param picture - Picture object to download
|
|
269
|
+
* @param filename - Suggested filename for download
|
|
270
|
+
* @returns Temporary download URL (remember to revoke it after use)
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```typescript
|
|
274
|
+
* const pictures = await readPictures("song.mp3");
|
|
275
|
+
* const downloadUrl = createPictureDownloadURL(pictures[0], "cover.jpg");
|
|
276
|
+
*
|
|
277
|
+
* const link = document.createElement('a');
|
|
278
|
+
* link.href = downloadUrl;
|
|
279
|
+
* link.download = "cover.jpg";
|
|
280
|
+
* link.click();
|
|
281
|
+
*
|
|
282
|
+
* // Clean up
|
|
283
|
+
* URL.revokeObjectURL(downloadUrl);
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
export function createPictureDownloadURL(
|
|
287
|
+
picture: Picture,
|
|
288
|
+
filename: string = "cover",
|
|
289
|
+
): string {
|
|
290
|
+
const blob = new Blob([picture.data], { type: picture.mimeType });
|
|
291
|
+
return URL.createObjectURL(blob);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Extract all pictures and create a gallery
|
|
296
|
+
*
|
|
297
|
+
* @param file - Audio file to extract pictures from
|
|
298
|
+
* @param container - HTML element to append gallery items to
|
|
299
|
+
* @param options - Gallery display options
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* ```typescript
|
|
303
|
+
* const galleryDiv = document.getElementById('pictureGallery');
|
|
304
|
+
* await createPictureGallery("song.mp3", galleryDiv, {
|
|
305
|
+
* className: 'album-art',
|
|
306
|
+
* includeDescription: true
|
|
307
|
+
* });
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
export async function createPictureGallery(
|
|
311
|
+
file: string | Uint8Array | ArrayBuffer | File,
|
|
312
|
+
container: HTMLElement,
|
|
313
|
+
options: {
|
|
314
|
+
className?: string;
|
|
315
|
+
includeDescription?: boolean;
|
|
316
|
+
onClick?: (picture: Picture, index: number) => void;
|
|
317
|
+
} = {},
|
|
318
|
+
): Promise<void> {
|
|
319
|
+
const pictures = await readPictures(file);
|
|
320
|
+
|
|
321
|
+
// Clear container
|
|
322
|
+
container.innerHTML = '';
|
|
323
|
+
|
|
324
|
+
pictures.forEach((picture, index) => {
|
|
325
|
+
const wrapper = document.createElement('div');
|
|
326
|
+
wrapper.className = options.className || 'picture-item';
|
|
327
|
+
|
|
328
|
+
const img = document.createElement('img');
|
|
329
|
+
displayPicture(picture, img);
|
|
330
|
+
img.alt = picture.description || `Picture ${index + 1}`;
|
|
331
|
+
|
|
332
|
+
if (options.onClick) {
|
|
333
|
+
img.style.cursor = 'pointer';
|
|
334
|
+
img.addEventListener('click', () => options.onClick!(picture, index));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
wrapper.appendChild(img);
|
|
338
|
+
|
|
339
|
+
if (options.includeDescription && picture.description) {
|
|
340
|
+
const caption = document.createElement('p');
|
|
341
|
+
caption.textContent = picture.description;
|
|
342
|
+
wrapper.appendChild(caption);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
container.appendChild(wrapper);
|
|
346
|
+
});
|
|
347
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Cloudflare Workers-specific TagLib API
|
|
3
|
+
*
|
|
4
|
+
* This module provides a specialized API for using TagLib in Cloudflare Workers
|
|
5
|
+
* and other edge computing environments where the standard Emscripten module
|
|
6
|
+
* loading may not work. It uses C-style function exports for compatibility.
|
|
7
|
+
*
|
|
8
|
+
* @module taglib-wasm/workers
|
|
9
|
+
*/
|
|
10
|
+
import type { AudioFormat, AudioProperties, ExtendedTag, Tag, TagLibConfig } from "./types.ts";
|
|
11
|
+
import { type TagLibModule } from "./wasm-workers.ts";
|
|
12
|
+
/**
|
|
13
|
+
* Represents an audio file with metadata and properties (Workers-compatible).
|
|
14
|
+
* This implementation uses C-style function calls for Cloudflare Workers compatibility.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const file = taglib.openFile(audioBuffer);
|
|
19
|
+
*
|
|
20
|
+
* // Get metadata
|
|
21
|
+
* const tag = file.tag();
|
|
22
|
+
* console.log(tag.title);
|
|
23
|
+
*
|
|
24
|
+
* // Modify metadata
|
|
25
|
+
* file.setTitle("New Title");
|
|
26
|
+
* file.save();
|
|
27
|
+
*
|
|
28
|
+
* // Clean up
|
|
29
|
+
* file.dispose();
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare class AudioFileWorkers {
|
|
33
|
+
private module;
|
|
34
|
+
private fileId;
|
|
35
|
+
private tagPtr;
|
|
36
|
+
private propsPtr;
|
|
37
|
+
constructor(module: TagLibModule, fileId: number);
|
|
38
|
+
/**
|
|
39
|
+
* Check if the file is valid and was loaded successfully.
|
|
40
|
+
* @returns true if the file is valid and can be processed
|
|
41
|
+
*/
|
|
42
|
+
isValid(): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Get the file format.
|
|
45
|
+
* @returns Audio format (e.g., "MP3", "FLAC", "OGG")
|
|
46
|
+
*/
|
|
47
|
+
format(): AudioFormat;
|
|
48
|
+
/**
|
|
49
|
+
* Get basic tag information.
|
|
50
|
+
* @returns Object containing title, artist, album, etc.
|
|
51
|
+
*/
|
|
52
|
+
tag(): Tag;
|
|
53
|
+
/**
|
|
54
|
+
* Get audio properties (duration, bitrate, etc.).
|
|
55
|
+
* @returns Audio properties or null if unavailable
|
|
56
|
+
*/
|
|
57
|
+
audioProperties(): AudioProperties | null;
|
|
58
|
+
/**
|
|
59
|
+
* Set the title tag.
|
|
60
|
+
* @param title - New title value
|
|
61
|
+
*/
|
|
62
|
+
setTitle(title: string): void;
|
|
63
|
+
/**
|
|
64
|
+
* Set the artist tag.
|
|
65
|
+
* @param artist - New artist value
|
|
66
|
+
*/
|
|
67
|
+
setArtist(artist: string): void;
|
|
68
|
+
/**
|
|
69
|
+
* Set the album tag.
|
|
70
|
+
* @param album - New album value
|
|
71
|
+
*/
|
|
72
|
+
setAlbum(album: string): void;
|
|
73
|
+
/**
|
|
74
|
+
* Set the comment tag.
|
|
75
|
+
* @param comment - New comment value
|
|
76
|
+
*/
|
|
77
|
+
setComment(comment: string): void;
|
|
78
|
+
/**
|
|
79
|
+
* Set the genre tag.
|
|
80
|
+
* @param genre - New genre value
|
|
81
|
+
*/
|
|
82
|
+
setGenre(genre: string): void;
|
|
83
|
+
/**
|
|
84
|
+
* Set the year tag.
|
|
85
|
+
* @param year - Release year
|
|
86
|
+
*/
|
|
87
|
+
setYear(year: number): void;
|
|
88
|
+
/**
|
|
89
|
+
* Set the track number tag.
|
|
90
|
+
* @param track - Track number
|
|
91
|
+
*/
|
|
92
|
+
setTrack(track: number): void;
|
|
93
|
+
/**
|
|
94
|
+
* Save changes to the file.
|
|
95
|
+
* Note: In Workers context, this saves to the in-memory buffer only.
|
|
96
|
+
* @returns true if save was successful
|
|
97
|
+
*/
|
|
98
|
+
save(): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Get the current file buffer after modifications.
|
|
101
|
+
* Note: This is not implemented in the Workers API.
|
|
102
|
+
* @returns Empty Uint8Array (not implemented)
|
|
103
|
+
* @deprecated Use the Core API for this functionality
|
|
104
|
+
*/
|
|
105
|
+
getFileBuffer(): Uint8Array;
|
|
106
|
+
/**
|
|
107
|
+
* Get extended metadata with format-agnostic field names.
|
|
108
|
+
* Note: Currently returns only basic fields in Workers API.
|
|
109
|
+
* @returns Extended tag object with basic fields populated
|
|
110
|
+
*/
|
|
111
|
+
extendedTag(): ExtendedTag;
|
|
112
|
+
/**
|
|
113
|
+
* Set extended metadata using format-agnostic field names.
|
|
114
|
+
* Note: Currently only supports basic fields in Workers API.
|
|
115
|
+
* @param tag - Partial extended tag object with fields to update
|
|
116
|
+
*/
|
|
117
|
+
setExtendedTag(tag: Partial<ExtendedTag>): void;
|
|
118
|
+
/**
|
|
119
|
+
* Clean up resources.
|
|
120
|
+
* Always call this when done to prevent memory leaks.
|
|
121
|
+
*/
|
|
122
|
+
dispose(): void;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Main TagLib class for Cloudflare Workers.
|
|
126
|
+
* Provides methods to initialize the library and open audio files
|
|
127
|
+
* in edge computing environments.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* import wasmBinary from "../build/taglib.wasm";
|
|
132
|
+
*
|
|
133
|
+
* // Initialize TagLib
|
|
134
|
+
* const taglib = await TagLibWorkers.initialize(wasmBinary);
|
|
135
|
+
*
|
|
136
|
+
* // Process audio file
|
|
137
|
+
* const file = taglib.openFile(audioBuffer);
|
|
138
|
+
* const metadata = file.tag();
|
|
139
|
+
* file.dispose();
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export declare class TagLibWorkers {
|
|
143
|
+
private module;
|
|
144
|
+
private constructor();
|
|
145
|
+
/**
|
|
146
|
+
* Initialize TagLib for Workers with Wasm binary
|
|
147
|
+
*
|
|
148
|
+
* @param wasmBinary - The WebAssembly binary as Uint8Array
|
|
149
|
+
* @param config - Optional configuration for the Wasm module
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* // In a Cloudflare Worker
|
|
154
|
+
* import wasmBinary from "../build/taglib.wasm";
|
|
155
|
+
*
|
|
156
|
+
* const taglib = await TagLibWorkers.initialize(wasmBinary);
|
|
157
|
+
* const file = taglib.open(audioBuffer);
|
|
158
|
+
* const metadata = file.tag();
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
static initialize(wasmBinary: Uint8Array, config?: TagLibConfig): Promise<TagLibWorkers>;
|
|
162
|
+
/**
|
|
163
|
+
* Open an audio file from a buffer.
|
|
164
|
+
*
|
|
165
|
+
* @param buffer - Audio file data as Uint8Array
|
|
166
|
+
* @returns AudioFileWorkers instance
|
|
167
|
+
* @throws {Error} If Wasm module is not initialized
|
|
168
|
+
* @throws {Error} If file format is invalid or unsupported
|
|
169
|
+
* @throws {Error} If Workers API C-style functions are not available
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const audioData = new Uint8Array(await request.arrayBuffer());
|
|
174
|
+
* const file = taglib.open(audioData);
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
open(buffer: Uint8Array): AudioFileWorkers;
|
|
178
|
+
/**
|
|
179
|
+
* @deprecated Use `open()` instead. This method will be removed in the next major version.
|
|
180
|
+
* Open an audio file from a buffer (backward compatibility).
|
|
181
|
+
* @param buffer Audio file data as Uint8Array
|
|
182
|
+
* @returns Audio file instance
|
|
183
|
+
*/
|
|
184
|
+
openFile(buffer: Uint8Array): AudioFileWorkers;
|
|
185
|
+
/**
|
|
186
|
+
* Get the underlying Wasm module for advanced usage.
|
|
187
|
+
* @returns The initialized TagLib Wasm module
|
|
188
|
+
*/
|
|
189
|
+
getModule(): TagLibModule;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Utility function to process audio metadata in a Cloudflare Worker
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```typescript
|
|
196
|
+
* export default {
|
|
197
|
+
* async fetch(request: Request): Promise<Response> {
|
|
198
|
+
* if (request.method === "POST") {
|
|
199
|
+
* const audioData = new Uint8Array(await request.arrayBuffer());
|
|
200
|
+
* const metadata = await processAudioMetadata(wasmBinary, audioData);
|
|
201
|
+
* return Response.json(metadata);
|
|
202
|
+
* }
|
|
203
|
+
* return new Response("Method not allowed", { status: 405 });
|
|
204
|
+
* }
|
|
205
|
+
* };
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
export declare function processAudioMetadata(wasmBinary: Uint8Array, audioData: Uint8Array, config?: TagLibConfig): Promise<{
|
|
209
|
+
tag: Tag;
|
|
210
|
+
properties: AudioProperties | null;
|
|
211
|
+
format: AudioFormat;
|
|
212
|
+
}>;
|
|
213
|
+
/**
|
|
214
|
+
* Re-export commonly used types for convenience.
|
|
215
|
+
* These types define the structure of metadata, audio properties,
|
|
216
|
+
* and configuration options.
|
|
217
|
+
*/
|
|
218
|
+
export type { AudioFormat, AudioProperties, ExtendedTag, Tag, TagLibConfig };
|
|
219
|
+
//# sourceMappingURL=workers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workers.d.ts","sourceRoot":"","sources":["../../src/workers.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,eAAe,EACf,WAAW,EACX,GAAG,EACH,YAAY,EACb,MAAM,YAAY,CAAC;AACpB,OAAO,EAIL,KAAK,YAAY,EAClB,MAAM,mBAAmB,CAAC;AAO3B;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,QAAQ,CAAS;gBAEb,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM;IAOhD;;;OAGG;IACH,OAAO,IAAI,OAAO;IAIlB;;;OAGG;IACH,MAAM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