Ë
    “Žòj+#  ã                   óœ   — d Z ddl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ZdZd	Zd
edefd„Zdedefd„Zdededefd„Z G d„ d«      Zy)a~  Terminal progress bar for the command line interface.

This module knows nothing about TTS. It is fed the source text up front, then
told which parts of it have been spoken, how far the audio timeline those parts
describe reaches, and how many audio bytes have actually arrived.

Progress is measured in audio, not in text. The service sends its boundary
metadata far ahead of the audio it describes -- in a measured run the text
cursor reached 67% after 0.6 seconds while only 7 of the eventual 382 seconds
of audio had arrived -- so the text cursor is useless as a progress bar on its
own. What it is good for is a ratio: the audio timeline covered by the metadata
received so far, divided by the fraction of the text that metadata covers,
extrapolates to the total length of the audio. In that same run the estimate
settled within 1% of the true total after 0.6 seconds of a 3.9 second job.
é    N)ÚCallableÚOptionalÚTextIOé   )ÚMP3_BITRATE_BPSÚTICKS_PER_SECONDu   â–ˆu    â–�â–Žâ–�â–Œâ–‹â–Šâ–‰é   éP   ÚsecondsÚreturnc                 ó€   — t        t        | «      d«      \  }}t        |d«      \  }}|r|d›d|d›d|d›�S |d›d|d›�S )a#  
    Formats a duration as MM:SS, or H:MM:SS once it reaches an hour.

    Args:
        seconds (float): The duration in seconds. Truncated, not rounded, so
                         the displayed time never runs ahead of the real one.

    Returns:
        str: The formatted duration.
    é<   Údú:Ú02d)ÚdivmodÚint)r   ÚminutesÚsecsÚhourss       ú^/var/www/html/academy/tools/voices/edge-venv/lib/python3.12/site-packages/edge_tts/progress.pyÚformat_timer   !   s]   € ô œ3˜w›<¨Ó,�M€GˆTÜ˜G RÓ(�N€Eˆ7ÙØ˜�˜!˜G C˜=¨¨$¨s¨Ð4Ð4Ø�cˆ]˜!˜D ˜:Ð&Ð&ó    Ú	num_bytesc                 ó4   — | dk  r| dz  › d�S | dz  dz  d›d�S )zÎ
    Formats a byte count as a short human readable string.

    Args:
        num_bytes (int): The number of bytes.

    Returns:
        str: The byte count in KB below one megabyte, otherwise in MB.
    i   i   ÚKBz.1fÚMB© )r   s    r   Úformat_bytesr   3   s9   € ð �;ÒØ˜tÑ#Ð$ BÐ'Ð'Ø˜$Ñ Ñ% cÐ*¨"Ð-Ð-r   ÚfractionÚwidthc                 óÒ   — | |z  }t        |«      }||k\  r	t        |z  S t        t        t	        t        «      dz
  t        ||z
  dz  «      «         }t        |z  |z   d||z
  dz
  z  z   S )a  
    Renders the bar body for the given fill fraction.

    Args:
        fraction (float): The fill fraction, between 0 and 1.
        width (int): The width of the bar in characters.

    Returns:
        str: The bar body, always exactly `width` characters long.
    r   é   ú )r   Ú
FULL_BLOCKÚPARTIAL_BLOCKSÚminÚlen)r    r!   ÚfilledÚwholeÚpartials        r   Ú
render_barr,   B   sv   € ð ˜Ñ€FÜ�‹K€EØ�‚~Ü˜EÑ!Ð!ÜœS¤¤^Ó!4°qÑ!8¼#¸vÈ¹~ÐQRÑ>RÓ:SÓTÑU€GÜ˜Ñ Ñ'¨#°¸±ÀÑ1BÑ*CÑCÐCr   c                   ó  — e Zd ZdZdddej
                  fdedee   dee	   de
deg e
f   d	dfd
„Zed	e
fd„«       Zed	e
fd„«       Zd	e
fd„Zd	e
fd„Zded	dfd„Zde	d	dfd„Zde	d	dfd„Zdd„Zdd„Zdd„Zd	efd„Zddeded	dfd„Zy)ÚProgressBarzË
    A single line, in place progress bar for a TTS run.

    Fed by `advance`, `set_audio_timeline` and `add_audio_bytes`; drawn by
    `render`; ended by `finish` on success or `close` on failure.
    Ngš™™™™™¹?ÚtextÚstreamr!   Úmin_intervalÚtime_fnr   c                 ó<  — || _         |�|nt        j                  | _        |€(t	        j
                  t        df«      j                  dz
  }|| _        || _	        || _
        d| _        d| _        d| _         |«       | _        d | _        d| _        d| _        d| _        y )Né   r   r   ç        F)Ú_textÚsysÚstderrÚ_streamÚshutilÚget_terminal_sizeÚDEFAULT_WIDTHÚcolumnsÚ_widthÚ_min_intervalÚ_time_fnÚ_cursorÚ_audio_bytesÚ_timeline_ticksÚ_startÚ_last_renderÚ	_fractionÚ	_completeÚ	_finished)Úselfr/   r0   r!   r1   r2   s         r   Ú__init__zProgressBar.__init__^   s•   € ð ˆŒ
Ø)/Ð);™vÄÇÁˆŒØˆ=Ü×,Ñ,¬m¸RÐ-@ÓA×IÑIÈAÑMˆEØˆŒØ)ˆÔØˆŒàˆŒØˆÔØ ˆÔÙ“iˆŒØ-1ˆÔØˆŒØˆŒØˆ�r   c                 ót   — | j                   syt        d| j                  t        | j                   «      z  «      S )z?The fraction of the source text spoken so far, between 0 and 1.r5   ç      ð?)r6   r'   rA   r(   ©rI   s    r   Útext_fractionzProgressBar.text_fractionw   s,   € ð �zŠzØÜ�3˜Ÿ™¤s¨4¯:©:£Ñ6Ó7Ð7r   c                 óÊ   — | j                   ry| j                  «       }|dkD  r6t        | j                  t	        d| j                  «       |z  «      «      | _        | j                  S )a_  
        The fraction of the audio received so far, between 0 and 1.

        Zero until the first boundary event arrives, since until then there is
        nothing to estimate the total length from. Never decreases: when the
        remaining text turns out to speak for longer than extrapolated, the bar
        holds rather than rewinding.
        rL   r   )rG   Ú_estimated_total_secondsÚmaxrF   r'   Ú_received_seconds)rI   Úestimated_totals     r   r    zProgressBar.fraction~   sZ   € ð �>Š>ØØ×7Ñ7Ó9ˆØ˜QÒÜ Ø—‘¤ C¨×)?Ñ)?Ó)AÀOÑ)SÓ TóˆDŒNð �~‰~Ðr   c                 ó.   — | j                   dz  t        z  S )z8Seconds of audio received, by exact CBR byte arithmetic.r#   )rB   r   rM   s    r   rR   zProgressBar._received_seconds‘   s   € à× Ñ  1Ñ$¤Ñ6Ð6r   c                 óp   — | j                   }|dk  s| j                  dk  ry| j                  t        z  |z  S )a“  
        Estimated length of the complete audio, or 0 if not yet knowable.

        The metadata received so far describes the audio up to
        `_timeline_ticks` and covers `text_fraction` of the text, so the whole
        text extrapolates to `timeline / text_fraction`. Once the metadata
        covers all of the text this stops being an extrapolation and becomes
        the exact total.
        r   r5   )rN   rC   r   )rI   rN   s     r   rP   z$ProgressBar._estimated_total_seconds•   s>   € ð ×*Ñ*ˆØ˜AÒ ×!5Ñ!5¸Ò!:ØØ×#Ñ#Ô&6Ñ6¸ÑFÐFr   Úspoken_textc                 óˆ   — |sy| j                   j                  || j                  «      }|dk\  r|t        |«      z   | _        yy)a‹  
        Moves the cursor past the given piece of spoken text.

        The piece is searched for from the current cursor onwards, so repeated
        words advance to their next occurrence and the cursor never moves
        backwards. Text that cannot be found is ignored.

        Args:
            spoken_text (str): The text from a boundary event.

        Returns:
            None
        Nr   )r6   ÚfindrA   r(   )rI   rV   Úfounds      r   ÚadvancezProgressBar.advance¤   s?   € ñ ØØ—
‘
—‘ ¨T¯\©\Ó:ˆØ�AŠ:Ø ¤3 {Ó#3Ñ3ˆD�Lð r   Úticksc                 ó:   — t        | j                  |«      | _        y)aO  
        Records how far into the audio the metadata received so far reaches.

        Args:
            ticks (int): The end of the latest boundary event on the audio
                         timeline, in 100-nanosecond ticks; that is, its
                         offset plus its duration.

        Returns:
            None
        N)rQ   rC   )rI   r[   s     r   Úset_audio_timelinezProgressBar.set_audio_timeline¸   s   € ô  # 4×#7Ñ#7¸Ó?ˆÕr   r   c                 ó.   — | xj                   |z  c_         y)zº
        Adds to the count of audio bytes received.

        Args:
            num_bytes (int): The number of bytes in the latest audio chunk.

        Returns:
            None
        N)rB   )rI   r   s     r   Úadd_audio_byteszProgressBar.add_audio_bytesÆ   s   € ð 	×Ò˜YÑ&Ör   c                 óÂ   — | j                  «       }| j                  �|| j                  z
  | j                  k  ry|| _        | j                  | j	                  «       «       y)zw
        Redraws the bar in place, at most once per `min_interval` seconds.

        Returns:
            None
        N)r@   rE   r?   Ú_writeÚ_line)rI   Únows     r   ÚrenderzProgressBar.renderÒ   sQ   € ð �m‰m‹oˆà×ÑÐ)Ø�d×'Ñ'Ñ'¨$×*<Ñ*<Ò<àØˆÔØ�‰�D—J‘J“LÕ!r   c                 ó|   — | j                   ryd| _         d| _        | j                  | j                  «       d¬«       y)zû
        Fills the bar to 100%, ends the line and stops further drawing.

        The estimated total is never exactly right, so a completed run is
        declared complete rather than left a percent short.

        Returns:
            None
        NTú
)Úend)rH   rG   ra   rb   rM   s    r   ÚfinishzProgressBar.finishâ   s2   € ð �>Š>ØØˆŒØˆŒØ�‰�D—J‘J“L dˆÕ+r   c                 ó°   — | j                   ryd| _         | j                  �6| j                  j                  d«       | j                  j	                  «        yy)zÔ
        Ends the line without claiming the run completed.

        Used when the run is abandoned, so a failed run is not left showing a
        bar filled in to 100%.

        Returns:
            None
        NTrf   )rH   rE   r9   ÚwriteÚflushrM   s    r   ÚclosezProgressBar.closeò   sH   € ð �>Š>ØØˆŒØ×ÑÐ(Ø�L‰L×Ñ˜tÔ$Ø�L‰L×ÑÕ ð )r   c                 ó~  — | j                   }| j                  «       | j                  z
  }|dkD  rt        ||z  |z
  «      nd}t	        |dz  «      d›d�}dt        | j                  «      › dt        |«      › d|› �}t        t        | j                  t        |«      z
  t        |«      z
  «      }|t        ||«      z   |z   S )	Nr   z--:--éd   Ú3dz%|z| z  ú<)r    r@   rD   r   r   r   rB   rQ   ÚMIN_BAR_WIDTHr>   r(   r,   )rI   r    ÚelapsedÚetaÚprefixÚsuffixÚ	bar_widths          r   rb   zProgressBar._line  s·   € Ø—=‘=ˆØ—-‘-“/ D§K¡KÑ/ˆØ;CÀaº<Œk˜' HÑ,¨wÑ6Ô7ÈWˆÜ˜ 3™Ó'¨Ð+¨2Ð.ˆØ”l 4×#4Ñ#4Ó5Ð6°b¼ÀWÓ9MÐ8NÈaÐPSÈuÐUˆÜœ t§{¡{´S¸³[Ñ'@Ä3ÀvÃ;Ñ'NÓOˆ	Øœ
 8¨YÓ7Ñ7¸&Ñ@Ð@r   Úlinerg   c                 ó¬   — | j                   j                  d|j                  | j                  «      z   |z   «       | j                   j	                  «        y )Nú)r9   rj   Úljustr>   rk   )rI   rw   rg   s      r   ra   zProgressBar._write  s;   € Ø�‰×Ñ˜4 $§*¡*¨T¯[©[Ó"9Ñ9¸CÑ?Ô@Ø�‰×ÑÕr   )r   N)Ú )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚtimeÚ	monotonicÚstrr   r   r   Úfloatr   rJ   ÚpropertyrN   r    rR   rP   rZ   r]   r_   rd   rh   rl   rb   ra   r   r   r   r.   r.   U   s-  „ ñð $(Ø#Ø!Ø'+§~¡~ñàðð ˜Ñ ðð ˜‰}ð	ð
 ðð ˜"˜e˜)Ñ$ðð 
óð2 ð8˜uò 8ó ð8ð ð˜%ò ó ðð$7 5ó 7ðG¨%ó Gð4 3ð 4¨4ó 4ð(@¨ð @°ó @ð
'¨ð 
'°ó 
'ó"ó ,ó !ð"A�só Añ˜3ð  Sð °$ô r   r.   )r   r:   r7   r€   Útypingr   r   r   Ú	constantsr   r   r%   r&   rq   r<   rƒ   r‚   r   r   r   r,   r.   r   r   r   ú<module>r‡      s„   ðñó  Û 
Û ß -Ñ -ç 8à€
à)€ð €Ø€ð'˜ð ' 3ó 'ð$.˜Cð . Có .ðD˜ð D sð D¨só D÷&yò yr   