-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathals.ts
More file actions
79 lines (65 loc) · 2.05 KB
/
als.ts
File metadata and controls
79 lines (65 loc) · 2.05 KB
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
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import CommonFormats from "src/CommonFormats.ts";
class alsHandler implements FormatHandler {
public name: string = "als";
public supportedFormats: FileFormat[] = [
{
name: "Ableton Live Set",
format: "als",
extension: "als",
mime: "application/gzip",
from: true,
to: false,
internal: "als",
category: "data",
lossless: true
},
CommonFormats.XML.builder("xml").allowTo(),
];
public ready: boolean = false;
async init () {
this.ready = true;
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
if (inputFormat.internal !== "als" || outputFormat.internal !== "xml") {
throw "Invalid conversion path.";
}
const decoder = new TextDecoder("utf-8", { fatal: true });
const encoder = new TextEncoder();
return Promise.all(inputFiles.map(async (inputFile) => {
if (
inputFile.bytes.length < 2
|| inputFile.bytes[0] !== 0x1f
|| inputFile.bytes[1] !== 0x8b
) {
throw "Invalid ALS file: expected gzip-compressed data.";
}
const decompressedStream = new Blob([inputFile.bytes as BlobPart])
.stream()
.pipeThrough(new DecompressionStream("gzip"));
const decompressedBytes = new Uint8Array(await new Response(decompressedStream).arrayBuffer());
let xml: string;
try {
xml = decoder.decode(decompressedBytes);
} catch (_) {
throw "Invalid ALS file: decompressed data is not UTF-8 XML.";
}
if (!xml.trimStart().startsWith("<")) {
throw "Invalid ALS file: decompressed data is not XML.";
}
const baseNameParts = inputFile.name.split(".");
const baseName = baseNameParts.length > 1
? baseNameParts.slice(0, -1).join(".")
: inputFile.name;
return {
name: `${baseName}.xml`,
bytes: encoder.encode(xml)
};
}));
}
}
export default alsHandler;