Hmbown/CodeWhale · error · Error

Invalid pet PCM range.

Error message

Invalid pet PCM range.

What it means

renderPetPCM validates the output request: sampleRate must be an integer in [8000, 96000], startSample a safe nonnegative integer, and length a nonnegative integer no greater than sampleRate * 120 (2 minutes of audio). Requests outside these bounds would allocate absurd buffers or produce invalid audio, so they throw before any rendering.

Solutions

  1. Clamp length to sampleRate * 120 and render in multiple windows, advancing startSample between calls.
  2. Ensure length and startSample are integers: Math.max(0, Math.floor(length)), Math.floor(startSample).
  3. Use a supported sample rate between 8000 and 96000 Hz (default 48000).
  4. Coerce arguments with Number() and validate Number.isSafeInteger before calling.

Example fix

// before
renderPetPCM(voices, 0, durationSec * 48000 * 10); // > 2 minutes
// after
const len = Math.min(durationSec * 48000, 48000 * 120);
renderPetPCM(voices, 0, Math.floor(len));
Defensive patterns

Strategy: validation

Validate before calling

const len = Math.min(Math.floor(length), sampleRate * 120);
if (!(sampleRate >= 8000 && sampleRate <= 96000) || !Number.isSafeInteger(startSample) || startSample < 0 || len < 0)
  throw new TypeError('invalid PCM request');

Try / catch

try { renderPetPCM(voices, s, l) } catch (err) { if (err.message.includes('PCM range')) return renderPetPCM(voices, s, Math.min(l, 48000 * 120)); throw err; }

Prevention

When it happens

Trigger: Calling renderPetPCM with a length above sampleRate*120, a startSample that is negative or fractional, sampleRate outside 8k–96k, or arguments arriving as strings/undefined from upstream code.

Common situations: Requesting a whole long trace's audio in one shot instead of streaming windows, computing length from a float duration times sampleRate, or a misconfigured sample rate constant.

Related errors


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

Appendix: source

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

                if (frame.needs === 'call' && window % 10 === 0)
                    add(`call:${window}`, pet_sim_js_1.CHANNELS[11].freq * 1.5, .38, .03);
            }
        }
        return out;
    }
}
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);
        }

View on GitHub (pinned to 433685b202)