o
    Vi                     @   s   d Z ddlZddlZddlZddlZddlZddlZddl	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZmZmZmZ dde
ee  d	ejfd
dZdde
ee  d	efddZedkriee  dS dS )z0
Command line interface for Perth watermarking.
    N)OptionalList)PerthImplicitWatermarker)DummyWatermarker)
get_config)
load_audio
save_audiocalculate_audio_metricsplot_audio_comparisonargsreturnc                 C   s   t jdt jd}|jddd |jdddd |jd	d
ddgdd |jddddd |jddddgdd |jdddd |jddddd || S )zParse command line arguments.zPerth - Audio Watermarking Tool)descriptionformatter_class
input_filezPath to the input audio file)helpz--outputz-ozmPath to save the output watermarked audio file. If not provided, appends '_watermarked' to the input filenamez--methodz-mperthdummyzWatermarking method to use)choicesr   z	--extractz-e
store_truezEExtract watermark from the input file instead of applying a watermark)actionr   z--devicez-dcpucudaz+Device to use for neural network processingz--configz-czPath to a configuration filez--visualizez-vzEGenerate visualization of watermark effect (only when not extracting))argparseArgumentParserArgumentDefaultsHelpFormatteradd_argument
parse_args)r   parser r   K/home/ubuntu/.local/lib/python3.10/site-packages/perth/cli/watermark_cli.pyr      s0   


r   c              
   C   sj  t | }t|j}|jr|dd|j |jr|dd|j |dd}|dd}ztd|j  t	|j\}}|dkr[td| d |dd}|dd	}t
|||d
}	ntd t }	|jrtd |	j||d}
td|
  tdt|
d W dS td | }|	j|d|d}|jr|j}ntj|j\}}| d| }t||| td|  td |	j||d}tdt|d t||}td td|d dd td|d d  td!|d" dd |jrtj|d d# }td$|  t|||| W dS  ty4 } ztd%| tjd& W Y d}~d'S d}~ww )(z'Main function for the watermarking CLI.generaldefault_watermarkerr   devicezLoading audio file: z(Initializing Perth watermarker (device: z)...
models_dirrun_name)r$   r#   r"   z!Initializing dummy watermarker...zExtracting watermark...)sample_ratezExtracted watermark: zWatermark confidence: z.4fr   zApplying watermark...N)	watermarkr%   _watermarkedzWatermarked audio saved to: zVerifying watermark...z#Watermark verification confidence: z
Audio Quality Metrics:z  Signal-to-Noise Ratio (SNR): snrz.2fz dBz  Mean Squared Error (MSE): msez.8fz%  Peak Signal-to-Noise Ratio (PSNR): psnrz_comparison.pngz
Generating visualization to: zError: )file   )r   r   configmethodsetr"   getprintr   r   r   r   extractget_watermarknpmeancopyapply_watermarkoutputospathsplitextr   r	   	visualizer
   	Exceptionsysstderr)r   parsed_argsr-   r.   r"   wavsrr#   r$   watermarkerr&   original_audiowatermarked_audiooutput_pathbaseext	extractedmetricsviz_pather   r   r   main,   sn   

rM   __main__)N)__doc__r   r9   r>   numpyr4   librosa	soundfilesftypingr   r   4perth.perth_net.perth_net_implicit.perth_watermarkerr   perth.dummy_watermarkerr   perth.configr   perth.utilsr   r   r	   r
   str	Namespacer   intrM   __name__exitr   r   r   r   <module>   s"   S