forked from commaai/openpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔉 Set Sound Volume Based on Ambient Noise Level
This is a squash commit of 7 commits. soundd: change system sound mixer volume (commaai#26633) * test changing sound volume * create system/hardware/pc/hardware.h * soundd: use Hardware::set_volume * implement Hardware::set_volume using pactl * Revert "test changing sound volume" This reverts commit 4bbd870. * don't run command in background * pactl: use default sink micd: scale sound volume with ambient noise level (commaai#26399) * test changing sound volume * test changing sound volume * create system/hardware/pc/hardware.h * implement Hardware::set_volume using pactl * soundd: use Hardware::set_volume * add sounddevice dependency * sounddevice example * simple micd * cleanup * remove this * fix process config * add to release files * hardware: get sound input device * no more offroad * debug * calculate volume from all measurements since last update * use microphone noise level to update sound volume * fix scale * mute microphone during alerts * log raw noise level * hardware: reduce tici min volume * improve scale * add package * clear measurements on muted * change default to min volume and respond quicker * fixes Co-authored-by: Shane Smiskol <shane@smiskol.com> * logarithmic scaling * fix * respond quicker * fixes * tweak scaling * specify default device * Revert "hardware: get sound input device" This reverts commit 50f594f. * tuning * forgot to update submaster * tuning * don't mute microphone, and clip measurement * remove submaster * fixes * tuning * implement Hardware::set_volume using pactl * Revert "test changing sound volume" This reverts commit 4bbd870. * draft * draft * calculate sound pressure level in dB * fix setting * faster filter * start at initial value * don't run command in background * pactl: use default sink * use sound pressure db * tuning * bump up max volume threshold * update filter slower * fix divide by zero * bump cereal Co-authored-by: Shane Smiskol <shane@smiskol.com> micd: don't update filtered sound level if playing sound (commaai#26652) * add is_sound_playing to hardware.py * micd: don't update filtered sound level if playing sound micd: apply A-weighting to the sound pressure level (commaai#26668) * record * record * draft * some clean up * some clean up * wishful tuning * log pressure level (db) for debugging * fix * tuning * ignore complex to real warning * remove this * Update selfdrive/ui/soundd/sound.cc * Update system/micd.py * remove warning supp * bump cereal to master Co-authored-by: Cameron Clough <cameronjclough@gmail.com> micd: revert check playing sound (high cpu usage) (commaai#26672) * don't use hardware * check micd proc * use pactl package * cleanup * Revert "cleanup" This reverts commit baf9887. * Revert "use pactl package" This reverts commit 0c1f3a4. * Revert "micd: don't update filtered sound level if playing sound (commaai#26652)" This reverts commit 86cd919. * Revert "check micd proc" This reverts commit 9ebbe2a. Co-authored-by: Cameron Clough <cameronjclough@gmail.com> Micd: update sound levels in callback (commaai#26674) * update once reached 4096 * update once reached 4096 * reduce * debug & cmt * fix * fifo again * fix * clean that up * update filter on demand Co-authored-by: Cameron Clough <cameronjclough@gmail.com> Merge in Cereal Changes from Upstream Add Fields for Ambient Noise Level Detection This is a squash commit of 3 commits. add microphone (commaai#382) * add microphone socket * increase freq * add raw noise level * rename to ambient * switch Micd fields (commaai#391) * add new field * uncalib Co-authored-by: Shane Smiskol <shane@smiskol.com> Micd: add A-weighted sound level fields (commaai#392) * new fields * add temp field * Revert "add temp field" This reverts commit 54b597470f1da13246599502d91dd4e18f4aa11f. * move Co-authored-by: Cameron Clough <cameronjclough@gmail.com>
- Loading branch information
1 parent
e43a1ba
commit ab450f7
Showing
10 changed files
with
158 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include "system/hardware/base.h" | ||
|
||
class HardwarePC : public HardwareNone { | ||
public: | ||
static std::string get_os_version() { return "openpilot for PC"; } | ||
static std::string get_name() { return "pc"; }; | ||
static cereal::InitData::DeviceType get_device_type() { return cereal::InitData::DeviceType::PC; }; | ||
static bool PC() { return true; } | ||
static bool TICI() { return util::getenv("TICI", 0) == 1; } | ||
static bool AGNOS() { return util::getenv("TICI", 0) == 1; } | ||
|
||
static void set_volume(float volume) { | ||
volume = util::map_val(volume, 0.f, 1.f, MIN_VOLUME, MAX_VOLUME); | ||
|
||
char volume_str[6]; | ||
snprintf(volume_str, sizeof(volume_str), "%.3f", volume); | ||
std::system(("pactl set-sink-volume @DEFAULT_SINK@ " + std::string(volume_str)).c_str()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#!/usr/bin/env python3 | ||
import sounddevice as sd | ||
import numpy as np | ||
|
||
from cereal import messaging | ||
from common.filter_simple import FirstOrderFilter | ||
from common.realtime import Ratekeeper | ||
from system.swaglog import cloudlog | ||
|
||
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): | ||
# https://www.engineeringtoolbox.com/sound-pressure-d_711.html | ||
sound_pressure = np.sqrt(np.mean(measurements ** 2)) # RMS of amplitudes | ||
if sound_pressure > 0: | ||
sound_pressure_level = 20 * np.log10(sound_pressure / REFERENCE_SPL) # dB | ||
else: | ||
sound_pressure_level = 0 | ||
return sound_pressure, sound_pressure_level | ||
|
||
|
||
def apply_a_weighting(measurements: np.ndarray) -> np.ndarray: | ||
# Generate a Hanning window of the same length as the audio measurements | ||
hanning_window = np.hanning(len(measurements)) | ||
measurements_windowed = measurements * hanning_window | ||
|
||
# Calculate the frequency axis for the signal | ||
freqs = np.fft.fftfreq(measurements_windowed.size, d=1 / SAMPLE_RATE) | ||
|
||
# Calculate the A-weighting filter | ||
# https://en.wikipedia.org/wiki/A-weighting | ||
A = 12194 ** 2 * freqs ** 4 / ((freqs ** 2 + 20.6 ** 2) * (freqs ** 2 + 12194 ** 2) * np.sqrt((freqs ** 2 + 107.7 ** 2) * (freqs ** 2 + 737.9 ** 2))) | ||
A /= np.max(A) # Normalize the filter | ||
|
||
# Apply the A-weighting filter to the signal | ||
return np.abs(np.fft.ifft(np.fft.fft(measurements_windowed) * A)) | ||
|
||
|
||
class Mic: | ||
def __init__(self, pm): | ||
self.pm = pm | ||
self.rk = Ratekeeper(RATE) | ||
|
||
self.measurements = np.empty(0) | ||
|
||
self.sound_pressure = 0 | ||
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') | ||
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() | ||
|
||
def callback(self, indata, frames, time, status): | ||
""" | ||
Using amplitude measurements, calculate an uncalibrated sound pressure and sound pressure level. | ||
Then apply A-weighting to the raw amplitudes and run the same calculations again. | ||
Logged A-weighted equivalents are rough approximations of the human-perceived loudness. | ||
""" | ||
|
||
self.measurements = np.concatenate((self.measurements, indata[:, 0])) | ||
|
||
while self.measurements.size >= FFT_SAMPLES: | ||
measurements = self.measurements[:FFT_SAMPLES] | ||
|
||
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:] | ||
|
||
def micd_thread(self, device=None): | ||
if device is None: | ||
device = "sysdefault" | ||
|
||
with sd.InputStream(device=device, channels=1, samplerate=SAMPLE_RATE, callback=self.callback) as stream: | ||
cloudlog.info(f"micd stream started: {stream.samplerate=} {stream.channels=} {stream.dtype=} {stream.device=}") | ||
while True: | ||
self.update() | ||
|
||
|
||
def main(pm=None): | ||
if pm is None: | ||
pm = messaging.PubMaster(['microphone']) | ||
|
||
mic = Mic(pm) | ||
mic.micd_thread() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |