Skip to content

Commit

Permalink
bumped to version 1.3
Browse files Browse the repository at this point in the history
changed defaults for matching threshold, alternations, tone_a_min and tone_b min

refactored frequency_extraction.py to get even more precise time for tones.
  • Loading branch information
TheGreatCodeholio committed May 15, 2024
1 parent a60051b commit 66368a5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "icad_tone_detection"
version = "1.2"
version = "1.3"
authors = [
{name = "TheGreatCodeholio", email = "[email protected]"},
]
Expand Down
3 changes: 2 additions & 1 deletion src/icad_tone_detection/audio_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def load_audio(audio_input):
audio = audio.set_channels(1) # Ensure the audio is mono
audio = audio.set_frame_rate(22050) # Set the frame rate to 22050 Hz
samples = np.array(audio.get_array_of_samples()).astype(np.float32)
samples /= np.iinfo(audio.sample_width * 8).max # Adjusted for correct normalization
max_val = float(2 ** (audio.sample_width * 8 - 1))
samples /= max_val
except Exception as e:
raise RuntimeError(f"Error processing audio: {e}")

Expand Down
24 changes: 12 additions & 12 deletions src/icad_tone_detection/frequency_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def calculate_times(self, start_index, end_index, time_samples):
Calculates accurate start and end times for frequency matches.
"""
start_time = round(time_samples[start_index], 3)
end_time = round(time_samples[end_index - 1], 3)
end_time = round(time_samples[end_index], 3) # Use end_index directly
return start_time, end_time

@staticmethod
Expand All @@ -99,15 +99,15 @@ def amplitude_to_decibels(amplitude, reference_value):

def match_frequencies(self, detected_frequencies, time_samples):
"""
Identifies and groups matching frequencies from a list of detected frequencies based on the matching threshold.
Each group's start time, end time, and the matching frequencies are returned.
Identifies and groups matching frequencies from a list of detected frequencies based on the matching threshold.
Each group's start time, end time, and the matching frequencies are returned.
Parameters:
detected_frequencies (list of float): The detected frequencies from the audio sample.
time_samples (np.array): Array of times corresponding to each frequency sample.
Parameters:
detected_frequencies (list of float): The detected frequencies from the audio sample.
time_samples (np.array): Array of times corresponding to each frequency sample.
Returns:
list of tuples: Each tuple contains the start time, end time, and a list of matching frequencies.
Returns:
list of tuples: Each tuple contains the start time, end time, and a list of matching frequencies.
"""

if not detected_frequencies:
Expand All @@ -125,15 +125,15 @@ def match_frequencies(self, detected_frequencies, time_samples):
current_match.append(frequencies[i])
else:
if len(current_match) >= 2:
start_time, end_time = self.calculate_times(start_index, i, time_samples)
freq_length = round((end_time - start_time) + .1, 2)
start_time, end_time = self.calculate_times(start_index, i - 1, time_samples)
freq_length = round(end_time - start_time, 3)
matching_frequencies.append((start_time, end_time, freq_length, current_match))
current_match = [frequencies[i]]
start_index = i

if len(current_match) >= 2:
start_time, end_time = self.calculate_times(start_index, len(frequencies), time_samples)
freq_length = round((end_time - start_time) + .1, 2)
start_time, end_time = self.calculate_times(start_index, len(frequencies) - 1, time_samples)
freq_length = round(end_time - start_time, 3)
matching_frequencies.append((start_time, end_time, freq_length, current_match))

return matching_frequencies
Expand Down
10 changes: 5 additions & 5 deletions src/icad_tone_detection/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ def __init__(self, two_tone_result, long_result, hi_low_result):
self.hi_low_result = hi_low_result


def tone_detect(audio_path, matching_threshold=2, time_resolution_ms=100, tone_a_min_length=0.8, tone_b_min_length=2.8, hi_low_interval=0.2,
hi_low_min_alternations=4, long_tone_min_length=3.8, debug=False):
def tone_detect(audio_path, matching_threshold=2.5, time_resolution_ms=25, tone_a_min_length=0.7, tone_b_min_length=2.7, hi_low_interval=0.2,
hi_low_min_alternations=6, long_tone_min_length=3.8, debug=False):
"""
Loads audio from various sources including local path, URL, BytesIO object, or a PyDub AudioSegment.
Parameters:
- audio_input: Can be a string (path or URL), bytes like object, or AudioSegment.
- matching_threshold (float): The percentage threshold used to determine if two frequencies
are considered a match. For example, a threshold of 2 means that two frequencies are considered matching
if they are within 2% of each other.
- time_resolution_ms (int): The time resolution in milliseconds for the STFT. Default is 100ms.
if they are within x% of each other. Default 2.5%
- time_resolution_ms (int): The time resolution in milliseconds for the STFT. Default is 25ms.
- tone_a_min_length (float): The minimum length in seconds of an A tone for two tone detections. Default 0.8 Seconds
- tone_b_min_length (float): The minimum length in seconds of a B tone for two tone detections. Default 2.8 Seconds
- long_tone_min_length (float): The minimum length a long tone needs to be to consider it a match. Default 3.8 Seconds
- hi_low_interval (float): The maximum allowed interval in seconds between two consecutive alternating tones. Default is 0.2 Seconds
- hi_low_min_alternations (int): The minimum number of alternations for a hi-low warble tone sequence to be considered valid. Default 4
- hi_low_min_alternations (int): The minimum number of alternations for a hi-low warble tone sequence to be considered valid. Default 6
- debug (bool): If debug is enabled, print all tones found in audio file. Default is False
Returns:
Expand Down

0 comments on commit 66368a5

Please sign in to comment.