Audio dbfs support

This commit is contained in:
Mashintime 2023-11-28 17:46:55 -05:00
parent bf31ca5891
commit f044b64ace
3 changed files with 10 additions and 6 deletions

View file

@ -34,7 +34,10 @@ export function stateDispatcher(data) {
stateStore.channel_busy = data["channel_busy"];
stateStore.is_codec2_traffic = data["is_codec2_traffic"];
stateStore.is_modem_running = data["is_modem_running"];
stateStore.dbfs_level = data["audio_dbfs"];
stateStore.dbfs_level = Math.round(data["audio_dbfs"]);
stateStore.dbfs_level_percent = Math.round(
Math.pow(10, data["audio_dbfs"] / 20) * 100,
);
stateStore.channel_busy_slot = data["channel_busy_slot"];
stateStore.beacon_state = data["is_beacon_running"];
stateStore.radio_status = data["radio_status"];

View file

@ -115,6 +115,7 @@ class RF:
# https://github.com/DJ2LS/FreeDATA/issues/127
# https://github.com/DJ2LS/FreeDATA/issues/99
self.mod_out_locked = True
self.rms_counter = 0
# Make sure our resampler will work
assert (self.AUDIO_SAMPLE_RATE_RX / self.MODEM_SAMPLE_RATE) == codec2.api.FDMDV_OS_48 # type: ignore
@ -1330,7 +1331,7 @@ class RF:
whether the channel is "busy."
"""
# Initialize dbfs counter
rms_counter = 0
# rms_counter = 0
# https://gist.github.com/ZWMiller/53232427efc5088007cab6feee7c6e4c
# Fast Fourier Transform, 10*log10(abs) is to scale it to dB
@ -1357,8 +1358,8 @@ class RF:
# Calculate audio dbfs
# https://stackoverflow.com/a/9763652
# calculate dbfs every 50 cycles for reducing CPU load
rms_counter += 1
if rms_counter > 50:
self.rms_counter += 1
if self.rms_counter > 5:
d = np.frombuffer(data, np.int16).astype(np.float32)
# calculate RMS and then dBFS
# https://dsp.stackexchange.com/questions/8785/how-to-compute-dbfs
@ -1372,8 +1373,7 @@ class RF:
except Exception as e:
self.states.set("audio_dbfs", -100)
rms_counter = 0
self.rms_counter = 0
# Convert data to int to decrease size
dfft = dfft.astype(int)

View file

@ -91,6 +91,7 @@ class StateManager:
"radio_frequency": self.radio_frequency,
"radio_mode": self.radio_mode,
"channel_busy_slot": self.channel_busy_slot,
"audio_dbfs": self.audio_dbfs,
}
# .wait() blocks until the event is set