From cc52071de6159987bd0145af7b4423e3ac327c23 Mon Sep 17 00:00:00 2001 From: Lumi Pakkanen Date: Wed, 11 Dec 2024 15:17:40 +0200 Subject: [PATCH] Sanitize divisions input in equalize modal ref #825 --- .../modals/generation/ConcordanceShell.vue | 8 ++++---- .../modals/modification/EqualizeScale.vue | 8 ++++---- src/stores/modal.ts | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/components/modals/generation/ConcordanceShell.vue b/src/components/modals/generation/ConcordanceShell.vue index dabfbaf9..13e25af4 100644 --- a/src/components/modals/generation/ConcordanceShell.vue +++ b/src/components/modals/generation/ConcordanceShell.vue @@ -23,7 +23,7 @@ function generate(shell = true, expand = true) { collecting: for (let i = 0; i < selectedVao.degrees.length; ++i) { const harmonic = selectedVao.harmonics[i] const degree = selectedVao.degrees[i] - const reducedDegree = mmod(degree, modal.largeDivisions) || modal.largeDivisions + const reducedDegree = mmod(degree, modal.safeLargeDivisions) || modal.safeLargeDivisions for (let j = 0; j < data.length; ++j) { if (data[j][0] === reducedDegree) { data[j][1] += ' & ' + harmonic.toString() @@ -33,10 +33,10 @@ function generate(shell = true, expand = true) { data.push([reducedDegree, harmonic.toString()]) } data.sort((a, b) => a[0] - b[0]) - if (!data.length || data[data.length - 1][0] !== modal.largeDivisions) { - data.push([modal.largeDivisions, '']) + if (!data.length || data[data.length - 1][0] !== modal.safeLargeDivisions) { + data.push([modal.safeLargeDivisions, '']) } - let postfix = `\\${modal.largeDivisions}` + let postfix = `\\${modal.safeLargeDivisions}` if (modal.equave.compare(OCTAVE)) { postfix += `ed ${linear(modal.equave).toString()}` } diff --git a/src/components/modals/modification/EqualizeScale.vue b/src/components/modals/modification/EqualizeScale.vue index 7156b20e..ba710a6d 100644 --- a/src/components/modals/modification/EqualizeScale.vue +++ b/src/components/modals/modification/EqualizeScale.vue @@ -21,7 +21,7 @@ const equalizedScaleData = computed(() => { const pitches = [...Array(scale.scale.size).keys()].map((i) => valueToCents(Math.abs(scale.scale.getRatio(i + scale.scale.baseMidiNote))) ) - const gridCents = valueToCents(scale.scale.equaveRatio) / modal.largeDivisions + const gridCents = valueToCents(scale.scale.equaveRatio) / modal.safeLargeDivisions if (modal.errorModel === 'rooted') { const error = misalignment(pitches, gridCents) return { error, degrees: [] } @@ -31,7 +31,7 @@ const equalizedScaleData = computed(() => { function modify(expand = true) { if (modal.errorModel === 'rooted') { - scale.sourceText += `\nequalize(${modal.largeDivisions})` + scale.sourceText += `\nequalize(${modal.safeLargeDivisions})` if (expand) { const { visitor, defaults } = scale.getUserScopeVisitor() scale.sourceText = visitor.expand(defaults) @@ -39,8 +39,8 @@ function modify(expand = true) { } else { const degrees = [...equalizedScaleData.value.degrees] degrees.shift() - degrees.push(modal.largeDivisions) - let postfix = `\\${modal.largeDivisions}` + degrees.push(modal.safeLargeDivisions) + let postfix = `\\${modal.safeLargeDivisions}` const equave = scale.relativeIntervals[scale.relativeIntervals.length - 1] if (equave.compare(OCTAVE)) { postfix += `ed ${linear(equave).toString()}` diff --git a/src/stores/modal.ts b/src/stores/modal.ts index acbae2a5..693ee0e7 100644 --- a/src/stores/modal.ts +++ b/src/stores/modal.ts @@ -385,6 +385,16 @@ export const useModalStore = defineStore('modal', () => { // Equalize const largeDivisions = ref(22) + const safeLargeDivisions = computed(() => { + const value = largeDivisions.value + if (isNaN(value) || !isFinite(value)) { + return 1 + } + if (value < 1) { + return 1 + } + return Math.round(value) + }) // Merge offset const overflowType = ref<'keep' | 'drop' | 'wrap'>('drop') @@ -461,7 +471,7 @@ export const useModalStore = defineStore('modal', () => { vao( mediumInteger.value, largeInteger.value, - largeDivisions.value, + safeLargeDivisions.value, tolerance.value, equave.value.totalCents() ) @@ -470,7 +480,7 @@ export const useModalStore = defineStore('modal', () => { return freeVAOs( mediumInteger.value, largeInteger.value, - largeDivisions.value, + safeLargeDivisions.value, tolerance.value, equave.value.totalCents() ) @@ -595,6 +605,7 @@ export const useModalStore = defineStore('modal', () => { // Equalize largeDivisions, + safeLargeDivisions, // Merge offset offsets,