Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an optional margin of equivalence to CS checks #638

Merged
merged 3 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scale-workshop",
"version": "3.0.0-beta.8",
"version": "3.0.0-beta.9",
"scripts": {
"dev": "vite",
"build": "run-p type-check \"build-only {@}\" --",
Expand All @@ -21,7 +21,7 @@
"moment-of-symmetry": "^0.4.2",
"pinia": "^2.1.7",
"qs": "^6.12.0",
"sonic-weave": "github:xenharmonic-devs/sonic-weave#v0.0.8",
"sonic-weave": "github:xenharmonic-devs/sonic-weave#v0.0.9",
"sw-synth": "^0.1.0",
"temperaments": "^0.5.3",
"vue": "^3.3.4",
Expand Down
36 changes: 33 additions & 3 deletions src/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,36 @@ export function constantStructureViolations(matrix: Interval[][]) {
return result
}

/**
* Compute a matrix of constant structure violations up to a margin.
* @param centsMatrix Interval matrix converted to cents.
* @param margin Margin of violation in cents.
* @returns Boolean array of CS violations within the given margin.
*/
export function marginViolations(centsMatrix: number[][], margin: number) {
const result: boolean[][] = []
for (let i = 0; i < centsMatrix.length; ++i) {
result.push(Array(centsMatrix[i].length).fill(false))
}
for (let i = 0; i < centsMatrix[0].length; ++i) {
for (let j = 0; j < centsMatrix.length; ++j) {
if (result[j][i]) {
continue
}
const cents = centsMatrix[j][i]
for (let k = i + 1; k < centsMatrix[0].length; ++k) {
for (let l = 0; l < centsMatrix.length; ++l) {
if (Math.abs(centsMatrix[l][k] - cents) < margin) {
result[j][i] = true
result[l][k] = true
}
}
}
}
}
return result
}

export function varietySignature(matrix: Interval[][]) {
const result: number[] = []
if (!matrix.length) {
Expand All @@ -431,12 +461,12 @@ export function varietySignature(matrix: Interval[][]) {
return result
}

export function brightnessSignature(matrix: Interval[][]) {
const totals = matrix.map((row) => row.reduce((prev, cur) => prev + cur.totalCents(), 0))
export function brightnessSignature(centsMatrix: number[][]) {
const totals = centsMatrix.map((row) => row.reduce((prev, cur) => prev + cur, 0))
const minimum = Math.min(...totals)
const maximum = Math.max(...totals)
if (minimum === maximum) {
return Array(matrix.length).fill(1)
return Array(centsMatrix.length).fill(1)
}
const normalizer = 1 / (maximum - minimum)
return totals.map((t) => (t - minimum) * normalizer)
Expand Down
4 changes: 2 additions & 2 deletions src/exporters/anamark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ class AnaMarkExporter extends BaseExporter {
'="#>-' +
i +
' % ' +
intervals[i - 1].totalCents().toFixed(6) +
intervals[i - 1].totalCents(true).toFixed(6) +
' ~999"' +
newline
} else {
file +=
'note ' + i + '="#=0 % ' + intervals[i - 1].totalCents().toFixed(6) + '"' + newline
'note ' + i + '="#=0 % ' + intervals[i - 1].totalCents(true).toFixed(6) + '"' + newline
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/exporters/reaper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class ReaperExporter extends BaseExporter {
if (interval.domain === 'linear') {
file += value.valueOf().toFixed(digits).replace('.', ',')
} else {
file += value.totalCents().toFixed(digits)
file += value.totalCents(true).toFixed(digits)
}
} else {
file += new Interval(
Expand Down
5 changes: 5 additions & 0 deletions src/stores/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const useStateStore = defineStore('state', () => {
)
const calculateVariety = ref(storage.getItem('calculateVariety') === 'true')
const calculateBrightness = ref(storage.getItem('calculateBrightness') === 'true')
const constantStructureMargin = ref(
parseInt(storage.getItem('constantStructureMargin') ?? '0', 10)
)

// Special keyboard codes also from local storage.
const deactivationCode = ref(storage.getItem('deactivationCode') ?? 'Backquote')
Expand All @@ -52,6 +55,7 @@ export const useStateStore = defineStore('state', () => {
calculateConstantStructureViolations,
calculateVariety,
calculateBrightness,
constantStructureMargin,
deactivationCode,
equaveUpCode,
equaveDownCode,
Expand Down Expand Up @@ -85,6 +89,7 @@ export const useStateStore = defineStore('state', () => {
calculateConstantStructureViolations,
calculateVariety,
calculateBrightness,
constantStructureMargin,
deactivationCode,
equaveUpCode,
equaveDownCode,
Expand Down
43 changes: 34 additions & 9 deletions src/views/AnalysisView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
constantStructureViolations,
freeEquallyTemperedChord,
intervalMatrix,
marginViolations,
rootedEquallyTemperedChord,
varietySignature
} from '@/analysis'
Expand Down Expand Up @@ -67,7 +68,7 @@ const strokeStyle = computed(() => {
// we want more accurate results here.
function formatMatrixCell(interval: Interval) {
if (cellFormat.value === 'cents') {
return interval.totalCents().toFixed(1)
return interval.totalCents(true).toFixed(1)
}
if (cellFormat.value === 'decimal') {
// Consistent with tuning table localization.
Expand All @@ -83,13 +84,24 @@ const matrix = computed(() =>
intervalMatrix(scale.relativeIntervals.slice(0, state.maxMatrixWidth))
)

const centsMatrix = computed(() => matrix.value.map((row) => row.map((i) => i.totalCents(true))))

const matrixRows = computed(() => matrix.value.map((row) => row.map(formatMatrixCell)))

const violations = computed(() => constantStructureViolations(matrix.value))
const violations = computed(() => {
const margin = state.constantStructureMargin
if (margin) {
return marginViolations(centsMatrix.value, margin)
} else {
return constantStructureViolations(matrix.value)
}
})

const variety = computed(() => varietySignature(matrix.value))

const brightness = computed(() => brightnessSignature(matrix.value).map((b) => Math.round(100 * b)))
const brightness = computed(() =>
brightnessSignature(centsMatrix.value).map((b) => Math.round(100 * b))
)

const heldScaleDegrees = computed(() => {
const result: Set<number> = new Set()
Expand All @@ -109,8 +121,8 @@ const equallyTemperedChordData = computed(() => {
divisions: 1
}
}
const frequencies = audio.virtualSynth.voices.map((voice) => voice.frequency)
const equaveCents = equave.value.totalCents()
const frequencies = audio.virtualSynth.voices.map((voice) => Math.abs(voice.frequency))
const equaveCents = equave.value.totalCents(true)
if (errorModel.value === 'rooted') {
return rootedEquallyTemperedChord(frequencies, maxDivisions.value, equaveCents)
}
Expand All @@ -128,6 +140,7 @@ function highlight(y?: number, x?: number) {
if (!state.calculateConstantStructureViolations) {
return
}
const margin = state.constantStructureMargin
if (highlights.length !== matrix.value.length) {
highlights.length = 0
for (let i = 0; i < matrix.value.length; ++i) {
Expand All @@ -136,11 +149,15 @@ function highlight(y?: number, x?: number) {
}
// Look at other violators
if (y !== undefined && x !== undefined && violations.value[y][x]) {
const value = matrix.value[y][x].value
const value: any = margin ? centsMatrix.value[y][x] : matrix.value[y][x].value
for (let i = 0; i < matrix.value.length; ++i) {
for (let j = 0; j < matrix.value[i].length; ++j) {
if (violations.value[i][j]) {
highlights[i][j] = matrix.value[i][j].value.strictEquals(value)
if (margin) {
highlights[i][j] = Math.abs(centsMatrix.value[i][j] - value) < margin
} else {
highlights[i][j] = matrix.value[i][j].value.strictEquals(value)
}
} else {
highlights[i][j] = false
}
Expand All @@ -160,9 +177,13 @@ function highlight(y?: number, x?: number) {
}

// Look at own column
const value = matrix.value[y][x].value
const value: any = margin ? centsMatrix.value[y][x] : matrix.value[y][x].value
for (let i = 0; i < matrix.value.length; ++i) {
highlights[i][x] = matrix.value[i][x].value.strictEquals(value)
if (margin) {
highlights[i][x] = Math.abs(centsMatrix.value[i][x] - value) < margin
} else {
highlights[i][x] = matrix.value[i][x].value.strictEquals(value)
}
}
}
</script>
Expand Down Expand Up @@ -257,6 +278,10 @@ function highlight(y?: number, x?: number) {
/>
<label for="calculate-violators"> Show constant structure violations</label>
</div>
<div class="control">
<label for="cs-margin">Constant structure margin in cents</label>
<input id="cs-margin" type="number" min="0" v-model="state.constantStructureMargin" />
</div>
<div class="control checkbox-container">
<input id="calculate-variety" type="checkbox" v-model="state.calculateVariety" />
<label for="calculate-variety"> Show variety signature</label>
Expand Down
2 changes: 1 addition & 1 deletion src/virtual-synth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class VirtualSynth {
frequency,
waveform
})
this.voices.sort((a, b) => a.frequency - b.frequency)
this.voices.sort((a, b) => Math.abs(a.frequency) - Math.abs(b.frequency))

const voiceOff = () => {
for (let i = 0; i < this.voices.length; ++i) {
Expand Down
Loading