
    e!                        d Z ddlmZ ddlZddlmZmZmZmZ ddl	Z	ddl
mZ ddlmZ erddlmZ  G d	 d
e	j                  Z G d de	j                  Z G d dee          ZdS )a(  
The Panel TextToSpeak Widget provides functionality for *text to
speech* via the the HTML5 SpeechSynthesis API.

See https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis

The term *utterance* is used throughout the API. It is the smallest
unit of speech in spoken language analysis.
    )annotationsN)TYPE_CHECKINGClassVarMappingType)Widget   )TextToSpeech)Modelc                      e Zd ZdZ ej        ddd          Z ej        dd          Z ej        dd          Z	 ej        dd	          Z
 ej        dd
          Zed             Zed             ZdS )Voicea  
    The current device (i.e. OS and Browser) provides a list of
    Voices. Each with a unique name and speaking a specific language.

    Wraps the HTML5 SpeecSynthesisVoice API

    See https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisVoice
    TFz
        A Boolean indicating whether the voice is the default voice
        for the current app language (True), or not (False.))constantdefaultdoczL
        Returns a BCP 47 language tag indicating the language of the voice.)r   r   z
        A Boolean indicating whether the voice is supplied by a local
        speech synthesizer service (True), or a remote speech
        synthesizer service (False.)zA
        Returns a human-readable name that represents the voice.ze
        Returns the type of URI and location of the speech synthesis
        service for this voice.c                V    g }| D ]#}t          di |}|                    |           $|S )z?Returns a list of Voice objects from the list of dicts provided )r   append)voicesresult_voicevoices       <lib/python3.11/site-packages/panel/widgets/text_to_speech.pyto_voices_listzVoice.to_voices_list9   sA      	! 	!FOOFOOEMM%        c                   | si S t          t          t          d | D                                           }d |D             }| D ]"}||j                                     |           #d |                                D             }|S )zmReturns a dictionary where the key is the `lang` and the value is a list of voices
        for that language.c              3  $   K   | ]}|j         V  d S N)lang).0r   s     r   	<genexpr>z&Voice.group_by_lang.<locals>.<genexpr>I   s$      %E%EUej%E%E%E%E%E%Er   c                    i | ]}|g S r   r   )r   r   s     r   
<dictcomp>z'Voice.group_by_lang.<locals>.<dictcomp>J   s    333t$333r   c                :    i | ]\  }}|t          |d            S )c                    | j         S r   name)xs    r   <lambda>z0Voice.group_by_lang.<locals>.<dictcomp>.<lambda>M   s    16 r   )key)sorted)r   r)   values      r   r"   z'Voice.group_by_lang.<locals>.<dictcomp>M   s/    \\\zsE#ve)9)9:::\\\r   )r*   listsetr   r   items)r   sorted_langr   r   s       r   group_by_langzVoice.group_by_langB   s      	IT#%E%Ef%E%E%E"E"EFFGG33{333 	- 	-E5:%%e,,,,\\V\\^^\\\r   N)__name__
__module____qualname____doc__paramBooleanr   Stringr   local_servicer&   	voice_uristaticmethodr   r0   r   r   r   r   r      s         emT5 ?@ A A AG 5< ,O P P PD "EM4 6( ) ) )M
 5< ,D E E ED d 1# $ $ $I   \   \  r   r   c                  B    e Zd ZdZ ej        dd          Z ej        dd          Z ej	        ddd	          Z
 ej	        dd
d	          Z ej        d          Z ej	        ddd	          Z fdZddZd Z ej        dd          d             Z xZS )	Utterancez
    An *utterance* is the smallest unit of speech in spoken language analysis.

    The Utterance Model wraps the HTML5 SpeechSynthesisUtterance API

    See https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance
     z
        The text that will be synthesised when the utterance is
        spoken. The text may be provided as plain text, or a
        well-formed SSML document.r   r   z'
        The language of the utterance.      ?)        g       @zj
        The pitch at which the utterance will be spoken at expressed
        as a number between 0 and 2.)r   boundsr   )g?g      $@zm
        The speed at which the utterance will be spoken at expressed
        as a number between 0.1 and 10.z<
        The voice that will be used to speak the utterance.r   )r@   r?   zg The
        volume that the utterance will be spoken at expressed as a
        number between 0 and 1.c                    |                     dg           } t                      j        di | i | _        |                     |           d S )Nr   r   )popsuper__init___voices_by_language
set_voices)selfparamsr   	__class__s      r   rF   zUtterance.__init__q   sS    Hb))""6"""#% r   Tc                    | j         | j        | j        | j        | j        d}| j        r| j        j        r| j        j        |d<   |r#t          t          j	                              |d<   |S )znReturns the object parameter values in a dictionary

        Returns:
            Dict: [description]
        )r   pitchratetextvolumer   uuid)
r   rM   rN   r+   rP   r   r&   strrQ   uuid4)rI   include_uuidr   s      r   to_dictzUtterance.to_dictw   sq     IZIJk
 
 : 	.$*/ 	."joF7O 	/ ..F6Nr   c                   |s,dg| j         j        _        d| j         j        _        d| _        dS t                              |          | _        t          | j                                                  | j         j        _        d| j        v rd}n,t          | j                                                  d         }|| j         j        _        || _        | j         	                    d           dS )zCUpdates the `lang` and `voice` parameter objects, default and valuezen-USNr   r   )
r5   r   objectsr   r   r0   rG   r,   keystrigger)rI   r   default_langs      r   rH   zUtterance.set_voices   s     	'.iDJO#&-DJO#DIF#(#6#6v#>#> "&t'?'D'D'F'F"G"G
d..."LL 8 = = ? ?@@CL".
 	
6"""""r   r   watchc                J   | j         r| j        s+d | j        j        _        d | _        g | j        j        _        d S | j         | j                 }| j        r| j        |v r| j        }n|d         }|D ]}|j        r|}|| j        j        _        || j        j        _        || _        d S )Nr   )rG   r   r5   r   r   rW   )rI   r   default_voicer   s       r   _handle_lang_changedzUtterance._handle_lang_changed   s    ' 	ty 	'+DJ$DJ')DJ$F)$)4: 	*$*.. JMM"1IM * *= *$)M#)
 #0
 "


r   )T)r1   r2   r3   r4   r5   r7   r+   ObjectSelectorr   NumberrM   rN   r   rP   rF   rU   rH   dependsr_   __classcell__rK   s   @r   r<   r<   Q   sa         EL *& ' ' 'E
  5 1* + + +D ELZ >( ) ) )E 5<K >+ - - -D !E  &? @ @ @E U\#j ?# $ $ $F            &# # #$ U]6&&&# # '&# # # # #r   r<   c                      e Zd ZU dZ ej        dd          Z ej        d          Z ej        d          Z	 ej        d          Z
 ej        dd	
          Z ej        dd
          Z ej        d          Z ej        dd
          Z ej        dd
          Z ej                    ZddddddddddddZded<   eZded<    fdZ ej        dd          d             ZddZd Z xZS )r
   a9  
    The `TextToSpeech` widget wraps the HTML5 SpeechSynthesis API

    See https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis

    Reference: https://panel.holoviz.org/reference/widgets/TextToSpeech.html

    :Example:

    >>> TextToSpeech(name="Speech Synthesis", value="Data apps are nice")
    TzF
        Whether or not to automatically speak when the value changes.r>   z9
        Removes all utterances from the utterance queue.rB   z9
        Puts the TextToSpeak object into a paused state.zj
        Puts the TextToSpeak object into a non-paused state: resumes
        it if it was already paused.z\
        A Boolean that returns true if the TextToSpeak object is in a
        paused state.)readonlyr   zh
        A Boolean that returns true if the utterance queue contains
        as-yet-unspoken utterances.z8
        Speak. I.e. send a new Utterance to the browseru   
        A Boolean that returns true if an utterance is currently in
        the process of being spoken — even if TextToSpeak is in a
        paused state.zm
        Returns a list of Voice objects representing all the available
        voices on the current device.Nr   )
auto_speakr   r&   rM   rN   speakr+   r   r   rP   _voicesz"ClassVar[Mapping[str, str | None]]_renamezClassVar[Type[Model]]_widget_typec                    |                     d          p
d|v o| j        }t                                          |          }|r|                                 |d<   |S )Nrh   r+   )getrg   rE   _process_param_changerU   )rI   msgrh   rK   s      r   rn   z"TextToSpeech._process_param_change   sY      HW^%Ggg++C00 	*<<>>CL
r   ri   r[   c                    g }| j         D ]#}t          di |}|                    |           $|| _        |                     | j                   d S )Nr   )ri   r   r   r   rH   )rI   r   r   r   s       r   _update_voiceszTextToSpeech._update_voices   s]    l 	! 	!FOOFOOEMM%    $$$$$r   c                    d| j         dS NzTextToSpeech(name=)r%   )rI   depths     r   __repr__zTextToSpeech.__repr__   s     3DI2222r   c                    d| j         dS rs   r%   )rI   s    r   __str__zTextToSpeech.__str__   s    2DI2222r   r   )r1   r2   r3   r4   r5   r6   rg   Eventcancelpauseresumepausedpendingrh   speakingListr   ri   rj   __annotations___BkTextToSpeechrk   rn   rb   rq   rv   rx   rc   rd   s   @r   r
   r
      s        
 
 t 2I J J JJ U[ < = = =F EK < = = =E U[ ( ) ) )F U]D /   F emT 0' ( ( (G EK ; < < <E u}d 1   H
 UZ ,) * * *F ejllG D$tdT$83 3G     +:L9999     U]9D)))% % *)%3 3 3 3
3 3 3 3 3 3 3r   r
   )r4   
__future__r   rQ   typingr   r   r   r   r5   panel.widgetsr   models.text_to_speechr
   r   bokeh.modelr   Parameterizedr   r<   r   r   r   <module>r      sU    # " " " " "                          C C C C C C "!!!!!!2 2 2 2 2E 2 2 2j^# ^# ^# ^# ^## ^# ^# ^#BN3 N3 N3 N3 N39f N3 N3 N3 N3 N3r   