Skip to content

Commit

Permalink
Sanitize divisions input in equalize modal
Browse files Browse the repository at this point in the history
ref #825
  • Loading branch information
frostburn committed Dec 11, 2024
1 parent e5d50c7 commit f16e289
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/components/modals/generation/ConcordanceShell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()}`
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/modals/modification/EqualizeScale.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] }
Expand All @@ -31,16 +31,16 @@ 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)
}
} 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()}`
Expand Down
15 changes: 13 additions & 2 deletions src/stores/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -461,7 +471,7 @@ export const useModalStore = defineStore('modal', () => {
vao(
mediumInteger.value,
largeInteger.value,
largeDivisions.value,
safeLargeDivisions.value,
tolerance.value,
equave.value.totalCents()
)
Expand All @@ -470,7 +480,7 @@ export const useModalStore = defineStore('modal', () => {
return freeVAOs(
mediumInteger.value,
largeInteger.value,
largeDivisions.value,
safeLargeDivisions.value,
tolerance.value,
equave.value.totalCents()
)
Expand Down Expand Up @@ -595,6 +605,7 @@ export const useModalStore = defineStore('modal', () => {

// Equalize
largeDivisions,
safeLargeDivisions,

// Merge offset
offsets,
Expand Down

0 comments on commit f16e289

Please sign in to comment.