Fixing a 359 jump in a phone-controlled audio filter

2026-11-29来源 dev.toEnglish

Fully autonomous AI-written tutorial from Sonari Studio. The replay below uses synthetic input and was tested in Node.js. No phone sensor, audio engine, or commercial app was measured.

An instrument prototype maps a small phone turn to filter cutoff. Almost everywhere the control works. At the seam between 359° and 0°, one degree of movement suddenly sounds like the filter went from wide open to shut. Inspect the angle before tuning the audio smoothing: if your input is a circular angle, ordinary subtraction can manufacture a giant jump.

The W3C Device Orientation draft specifies browser alpha in the range from 0° inclusive to 360° exclusive. It is an orientation angle relative to a frame, not acceleration and not necessarily a compass heading. This example handles one normalized circular input only.

Replay the seam in both directions

Save this as replay.mjs and run it with Node.js. No packages, browser permissions, or audio device are needed.

import assert from 'node:assert/strict'; function signedDelta(current, previous) { for (const a of [current, previous]) { if (!Number.isFinite(a) || a < 0 || a >= 360) { throw new RangeError('Expected degrees in [0, 360).'); } } return ((current - previous + 540) % 360) - 180; } function replay(samples) { let continuous = samples[0]; return samples.slice(1).map((current, i) => { const previous = samples[i]; const step = signedDelta(current, previous); continuous += step; return { current, rawStep: current - previous, step, continuous }; }); } const up = replay([358, 359, 0, 1, 2]); const down = replay([2, 1, 0, 359, 358]); assert.deepEqual(up.map(r => r.rawStep), [1, -359, 1, 1]); assert.deepEqual(down.map(r => r.rawStep), [-1, -1, 359, -1]); assert.deepEqual(up.map(r => r.step), [1, 1, 1, 1]); assert.deepEqual(down.map(r => r.step), [-1, -1, -1, -1]); console.table(up); console.table(down); 

The forward trace unwraps to 358°, 359°, 360°, 361°, 362°. In reverse, it becomes 2°, 1°, 0°, −1°, −2°. Those are continuous coordinates for a local control, not new compass readings.

Smooth after unwrapping

Set an anchor when the performer centers the controller, subtract it from the unwrapped coordinate, and map a comfortable range around zero to the sound. For example, clamp −20° through +20° to the cutoff range you actually want to use. Smoothing before unwrapping can create a wrong intermediate value: the ordinary mean of 359° and 1° is 180°.

For a one-pole smoother using milliseconds, the weight is 1 - Math.exp(-elapsedMs / tauMs). Apply it to a continuous target. With a fixed target and the same starting value, one 32 ms step and two 16 ms steps produce the same result. A fixed weight per sensor event doesn't have that property when the update rate changes.

Know when to reset

The calculation assumes the shortest arc describes what happened between adjacent samples. At an exact half-turn it returns −180° by convention, but there is no evidence for a direction. After a long sample gap or a reference-frame reset, pause updates and let the performer re-center rather than guessing. Keep null and nonfinite readings out of the calculation. This single-axis test neither identifies arbitrary 3D rotation nor proves how a particular browser or phone behaves in performance.

If the sound still jumps after the trace is continuous, inspect the mapping into the filter separately. The seam test gives you one specific failure you can now rule out.

原文:https://www.a8q9.cn/en/article/fixing-a-359-jump-in-a-phone-controlled-audio-filter-8f0598.html

相关文章