-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit-voices.ts
49 lines (42 loc) · 1.36 KB
/
split-voices.ts
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
import { db } from "@/server/db";
import { kernVoices } from "@/server/db/schema";
import assert from "assert";
import dotenv from "dotenv";
dotenv.config();
async function insertSplitVoices() {
const scores = await db.query.kernScores.findMany();
await db.transaction(async (tx) => {
let counter = 0;
for (const score of scores) {
counter++;
if (counter % 100 === 0) {
console.log(counter);
}
const kernStartIndex = score.kernData.indexOf("**kern");
const kernEndIndex = score.kernData.lastIndexOf("*-") + "*-".length;
const scoreData = score.kernData.slice(kernStartIndex, kernEndIndex);
const firstLine =
scoreData.split("\n")[0] ?? assert.fail("No first line found");
const columnHeaders = firstLine.split("\t");
const voices: string[] = Array(columnHeaders.length).fill("");
for (const line of scoreData.split("\n")) {
line.split("\t").forEach((token, i) => {
voices[i] += token + "\n";
});
}
await tx
.insert(kernVoices)
.values(
voices
.filter((voice) => voice.startsWith("**kern"))
.map((voice) => ({ scoreId: score.id, voice })),
);
}
});
}
insertSplitVoices()
.then(() => process.exit(0))
.catch((error) => {
console.error("script pooped :(", error);
process.exit(1);
});