This commit is contained in:
CroneKorkN 2025-06-01 15:52:48 +02:00
parent 5d1ed2d422
commit 008789755d
Signed by: cronekorkn
SSH key fingerprint: SHA256:v0410ZKfuO1QHdgKBsdQNF64xmTxOF8osF1LIqwTcVw

56
process
View file

@ -43,15 +43,18 @@ def process_recording(filename):
print(f'median amplitude: {median_amplitude:.5f}rDB') print(f'median amplitude: {median_amplitude:.5f}rDB')
# initialize to a very old date # initialize to a very old date
last_detection_at = datetime.datetime.min.replace(tzinfo=recording_date.tzinfo)
is_detecting = False current_event = None
# read blocks of audio data with overlap from sound variable # read blocks of audio data with overlap from sound variable
for block_num in range(0, len(sound) // (samples_per_block - overlapping_samples)): block_num = 0
while block_num < (len(sound) // (samples_per_block - overlapping_samples)):
# get block of audio data
start_sample = block_num * (samples_per_block - overlapping_samples) start_sample = block_num * (samples_per_block - overlapping_samples)
end_sample = start_sample + samples_per_block end_sample = start_sample + samples_per_block
block = sound[start_sample:end_sample] block = sound[start_sample:end_sample]
# get block date and calculate FFT
block_date = recording_date + datetime.timedelta(seconds=block_num * BLOCK_SECONDS) block_date = recording_date + datetime.timedelta(seconds=block_num * BLOCK_SECONDS)
labels = rfftfreq(len(block), d=1/samplerate) labels = rfftfreq(len(block), d=1/samplerate)
complex_amplitudes = rfft(block) complex_amplitudes = rfft(block)
@ -74,24 +77,49 @@ def process_recording(filename):
max_freq_detected = DETECT_FREQUENCY_FROM <= max_freq <= DETECT_FREQUENCY_TO max_freq_detected = DETECT_FREQUENCY_FROM <= max_freq <= DETECT_FREQUENCY_TO
amplitude_detected = max_amplitude > AMPLITUDE_THRESHOLD amplitude_detected = max_amplitude > AMPLITUDE_THRESHOLD
low_noise_detected = noise < 0.1 low_noise_detected = noise < 0.1
no_recent_detection = is_detecting or (block_date - last_detection_at).total_seconds() > DETECTION_DISTANCE
# conclude detection # conclude detection
if ( if (
max_freq_detected and max_freq_detected and
amplitude_detected and amplitude_detected and
low_noise_detected and low_noise_detected
no_recent_detection
): ):
if not is_detecting: # detecting an event
is_detecting = True if not current_event:
last_detection_at = block_date current_event = {
print("🔊") 'start_at': block_date,
'end_at': block_date,
print(f'{block_date}: {max_amplitude:.1f}rDB @ {max_freq:.1f}Hz (noise {noise:.3f}rDB)') 'start_freq': max_freq,
'end_freq': max_freq,
'max_amplitude': max_amplitude,
}
else:
current_event.update({
'end_at': block_date,
'end_freq': max_freq,
'max_amplitude': max(max_amplitude, current_event['max_amplitude']),
})
#print(f'{block_date}: {max_amplitude:.1f}rDB @ {max_freq:.1f}Hz (noise {noise:.3f}rDB)')
else: else:
if is_detecting: # not detecting an event
is_detecting = False if current_event:
# conclude current event
print(f'🔊 {current_event["start_at"]}: {current_event["start_freq"]:.1f}Hz -> {current_event["end_freq"]:.1f}Hz @{current_event["max_amplitude"]:.0f}rDB')
current_event = None
write_clip()
write_plot()
block_num += DETECTION_DISTANCE // BLOCK_SECONDS
block_num += 1
def write_clip():
pass
def write_plot():
pass
def main(): def main():