Add fft_analysis/src/fft_analysis/cli.py
This commit is contained in:
parent
cc96cd886d
commit
de7214408c
1 changed files with 67 additions and 0 deletions
67
fft_analysis/src/fft_analysis/cli.py
Normal file
67
fft_analysis/src/fft_analysis/cli.py
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import argparse
|
||||
import sys
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import NoReturn
|
||||
|
||||
import numpy as np
|
||||
|
||||
from fft_analysis.io_utils import load_audio_file, save_spectrum_to_json
|
||||
from fft_analysis.core import perform_fft
|
||||
|
||||
|
||||
def main() -> NoReturn:
|
||||
"""Command-line interface for running FFT analysis on an audio file."""
|
||||
parser = argparse.ArgumentParser(
|
||||
description=(
|
||||
"Performs FFT analysis on an input audio file and saves the result as a JSON spectrum."
|
||||
)
|
||||
)
|
||||
parser.add_argument(
|
||||
"--input",
|
||||
required=True,
|
||||
help="Pfad zur Eingabe-Audiodatei (WAV/FLAC)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
required=True,
|
||||
help="Pfad zur Ausgabe-Datei mit dem FFT-Spektrum im JSON-Format",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Setup logging for CLI execution
|
||||
logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
input_path = Path(args.input)
|
||||
output_path = Path(args.output)
|
||||
|
||||
if not input_path.exists() or not input_path.is_file():
|
||||
logger.error(f"Input file not found: {input_path}")
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
logger.info(f"Loading audio file: {input_path}")
|
||||
audio_data, _ = load_audio_file(str(input_path))
|
||||
|
||||
if not isinstance(audio_data, np.ndarray):
|
||||
raise TypeError("Loaded audio data is not a numpy.ndarray")
|
||||
|
||||
logger.info("Performing FFT analysis...")
|
||||
spectrum = perform_fft(audio_data)
|
||||
|
||||
if not isinstance(spectrum, np.ndarray):
|
||||
raise TypeError("FFT result is not a numpy.ndarray")
|
||||
|
||||
logger.info(f"Saving FFT spectrum to: {output_path}")
|
||||
save_spectrum_to_json(spectrum, str(output_path))
|
||||
logger.info("FFT analysis completed successfully.")
|
||||
|
||||
except Exception as exc:
|
||||
logger.exception(f"An error occurred during FFT processing: {exc}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in a new issue