Skip to content

Commit

Permalink
Bypass frequency range optimization with extreme scales (#631)
Browse files Browse the repository at this point in the history
Format NaN and Infinity as is in the tuning table.
  • Loading branch information
frostburn authored Mar 31, 2024
1 parent ef6dab6 commit fe00121
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,21 @@ export class Scale {
* @returns An array of frequencies corresponding to the specified range.
*/
getFrequencyRange(start: number, end: number) {
// Deal with implicit 1/1
start -= 1
end -= 1
// Center on base MIDI note
start -= this.baseMidiNote
end -= this.baseMidiNote
const numEquaves = Math.floor(start / this.size)
// Deal with implicit 1/1 and center on base MIDI note
const low = start - 1 - this.baseMidiNote
const high = end - 1 - this.baseMidiNote
const numEquaves = Math.floor(low / this.size)
let referenceFrequency = this.baseFrequency * this.equaveRatio ** numEquaves
let index = start - numEquaves * this.size
const result = []
for (let i = start; i < end; ++i) {
if (!isFinite(referenceFrequency)) {
// The scale is too extreme for optimized calculation. Spend compute.
for (let i = start; i < end; ++i) {
result.push(this.getFrequency(i))
}
return result
}
let index = low - numEquaves * this.size
for (let i = low; i < high; ++i) {
result.push(referenceFrequency * this.intervalRatios[index])
index++
if (index >= this.size) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export function sanitizeFilename(input: string) {
}

export function formatExponential(x: number, fractionDigits = 3) {
if (isNaN(x) || !isFinite(x)) {
return x.toString()
}
if (Math.abs(x) < 10000) {
return x.toFixed(fractionDigits)
}
Expand Down

0 comments on commit fe00121

Please sign in to comment.