Crush a sound to fewer bits and a lower rate with a Bitcrusher (#322)
A new effect for sound FX and instruments makes a sound rough like an
old console. It rounds the sound to fewer steps and holds it at a lower
sample rate, and all three of its numbers move while the sound plays.
- Bitcrusher node with bits (1 to 16, not only whole), rate (100 Hz to
48 kHz, sample-and-hold) and mix. A mix typed in at 0 leaves it out.
- It runs on the audio thread as a second processor next to the pitch
player. The module is now fxWorklet.ts and the test hook is called
fxWorkletUrl.
- A play that needs the worklet waits for it, for a Pitch or a
Bitcrusher alike. Without the worklet the sound goes through as is.
- Bits and mix move in what they hold, the rate in octaves. An
Envelope can move all three in an instrument.
- A stage that runs on the worklet is stopped with its voice, like the
drivers of an Envelope or a Mix.
- The math is a plain class, Crusher, so it is tested in Node.
The cleanup check of e2e 64 from af9b202 turned suite 553 red. It
asked that no editor run was left at all. In CI a step before 64 still
leaves one of its own running. The check now asks only about its own
behavior and prints what is still running, so the next suite names it.
Tests:
- Unit crusher (new) 4, soundfx +2, sound-library +3. All of tests/core,
tests/viewport and tests/editor/graph: 176 files, 2934 green.
- Counter-check: without the rounding 2 of 4 crusher tests fail.
- e2e 34 (melektaus-atx): ALL PASSED, 58 checks (1 new). Offline in
Chrome 2 bits at 4 kHz leave five levels held for 12 samples each.
64 stereo voices for 2 s render in 1.9 s, about 68 at once.
- e2e 64 (melektaus-atx): ALL PASSED, 30 checks.
- Gate (melektaus-atx): CHECK EXIT 0, 254 files, 3946 tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
af9b202100
commit
165f022fc0
11 files changed
+464
-59
No files matched your search
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* The audio thread's side of the effects that Web Audio has no node for. A pitched voice (#317)
|
||||
* plays one sound through a `PitchPlayer`, its pitch the AudioParam `pitch` in semitones, so it
|
||||
* glides, ramps and eases like any other knob. A Bitcrusher (#322) runs a `Crusher`. Loaded into
|
||||
* the context once by the sound library; nothing on the page imports it.
|
||||
*/
|
||||
import { Crusher } from '../core/soundfx/crusher';
|
||||
import { PitchPlayer } from '../core/soundfx/pitchPlayer';
|
||||
|
||||
/** What the audio thread has and the page's types do not. */
|
||||
declare const sampleRate: number;
|
||||
declare const currentTime: number;
|
||||
declare class AudioWorkletProcessor {
|
||||
readonly port: MessagePort;
|
||||
}
|
||||
declare function registerProcessor<T>(
|
||||
name: string,
|
||||
processor: new (options: { processorOptions: T }) => object,
|
||||
): void;
|
||||
|
||||
export interface PitchOptions {
|
||||
/** The file the samples are of: they are sent with the first voice of it only and kept here. */
|
||||
file: string;
|
||||
channels?: Float32Array[];
|
||||
/** The sound's rate over the context's. */
|
||||
step: number;
|
||||
loop: boolean;
|
||||
/** Where a loop runs, in samples of the sound (#331); without them it is the whole sound. */
|
||||
loopStart?: number;
|
||||
loopEnd?: number;
|
||||
/** The second of the audio clock its first sample is heard at (#332); before it, silence. */
|
||||
startAt?: number;
|
||||
/** Samples of the sound before where it starts, for a clip of a song taken up mid-way (#334). */
|
||||
skip?: number;
|
||||
}
|
||||
|
||||
/** Every sound a voice has been played from, for the next voice of it – processors share this scope. */
|
||||
const sounds = new Map<string, Float32Array[]>();
|
||||
|
||||
class PitchProcessor extends AudioWorkletProcessor {
|
||||
private readonly player: PitchPlayer;
|
||||
private stopped = false;
|
||||
private startAt: number;
|
||||
|
||||
constructor({ processorOptions }: { processorOptions: PitchOptions }) {
|
||||
super();
|
||||
const { file, channels, step, loop, loopStart, loopEnd, startAt, skip } = processorOptions;
|
||||
this.startAt = startAt ?? 0;
|
||||
if (channels) sounds.set(file, channels);
|
||||
const region = loop && loopEnd !== undefined ? { start: loopStart ?? 0, end: loopEnd } : loop;
|
||||
this.player = new PitchPlayer(sounds.get(file) ?? [], step, sampleRate, region, skip);
|
||||
this.port.onmessage = (event: MessageEvent) => {
|
||||
if (event.data === 'leave') this.player.leaveLoop();
|
||||
else this.stopped = true;
|
||||
};
|
||||
}
|
||||
|
||||
process(
|
||||
_inputs: Float32Array[][],
|
||||
outputs: Float32Array[][],
|
||||
parameters: Record<string, Float32Array>,
|
||||
): boolean {
|
||||
if (this.stopped) return false;
|
||||
let out = outputs[0]!;
|
||||
let pitch = parameters.pitch!;
|
||||
// A block is silence until then, the one it falls in starts on that sample.
|
||||
if (this.startAt > currentTime) {
|
||||
const wait = Math.round((this.startAt - currentTime) * sampleRate);
|
||||
if (wait >= (out[0]?.length ?? 0)) return true;
|
||||
out = out.map((channel) => channel.subarray(wait));
|
||||
if (pitch.length > 1) pitch = pitch.subarray(wait);
|
||||
this.startAt = 0;
|
||||
}
|
||||
if (this.player.render(out, pitch)) return true;
|
||||
this.port.postMessage('ended');
|
||||
return false;
|
||||
}
|
||||
|
||||
static get parameterDescriptors() {
|
||||
return [{ name: 'pitch', defaultValue: 0 }];
|
||||
}
|
||||
}
|
||||
|
||||
registerProcessor('pitch-player', PitchProcessor);
|
||||
|
||||
/** A Bitcrusher (#322) between two stages of a voice; it runs until the voice tells it to stop. */
|
||||
class CrushProcessor extends AudioWorkletProcessor {
|
||||
private readonly crusher = new Crusher(sampleRate);
|
||||
private stopped = false;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.port.onmessage = () => {
|
||||
this.stopped = true;
|
||||
};
|
||||
}
|
||||
|
||||
process(
|
||||
inputs: Float32Array[][],
|
||||
outputs: Float32Array[][],
|
||||
parameters: Record<string, Float32Array>,
|
||||
): boolean {
|
||||
if (this.stopped) return false;
|
||||
this.crusher.render(inputs[0] ?? [], outputs[0]!, parameters.bits!, parameters.rate!, parameters.mix!);
|
||||
return true;
|
||||
}
|
||||
|
||||
static get parameterDescriptors() {
|
||||
return [
|
||||
{ name: 'bits', defaultValue: 16 },
|
||||
{ name: 'rate', defaultValue: 48000 },
|
||||
{ name: 'mix', defaultValue: 0 },
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
registerProcessor('bit-crusher', CrushProcessor);
|
||||
Reference in new issue
Block a user