|
| 1 | +const { application } = require("diffscope:core"); |
| 2 | +const { parse: parsePath } = require("diffscope:fs"); |
| 3 | +const { loadI18n, t } = require("diffscope:runtime"); |
| 4 | +const { dialogs } = require("diffscope:shell"); |
| 5 | + |
| 6 | +loadI18n("i18n"); |
| 7 | + |
| 8 | +const lrcFileFilters = [{ name: t("lrcLabels.fileFilter"), patterns: ["*.lrc"] }]; |
| 9 | +const timestampPattern = /^\[(\d+):([0-5]\d)(?:\.(\d{1,3}))?\]/; |
| 10 | +const wordTimestampPattern = /<\d+:[0-5]\d(?:\.\d{1,3})?>/g; |
| 11 | +const metadataPattern = /^\[([a-z][a-z\d_-]*|#):(.*)\]$/i; |
| 12 | + |
| 13 | +function parseLrc(contents) { |
| 14 | + const entries = []; |
| 15 | + let offset = 0; |
| 16 | + |
| 17 | + const lines = contents.split(/\r\n|\n|\r/); |
| 18 | + for (let index = 0; index < lines.length; ++index) { |
| 19 | + let text = lines[index].trim(); |
| 20 | + if (text === "") { |
| 21 | + continue; |
| 22 | + } |
| 23 | + |
| 24 | + const metadata = metadataPattern.exec(text); |
| 25 | + if (metadata !== null) { |
| 26 | + if (metadata[1].toLowerCase() === "offset") { |
| 27 | + const value = metadata[2].trim(); |
| 28 | + if (!/^[+-]?\d+$/.test(value)) { |
| 29 | + return { errorLine: index + 1 }; |
| 30 | + } |
| 31 | + offset = Number(value); |
| 32 | + } |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + const times = []; |
| 37 | + let timestamp = timestampPattern.exec(text); |
| 38 | + while (timestamp !== null) { |
| 39 | + const fraction = timestamp[3] || ""; |
| 40 | + const milliseconds = (Number(timestamp[1]) * 60 + Number(timestamp[2])) * 1000 |
| 41 | + + Number(fraction) * 10 ** (3 - fraction.length); |
| 42 | + times.push(milliseconds); |
| 43 | + text = text.slice(timestamp[0].length).trim(); |
| 44 | + timestamp = timestampPattern.exec(text); |
| 45 | + } |
| 46 | + |
| 47 | + text = text.replace(wordTimestampPattern, "").trim(); |
| 48 | + if (times.length === 0 || /^\[\s*[+-]?\d+:/.test(text) || /<\s*[+-]?\d+:/.test(text)) { |
| 49 | + return { errorLine: index + 1 }; |
| 50 | + } |
| 51 | + for (const milliseconds of times) { |
| 52 | + entries.push({ milliseconds, text }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return { entries, offset }; |
| 57 | +} |
| 58 | + |
| 59 | +function formatTimestamp(milliseconds) { |
| 60 | + const time = Math.round(milliseconds); |
| 61 | + const minutes = Math.floor(time / 60000); |
| 62 | + const seconds = Math.floor(time / 1000) % 60; |
| 63 | + const fraction = time % 1000; |
| 64 | + return `[${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}.${String(fraction).padStart(3, "0")}]`; |
| 65 | +} |
| 66 | + |
| 67 | +function promptStartTick(project, title, maximum = 2147483647) { |
| 68 | + const form = dialogs.form({ |
| 69 | + title, |
| 70 | + fields: [ |
| 71 | + { |
| 72 | + // TODO Replace this field with a musical position input when available. |
| 73 | + type: "integer", |
| 74 | + name: "startTick", |
| 75 | + label: t("lrcLabels.startPositionLabel"), |
| 76 | + value: Math.min(project.timeline.playhead, maximum), |
| 77 | + maximum, |
| 78 | + control: "spinBox" |
| 79 | + } |
| 80 | + ], |
| 81 | + parent: project.window |
| 82 | + }); |
| 83 | + return form === null ? null : form.startTick; |
| 84 | +} |
| 85 | + |
| 86 | +function importLrc() { |
| 87 | + const project = application.project; |
| 88 | + const file = dialogs.openFile({ |
| 89 | + title: t("lrcLabels.import.openTitle"), |
| 90 | + filters: lrcFileFilters, |
| 91 | + parent: project.window |
| 92 | + }); |
| 93 | + if (file === null) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + const title = t("lrcLabels.import.title"); |
| 98 | + const startTick = promptStartTick(project, title); |
| 99 | + if (startTick === null) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const result = parseLrc(file.readText()); |
| 104 | + if (result.errorLine !== undefined || result.entries.length === 0) { |
| 105 | + const message = result.errorLine !== undefined |
| 106 | + ? t("lrcLabels.import.invalidLine", { line: result.errorLine.toLocaleString() }) |
| 107 | + : t("lrcLabels.import.noTimestampedLines"); |
| 108 | + dialogs.alert(message, { |
| 109 | + title: t("lrcLabels.import.failureTitle"), |
| 110 | + severity: "warning", |
| 111 | + parent: project.window |
| 112 | + }); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const document = project.document; |
| 117 | + const labels = document.model.labels; |
| 118 | + const timeline = project.timeline; |
| 119 | + const startMilliseconds = timeline.toMilliseconds(startTick); |
| 120 | + const transaction = document.beginTransaction(); |
| 121 | + for (const { milliseconds, text } of result.entries) { |
| 122 | + const projectMilliseconds = startMilliseconds + (milliseconds - result.offset); |
| 123 | + const position = timeline.fromMilliseconds(Math.max(0, projectMilliseconds)); |
| 124 | + labels.create({ position, text }); |
| 125 | + } |
| 126 | + transaction.commit(title); |
| 127 | +} |
| 128 | + |
| 129 | +function exportLrc() { |
| 130 | + const project = application.project; |
| 131 | + const title = t("lrcLabels.export.title"); |
| 132 | + const labels = Array.from(project.document.model.labels); |
| 133 | + if (labels.length === 0) { |
| 134 | + dialogs.alert(t("lrcLabels.export.noLabels"), { |
| 135 | + title, |
| 136 | + severity: "warning", |
| 137 | + parent: project.window |
| 138 | + }); |
| 139 | + return; |
| 140 | + } |
| 141 | + labels.sort((left, right) => left.position - right.position); |
| 142 | + |
| 143 | + const projectPath = project.filePath; |
| 144 | + const file = dialogs.saveFile({ |
| 145 | + title: t("lrcLabels.export.saveTitle"), |
| 146 | + suggestedName: projectPath === null ? "labels.lrc" : `${parsePath(projectPath).name}.lrc`, |
| 147 | + filters: lrcFileFilters, |
| 148 | + parent: project.window |
| 149 | + }); |
| 150 | + if (file === null) { |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + const startTick = promptStartTick(project, title, labels[0].position); |
| 155 | + if (startTick === null) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + const timeline = project.timeline; |
| 160 | + const startMilliseconds = timeline.toMilliseconds(startTick); |
| 161 | + const lines = labels.map(({ position, text }) => { |
| 162 | + const timestamp = formatTimestamp(timeline.toMilliseconds(position) - startMilliseconds); |
| 163 | + return timestamp + text.replace(/\r\n|\n|\r/g, " "); |
| 164 | + }); |
| 165 | + file.writeText(lines.join("\n") + "\n"); |
| 166 | +} |
| 167 | + |
| 168 | +module.exports = defineScript({ |
| 169 | + id: "org.diffscope.lrc-labels", |
| 170 | + name: t("lrcLabels.title"), |
| 171 | + version: "1.0.0", |
| 172 | + description: t("lrcLabels.description"), |
| 173 | + actions: [ |
| 174 | + { |
| 175 | + name: t("lrcLabels.import.title"), |
| 176 | + description: t("lrcLabels.import.description"), |
| 177 | + requiresProject: true, |
| 178 | + execute: importLrc |
| 179 | + }, |
| 180 | + { |
| 181 | + name: t("lrcLabels.export.title"), |
| 182 | + description: t("lrcLabels.export.description"), |
| 183 | + requiresProject: true, |
| 184 | + execute: exportLrc |
| 185 | + } |
| 186 | + ] |
| 187 | +}); |
0 commit comments