-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathflptojson.ts
More file actions
129 lines (106 loc) · 3.56 KB
/
Copy pathflptojson.ts
File metadata and controls
129 lines (106 loc) · 3.56 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// file: flptojson.ts
// npm install ts-flp buffer
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import { Buffer } from "buffer";
import CommonFormats, { Category } from "src/CommonFormats.ts";
if (typeof window !== "undefined") {
(window as any).Buffer = Buffer;
}
import {
parseFlp,
readProjectMeta,
readProjectTimeInfo,
listSamples,
listPlugins,
getFlVersion,
getPPQ
} from "ts-flp";
class flptojsonHandler implements FormatHandler {
public name: string = "flptojson";
public supportedFormats: FileFormat[] = [
{
name: "FL Studio Project File",
format: "flp",
extension: "flp",
mime: "application/octet-stream",
from: true,
to: false,
internal: "flp",
category: Category.AUDIO,
lossless: false,
},
// Unsure about this, it might be lossless
CommonFormats.JSON.supported("json", false, true)
];
public ready: boolean = true;
async init () {
this.ready = true;
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
if (outputFormat.format !== "json") {
throw new TypeError(`Unsupported output format ${outputFormat.format}. Only JSON is supported.`);
}
const outputFiles: FileData[] = [];
for (const inputFile of inputFiles) {
try {
//FLP to raw byte convert for Parsing
const rawBytes = inputFile.bytes as Uint8Array;
const buffer = Buffer.from(rawBytes);
const parsed = parseFlp(buffer);
if (!parsed) {
throw new Error("Parser returned null. The file might be corrupted or encrypted.");
}
const meta = readProjectMeta(parsed);
const timeInfo = readProjectTimeInfo(parsed);
const samples = listSamples(parsed);
const plugins = listPlugins(parsed);
const version = getFlVersion(parsed) || "Unknown";
const ppq = getPPQ(parsed) || 96;
// Construct JSON songData structure
const songData = {
meta: {
title: meta.name || "Untitled",
artist: meta.artist || "Unknown",
genre: meta.genre || "Unknown",
comments: meta.description || "",
bpm: meta.bpm || 130,
version: version,
ppq: ppq
},
stats: {
created: timeInfo.creationDate instanceof Date
? timeInfo.creationDate.toISOString()
: null,
workTimeSeconds: timeInfo.workTimeSeconds || 0
},
content: {
samples: samples.map(s => s.path),
plugins: plugins.map(p => ({
name: p.name || "Unknown",
vendor: p.vendor || "Unknown"
}))
}
};
// JSON encoding
const jsonString = JSON.stringify(songData, null, 2);
const encoder = new TextEncoder();
const outputBytes = encoder.encode(jsonString);
const baseName = inputFile.name.split(".").slice(0, -1).join(".");
const newName = `${baseName}.json`;
outputFiles.push({
bytes: outputBytes,
name: newName
});
} catch (e: any) { // Error handling
console.error(`[flptojson] Error converting ${inputFile.name}:`, e);
throw new Error(`Conversion failed for ${inputFile.name}: ${e.message}`);
}
}
return outputFiles;
}
}
export default flptojsonHandler