Hmbown/CodeWhale · error · Error

Invalid pet voice.

Error message

Invalid pet voice.

What it means

The pet audio synthesizer validates each voice descriptor before rendering PCM samples. A voice must have finite start/duration/frequency/gain/pan within physical and safety bounds, and a kind of 'tone' or 'noise'; otherwise 'Invalid pet voice.' is thrown to prevent generating undefined or out-of-Nyquist-range audio.

Solutions

  1. Log the offending voice object and clamp/fix its fields before synthesis
  2. Validate voice descriptors at the call site (start>=0, 0<duration<=10, 0<frequency<=sampleRate/2, 0<=gain<=1, |pan|<=1, kind in ['tone','noise'])
  3. Check for NaN/Infinity earlier in the pipeline that computes the voice
  4. Fix the kind string to exactly 'tone' or 'noise'

Example fix

// before
const voices = [{ start: 0, duration: 20, frequency: 440, gain: 1.5, pan: 0, kind: 'sine' }];
// after
const voices = [{ start: 0, duration: 5, frequency: 440, gain: 1.0, pan: 0, kind: 'tone' }].map(v => ({
  ...v,
  duration: Math.min(v.duration, 10),
  frequency: Math.min(v.frequency, sampleRate / 2),
  gain: Math.max(0, Math.min(1, v.gain)),
  kind: v.kind === 'noise' ? 'noise' : 'tone'
}));
Defensive patterns

Strategy: validation

Validate before calling

function isValidPetVoice(v, sampleRate) {
  return [v.start, v.duration, v.frequency, v.gain, v.pan].every(Number.isFinite)
    && v.start >= 0 && v.duration > 0 && v.duration <= 10
    && v.frequency > 0 && v.frequency <= sampleRate / 2
    && v.gain >= 0 && v.gain <= 1 && Math.abs(v.pan) <= 1
    && ['tone', 'noise'].includes(v.kind);
}

Type guard

const isPetVoice = (v) => typeof v === 'object' && v !== null
  && ['tone', 'noise'].includes(v.kind)
  && [v.start, v.duration, v.frequency, v.gain, v.pan].every(Number.isFinite);

Try / catch

try {
  synth(voices);
} catch (err) {
  if (err.message === 'Invalid pet voice.') {
    const bad = voices.find(v => !isValidPetVoice(v, sampleRate));
    console.error('dropping invalid voice', bad);
    synth(voices.filter(isValidPetVoice));
  } else throw err;
}

Prevention

When it happens

Trigger: Calling the synth path with a voice whose duration exceeds 10s, frequency above sampleRate/2 or <= 0, gain outside 0..1, |pan| > 1, non-finite values (NaN/Infinity), or kind not 'tone'/'noise'.

Common situations: Config files with gain=1.5 or pan=-2, duration computed from a bad timer, NaN propagated from an earlier division, typos in kind (' Tone'), or a sample-rate change making a fixed frequency exceed Nyquist.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@433685b202 (2026-09-15). Data as JSON: /api/errors/7fe5e4dc997a1046. Report an issue: GitHub.

Appendix: source

Thrown at pet/ios/Resources/pet-native.js:1630

}
exports.PetScore = PetScore;
/** Sample-addressed noise: no global RNG, identical samples when chunked/seeking. */
function noise(seed, sample) {
    let x = (seed + Math.imul(sample, 0x6D2B79F5)) >>> 0;
    x = Math.imul(x ^ x >>> 15, x | 1);
    x ^= x + Math.imul(x ^ x >>> 7, x | 61);
    return ((x ^ x >>> 14) >>> 0) / 2147483648 - 1;
}
function renderPetPCM(voices, startSample, length, sampleRate = 48_000) {
    if (!Number.isInteger(sampleRate) || sampleRate < 8000 || sampleRate > 96000
        || !Number.isSafeInteger(startSample) || startSample < 0 || !Number.isInteger(length) || length < 0 || length > sampleRate * 120)
        throw new Error('Invalid pet PCM range.');
    const left = new Float32Array(length), right = new Float32Array(length);
    for (const v of voices) {
        if (![v.start, v.duration, v.frequency, v.gain, v.pan].every(Number.isFinite)
            || v.start < 0 || v.duration <= 0 || v.duration > 10 || v.frequency <= 0 || v.frequency > sampleRate / 2
            || v.gain < 0 || v.gain > 1 || Math.abs(v.pan) > 1 || !['tone', 'noise'].includes(v.kind))
            throw new Error('Invalid pet voice.');
        const first = Math.max(startSample, Math.ceil(v.start * sampleRate));
        const last = Math.min(startSample + length, Math.ceil((v.start + v.duration) * sampleRate));
        const pan = (v.pan + 1) * Math.PI / 4, seed = (0xC0FFEE ^ (0, model_js_1.stableHash)(v.id)) >>> 0;
        for (let absolute = first; absolute < last; absolute++) {
            const age = absolute / sampleRate - v.start;
            const envelope = Math.min(1, age / .015, (v.duration - age) / .045);
            const sample = v.kind === 'noise' ? noise(seed, absolute) * Math.exp(-age * 14)
                : Math.sin(2 * Math.PI * v.frequency * age) * .88 + Math.sin(4 * Math.PI * v.frequency * age) * .12;
            const value = sample * Math.max(0, envelope) * v.gain, at = absolute - startSample;
            left[at] += value * Math.cos(pan);
            right[at] += value * Math.sin(pan);
        }
    }
    // Limiting is a presentation operation and cannot perturb voice scheduling.
    for (let i = 0; i < length; i++) {
        left[i] = (0, model_js_1.clamp)(left[i], -1, 1);
        right[i] = (0, model_js_1.clamp)(right[i], -1, 1);
    }

View on GitHub (pinned to 433685b202)