o
    Si                     @   s   d Z ddlZddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
mZ dZdd	 Zd
d Zdd Zdd Zdd ZedkrEe  dS dS )z#Command-line for audio compression.    N)Path   )compress
decompressMODELS)
save_audioconvert_audioz.ecdcc                  C   s   t jddd} | jdtdd | jdtdd	d
 | jddtdg ddd | jddddd | jddddd | jddddd | jddtddd  | jd!d"dd#d | S )$NencodeczHigh fidelity neural audio codec. If input is a .ecdc, decompresses it. If input is .wav, compresses it. If output is also wav, do a compression/decompression cycle.)descriptioninputz?Input file, whatever is supported by torchaudio on your system.)typehelpoutput?z0Output file, otherwise inferred from input file.)r   nargsr   z-bz--bandwidth   )g      ?g      @g      @g      (@g      8@zGTarget bandwidth (1.5, 3, 6, 12 or 24). 1.5 is not supported with --hq.)r   defaultchoicesr   z-qz--hq
store_truez6Use HQ stereo model operating on 48 kHz sampled audio.)actionr   z-lz--lmzAUse a language model to reduce the model size (5x slower though).z-fz--forcez#Overwrite output file if it exists.z-sz--decompress_suffix_decompressedzESuffix for the decompressed output file (if no output path specified))r   r   r   z-rz	--rescalez3Automatically rescale the output to avoid clipping.)argparseArgumentParseradd_argumentr   floatstr)parser r   D/home/ubuntu/.local/lib/python3.10/site-packages/encodec/__main__.py
get_parser   sJ   r   c                  G   s   t | dtji td d S )Nfiler   )printsysstderrexitargsr   r   r   fatal8   s   r'   c                 C   sL   | j j std| j  d | j  r"| js$td| j  d d S d S d S )NzOutput folder for  does not exist.zOutput file z& exist. Use -f / --force to overwrite.)r   parentexistsr'   forcer%   r   r   r   check_output_exists=   s
   r,   c                 C   sF   |j rd S |   }d}||kr!td| d| dtjd d S d S )NgGz?zClipping!! max scale z, limit is z?. To avoid clipping, use the `-r` option to rescale the output.)r    )rescaleabsmaxr!   r"   r#   )wavr&   mxlimitr   r   r   check_clippingD   s   
r3   c                  C   s  t   } | j std| j d | jj tkrZ| jd u r0| j	| jj
| j d| _n| jj dkr<td t|  t| j \}}t||  t|| j|| jd d S | jd u rg| jt| _n| jj tdfvrxtdt  t|  | jrdnd}t|  }| j|jvrtd	| j d
|  || j t| j\}}t|||j|j}t||| jd}| jj tkr| j| d S | jj dksJ t|\}}t||  t|| j|| jd d S )NzInput file r(   z.wavzOutput extension must be .wav)r-   z!Output extension must be .wav or encodec_48khzencodec_24khzz
Bandwidth z is not supported by the model )use_lm) r   
parse_argsr   r*   r'   suffixlowerSUFFIXr   	with_namestemdecompress_suffixwith_suffixr,   r   
read_bytesr3   r   r-   hqr   	bandwidthtarget_bandwidthsset_target_bandwidth
torchaudioloadr   sample_ratechannelsr   lmwrite_bytes)r&   outout_sample_rate
model_namemodelr0   sr
compressedr   r   r   mainP   s>   


 



rP   __main__)__doc__r   pathlibr   r"   rD   r   r   r   utilsr   r   r:   r   r'   r,   r3   rP   __name__r   r   r   r   <module>   s    "*
