Sounds fade, dull and doppler with distance again

The OpenAL port kept the whole authored audio model and then threw most of
its output away. Every frame the engine computed a distance-attenuation
curve, a high-frequency rolloff, doppler cents, a reverb level and a
front/rear placement, and every one of those consumers had been commented
out when the two AWE32 cards were replaced. What reached the speakers was
OpenAL's own defaults instead: a straight-line fade to silence, no
filtering, doppler at the wrong constants with an inverted velocity, no
reverb, and every cockpit sound dead centre.

Restored, per AUDIO.INI, which is byte-identical to the file that shipped
in August 1995:

  - the authored knee/rolloff distance curve, replacing AL_LINEAR_DISTANCE.
    This also un-blinds the transient cull, the voice-steal weighting and
    the mix ducking, which all key off it and were treating far sources as
    full presence
  - the CC7 squared volume law; writing the scale linearly ran everything
    about 6 dB hot at mid-scale
  - brightness and distance muffling, and the wet-exterior/dry-cockpit
    reverb split, both through a new OpenAL EFX bridge
  - doppler on the moving-source path only, as the original had it
  - front/rear placement from the authored position enum

The larger find is that AL_PITCH was never called anywhere in the tree, so
the entire pitch chain was inert - not only doppler but pitch_mix_offset,
which our own sequences author 97 times. Doppler alone would have changed
nothing audible.

Note pitch is applied for parity with the BT engine but is identity here:
our content predates NoteAudioControlID, so every source runs at note 60.

Builds clean on VS2022 Release|Win32. Smoke-tested against vRIO on COM1 -
reaches gameplay and holds a steady frame loop. ALC_EXT_EFX is present on
the build machine with all nine entry points, so the filter and reverb work
is live rather than inert. Not yet listened to on the pod, which is the
real test: the volume law changes the level of everything.

docs/SOUND.md documents the original two-card quadraphonic design, where
the surviving original assets are, and what remains.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cyd
2026-08-05 23:00:37 -05:00
co-authored by Claude Opus 5
parent 6ce729bab5
commit ce1b0ab9c3
9 changed files with 962 additions and 34 deletions
+243 -32
View File
@@ -2,6 +2,7 @@
#pragma hdrstop
#include "l4audio.h"
#include "l4audefx.h"
#include "l4audlvl.h"
#include "l4app.h"
#include "l4audrnd.h"
@@ -9,6 +10,49 @@
#include "..\munga\player.h"
#include "..\rp\vtv.h"
//
// FIDELITY (docs/SOUND.md): the AWE32 played each patch at the requested MIDI
// note relative to the sample root (60). RP's authored 4.10 content predates
// NoteAudioControlID -- its AudioControlID enum stops at AttackTimeAudioControlID
// -- so every source runs at DEFAULT_NOTE and this factor is 1.0 today. It is
// applied anyway so the pitch path is complete if authored notes ever appear,
// and to keep the shared MUNGA engine in step with the BT tree.
//
static inline float RPNotePitchFactor(int note_value)
{
return (float)pow(2.0, ((double)note_value - 60.0) / 12.0);
}
//
// FIDELITY (docs/SOUND.md F12): the authored DirectPatchSource `position=`
// enum picked a SOUND CARD (front pair for Front/FrontLeft/FrontRight, rear
// pair for Rear/RearLeft/RearRight) and a MIDI pan (CC10 centre/left/right).
// The port read audioPosition from the stream and then discarded it -- every
// cockpit sound played dead centre because SetupPatch pins each source
// AL_SOURCE_RELATIVE at the origin.
//
// Sources are listener-relative and no AL_ORIENTATION is ever set, so OpenAL's
// default listener frame applies: facing -Z with +Y up. Front is therefore
// -Z, rear +Z, left -X, right +X; the corner values combine both at equal
// weight. RP's own content only ever authors Front (28 sites) and Rear (13),
// but the corners are mapped for completeness since the enum allows them.
//
static void RPGetDirectPatchPosition(DirectPatchPosition p, float *x, float *z)
{
const float diag = 0.7071068f; // unit vector split across both axes
switch (p)
{
case FrontDirectPatchPosition: *x = 0.0f; *z = -1.0f; break;
case RearDirectPatchPosition: *x = 0.0f; *z = 1.0f; break;
case FrontLeftDirectPatchPosition: *x = -diag; *z = -diag; break;
case FrontRightDirectPatchPosition: *x = diag; *z = -diag; break;
case RearLeftDirectPatchPosition: *x = -diag; *z = diag; break;
case RearRightDirectPatchPosition: *x = diag; *z = diag; break;
default: *x = 0.0f; *z = 0.0f; break;
}
}
//#############################################################################
//####################### L4AudioSpatialization #########################
//#############################################################################
@@ -923,6 +967,22 @@ void
patch_resource->SetDistance(GetDistanceToSource());
patch_resource->SetupPatch(channelSet);
//
// FIDELITY (docs/SOUND.md F12): place the source per the authored position
// enum. SetupPatch has just pinned it AL_SOURCE_RELATIVE at the origin, so
// this must run after it. With AL_NONE as the distance model the unit
// radius costs no attenuation -- it only supplies direction.
//
{
float pos_x, pos_z;
RPGetDirectPatchPosition(audioPosition, &pos_x, &pos_z);
for (int i = 0; i < channelSet.count; i++)
{
alSource3f(channelSet.sources[i], AL_POSITION, pos_x, 0.0f, pos_z);
}
}
//
// Set the channel to default control values
//
@@ -1058,6 +1118,26 @@ void
{
lastMIDIFilterCutoff = midi_filter_cutoff;
}
//
// FIDELITY (docs/SOUND.md F9): this block previously computed the AWE
// initial-filter-cutoff (NRPN 21) and then only updated its own
// bookkeeping member -- the cutoff was never applied to anything, so
// authored brightness (ctl 5) was inert. Route it through EFX instead.
// Direct sources take brightness alone, gated on use_brightness_scale;
// the distance rolloff belongs to the 3D paths.
//
if (EFX_Available())
{
const float cutoff_scale =
(float)midi_filter_cutoff / (float)MIDI_MAX_CONTROL_VALUE;
const float gainhf = EFX_CutoffScaleToGainHF(cutoff_scale);
for (int i = 0; i < channelSet.count; i++)
{
EFX_SetSourceLowpassGainHF(channelSet.sources[i], gainhf);
}
}
}
//
@@ -1069,17 +1149,22 @@ void
const MIDIValue volume_resolution = 2; // HACK - should come from audio.ini
volume_scale = CalculateSourceVolumeScale();
L4AudioLocation *audio_location = Cast_Object(L4AudioLocation*, GetAudioLocation());
Check(application);
L4AudioRenderer *audio_renderer =
Cast_Object(L4AudioRenderer*, application->GetAudioRenderer());
Check(audio_renderer);
AudioHead *audio_head = audio_renderer->GetAudioHead();
Check(audio_head);
//
// FIDELITY (docs/SOUND.md F4): the original ended its volume path in MIDI
// CC7, whose GM/SoundFont curve is concave -- amplitude ~ (v/127)^2. Writing
// volume_scale linearly to AL_GAIN played every intermediate level about
// +6 dB hot at mid-scale and compressed the authored dynamic range.
//
// AL_MAX_DISTANCE is no longer written here: the distance model is AL_NONE
// (see MUNGA/AUDIO.cpp) so it has no effect, and DirectPatch is the
// non-positional cockpit path which never took distance attenuation anyway.
//
const float direct_note_pitch = RPNotePitchFactor((int)GetCurrentNoteValue());
for (int i=0; i < channelSet.count; i++)
{
alSourcef(channelSet.sources[i],AL_MAX_DISTANCE,audio_location->getMaxDistance(audio_head));
alSourcef(channelSet.sources[i], AL_GAIN, volume_scale);
alSourcef(channelSet.sources[i], AL_GAIN, volume_scale * volume_scale);
alSourcef(channelSet.sources[i], AL_PITCH, (float)relativePitch * direct_note_pitch);
}
}
@@ -1206,6 +1291,17 @@ void
patch_resource->SetDistance(GetDistanceToSource());
patch_resource->SetupPatch(channelSet);
//
// FIDELITY (docs/SOUND.md F11): wet exterior. The original sent CC91 =
// global_reverb_scale on all four channels of a 3D source and CC91 = 0 on
// the cockpit DirectPatch channels -- a deliberate outside/inside contrast
// that the port lost when every send site was commented out.
//
for (int i = 0; i < channelSet.count; i++)
{
EFX_AttachReverbSend(channelSet.sources[i]);
}
/*patch_resource->SetDistance(GetDistanceToSource());
for (i = 0; i < AudioChannelSetSize; i++)
{
@@ -1405,15 +1501,69 @@ void
pitch_offset = CalculateSourcePitchOffset();
//
// FIDELITY (docs/SOUND.md F10): add the AUTHORED doppler. AUDIO.INI's
// doppler_range=600 / speed_of_sound=250 are computed into
// AudioLocation::dopplerCents on every spatial update, and the original
// applied it on this dynamic path only -- static and direct sources stayed
// doppler-free. GetDopplerCents() previously had no callers at all.
//
pitch_offset += GetAudioLocation()->GetDopplerCents();
double relativePitch = pow(2.0,pitch_offset/1200.0);
Clamp(relativePitch,0.5,2.0);
//
// FIDELITY (docs/SOUND.md): relativePitch was computed here and never
// applied -- there was no AL_PITCH call anywhere in the tree, so the whole
// authored pitch chain (pitch_mix_offset / PitchAudioControlID, authored 97
// times across RP's sequences) was inert along with doppler.
//
// AL_VELOCITY is still written for bookkeeping but is now inert: doppler
// factor is 0 (see MUNGA/AUDIO.cpp) because this feed is sign-inverted
// relative to the AL_POSITION frame and never subtracted head velocity.
// AL_MAX_DISTANCE is dropped -- the distance model is AL_NONE and the
// authored curve is applied in CalculateSourceVolumeScale instead.
//
//
// FIDELITY (docs/SOUND.md F9): the AUTHORED high-frequency rolloff. The
// original drove the AWE filter cutoff on this path from
// highFreqCutoffScale x brightnessScale, ungated, on all four quadrant
// channels -- every moving 3D sound got duller with distance. AUDIO.INI
// still computes highFreqCutoffScale each frame (rolloff 2.0, knee 60,
// scale 0.005) and GetHighFreqCutoffScale() previously had zero callers.
//
float dynamic_gainhf = 1.0f;
if (EFX_Available())
{
PatchResource *filter_patch =
Cast_Object(PatchResource*, GetAudioResource());
Check(filter_patch);
Scalar filter_scale =
GetAudioLocation()->GetHighFreqCutoffScale() *
CalculateSourceBrightnessScale();
Scalar max_cutoff = (Scalar)filter_patch->GetMaxMIDIFilterCutoff();
Scalar midi_cutoff = filter_scale * max_cutoff;
dynamic_gainhf = EFX_CutoffScaleToGainHF(
(float)(midi_cutoff / (Scalar)MIDI_MAX_CONTROL_VALUE)
);
}
const float dynamic_note_pitch = RPNotePitchFactor((int)GetCurrentNoteValue());
for (int i=0; i < channelSet.count; i++)
{
alSource3f(channelSet.sources[i],AL_POSITION,pos.x,pos.y,pos.z);
alSourcef(channelSet.sources[i], AL_GAIN, volume_scale);
alSourcef(channelSet.sources[i], AL_GAIN, volume_scale * volume_scale);
alSourcef(channelSet.sources[i], AL_PITCH, (float)relativePitch * dynamic_note_pitch);
alSource3f(channelSet.sources[i],AL_VELOCITY,-relative_velocity.x,-relative_velocity.y,-relative_velocity.z);
alSourcef(channelSet.sources[i],AL_MAX_DISTANCE,audio_location->getMaxDistance(audio_head));
if (EFX_Available())
{
EFX_SetSourceLowpassGainHF(channelSet.sources[i], dynamic_gainhf);
}
}
}
@@ -1430,24 +1580,27 @@ AudioControlValue
//
// Call inherited method to calculate volume scale
//
Scalar
Scalar
volume_scale = L4AudioSource::CalculateSourceVolumeScale();
return volume_scale;
//
// Update the spatial model that will result in the value
// for distance related volume attenuation
//
/*Check(application);
Check(application->GetAudioRenderer());
UpdateSpatialModel(application->GetAudioRenderer()->GetAudioHead());
//
// Apply distance attenuation to the volume scale
//
// FIDELITY (docs/SOUND.md F3): apply the AUTHORED distance attenuation.
// AUDIO.INI's knee/rolloff curve (amplitude_rolloff=2.0, knee=60,
// distance_scale=0.003, clipping_radius=550) is computed into
// distanceVolumeScale on every spatial update; this multiply was commented
// out behind an early return and AL_LINEAR_DISTANCE substituted, which faded
// distant audio on a straight line to zero instead of the authored
// 1/(1+(k(d-knee))^2). Restoring it also un-blinds the volume-based
// transient cull, the AudioWeighting voice-steal, and the CalculateMix
// ducking chain, all of which key off this value and were treating far
// sources as full-presence.
//
// The spatial model is already refreshed each Execute, so the
// UpdateSpatialModel call the original comment carried is not needed here.
//
Check(GetAudioLocation());
volume_scale *= GetAudioLocation()->GetDistanceVolumeScale();
return volume_scale;*/
return volume_scale;
}
//#############################################################################
@@ -1468,6 +1621,26 @@ Static3DPatchSource::Static3DPatchSource(
MemoryStream_Read(stream, &useInternalSpatialization);
}
//
//#############################################################################
//#############################################################################
//
AudioControlValue
Static3DPatchSource::CalculateSourceVolumeScale()
{
Check(this);
//
// FIDELITY (docs/SOUND.md F3): same authored distance attenuation as the
// dynamic path. The spatial model computes distanceVolumeScale on every
// execute; without this multiply statics were left to AL_LINEAR_DISTANCE.
//
Scalar volume_scale = L4AudioSource::CalculateSourceVolumeScale();
Check(GetAudioLocation());
volume_scale *= GetAudioLocation()->GetDistanceVolumeScale();
return volume_scale;
}
Logical Static3DPatchSource::IsAudioSourceClipped(AudioHead *audio_head)
{
if (AudioSource::IsAudioSourceClipped(audio_head) || l4_application->GetMissionPlayer()->GetPlayerVehicle()->GetSimulationState() == VTV::BurningState)
@@ -1694,6 +1867,16 @@ void
Check(patch_resource);
patch_resource->SetDistance(GetDistanceToSource());
patch_resource->SetupPatch(channelSet);
//
// FIDELITY (docs/SOUND.md F11): statics are exterior sources too, so they
// take the same wet send as the dynamic path.
//
for (int i = 0; i < channelSet.count; i++)
{
EFX_AttachReverbSend(channelSet.sources[i]);
}
/*for (i = 0; i < AudioChannelSetSize; i++)
{
if ((channel = channelSet.GetNth(i)) != NULL)
@@ -1909,12 +2092,6 @@ void
Scalar volume_scale = CalculateSourceVolumeScale();
L4AudioLocation *audio_location = Cast_Object(L4AudioLocation*, GetAudioLocation());
Check(application);
L4AudioRenderer *audio_renderer =
Cast_Object(L4AudioRenderer*, application->GetAudioRenderer());
Check(audio_renderer);
AudioHead *audio_head = audio_renderer->GetAudioHead();
Check(audio_head);
Scalar pitch_offset;
@@ -1933,12 +2110,46 @@ void
relative_position = audio_location->GetVectorToSource();
}
//
// FIDELITY (docs/SOUND.md F4 + pitch): squared CC7 volume law, and the
// authored pitch chain applied -- see the DirectPatch/Dynamic3D paths. The
// original left static sources doppler-free, so no doppler term here.
// AL_MAX_DISTANCE dropped with the AL_NONE distance model; the authored
// curve is applied in CalculateSourceVolumeScale.
//
//Static models have their position freely available as relative positions and stand still
//
// FIDELITY (docs/SOUND.md F9): statics took brightness alone in the
// original -- no distance term on this path.
//
float static_gainhf = 1.0f;
if (EFX_Available() && UseSourceBrightnessScale())
{
PatchResource *filter_patch =
Cast_Object(PatchResource*, GetAudioResource());
Check(filter_patch);
Scalar midi_cutoff =
CalculateSourceBrightnessScale() *
(Scalar)filter_patch->GetMaxMIDIFilterCutoff();
static_gainhf = EFX_CutoffScaleToGainHF(
(float)(midi_cutoff / (Scalar)MIDI_MAX_CONTROL_VALUE)
);
}
const float static_note_pitch = RPNotePitchFactor((int)GetCurrentNoteValue());
for (int i=0; i < channelSet.count; i++)
{
alSourcef(channelSet.sources[i], AL_GAIN, volume_scale);
alSourcef(channelSet.sources[i], AL_GAIN, volume_scale * volume_scale);
alSourcef(channelSet.sources[i], AL_PITCH, (float)relativePitch * static_note_pitch);
alSource3f(channelSet.sources[i],AL_POSITION,relative_position.x,relative_position.y,relative_position.z);
alSourcef(channelSet.sources[i],AL_MAX_DISTANCE,audio_location->getMaxDistance(audio_head));
if (EFX_Available())
{
EFX_SetSourceLowpassGainHF(channelSet.sources[i], static_gainhf);
}
}
//