
    Pi&                         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ZddlmZ ddlm	Z	 ddl
mZ ddlmZ ej        dk    rddlZ G d d	          ZdS )
z$Low-level bindings to the espeak API    N)CDLL)Path)Union)EspeakVoicewin32c                       e Zd ZdZdeeef         fdZd Ze	d             Z
ed             Ze	defd            Zd	 Zd
 ZdefdZd Zd Zd Zd ZdS )	EspeakAPIzExposes the espeak API to the EspeakWrapper

    This class exposes only low-level bindings to the API and should not be
    used directly.

    libraryc                 P   d | _         	 t          j                            t	          |                    }|                     |          }~n2# t          $ r%}t          dt	          |                     d d }~ww xY wt          j	                    | _
        t          j        dk    rt          j        | j                   n&t!          j        | | j        | j         | j
                   t'          j        | j
                  |j        z  }t-          j        ||d           t          j                            t	          |                    | _         	 | j                             ddd d          dk    rt          d          n# t2          $ r t          d          d w xY w|| _        d S )	Nzfailed to load espeak library: r   F)follow_symlinks   r   z*failed to initialize espeak shared libraryzfailed to load espeak library)_libraryctypescdllLoadLibrarystr_shared_library_pathOSErrorRuntimeErrortempfilemkdtemp_tempdirsysplatformatexitregister_delete_win32weakreffinalize_deletepathlibr   nameshutilcopyespeak_InitializeAttributeError_library_path)selfr
   espeaklibrary_patherrorespeak_copys         q/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/phonemizer/backend/espeak/api.py__init__zEspeakAPI.__init__+   s    	J ";223w<<@@F44V<<L 	J 	J 	J>#e**>>@ @EIJ	J
 !(** <7""OD.////T4<NNNl4=11L4EEL+uEEEE //K0@0@AA	;}..tQa@@AEE"@B B B F  	; 	; 	;/1 16:;	; *s$   AA 
A; A66A;0F Fc                 F    |                      | j        | j                   d S )N)r    r   r   r(   s    r-   r   zEspeakAPI._delete_win32a   s"     	T]DM22222    c                     	 |                                   n# t          $ r Y nw xY wt          j        dk    rdd l}|                    | j                   t          j        |           d S )Nr   r   )	espeak_Terminater&   r   r   _ctypesFreeLibrary_handler#   rmtree)r
   tempdirr4   s      r-   r    zEspeakAPI._deletef   s    	$$&&&& 	 	 	D	
 <7"" NNN000 	gs    
$$c                     | j         S )z0Absolute path to the espeak library being in use)r'   r0   s    r-   r*   zEspeakAPI.library_pathz   s     !!r1   returnc                 R   t          j        | j                                                  }|                                r|S 	 t          j        t          j        |           j                                                  S # t          t          f$ r t          d|  d          dw xY w)zReturns the absolute path to `library`

        This function is cross-platform and works for Linux, MacOS and Windows.
        Raises a RuntimeError if the library path cannot be retrieved

        zfailed to retrieve the path to z libraryN)r!   r   _nameresolveis_filedlinfoDLInfopath	ExceptionImportErrorr   )r
   rA   s     r-   r   zEspeakAPI._shared_library_path   s     |GM**2244<<>> 	K	O<g 6 6 ;<<DDFFF;' 	O 	O 	OC'CCCE EJNO	Os   <B   &B&c                     | j         j        }t          j        |_        t          j                    } |t          j        |                    }||j        fS )zBindings to espeak_Info

        Returns
        -------
        version, data_path: encoded strings containing the espeak version
            number and data path respectively

        )r   espeak_Infor   c_char_prestypebyrefvalue)r(   f_info	data_pathversions       r-   infozEspeakAPI.info   sJ     *O%%	&i0011	''r1   c                     | j         j        }t          j        t          j                  g|_        t          j        t          j        t          j                            |_         ||          S )zBindings to espeak_ListVoices

        Parameters
        ----------
        name (str or None): if specified, a filter on voices to be listed

        Returns
        -------
        voices: a pointer to EspeakVoice.Struct instances

        )r   espeak_ListVoicesr   POINTERr   VoiceStructargtypesrG   )r(   r"   f_list_voicess      r-   list_voiceszEspeakAPI.list_voices   sZ     7"(.1H"I"I!J &N;233!5 !5}T"""r1   c                 T    | j         j        }t          j        g|_         ||          S )zBindings to espeak_SetVoiceByName

        Parameters
        ----------
        name (str) : the voice name to setup

        Returns
        -------
        0 on success, non-zero integer on failure

        )r   espeak_SetVoiceByNamer   rF   rR   )r(   r"   f_set_voice_by_names      r-   set_voice_by_namezEspeakAPI.set_voice_by_name   s.     #mA(.'8$""4(((r1   c                 ~    | j         j        }t          j        t          j                  |_         |            j        S )zBindings to espeak_GetCurrentVoice

        Returns
        -------
        a EspeakVoice.Struct instance or None if no voice has been setup

        )r   espeak_GetCurrentVoicer   rP   r   rQ   rG   contents)r(   f_get_current_voices     r-   get_current_voicezEspeakAPI.get_current_voice   s7     #mB&,n[5L&M&M#""$$--r1   c                     | j         j        }t          j        |_        t          j        t          j                  t          j        t          j        g|_         ||||          S )a  Bindings to espeak_TextToPhonemes

        Parameters
        ----------
        text_ptr (pointer): the text to be phonemized, as a pointer to a
            pointer of chars
        text_mode (bits field): see espeak sources for details
        phonemes_mode (bits field): see espeak sources for details

        Returns
        -------
        an encoded string containing the computed phonemes

        )r   espeak_TextToPhonemesr   rF   rG   rP   c_intrR   )r(   text_ptr	text_modephonemes_modef_text_to_phonemess        r-   text_to_phonemeszEspeakAPI.text_to_phonemes   sU     "]@%+_"N6?++LL'# "!(I}EEEr1   c                 p    | j         j        }t          j        t          j        g|_         |||           dS )a=  "Bindings on espeak_SetPhonemeTrace

        This method must be called before any call to synthetize()

        Parameters
        ----------
        mode (bits field): see espeak sources for details
        file_pointer (FILE*): a pointer to an opened file in which to output
            the phoneme trace

        N)r   espeak_SetPhonemeTracer   r`   c_void_prR   )r(   modefile_pointerf_set_phoneme_traces       r-   set_phoneme_tracezEspeakAPI.set_phoneme_trace   s?     #mBLO($ 	D,/////r1   c           
      
   | j         j        }t          j        t          j        t          j        t          j        t          j        t          j        t          j                  t          j        g|_         |||ddd|dd          S )a  Bindings on espeak_Synth

        The output phonemes are sent to the file specified by a call to
        set_phoneme_trace().

        Parameters
        ----------
        text (pointer) : a pointer to chars
        size (int) : number of chars in `text`
        mode (bits field) : see espeak sources for details

        Returns
        -------
        0 on success, non-zero integer on failure

        r      N)	r   espeak_Synthr   rh   c_size_tc_uintr`   rP   rR   )r(   ra   sizeri   f_synthetizes        r-   
synthetizezEspeakAPI.synthetize   se    " }1OOMLMN6=))O! |HdAq!T4FFFr1   N)__name__
__module____qualname____doc__r   r   r   r.   r   staticmethodr    propertyr*   r   rM   rT   intrX   r]   re   rl   rt    r1   r-   r	   r	   #   s1        4*c4i 0 4* 4* 4* 4*l3 3 3
   \& " " X" O O O O \O&( ( (# # #$) ) ) ) ) 
. 
. 
.F F F.0 0 0$G G G G Gr1   r	   )rx   r   r   r!   r#   r   r   r   r   r   typingr   phonemizer.backend.espeak.voicer   r   r?   r	   r|   r1   r-   <module>r      s    + *     



                     7 7 7 7 7 7<7MMMpG pG pG pG pG pG pG pG pG pGr1   