o
    0i:                     @  s   d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZ eeZdd	 Zd
d Zdd Zdd Zdd Zdd ZdddZdd Zedkr[e  dS dS )zCommand-line interface for Supertonic TTS.

This module provides a command-line interface for easy text-to-speech
synthesis, batch processing, and model management.
    )annotationsN   )__version__)AVAILABLE_MODELSDEFAULT_MODEL)TTSc              
   C  s  | j rtjtjdd ntjtjdd zddl}W n ty4   td td td t	d Y nw | j rEtd	| j
dd
  d ztd| j d t }t| jd}t | }td|dd | j rtd t }|jj| j
\}}|jj| j
}t | }	td|	dd td| j
dd  t| j
dkrdnd  |std|dd   || j
krtd|dd  t|dkrdnd  td| jp| j d t }
| jr|| j}n|| j}t |
 }td|dd td| j d t }|j| j
|| j| j| j| j| j| j d\}}t | }td|dd td |d dd! || |j |  td" W dS  t y{ } ztd#|  | j rkt!"d$ t	d W Y d}~dS d}~ww )%z;Generate speech and play it directly without saving a file.%(levelname)s: %(message)slevelformatr   Nu9   ❌ Error: sounddevice is required for the 'say' command.z4   Install it with: pip install supertonic[playback]z   Or: pip install sounddevicer      🎤 Generating speech: 2   ...Loading model ()...model   -> Model loaded in .2fsProcessing text...   -> Text processed in .3f   Original: P          ⚠️  Unsupported chars: 
      Preprocessed: Loading voice style (   -> Voice style loaded in Generating speech (lang=voice_styleZtotal_stepsspeedmax_chunk_lengthsilence_durationlangverbose   -> Speech generated in zPlaying z
s audio...z   -> Audio played   ❌ Error: z#TTS playback failed with exception:)#r(   loggingbasicConfigINFOWARNINGZsounddeviceImportErrorprintsysexittextr   timer   text_processorvalidate_text_preprocess_textlencustom_style_pathvoiceget_voice_style_from_pathget_voice_styler'   
synthesizestepsr$   r%   r&   ZplayZsqueezesample_ratewait	Exceptionlogger	exception)argssd
load_starttts	load_time
text_startis_validunsupportedpreprocessed	text_timestyle_startr#   
style_time
start_timewavdurationelapsed_timee rU   H/home/kim/smarthome/.venv/lib/python3.10/site-packages/supertonic/cli.pycmd_say   s   .
$


rW   c              
   C  s  | j rtjtjdd ntjtjdd | j r$td| jdd  d ztd| j d t }t	| jd	}t | }td
|dd | j rtd t }|jj
| j\}}|jj
| j}t | }td|dd td| jdd  t| jdkrdnd  |std|dd   || jkrtd|dd  t|dkrdnd  td| jp| j d t }	| jr|| j}
n|| j}
t |	 }td|dd td| j d t }|j| j|
| j| j| j| j| j| j d\}}t | }td|dd td|d dd| j d ||| j td| j  W dS  ty\ } ztd|  | j rLtd  td! W Y d}~dS d}~ww )"z$Generate speech from text using TTS.r   r	   r   Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r)   zSaving r   zs audio to z   -> Audio saved to r*   z%TTS generation failed with exception:r   )r(   r+   r,   r-   r.   r0   r3   r   r4   r   r5   r6   r7   r8   r9   r:   r;   r<   r'   r=   r>   r$   r%   r&   outputZ
save_audiorA   rB   rC   r1   r2   )rD   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r#   rO   rP   rQ   rR   rS   rT   rU   rU   rV   cmd_ttsm   sp   .
$


rY   c              
   C  s~   zt  }|j}tdt| d |D ]	}td|  qW dS  ty> } ztd|  td W Y d}~dS d}~ww )zList available voice styles.u   📢 Available voice styles (z):
u     • r*   r   N)r   voice_style_namesr0   r8   rA   r1   r2   )rD   rG   stylesstylerT   rU   rU   rV   cmd_list_voices   s   r]   c              
   C  s   z&t  }td td|j  td|j d tdd|j  W d	S  tyE } ztd|  td W Y d	}~d	S d	}~ww )
zShow model information.u%   ℹ️  Supertonic Model Information
zModel directory: zSample rate: z Hzz
Available voice styles: z, r*   r   N)	r   r0   Z	model_dirr?   joinrZ   rA   r1   r2   )rD   rG   rT   rU   rU   rV   cmd_info   s   r_   c              
   C  sz   ddl m}m} td z| }|| td|  W dS  ty< } ztd|  td W Y d}~dS d}~ww )z Download model from HuggingFace.r   )download_modelget_cache_diru$   📥 Downloading Supertonic model...u   ✅ Model downloaded to: u   ❌ Download failed: N)loaderr`   ra   r0   rA   r1   r2   )rD   r`   ra   	cache_dirrT   rU   rU   rV   cmd_download   s   rd   c                 C  s   t dt  dS )zShow version information.zsupertonic N)r0   r   )rD   rU   rU   rV   cmd_version   s   re   returnargparse.ArgumentParserc            
      C  s  t jddt jdd} | jddd}dd	 }|jd
dd}|jddd |jdtttdt d |jdddd |jdtddd |jdtdg ddd |jdt	dd d |jd!t
d"d#d |jd$t	dd%d |jd&t
d'd(d || |jtd) |jd*d+gd,d-}|jdd.d |jd/d0d1d2d3 |jdtttdt d |jdddd |jdtddd |jdtdg ddd |jdt	dd d |jd!t
d"d#d |jd$t	dd%d |jd&t
d'd(d || |jtd) |jd4d5gd6d-}|jdd.d |jd/d0d1d2d3 |jdddd |jdt	dd d || |jtd) |jd7d8gd9d-}|jtd) |jd:d;gd<d-}|jtd) |jd=d>gd?d-}|jtd) |jd@dAgdBd-}	|	jtd) | S )CzCreate and return the CLI argument parser.

    This function is separated to allow documentation generation tools
    to extract CLI arguments automatically.

    Returns:
        ArgumentParser configured with all Supertonic CLI commands
    Z
supertonicz2Supertonic - High-quality Text-to-Speech synthesisu  
Examples:
  # Generate and play speech directly (no file saved)
  supertonic say 'Hello, welcome to the world!'

  # Generate speech from text and save to file
  supertonic tts 'Hello, welcome to the world!' -o output.wav

  # Use different voice and quality
  supertonic say 'This is a female voice style.' --voice F1 --steps 10
  supertonic tts 'This is a female voice style.' -o hello.wav --voice F1 --steps 10

  # Multilingual support (Korean, Spanish, Portuguese, French)
  supertonic say '안녕하세요! 반갑습니다.' --lang ko
  supertonic tts 'Bonjour le monde!' -o french.wav --lang fr
  supertonic tts 'Hola, bienvenido!' -o spanish.wav --lang es

  # Use custom voice style from JSON file
  supertonic say 'This is a custom voice test.' --custom-style-path ./my_voice.json

  # Long text with custom chunking
  supertonic tts 'This is a very long text.' -o output.wav --max-chunk-length 200

  # List available voices
  supertonic list-voices
        )progdescriptionformatter_classepilogcommandzAvailable commands)desthelpc                 S  s   | j ddddd d S )Nz-vz	--verbose
store_truez+Enable verbose output with detailed logging)actionrn   )add_argument)prU   rU   rV   add_common_args  s   
z&create_parser.<locals>.add_common_argsZsayz:Generate speech and play it directly without saving a file)rn   r3   zText to synthesize and playz--modelzQModel to use: supertonic (English only) or supertonic-2 (multilingual). Default: )typedefaultchoicesrn   z--voiceZM1zVoice style (default: M1))ru   rn   z--custom-style-pathNzDPath to custom voice style JSON file (overrides --voice if provided))rt   ru   rn   z--langen)rw   koesptfrzaLanguage code: en (English), ko (Korean), es (Spanish), pt (Portuguese), fr (French). Default: enz--steps   z)Quality steps (default: 5, higher=better)z--speedg?z4Speech speed (0.7-2.0, default: 1.05, 2.0=2x faster)z--max-chunk-lengthz>Maximum characters per chunk (default: auto based on language)z--silence-durationg333333?z0Silence between chunks in seconds (default: 0.3))funcrG   tzGenerate speech from text)aliasesrn   zText to synthesizez-oz--outputTzOutput WAV file)requiredrn   r=   r   z/(Deprecated: use tts) Generate speech from textzlist-voiceslvzList available voice stylesinfoizShow model informationdownloaddzDownload model from HuggingFaceversionvzShow version information)argparseArgumentParserRawDescriptionHelpFormatteradd_subparsers
add_parserrq   strr   r   intfloatset_defaultsrW   rY   r]   r_   rd   re   )
parserZ
subparsersrs   Z
parser_sayZ
parser_ttsZparser_synthZparser_voicesZparser_infoZparser_downloadZparser_versionrU   rU   rV   create_parser   s  	 	r   c               
   C  s   t  } |  }|js|   td z|| W dS  ty.   td td Y dS  t	yL } ztd|  td W Y d}~dS d}~ww )zMain CLI entry point.r   u   
⚠️  Interrupted by user   r*   N)
r   
parse_argsrl   
print_helpr1   r2   r}   KeyboardInterruptr0   rA   )r   rD   rT   rU   rU   rV   main  s   
r   __main__)rf   rg   )__doc__
__future__r   r   r+   r1   r4   r   r   configr   r   Zpipeliner   	getLogger__name__rB   rW   rY   r]   r_   rd   re   r   r   rU   rU   rU   rV   <module>   s,    
XN
 >
