
    җi                        d Z ddlmZ ddlZddlmZ ddlZddlmZm	Z	m
Z
mZmZmZ ddlmZmZ ddlmZ ddlmZ erdd	lmZ d
 Z G d de      Zy)z
AVSpeech driver for pyttsx3.

This driver uses the AVSpeechSynthesizer class from AVFoundation on macOS.
It is based on the NSSpeechSynthesizer driver and provides similar functionality.
    )annotationsN)TYPE_CHECKING)AVAudioSessionAVSpeechBoundaryImmediateAVSpeechSynthesisVoiceAVSpeechSynthesizerAVSpeechUtterance"AVSpeechUtteranceDefaultSpeechRate)CFRunLoopRunInModekCFRunLoopDefaultMode)NSObject)Voice)Iteratorc                l    t         j                         j                         }|j                  |        |S )z"Build an AVSpeech driver instance.)AVSpeechDriverallocinitsetProxy)proxydrivers     b/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/pyttsx3/drivers/avspeech.pybuildDriverr      s*    !!#((*F
OOEM    c                  \   e Zd ZdZd Zej                  dd       Zej                  dd       ZddZ	ddZ
ddZ ej                  d      dd	       Zej                  dd
       ZddZddZddZej                  dd       Zej                  dd       Zej                  dd       Zy)r   zAVSpeech driver for pyttsx3.c                   t        j                  t        |       j                         }|d}t	        |      t        j                         }|j                  dd       |j                  dd       d|_	        t        j                         j                         |_        |j                  j                  |       t        j                  d      |_        t"        |_        d|_        g |_        d|_        |S )zInitialize the AVSpeech driver.Nz'Unable to instantiate an AVSpeechDriverplaybackTz&com.apple.voice.compact.en-US.Samanthag      ?F)objcsuperr   r   RuntimeErrorr   sharedInstancesetCategory_error_setActive_error__proxyr   r   _ttssetDelegate_r   voiceWithIdentifier__current_voicer
   _rate_volume_queue_should_stop_loop)selfinstancemsgsessions       r   r   zAVSpeechDriver.init)   s    ::nd388:;Cs## !//1"":t4  t, +11388:""8,"8"M"M4#
 <%*"r   c                    || _         y)z&Set the proxy for the AVSpeech driver.N)r#   )r,   r   s     r   r   zAVSpeechDriver.setProxyB   s     r   c                    | j                   r3t        | j                   d      r| j                   j                  d       yyy)z1Initialize the busy state of the AVSpeech driver.r*   FN)r#   hasattrsetBusyr,   s    r   initialize_busy_statez$AVSpeechDriver.initialize_busy_stateG   s1     ;;74;;9KK& :;r   c                H    | j                   j                  d       d| _         y)zDestroy the AVSpeech driver.N)r$   r%   r4   s    r   destroyzAVSpeechDriver.destroyM   s    		t$	r   c                (   d| _         | j                   s| j                  s| j                  j                         rX| j	                  d       t        t        dd       | j                   s)| j                  r;| j                  j                         rVyyyy)zStart the AVSpeech driver loop.FN{Gz?)r+   r*   r$   
isSpeakingprocessQueue_r   r   r4   s    r   	startLoopzAVSpeechDriver.startLoopR   sl    !&((dkkTYY=Q=Q=St$4dEB ((dkkTYY=Q=Q=S(=S(r   c                    d| _         y)zEnd the AVSpeech driver loop.TN)r+   r4   s    r   endLoopzAVSpeechDriver.endLoopY   s
    !%r   s   v@:@c                @   | j                   s| j                          y| j                  j                         sg| j                   j	                  d      \  }} ||  t        j                  d|d   j                         h       | j                  j                  d       yy)z Process the queue of utterances.Nr   zProcessing utterance: %s:T)
r*   r>   r$   r:   poploggingdebugspeechStringr#   r3   )r,   _commandargss       r   r;   zAVSpeechDriver.processQueue_]   sz     {{LLNyy##% KKOOA.MGTTNMM5Q8L8L8N7OPKK%	 &r   c                x   t        j                  |      }| j                  r|j                  | j                         |j	                  | j
                         |j                  | j                         | j                  j                  | j                  j                  |ff       t        j                  d|       y)zHSpeak the given text. Note - should queue it - not speak it immediately.zQueued utterance: %sN)r	   speechUtteranceWithString_r'   	setVoice_setRate_r(   
setVolume_r)   r*   appendr$   speakUtterance_rA   rB   )r,   text	utterances      r   sayzAVSpeechDriver.sayj   s     &@@F	 3 344::&T\\*DII55	|DE,d3r   c                b    | j                   j                  t               | j                          y)zStop the current utterance.N)r$   stopSpeakingAtBoundary_r   r>   r4   s    r   stopzAVSpeechDriver.stopu   s    		))*CDr   c                    t        j                  d|j                                | j                  j	                  dd       | j                  j                  d       | j                  d       y)z0Notify the engine when an utterance is finished.zFinished utterance: %szfinished-utteranceT)	completedFN)rA   rB   rC   r#   notifyr3   r;   )r,   ttsrO   s      r   +speechSynthesizer_didFinishSpeechUtterance_z:AVSpeechDriver.speechSynthesizer_didFinishSpeechUtterance_{   sR    .	0F0F0HI/4@E"4 r   c                    |d   }|d   }||j                   |j                   |j                  z    }| j                  j                  d||j                   |j                         y)z(Notify the engine when a word is spoken.NSRangeAVSpeechSynthesisSpeechStringzstarted-word)namelocationlengthN)r]   r^   r#   rV   )r,   rW   infocharacterRangerO   current_words         r   /speechSynthesizer_willSpeakRangeOfSpeechString_z>AVSpeechDriver.speechSynthesizer_willSpeakRangeOfSpeechString_   sm    i89	 ##n&=&=@U@U&U
 	#,,!((	 	 	
r   c              #     K   | j                   s| j                  j                         rO| j                  d       t	        t
        dd       d | j                   r3| j                  j                         rNyyw)z!Iterate the AVSpeech driver loop.Nr9   F)r*   r$   r:   r;   r   r   r4   s    r   iteratezAVSpeechDriver.iterate   sT      kkTYY113t$4dEB kkTYY113s   AA9A97A9c           
        |dk(  r[t        j                         D cg c]=  }t        |j                         |j	                         |j                         gdd      ? c}S |dk(  r(| j                  r| j                  j                         S dS |dk(  r| j                  S |dk(  r| j                  S d| }t        |      c c}w )z$Get the value of the given property.voicesN)idr\   	languagesgenderagevoiceratevolumeUnknown property: )
r   speechVoicesr   
identifierr\   languager'   r(   r)   KeyError)r,   r\   rk   r.   s       r   getPropertyzAVSpeechDriver.getProperty   s     8 4@@B	  '')$~~/0	 	 7?7;7J7J4&&113TPTT6>::8<<"4&)sm#	s   AC c                    |dk(  rt        j                  |      | _        y|dk(  r|t        z  | _        y|dk(  r|| _        yd| }t        |      )z$Set the value of the given property.rk   rl   rm   rn   N)r   r&   r'   r
   r(   r)   rr   )r,   r\   valuer.   s       r   setPropertyzAVSpeechDriver.setProperty   sW     7?"8"M"Me"TDV^!CCDJX DL&tf-C3-r   N)returnNone)rw   zIterator[None])r\   strrw   zstr | float | list[Voice])r\   ry   ru   zstr | float | Nonerw   rx   )__name__
__module____qualname____doc__r   r   python_methodr   r5   r7   r<   r>   typedSelectorr;   rP   rS   rX   rb   rd   rs   rv    r   r   r   r   &   s    &2 
  
' '

C& T 
& !
& 
4 4!
 
  
 , 

  
 r   r   )r}   
__future__r   rA   typingr   r   AVFoundationr   r   r   r   r	   r
   CoreFoundationr   r   
Foundationr   pyttsx3.voicer   collections.abcr   r   r   r   r   r   <module>r      sG    #      E  (T X T r   