|
| 1 | +const { application } = require("diffscope:core"); |
| 2 | +const { dialogs } = require("diffscope:shell"); |
| 3 | +const { loadI18n, t } = require("diffscope:runtime"); |
| 4 | + |
| 5 | +loadI18n("i18n"); |
| 6 | + |
| 7 | +function showSelectionWarning(project) { |
| 8 | + dialogs.alert(t("freezeGeneratedContent.selectionWarning"), { |
| 9 | + title: t("freezeGeneratedContent.title"), |
| 10 | + severity: "warning", |
| 11 | + parent: project.window |
| 12 | + }); |
| 13 | +} |
| 14 | + |
| 15 | +function freezeParameter(parameter) { |
| 16 | + const original = parameter.original.toArray(); |
| 17 | + const edited = parameter.freeEdited; |
| 18 | + const samples = edited.toArray().slice(); |
| 19 | + let changed = false; |
| 20 | + for (let index = 0; index < original.length; ++index) { |
| 21 | + const value = original[index]; |
| 22 | + if (value === null || (index < samples.length && samples[index] !== null)) { |
| 23 | + continue; |
| 24 | + } |
| 25 | + while (samples.length < index) { |
| 26 | + samples.push(null); |
| 27 | + } |
| 28 | + samples[index] = value; |
| 29 | + changed = true; |
| 30 | + } |
| 31 | + if (changed) { |
| 32 | + edited.splice(0, edited.length, samples); |
| 33 | + } |
| 34 | + return changed; |
| 35 | +} |
| 36 | + |
| 37 | +module.exports = defineScript({ |
| 38 | + id: "org.diffscope.freeze-generated-content", |
| 39 | + name: t("freezeGeneratedContent.title"), |
| 40 | + version: "1.0.0", |
| 41 | + description: t("freezeGeneratedContent.description"), |
| 42 | + actions: [ |
| 43 | + { |
| 44 | + name: t("freezeGeneratedContent.title"), |
| 45 | + description: t("freezeGeneratedContent.description"), |
| 46 | + requiresProject: true, |
| 47 | + execute() { |
| 48 | + const project = application.project; |
| 49 | + const document = project.document; |
| 50 | + const selection = document.selection; |
| 51 | + const notes = new Map(); |
| 52 | + let clips = []; |
| 53 | + |
| 54 | + if (selection.type === "clip") { |
| 55 | + clips = selection.clips.items.filter(clip => clip.kind === "singing"); |
| 56 | + for (const clip of clips) { |
| 57 | + for (const note of clip.notes) { |
| 58 | + notes.set(note.handle, note); |
| 59 | + } |
| 60 | + } |
| 61 | + } else if (selection.type === "note") { |
| 62 | + for (const note of selection.notes.items) { |
| 63 | + notes.set(note.handle, note); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (clips.length === 0 && notes.size === 0) { |
| 68 | + showSelectionWarning(project); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const fields = []; |
| 73 | + if (clips.length !== 0) { |
| 74 | + fields.push({ |
| 75 | + type: "boolean", |
| 76 | + name: "parameters", |
| 77 | + label: t("freezeGeneratedContent.contents.parameters"), |
| 78 | + value: true |
| 79 | + }); |
| 80 | + } |
| 81 | + fields.push({ |
| 82 | + type: "boolean", |
| 83 | + name: "phonemes", |
| 84 | + label: t("freezeGeneratedContent.contents.phonemes"), |
| 85 | + value: true |
| 86 | + }); |
| 87 | + fields.push({ |
| 88 | + type: "boolean", |
| 89 | + name: "pronunciation", |
| 90 | + label: t("freezeGeneratedContent.contents.pronunciation"), |
| 91 | + value: true |
| 92 | + }); |
| 93 | + |
| 94 | + const readLock = document.mustAcquireReadLock(); |
| 95 | + const result = dialogs.form({ |
| 96 | + title: t("freezeGeneratedContent.title"), |
| 97 | + fields, |
| 98 | + parent: project.window |
| 99 | + }); |
| 100 | + readLock.release(); |
| 101 | + if (result === null) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + const transaction = document.beginTransaction(); |
| 106 | + let changed = false; |
| 107 | + if (result.parameters) { |
| 108 | + for (const clip of clips) { |
| 109 | + for (const [, parameter] of clip.parameters) { |
| 110 | + if (freezeParameter(parameter)) { |
| 111 | + changed = true; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + if (result.phonemes) { |
| 117 | + for (const note of notes.values()) { |
| 118 | + if (note.editedPhonemes.length === 0 && note.originalPhonemes.length !== 0) { |
| 119 | + note.editedPhonemes.fromOpenDSPX(note.originalPhonemes.toOpenDSPX()); |
| 120 | + changed = true; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + if (result.pronunciation) { |
| 125 | + for (const note of notes.values()) { |
| 126 | + const pronunciation = note.originalPronunciation; |
| 127 | + if (note.editedPronunciation === "" && pronunciation !== "") { |
| 128 | + note.editedPronunciation = pronunciation; |
| 129 | + changed = true; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (!changed) { |
| 135 | + transaction.abort(); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + transaction.commit(t("freezeGeneratedContent.title")); |
| 140 | + } |
| 141 | + } |
| 142 | + ] |
| 143 | +}); |
0 commit comments