Skip to content

Commit

Permalink
Add check for config.fail_on_decode_error
Browse files Browse the repository at this point in the history
  • Loading branch information
benfmiller committed Sep 22, 2024
1 parent 4da464c commit 7b70762
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
8 changes: 8 additions & 0 deletions audalign/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ def uniform_level_file(
width: float = 5,
overlap_ratio: float = 0.5,
exclude_min_db: float = -70,
config: BaseConfig = FingerprintConfig(),
) -> None:
"""
Levels the file using either of two methods: normalize or average.
Expand Down Expand Up @@ -838,6 +839,7 @@ def uniform_level_file(
width=width,
overlap_ratio=overlap_ratio,
exclude_min_db=exclude_min_db,
config=config,
)


Expand All @@ -851,6 +853,7 @@ def uniform_level_directory(
exclude_min_db: float = -70,
multiprocessing: bool = True,
num_processors: int = None,
config: BaseConfig = FingerprintConfig(),
) -> None:
"""
Levels the file using either of two methods: normalize or average.
Expand Down Expand Up @@ -886,6 +889,7 @@ def uniform_level_directory(
exclude_min_db=exclude_min_db,
use_multiprocessing=multiprocessing,
num_processes=num_processors,
config=config,
)


Expand All @@ -897,6 +901,7 @@ def remove_noise_file(
write_extension: str = None,
alt_noise_filepath: str = None,
prop_decrease: float = 1,
config: BaseConfig = FingerprintConfig(),
**kwargs,
):
"""Remove noise from audio file by specifying start and end seconds of representative sound sections. Writes file to destination
Expand All @@ -920,6 +925,7 @@ def remove_noise_file(
write_extension=write_extension,
alt_noise_filepath=alt_noise_filepath,
prop_decrease=prop_decrease,
config=config,
**kwargs,
)

Expand All @@ -934,6 +940,7 @@ def remove_noise_directory(
prop_decrease: float = 1,
multiprocessing: bool = True,
num_processors: int = None,
config: BaseConfig = FingerprintConfig(),
**kwargs,
):
"""Remove noise from audio files in directory by specifying start and end seconds of
Expand Down Expand Up @@ -962,6 +969,7 @@ def remove_noise_directory(
prop_decrease=prop_decrease,
use_multiprocessing=multiprocessing,
num_processes=num_processors,
config=config,
**kwargs,
)

Expand Down
11 changes: 10 additions & 1 deletion audalign/recognizers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import audalign.filehandler as filehandler
from audalign.config import BaseConfig
from pydub.exceptions import CouldntDecodeError


class BaseRecognizer(ABC):
Expand Down Expand Up @@ -47,10 +48,18 @@ def align_get_file_names(
file_names = [os.path.basename(x) for x in file_list]
elif file_dir:
file_names = filehandler.get_audio_files_directory(
file_dir, False, self.config.can_read_extensions, self.config.cant_read_extensions)
file_dir,
False,
self.config.can_read_extensions,
self.config.cant_read_extensions,
)
elif fine_aud_file_dict:
if fine_aud_file_dict == None or len(fine_aud_file_dict.keys()) == 0:
raise CouldntDecodeError("No files found", fine_aud_file_dict)
file_names = [os.path.basename(x) for x in fine_aud_file_dict.keys()]
else:
if file_list == None or len(file_list) == 0:
raise CouldntDecodeError("No files found", file_list)
file_names = [os.path.basename(x) for x in file_list]
return file_names

Expand Down
4 changes: 3 additions & 1 deletion audalign/recognizers/correcognize/correcognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ def _correcognize_dir(
**kwargs,
)

except CouldntDecodeError:
except CouldntDecodeError as e:
print(f'File "{against_file_path}" could not be decoded')
if config.fail_on_decode_error:
raise e
return {}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ def _correcognize_dir(
**kwargs,
)

except CouldntDecodeError:
except CouldntDecodeError as e:
print(f'File "{against_file_path}" could not be decoded')
if config.fail_on_decode_error:
raise e
return {}


Expand Down
3 changes: 3 additions & 0 deletions audalign/recognizers/fingerprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pickle
import json
import typing
from pydub.exceptions import CouldntDecodeError


class FingerprintRecognizer(BaseRecognizer):
Expand Down Expand Up @@ -266,6 +267,8 @@ def _fingerprint_directory(
print("All files in directory already fingerprinted")
else:
print("Directory contains 0 files or could not be found")
if self.config.fail_on_decode_error:
raise CouldntDecodeError("Directory contains 0 files or could not be found")
return

if _file_audsegs is not None:
Expand Down
10 changes: 6 additions & 4 deletions audalign/recognizers/fingerprint/fingerprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ def _fingerprint_worker(
except FileNotFoundError:
print(f'"{file_path}" not found')
return None, None
except (
CouldntDecodeError,
IndexError,
): # Pydub throws IndexErrors for some files on Ubuntu (json, txt, others?)
except CouldntDecodeError as e:
print(f'File "{file_name}" could not be decoded')
if config.fail_on_decode_error:
raise e
return None, None
except IndexError: # Pydub throws IndexErrors for some files on Ubuntu (json, txt, others?)
print(f'File "{file_name}" could not be decoded')
return None, None
elif type(file_path) == tuple:
Expand Down
4 changes: 3 additions & 1 deletion audalign/recognizers/visrecognize/visrecognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ def _visrecognize_directory(
imgB_title=os.path.basename(file_path),
)
return single_file_match
except CouldntDecodeError:
except CouldntDecodeError as e:
print(f'File "{file_path}" could not be decoded')
if config.fail_on_decode_error:
raise e
return {}


Expand Down

0 comments on commit 7b70762

Please sign in to comment.