Skip to content

resolvePitchParams() error message hardcodes algorithm list - becomes stale when new variable-ratio algorithms added #1

Description

@Elshayib

Bug Report

Issue

In host.js, the resolvePitchParams() function has a hardcoded error message listing specific algorithms that support variable ratios. This error message becomes outdated when new algorithms are added that support variable ratios.

Location

host.js lines 25-30

Details

export function resolvePitchParams(opts) {
  let semitones = opts?.semitones ?? 0
  if (!Number.isFinite(semitones)) throw new TypeError('pitchShift: `semitones` must be a finite number')
  let raw = opts?.ratio
  if (typeof raw === 'function' || raw instanceof Float32Array) {
    throw new TypeError('pitchShift: variable `ratio` (function or Float32Array) is supported by vocoder, phaseLock, transient, formant, paulstretch, sms, hpss, and sample')
  }
  // ...
}

The Bug: The error message explicitly lists algorithms: "vocoder, phaseLock, transient, formant, paulstretch, sms, hpss, and sample". When a new algorithm is added that supports variable ratios (e.g., a future melody algorithm), the error message won't be updated, misleading users.

Additionally, lpc and delay algorithms were added later (as seen in pitch-shift.js and test.js) and also support variable ratios, but they're not in this error message list.

Impact

  • Medium: Misleading error messages when using variable ratios with newer algorithms
  • Maintenance burden: every new variable-ratio algorithm requires updating this error message

Suggested Fix

Replace the hardcoded list with a reference to a central capability registry, or at minimum make the message generic:

if (typeof raw === 'function' || raw instanceof Float32Array) {
  throw new TypeError('pitchShift: variable `ratio` (function or Float32Array) is not supported by this algorithm. Use the main `pitchShift()` entry point which auto-selects a compatible method, or use one of the algorithms that explicitly supports variable ratios.')
}

Or better, maintain a SUPPORTS_VARIABLE_RATIO Set/Map that algorithms register themselves into:

// At top of host.js or in a shared capabilities file
const VARIABLE_RATIO_ALGORITHMS = new Set([
  'vocoder', 'phaseLock', 'transient', 'formant', 
  'paulstretch', 'sms', 'hpss', 'sample', 'delay', 'lpc'
])

export function resolvePitchParams(opts) {
  // ...
  if (typeof raw === 'function' || raw instanceof Float32Array) {
    const algos = [...VARIABLE_RATIO_ALGORITHMS].join(', ')
    throw new TypeError(`pitchShift: variable \`ratio\` (function or Float32Array) is supported by: ${algos}. Use the main \`pitchShift()\` entry point for auto-selection.`)
  }
  // ...
}

Then each algorithm file can import and register itself:

// In delay.js, lpc.js, etc.
import { VARIABLE_RATIO_ALGORITHMS } from './host.js'
VARIABLE_RATIO_ALGORITHMS.add('delay')

This makes the capability explicit and self-documenting.

Related Note

The lpc and delay algorithms (added in recent commits) support variable ratios per the tests but are missing from the error message list.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions