
    $h                        S r SSKJr  SSKrSSKJr  SSKrSSKJrJ	r	J
r
JrJrJr  SSKJrJr  SSKJr  SSKJr  \(       a  SS	KJr  S
 r " S S\5      rg)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    [         R                  5       R                  5       nUR                  U 5        U$ )z"Build an AVSpeech driver instance.)AVSpeechDriverallocinitsetProxy)proxydrivers     R/home/james-whalen/.local/lib/python3.13/site-packages/pyttsx3/drivers/avspeech.pybuildDriverr      s*    !!#((*F
OOEM    c                  ~   \ rS rSrSrS r\R                  SS j5       r\R                  SS j5       r	SS jr
SS jrSS jr\R                  " S	5      SS
 j5       r\R                  SS j5       rSS jrSS jrSS jr\R                  SS j5       r\R                  SS j5       r\R                  SS j5       rSrg)r   &   zAVSpeech driver for pyttsx3.c                   [         R                  " [        U 5      R                  5       nUc  Sn[	        U5      e[
        R                  " 5       nUR                  SS5        UR                  SS5        SUl	        [        R                  " 5       R                  5       Ul        UR                  R                  U5        [        R                  " S5      Ul        ["        Ul        SUl        / Ul        SUl        U$ )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   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                    Xl         g)z&Set the proxy for the AVSpeech driver.N)r$   )r-   r   s     r   r   AVSpeechDriver.setProxyB   s	     r   c                    U R                   (       a8  [        U R                   S5      (       a  U R                   R                  S5        ggg)z1Initialize the busy state of the AVSpeech driver.r+   FN)r$   hasattrsetBusyr-   s    r   initialize_busy_state$AVSpeechDriver.initialize_busy_stateG   s4     ;;74;;99KK& :;r   c                H    U R                   R                  S5        SU l         g)zDestroy the AVSpeech driver.N)r%   r&   r7   s    r   destroyAVSpeechDriver.destroyM   s    		t$	r   c                h   SU l         U R                   (       d  U R                  (       d  U R                  R                  5       (       ai  U R	                  S5        [        [        SS5        U R                   (       d5  U R                  (       a  MF  U R                  R                  5       (       a  Mg  gggg)zStart the AVSpeech driver loop.FN{Gz?)r,   r+   r%   
isSpeakingprocessQueue_r   r   r7   s    r   	startLoopAVSpeechDriver.startLoopR   st    !&((dkkTYY=Q=Q=S=St$4dEB ((dkkkTYY=Q=Q=S=S(=S(r   c                    SU l         g)zEnd the AVSpeech driver loop.TN)r,   r7   s    r   endLoopAVSpeechDriver.endLoopY   s
    !%r   s   v@:@c                T   U R                   (       d  U R                  5         gU R                  R                  5       (       dg  U R                   R	                  S5      u  p#U" U6   [
        R                  " SUS   R                  5       15        U R                  R                  S5        gg)z Process the queue of utterances.Nr   zProcessing utterance: %s:T)
r+   rD   r%   r?   poploggingdebugspeechStringr$   r6   )r-   _commandargss       r   r@   AVSpeechDriver.processQueue_]   s{     {{LLNyy##%% KKOOA.MGTNMM5Q8L8L8N7OPKK%	 &r   c                   [         R                  " U5      nU R                  (       a  UR                  U R                  5        UR	                  U R
                  5        UR                  U R                  5        U R                  R                  U R                  R                  U445        [        R                  " SU5        g)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_rH   rI   )r-   text	utterances      r   sayAVSpeechDriver.sayj   s     &@@F	 3 344::&T\\*DII55	|DE,d3r   c                b    U R                   R                  [        5        U R                  5         g)zStop the current utterance.N)r%   stopSpeakingAtBoundary_r   rD   r7   s    r   stopAVSpeechDriver.stopu   s    		))*CDr   c                    [         R                  " SUR                  5       5        U R                  R	                  SSS9  U R                  R                  S5        U R                  S5        g)z0Notify the engine when an utterance is finished.zFinished utterance: %szfinished-utteranceT)	completedFN)rH   rI   rJ   r$   notifyr6   r@   )r-   ttsrW   s      r   +speechSynthesizer_didFinishSpeechUtterance_:AVSpeechDriver.speechSynthesizer_didFinishSpeechUtterance_{   sR    .	0F0F0HI/4@E"4 r   c                    US   nUS   nUUR                   UR                   UR                  -    nU R                  R                  SUUR                   UR                  S9  g)z(Notify the engine when a word is spoken.NSRangeAVSpeechSynthesisSpeechStringzstarted-word)namelocationlengthN)rh   ri   r$   r`   )r-   ra   infocharacterRangerW   current_words         r   /speechSynthesizer_willSpeakRangeOfSpeechString_>AVSpeechDriver.speechSynthesizer_willSpeakRangeOfSpeechString_   sm    i89	 ##n&=&=@U@U&U
 	#,,!((	 	 	
r   c              #  "  #    U R                   (       d  U R                  R                  5       (       a[  U R                  S5        [	        [
        SS5        Sv   U R                   (       a  M9  U R                  R                  5       (       a  MZ  gg7f)z!Iterate the AVSpeech driver loop.Nr>   F)r+   r%   r?   r@   r   r   r7   s    r   iterateAVSpeechDriver.iterate   s[      kkTYY1133t$4dEB kkkTYY1133s   A(B,BBc                   US:X  a\  [         R                  " 5        Vs/ s H;  n[        UR                  5       UR	                  5       UR                  5       /SSS9PM=     sn$ US:X  a-  U R                  (       a  U R                  R                  5       $ S$ US:X  a  U R                  $ US:X  a  U R                  $ SU 3n[        U5      es  snf )z$Get the value of the given property.voicesN)idrg   	languagesgenderagevoiceratevolumeUnknown property: )
r   speechVoicesr   
identifierrg   languager(   r)   r*   KeyError)r-   rg   rx   r/   s       r   getPropertyAVSpeechDriver.getProperty   s     8 4@@B	 CE '')$~~/0 C	 	 7?7;7J7J4&&113TPTT6>::8<<"4&)sm#	s   AC
c                    US:X  a  [         R                  " U5      U l        gUS:X  a  U[        -  U l        gUS:X  a  X l        gSU 3n[        U5      e)z$Set the value of the given property.rx   ry   rz   r{   N)r   r'   r(   r
   r)   r*   r   )r-   rg   valuer/   s       r   setPropertyAVSpeechDriver.setProperty   sU     7?"8"M"Me"TDV^!CCDJX L&tf-C3-r   )r(   r$   r)   r,   r%   r*   N)returnNone)r   zIterator[None])rg   strr   zstr | float | list[Voice])rg   r   r   zstr | float | Noner   r   )__name__
__module____qualname____firstlineno____doc__r   r   python_methodr   r8   r;   rA   rD   typedSelectorr@   rX   r\   rb   rm   rp   r   r   __static_attributes__ r   r   r   r   &   s    &2 
  
' '

C& 
 
& !
& 
4 4!
 
  
 , 

  
 r   r   )r   
__future__r   rH   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   