diff --git a/process b/process index e9f9fc6..8ca8564 100755 --- a/process +++ b/process @@ -43,15 +43,18 @@ def process_recording(filename): print(f'median amplitude: {median_amplitude:.5f}rDB') # 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 - 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) end_sample = start_sample + samples_per_block block = sound[start_sample:end_sample] + # get block date and calculate FFT block_date = recording_date + datetime.timedelta(seconds=block_num * BLOCK_SECONDS) labels = rfftfreq(len(block), d=1/samplerate) complex_amplitudes = rfft(block) @@ -74,24 +77,49 @@ 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 and - no_recent_detection + low_noise_detected ): - 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 {noise:.3f}rDB)') + # detecting an event + if not current_event: + current_event = { + 'start_at': block_date, + 'end_at': block_date, + '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: - if is_detecting: - is_detecting = False + # not detecting an event + 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():