
    rHJj'                         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k7  rddlZ G d d	      Zy)
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   deeed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y)	EspeakAPIzExposes the espeak API to the EspeakWrapper

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

    library	data_pathNc                    d | _         |t        |      j                  d      }	 t        j                  j                  t        |            }| j                  |      }~t        j                         | _        t        j                  dk(  r t        j                  | j                          n6t#        j$                  | | j&                  | j                   | j                         t)        j*                  | j                        |j,                  z  }t/        j0                  ||d       t        j                  j                  t        |            | _         	 | j                   j3                  dd|d      dk  rt        d      	 || _        y # t        $ r}t        dt        |             d d }~ww xY w# t4        $ r t        d	      d w xY w)
Nzutf-8zfailed to load espeak library: r   F)follow_symlinks   r   z*failed to initialize espeak shared libraryzfailed to load espeak library)_librarystrencodectypescdllLoadLibrary_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
   r   espeaklibrary_patherrorespeak_copys          g/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/phonemizer/backend/espeak/api.py__init__zEspeakAPI.__init__+   s     I--g6I	J ";;223w<@F44V<L !((* <<7"OOD../T4<<Nll4==1L4E4EEL+uE //K0@A	;}}..tQ	1EJ"@B B K *E  	J1#e*>@EIJ	J8  	;/16:;	;s#   :F ,F1 	F.F))F.1Gc                 P    | j                  | j                  | j                         y )N)r"   r   r   r*   s    r/   r   zEspeakAPI._delete_win32d   s     	T]]DMM2    c                     	 | j                          t        j                  dk(  rdd l}|j                  | j                         t        j                  |       y # t        $ r Y Sw xY w)Nr   r   )	espeak_Terminater(   r   r   _ctypesFreeLibrary_handler%   rmtree)r
   tempdirr6   s      r/   r"   zEspeakAPI._deletei   sZ    	$$& <<7" 0 	g  		s   A 	A&%A&c                     | j                   S )z0Absolute path to the espeak library being in use)r)   r2   s    r/   r,   zEspeakAPI.library_path}   s     !!!r3   returnc                 H   t        j                  | j                        j                         }|j	                         r|S 	 t        j                  t        j                  |       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
   rC   s     r/   r   zEspeakAPI._shared_library_path   s     ||GMM*224<<>K	O<<g 6 ; ;<DDFF;' 	O1'(C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_infor   versions       r/   infozEspeakAPI.info   sL     **OO%	i01	''r3   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argtypesrI   )r*   r$   f_list_voicess      r/   list_voiceszEspeakAPI.list_voices   s[     77"(..1H1H"I!J &NN;223!5T""r3   c                 j    | 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   rH   rS   )r*   r$   f_set_voice_by_names      r/   set_voice_by_namezEspeakAPI.set_voice_by_name   s/     #mmAA(.'8$"4((r3   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   rQ   r   rR   rI   contents)r*   f_get_current_voices     r/   get_current_voicezEspeakAPI.get_current_voice   s;     #mmBB&,nn[5L5L&M#"$---r3   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   rH   rI   rQ   c_intrS   )r*   text_ptr	text_modephonemes_modef_text_to_phonemess        r/   text_to_phonemeszEspeakAPI.text_to_phonemes   s[     "]]@@%+__"NN6??+LLLL'# "(I}EEr3   c                     | j                   j                  }t        j                  t        j                  g|_         |||       y)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   ra   c_void_prS   )r*   modefile_pointerf_set_phoneme_traces       r/   set_phoneme_tracezEspeakAPI.set_phoneme_trace   s9     #mmBBLLOO($ 	D,/r3   c           
      R   | 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   ri   c_size_tc_uintra   rQ   rS   )r*   rb   sizerj   f_synthetizes        r/   
synthetizezEspeakAPI.synthetize   sr    " }}11OOOOMMLLMMNN6==)OO! HdAq!T4FFr3   )__name__
__module____qualname____doc__r   r   r   r0   r   staticmethodr"   propertyr,   r   rN   rU   intrY   r^   rf   rm   ru    r3   r/   r	   r	   #   s    7*c4i 0 7*U3d?=S 7*r3
  & " " O O O&(#$) ) 
.F.0$Gr3   r	   )ry   r   r   r#   r%   r   r   r    r   r   typingr   phonemizer.backend.espeak.voicer   r   rA   r	   r}   r3   r/   <module>r      sH    +     
      7<<7sG sGr3   