From db3fbdae18d7fc8354930d58c56b93a495ff146a Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 6 Dec 2023 14:59:16 -0800 Subject: [PATCH] Soundd: only update ambient db when not playing an alert (#30620) * move to soundd + only when quiet * not filtered old-commit-hash: 5600a8288909e0b6ca95becbdb5746a0a9e35af4 --- selfdrive/ui/soundd.py | 14 +++++++++++--- system/micd.py | 6 ------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/selfdrive/ui/soundd.py b/selfdrive/ui/soundd.py index dac4bb74d8..a3dc6fedfe 100644 --- a/selfdrive/ui/soundd.py +++ b/selfdrive/ui/soundd.py @@ -1,12 +1,16 @@ import math -import time import numpy as np +import time import wave from typing import Dict, Optional, Tuple from cereal import car, messaging from openpilot.common.basedir import BASEDIR +from openpilot.common.filter_simple import FirstOrderFilter + +from openpilot.system import micd + from openpilot.common.realtime import Ratekeeper from openpilot.system.hardware import PC from openpilot.system.swaglog import cloudlog @@ -15,6 +19,7 @@ SAMPLE_RATE = 48000 MAX_VOLUME = 1.0 MIN_VOLUME = 0.1 CONTROLS_TIMEOUT = 5 # 5 seconds +FILTER_DT = 1. / (micd.SAMPLE_RATE / micd.FFT_SAMPLES) AMBIENT_DB = 30 # DB where MIN_VOLUME is applied DB_SCALE = 30 # AMBIENT_DB + DB_SCALE is where MAX_VOLUME is applied @@ -56,6 +61,8 @@ class Soundd: self.controls_timeout_alert = False + self.spl_filter_weighted = FirstOrderFilter(0, 2.5, FILTER_DT, initialized=False) + def load_sounds(self): self.loaded_sounds: Dict[int, np.ndarray] = {} @@ -137,8 +144,9 @@ class Soundd: while True: sm.update(0) - if sm.updated['microphone']: - self.current_volume = self.calculate_volume(sm["microphone"].filteredSoundPressureWeightedDb) + if sm.updated['microphone'] and self.current_alert == AudibleAlert.none: # only update volume filter when not playing alert + self.spl_filter_weighted.update(sm["microphone"].soundPressureWeightedDb) + self.current_volume = self.calculate_volume(float(self.spl_filter_weighted.x)) self.get_audible_alert(sm) diff --git a/system/micd.py b/system/micd.py index 0b7a4dadfb..452c04bc03 100755 --- a/system/micd.py +++ b/system/micd.py @@ -2,7 +2,6 @@ import numpy as np from cereal import messaging -from openpilot.common.filter_simple import FirstOrderFilter from openpilot.common.realtime import Ratekeeper from openpilot.system.swaglog import cloudlog @@ -10,7 +9,6 @@ RATE = 10 FFT_SAMPLES = 4096 REFERENCE_SPL = 2e-5 # newtons/m^2 SAMPLE_RATE = 44100 -FILTER_DT = 1. / (SAMPLE_RATE / FFT_SAMPLES) def calculate_spl(measurements): @@ -50,15 +48,12 @@ class Mic: self.sound_pressure_weighted = 0 self.sound_pressure_level_weighted = 0 - self.spl_filter_weighted = FirstOrderFilter(0, 2.5, FILTER_DT, initialized=False) - def update(self): msg = messaging.new_message('microphone', valid=True) msg.microphone.soundPressure = float(self.sound_pressure) msg.microphone.soundPressureWeighted = float(self.sound_pressure_weighted) msg.microphone.soundPressureWeightedDb = float(self.sound_pressure_level_weighted) - msg.microphone.filteredSoundPressureWeightedDb = float(self.spl_filter_weighted.x) self.pm.send('microphone', msg) self.rk.keep_time() @@ -79,7 +74,6 @@ class Mic: self.sound_pressure, _ = calculate_spl(measurements) measurements_weighted = apply_a_weighting(measurements) self.sound_pressure_weighted, self.sound_pressure_level_weighted = calculate_spl(measurements_weighted) - self.spl_filter_weighted.update(self.sound_pressure_level_weighted) self.measurements = self.measurements[FFT_SAMPLES:]