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

Bypass frequency range optimization with extreme scales #631

Merged
merged 1 commit into from
Mar 31, 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
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
Loading