Overview
Theutils.py module provides essential utility functions for file operations, audio processing, outlier detection, and logging.
Functions
ffmpeg_exists
Checks if FFmpeg is available in the system PATH.Returns:
bool - True if FFmpeg is available, False otherwiseExample:detect_outliers
Finds pairwise estimates that disagree strongly with the optimized solution.Parameters:
pairwise(Dict[Tuple[str, str], Tuple[float, float]]): Dictionary mapping file pairs to (offset, confidence) tuplesoptimized(Dict[str, float]): Dictionary mapping filenames to optimized offsetsthreshold(float): Flag pairs with error > this many seconds (default: 0.5)
List[Tuple[str, str, float, float, float]] - List of outlier tuples containing:- fileA (str): First file name
- fileB (str): Second file name
- measured_offset (float): Measured offset in seconds
- expected_offset (float): Expected offset based on optimization
- error (float): Absolute difference between measured and expected
ensure_dir
Creates a directory if it doesn’t exist.Parameters:
path(str): Directory path to create
setup_logger
Returns a logger with the given name. Configuration is handled centrally in ui.py/main.py.Parameters:
name(str): Logger name (default: “sync”)level(int): Logging level (default: logging.INFO)
logging.Logger - Configured logger instanceExample:log_execution_time
Context manager to log the duration of an operation.Parameters:
logger(logging.Logger): Logger instance to useoperation_name(str): Name of the operation being timed
next_pow2
Calculates the next power of 2 greater than or equal to n.Parameters:
n(int): Input number
int - Next power of 2Example:load_audio
Loads and normalizes audio from a WAV file.Parameters:
path(str): Path to the WAV file
Tuple[np.ndarray, int] - Tuple containing:data(np.ndarray): Normalized audio data as float32 array (mono)sr(int): Sample rate in Hz
- Converts int16 data to float32 by dividing by 32768.0
- Converts int32 data to float32 by dividing by 2^31
- Converts stereo to mono by averaging channels
Logger
Module-level logger instance for the utils module.