summaryrefslogtreecommitdiff
path: root/assets/js/stella.js
blob: aa6bbaa160625153f4985051a3735b66e4536c5c (plain)
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
class Stella {
    arrayBuffer;
    metadata;
    stems;

    constructor(ab) {
        this.arrayBuffer = ab;
        this.metadata = JSON.parse(new TextDecoder("utf-8").decode(pako.inflateRaw(ab.slice(8, 512))).trim());
        this.stems = {};
        this.urls = {};

        for (let stem of Object.keys(this.metadata.stems)) {
            this.stems[stem] = pako.inflateRaw(ab.slice(this.metadata.stems[stem][0], this.metadata.stems[stem][0] + this.metadata.stems[stem][1]));
            this.urls[stem] = URL.createObjectURL(new Blob([this.stems[stem]], { type: "audio/flac" }));
        }
    }

    destroy() {
        for (let url of Object.values(this.urls)) {
            URL.revokeObjectURL(url);
        }

        this.urls = null;
        this.stems = null;
        this.arrayBuffer = null;
    }

    static async build(url) {
        let buffer = await (await fetch(url)).arrayBuffer();
        return new Stella(buffer);
    }
}