This commit is contained in:
CroneKorkN 2025-06-01 13:42:55 +02:00
parent ba83fe23c9
commit 67646a689d
Signed by: cronekorkn
SSH key fingerprint: SHA256:v0410ZKfuO1QHdgKBsdQNF64xmTxOF8osF1LIqwTcVw

22
process
View file

@ -20,6 +20,7 @@ DETECT_FREQUENCY_TO = DETECT_FREQUENCY + DETECT_FREQUENCY_TOLERANCE # Hz
ADJACENCY_FACTOR = 2 # area to look for noise around the target frequency
AMPLITUDE_THRESHOLD = 200 # relative DB (rDB) (because not calibrated)
BLOCK_SECONDS = 3 # seconds (longer means more frequency resolution, but less time resolution)
DETECTION_DISTANCE = 30 # seconds (minimum time between detections)
def process_recording(filename):
@ -35,6 +36,10 @@ def process_recording(filename):
samplerate = info.samplerate
block_samples = int(BLOCK_SECONDS * samplerate)
# initialize to a very old date
last_detection_at = datetime.datetime.min.replace(tzinfo=recording_date.tzinfo)
is_detecting = False
# iterate blocks
for num, block in enumerate(soundfile.blocks(path, blocksize=block_samples, overlap=int(block_samples*0.8))):
block_date = recording_date + datetime.timedelta(seconds=num * BLOCK_SECONDS)
@ -58,12 +63,23 @@ def process_recording(filename):
max_freq_detected = DETECT_FREQUENCY_FROM <= max_freq <= DETECT_FREQUENCY_TO
amplitude_detected = max_amplitude > AMPLITUDE_THRESHOLD
low_noise_detected = noise < 0.1
no_recent_detection = is_detecting or (block_date - last_detection_at).total_seconds() > DETECTION_DISTANCE
# conclude detection
if max_freq_detected and amplitude_detected and low_noise_detected:
if (
max_freq_detected and
amplitude_detected and
low_noise_detected and
no_recent_detection
):
if not is_detecting:
is_detecting = True
last_detection_at = block_date
print("🔊")
print(f'{block_date}: {max_amplitude:.1f}rDB @ {max_freq:.1f}Hz ({noise:.3f}rDB noise)')
else:
is_detecting = False