diff --git a/package-lock.json b/package-lock.json index a9dc78ae..a82b7296 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "anser": "^2.3.5", "buttplug": "^3.2.2", "buttplug-wasm": "^2.0.1", - "cacophony": "^0.40.0", + "cacophony": "^0.41.0", "dompurify": "^3.4.1", "eventemitter3": "^5.0.4", "jzz": "^1.9.6", @@ -4276,9 +4276,9 @@ } }, "node_modules/cacophony": { - "version": "0.40.0", - "resolved": "https://registry.npmjs.org/cacophony/-/cacophony-0.40.0.tgz", - "integrity": "sha512-ZTzpC8SpcG7ss6cynECF23KGcn9bvvsWcHWpaJCRR34ooq3w3yjwrzdhwV0iPg5z9eTHQmKrA2Sy/EIx+746tA==", + "version": "0.41.0", + "resolved": "https://registry.npmjs.org/cacophony/-/cacophony-0.41.0.tgz", + "integrity": "sha512-BLEacdG/FOceHayXghQQ7VZGqOh1QFeK9WhAJPY3vqAs0SAba8FHKYi2sxmtyGEKwcoiIqp4hLAhMsSOc0pLEQ==", "license": "ISC", "dependencies": { "fft.js": "^4.0.4", diff --git a/package.json b/package.json index 3504c051..075314d9 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "anser": "^2.3.5", "buttplug": "^3.2.2", "buttplug-wasm": "^2.0.1", - "cacophony": "^0.40.0", + "cacophony": "^0.41.0", "dompurify": "^3.4.1", "eventemitter3": "^5.0.4", "jzz": "^1.9.6", diff --git a/src/audio/earlyReflections.test.ts b/src/audio/earlyReflections.test.ts index 1eb5ee71..36460e15 100644 --- a/src/audio/earlyReflections.test.ts +++ b/src/audio/earlyReflections.test.ts @@ -112,10 +112,20 @@ describe('reflectionImages: box room', () => { }); it('skips a plane the source is behind', () => { - const images = reflectionImages(room(), wa([6, 0, 1]), wa([0, 0, 1]), UNITY, inverse); - expect([...byMooPosition(images).keys()]).not.toContain('4,0,1'); + // At 1.5 m the floor and ceiling images are 2 ms late, clear of the 1 ms fusion cut. + const images = reflectionImages(room(), wa([6, 0, 1.5]), wa([0, 0, 1.5]), UNITY, inverse); + expect([...byMooPosition(images).keys()]).not.toContain('4,0,1.5'); expect(images).toHaveLength(5); }); + + it('leaves an image less than 1 ms behind the direct sound to the direct (ears 0.15 m from a wall)', () => { + // A footstep at the listener's feet, 0.15 m from the wall x=-5: that wall's + // image is 0.08 ms late; the ceiling and the far walls stay. + const images = reflectionImages(room(), wa([-4.85, 0, 0.01]), wa([-4.85, 0, 1.7]), UNITY, inverse); + expect([...byMooPosition(images).keys()]).not.toContain('-5.15,0,0.01'); + expect(images.every((image) => image.delay >= 0.001)).toBe(true); + expect(images).toHaveLength(4); + }); }); describe('reflectionImages: outdoors', () => { diff --git a/src/audio/earlyReflections.ts b/src/audio/earlyReflections.ts index c465e9a2..2fcab960 100644 --- a/src/audio/earlyReflections.ts +++ b/src/audio/earlyReflections.ts @@ -215,6 +215,11 @@ export function reflectionImages( if (u < 0 || u > 1) continue; } const path = length(sub(image, l)); + // An image less than 1 ms behind the direct sound (a listener or source + // almost touching the plane) fuses with it; rendered as its own point it + // comes from a wrong direction (Chromium's HRTF clamps elevations below + // -45 degrees) and combs the direct sound. Leave it to the direct. + if (path - direct < 0.001 * room.speedOfSound) continue; images.push({ position: mongooseToWebAudioVector(image)!, delay: Math.min(MAX_REFLECTION_DELAY, Math.max(0, (path - direct) / room.speedOfSound)), diff --git a/src/audio/effects/MediaEffects.test.ts b/src/audio/effects/MediaEffects.test.ts index 00f733d8..9bd85c45 100644 --- a/src/audio/effects/MediaEffects.test.ts +++ b/src/audio/effects/MediaEffects.test.ts @@ -250,7 +250,7 @@ describe('buildEffectsSupport', () => { expect(s.types).toContain('lowpass'); expect(s.types).not.toContain('biquad'); // V8: never advertise a non-wire name expect(s.types).not.toContain('echo'); // alias, not advertised - expect(s.reverbAlgorithms).toEqual(['fdn', 'plate']); + expect(s.reverbAlgorithms).toEqual(['fdn', 'plate', 'diffuse']); expect(s.chains).toBe(true); expect(s.automation).toBe(true); }); diff --git a/src/audio/effects/MediaEffects.ts b/src/audio/effects/MediaEffects.ts index f68f02b7..1e821441 100644 --- a/src/audio/effects/MediaEffects.ts +++ b/src/audio/effects/MediaEffects.ts @@ -46,7 +46,7 @@ const MAX_EFFECTS_PER_CHAIN = 8; export function buildEffectsSupport(voice: unknown = Playback.prototype): EffectsSupport { return { types: ADVERTISED_EFFECT_TYPES, - reverbAlgorithms: ['fdn', 'plate'], + reverbAlgorithms: ['fdn', 'plate', 'diffuse'], presets: Object.keys(CHAIN_PRESETS), automation: true, chains: true, diff --git a/src/audio/effects/buildEffect.ts b/src/audio/effects/buildEffect.ts index b5bf6ae5..b5e58a5c 100644 --- a/src/audio/effects/buildEffect.ts +++ b/src/audio/effects/buildEffect.ts @@ -29,6 +29,8 @@ export function buildEffect(cacophony: Cacophony, resolved: ResolvedEffect): Bui switch (resolved.factory) { case 'createFdnReverb': return cacophony.createFdnReverb(o as OptionsOf<'createFdnReverb'>); + case 'createDiffuseReverb': + return cacophony.createDiffuseReverb(o as OptionsOf<'createDiffuseReverb'>); case 'createReverb': return cacophony.createReverb(o as OptionsOf<'createReverb'>); case 'createDelay': diff --git a/src/audio/effects/resolveEffect.test.ts b/src/audio/effects/resolveEffect.test.ts index 5537354e..4d7ccdf6 100644 --- a/src/audio/effects/resolveEffect.test.ts +++ b/src/audio/effects/resolveEffect.test.ts @@ -67,6 +67,31 @@ describe('resolveEffect — type → factory mapping', () => { }); }); +describe('resolveEffect — diffuse reverb', () => { + it('maps the room vocabulary to createDiffuseReverb, without diffusion', () => { + const r = worklet( + resolveEffect({ + type: 'reverb', + algorithm: 'diffuse', + params: { + decayTimes: [0.7, 0.83, 0.85, 0.95, 0.91, 0.7, 0.48, 0.34], + preDelay: 0, + minDelay: 0.0102, + diffusion: 0.5, + mix: 1, + }, + }), + ); + expect(r.factory).toBe('createDiffuseReverb'); + expect(r.options).toEqual({ + decayTimes: [0.7, 0.83, 0.85, 0.95, 0.91, 0.7, 0.48, 0.34], + preDelay: 0, + minDelay: 0.0102, + mix: 1, + }); + }); +}); + describe('resolveEffect — FDN reverb', () => { it('passes the neutral vocabulary through 1:1, with one T60 per octave band', () => { const r = worklet( diff --git a/src/audio/effects/resolveEffect.ts b/src/audio/effects/resolveEffect.ts index 4428dd23..dccc98d2 100644 --- a/src/audio/effects/resolveEffect.ts +++ b/src/audio/effects/resolveEffect.ts @@ -103,6 +103,15 @@ function resolveFdnReverb(p: EffectParams): EffectParams { return o; } +/** + * Diffuse (decaying-noise) room reverb: the FDN's room vocabulary without + * `diffusion` (a noise tail is fully diffuse). Same clamps as the FDN. + */ +function resolveDiffuseReverb(p: EffectParams): EffectParams { + const { diffusion: _diffusion, ...rest } = resolveFdnReverb(p); + return rest; +} + /** * Plate (Dattorro) reverb: the option set is disjoint from FDN, so the neutral * vocabulary is translated and APPROXIMATED (design §4 reverb note): @@ -272,6 +281,9 @@ export function resolveEffect( switch (type) { case 'reverb': { + if (spec.algorithm === 'diffuse') { + return { kind: 'worklet', factory: 'createDiffuseReverb', options: resolveDiffuseReverb(p) }; + } const plate = spec.algorithm === 'plate'; return { kind: 'worklet', diff --git a/src/audio/effects/types.ts b/src/audio/effects/types.ts index 24bdfc14..55f9528e 100644 --- a/src/audio/effects/types.ts +++ b/src/audio/effects/types.ts @@ -17,7 +17,7 @@ export type EffectParamValue = number | string | readonly number[]; export type EffectParams = Record; /** Reverb algorithm discriminator (protocol-level, NOT a `params` key). */ -export type ReverbAlgorithm = 'fdn' | 'plate'; +export type ReverbAlgorithm = 'fdn' | 'plate' | 'diffuse'; /** Tremolo / auto-pan LFO waveform. Maps to cacophony's `shape` string alias. */ export type Waveform = 'sine' | 'triangle' | 'square'; @@ -78,7 +78,7 @@ export interface EffectSpec { type: string; /** Optional. Names this effect for automation targeting within a chain. */ id?: string; - /** Optional. Reverb only: `"fdn"` (default) or `"plate"`. */ + /** Optional. Reverb only: `"fdn"` (default), `"plate"` or `"diffuse"` (decaying-noise room tail). */ algorithm?: string; /** Optional. Per-effect preset name; explicit `params` override preset values. */ preset?: string; @@ -105,6 +105,7 @@ export interface ChainSpec { /** cacophony factory names this client builds worklet effects through. */ export type WorkletFactory = | 'createFdnReverb' + | 'createDiffuseReverb' | 'createReverb' | 'createDelay' | 'createChorus'