Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micd: update sound levels in callback #26674

Merged
merged 10 commits into from
Dec 3, 2022
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions system/micd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

RATE = 10
DT_MIC = 1. / RATE
FFT_SAMPLES = 4096
REFERENCE_SPL = 2e-5 # newtons/m^2
SAMPLE_RATE = 44100

Expand Down Expand Up @@ -45,41 +46,46 @@ def __init__(self, pm):
self.rk = Ratekeeper(RATE)

self.measurements = np.empty(0)
self.spl_filter_weighted = FirstOrderFilter(0, 2.5, DT_MIC, initialized=False)

def update(self):
"""
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.
self.sound_pressure = 0
self.sound_pressure_weighted = 0
self.sound_pressure_level_weighted = 0

Logged A-weighted equivalents are rough approximations of the human-perceived loudness.
"""

if len(self.measurements) > 0:
sound_pressure, _ = calculate_spl(self.measurements)
measurements_weighted = apply_a_weighting(self.measurements)
sound_pressure_weighted, sound_pressure_level_weighted = calculate_spl(measurements_weighted)
self.spl_filter_weighted.update(sound_pressure_level_weighted)
else:
sound_pressure = 0
sound_pressure_weighted = 0
sound_pressure_level_weighted = 0
self.spl_filter_weighted = FirstOrderFilter(0, 2.5, DT_MIC, initialized=False)

self.measurements = np.empty(0)
def update(self):
if self.sound_pressure_level_weighted != 0:
sshane marked this conversation as resolved.
Show resolved Hide resolved
self.spl_filter_weighted.update(self.sound_pressure_level_weighted)

msg = messaging.new_message('microphone')
msg.microphone.soundPressure = float(sound_pressure)
msg.microphone.soundPressureWeighted = float(sound_pressure_weighted)
msg.microphone.soundPressure = float(self.sound_pressure)
msg.microphone.soundPressureWeighted = float(self.sound_pressure_weighted)

msg.microphone.soundPressureWeightedDb = float(sound_pressure_level_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.measurements = self.measurements[FFT_SAMPLES:]

def micd_thread(self, device=None):
if device is None:
device = "sysdefault"
Expand Down