engine sounds: volume leveling

This commit is contained in:
whoisdomi
2026-11-20 15:20:47 -06:00
parent 689eaf7d55
commit c56601ca7a
7 changed files with 60 additions and 38 deletions
@@ -112,6 +112,7 @@ class LoopSynth:
def render(self, rpm: float, load: float, frames: int, volume: float = 1.0) -> np.ndarray:
p = self.profile
volume *= 10 ** (p.trim_db / 20)
load = min(max(load, 0.0), 1.0)
ramp = np.arange(1, frames + 1, dtype=np.float64) / frames
rpm_ramp = self.rpm + (rpm - self.rpm) * ramp
+14 -11
View File
@@ -37,6 +37,7 @@ class Profile:
pulse_partial: int = 8 # partial the pulses follow
tremolo: tuple[float, float] = (0.0, 0.0) # rate in Hz, depth
gain: tuple[float, float] = (0.35, 1.0) # loudness at no load and full load
trim_db: float = 0.0 # evens out loudness between profiles at the same volume setting
pops: float = 0.0 # overrun pops and crackle, 0 none to 1 heavy
burble: bool = False # recorded burble bursts instead of single pops at low revs and on downshifts
pop_pitch: float = 1.0 # playback speed of every pop; below 1 is deeper
@@ -61,7 +62,7 @@ RACE_REDLINE = 8600.0
def _recorded(name: str, label: str, *, cylinders: int, idle: float, redline: float, loops: str = '', pops: float = 0.0,
burble: bool = False, pop_pitch: float = 1.0, turbo: float = 0.0, upshift: float = 0.38,
downshift: float = 0.2, gearing: float = 1.0) -> Profile:
downshift: float = 0.2, gearing: float = 1.0, trim_db: float = 0.0) -> Profile:
"""A bundled loop bank (of the same name unless given), geared like the race car but scaled to its own rev range.
upshift and downshift are the light-throttle shift points as shares of redline; full throttle always shifts at 96%.
@@ -75,26 +76,28 @@ def _recorded(name: str, label: str, *, cylinders: int, idle: float, redline: fl
upshift_rpm=(upshift * redline, 0.96 * redline), downshift_rpm=(downshift * redline, max(0.6, downshift + 0.25) * redline),
launch_slip_rpm=0.4 * redline, rev_seconds=0.8, rise_seconds=0.08, fall_seconds=0.35, limiter=True,
partials=_engine_partials(firing=cylinders, count=40, rough=0.25, bright=0.5),
noise=(0.03, 0.18), gain=(0.7, 1.0),
noise=(0.03, 0.18), gain=(0.7, 1.0), trim_db=trim_db,
)
PROFILES: dict[str, Profile] = {profile.name: profile for profile in (
_recorded('bmw_m3', 'BMW M3', cylinders=6, idle=700.0, redline=7000.0, pops=1.0, burble=True),
_recorded('bmw_m3_deep', 'BMW M3 Deep', cylinders=6, idle=700.0, redline=7000.0, pops=1.0, burble=True, pop_pitch=0.8),
_recorded('bmw_m52', 'BMW M52', cylinders=6, idle=700.0, redline=7000.0),
# trim_db evens the average of 30 and 65 mph cruise and the demo drive (tools/render_drive.py) at -17.6 LUFS.
_recorded('bmw_m3', 'BMW M3', cylinders=6, idle=700.0, redline=7000.0, pops=1.0, burble=True, trim_db=-1.4),
_recorded('bmw_m3_deep', 'BMW M3 Deep', cylinders=6, idle=700.0, redline=7000.0, pops=1.0, burble=True, pop_pitch=0.8,
trim_db=0.2),
_recorded('bmw_m52', 'BMW M52', cylinders=6, idle=700.0, redline=7000.0, trim_db=0.5),
_recorded('f1_car', 'F1 Car', cylinders=12, idle=2000.0, redline=17000.0),
_recorded('ferrari_f136', 'Ferrari F136', cylinders=8, idle=1000.0, redline=9000.0),
# Banks from real recordings that end at 3000 and 5250 rpm; above about 1.5 times that the loops sound sped up.
_recorded('hellcat', 'Hellcat', cylinders=8, idle=700.0, redline=4500.0),
_recorded('jaguar_ftype', 'Jaguar F-Type', cylinders=8, idle=721.0, redline=6500.0, pops=1.0, burble=True),
_recorded('lfa', 'LFA', cylinders=10, idle=1000.0, redline=9000.0),
_recorded('hellcat', 'Hellcat', cylinders=8, idle=700.0, redline=4500.0, trim_db=0.5),
_recorded('jaguar_ftype', 'Jaguar F-Type', cylinders=8, idle=721.0, redline=6500.0, pops=1.0, burble=True, trim_db=3.6),
_recorded('lfa', 'LFA', cylinders=10, idle=1000.0, redline=9000.0, trim_db=-0.5),
# Recorded only at idle and from 3250 rpm up, so it is geared short and shifts like a race car to stay in that range.
_recorded('nissan_super_gt', 'Nissan Super GT1', cylinders=4, idle=1556.0, redline=7500.0, upshift=0.62, downshift=0.45,
gearing=1.2),
gearing=1.2, trim_db=-0.2),
_recorded('nissan_super_gt2', 'Nissan Super GT2', cylinders=4, idle=1556.0, redline=7500.0, upshift=0.62, downshift=0.45,
gearing=1.2),
_recorded('supra_turbo', 'Supra Turbo', cylinders=6, idle=800.0, redline=6000.0, turbo=1.0),
gearing=1.2, trim_db=1.8),
_recorded('supra_turbo', 'Supra Turbo', cylinders=6, idle=800.0, redline=6000.0, turbo=1.0, trim_db=-4.0),
)}
@@ -50,6 +50,7 @@ class Synth:
def render(self, rpm: float, load: float, frames: int, volume: float = 1.0) -> np.ndarray:
p = self.profile
volume *= 10 ** (p.trim_db / 20)
load = min(max(load, 0.0), 1.0)
ramp = np.arange(1, frames + 1, dtype=np.float64) / frames
frequency = self.frequency + (rpm * p.pitch - self.frequency) * ramp
@@ -1,7 +1,12 @@
from dataclasses import replace
import numpy as np
import pytest
from openpilot.starpilot.engine_sound.loops import BUNDLED_ROOT, LoopSynth, load_bank
from openpilot.starpilot.engine_sound.model import EngineModel
from openpilot.starpilot.engine_sound.profiles import PROFILES, RACE_GEARS, RACE_REDLINE
from openpilot.starpilot.engine_sound.synth import Synth
DT = 0.01
SUPER_GT_RECORDED_FROM = 3250.0
@@ -31,6 +36,17 @@ def test_super_gt2_plays_its_own_lower_bank():
assert (PROFILES['nissan_super_gt'].label, PROFILES['nissan_super_gt2'].label) == ('Nissan Super GT1', 'Nissan Super GT2')
@pytest.mark.parametrize('voice', ('loops', 'synth'))
def test_trim_scales_the_whole_voice(voice):
base = replace(PROFILES['supra_turbo'], trim_db=0.0)
def level(trim_db):
profile = replace(base, trim_db=trim_db)
synth = LoopSynth(profile, load_bank(BUNDLED_ROOT / profile.loops)) if voice == 'loops' else Synth(profile)
out = np.concatenate([synth.render(2000.0, 0.3, 480, 0.05) for _ in range(50)])[4800:]
return float(np.sqrt(np.mean(out ** 2)))
assert level(6.0) / level(0.0) == pytest.approx(10 ** (6 / 20), rel=0.02)
@pytest.mark.parametrize('name', SUPER_GTS)
def test_super_gt_light_pedal_stays_in_the_recorded_range_and_clear_of_redline(name):
p = PROFILES[name]
@@ -1,10 +1,6 @@
from openpilot.common.params import Params
from openpilot.starpilot.engine_sound import settings
from openpilot.starpilot.engine_sound.profiles import DEFAULT_PROFILE, PROFILES
from openpilot.starpilot.ui.engine_sound_notice import NOTICE_NS, EngineSoundNotice
from openpilot.starpilot.ui.onroad_state import AlertSize, OnroadAlert
NOW = 5_000_000_000
def test_toggle_flips_saved_state(tmp_path):
@@ -38,25 +34,3 @@ def test_next_profile_recovers_an_unknown_saved_car(tmp_path):
params = Params(str(tmp_path))
params.put('EngineSoundProfile', 'tractor', block=True)
assert settings.next_profile(params) == DEFAULT_PROFILE
def test_notice_shows_the_car_then_yields():
notice = EngineSoundNotice()
assert notice.update(OnroadAlert(), NOW) == OnroadAlert()
notice.show(False, True, 'jaguar_ftype', NOW)
alert = notice.update(OnroadAlert(), NOW + 1)
assert (alert.size, alert.text1, alert.text2) == (AlertSize.MID, 'Jaguar F-Type', 'Engine Sound')
native = OnroadAlert(AlertSize.FULL, 'TAKE CONTROL')
assert notice.update(native, NOW + 1) is native
assert notice.update(OnroadAlert(), NOW + NOTICE_NS) == OnroadAlert()
def test_notice_toggle_text():
notice = EngineSoundNotice()
notice.show(True, True, 'lfa', NOW)
assert (notice.update(OnroadAlert(), NOW).text1, notice.update(OnroadAlert(), NOW).text2) == ('Engine Sounds On', 'LFA')
notice.show(True, False, 'lfa', NOW)
alert = notice.update(OnroadAlert(), NOW)
assert (alert.size, alert.text1) == (AlertSize.SMALL, 'Engine Sounds Off')
notice.show(False, False, 'lfa', NOW)
assert notice.update(OnroadAlert(), NOW).text2 == 'Engine Sounds Off'
@@ -0,0 +1,27 @@
from openpilot.starpilot.ui.engine_sound_notice import NOTICE_NS, EngineSoundNotice
from openpilot.starpilot.ui.onroad_state import AlertSize, OnroadAlert
NOW = 5_000_000_000
def test_notice_shows_the_car_then_yields():
notice = EngineSoundNotice()
assert notice.update(OnroadAlert(), NOW) == OnroadAlert()
notice.show(False, True, 'jaguar_ftype', NOW)
alert = notice.update(OnroadAlert(), NOW + 1)
assert (alert.size, alert.text1, alert.text2) == (AlertSize.MID, 'Jaguar F-Type', 'Engine Sound')
native = OnroadAlert(AlertSize.FULL, 'TAKE CONTROL')
assert notice.update(native, NOW + 1) is native
assert notice.update(OnroadAlert(), NOW + NOTICE_NS) == OnroadAlert()
def test_notice_toggle_text():
notice = EngineSoundNotice()
notice.show(True, True, 'lfa', NOW)
alert = notice.update(OnroadAlert(), NOW)
assert (alert.text1, alert.text2) == ('Engine Sounds On', 'LFA')
notice.show(True, False, 'lfa', NOW)
alert = notice.update(OnroadAlert(), NOW)
assert (alert.size, alert.text1) == (AlertSize.SMALL, 'Engine Sounds Off')
notice.show(False, False, 'lfa', NOW)
assert notice.update(OnroadAlert(), NOW).text2 == 'Engine Sounds Off'
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "36e558c11f405fbffb09905de14c222921db79be5d9495124d8d0b1e53b2e775", "schema_version": 1}
{"protected_inventory_sha256": "7f7727fb2c0a8dba681ae9f930dcfa58e24baab04d8f0d40261ed31545fee454", "schema_version": 1}