o
    CZŽh·  ã                   @   sl   d dl Z d dlmZ d dlT d dlmZ d dlmZ ddlm	Z	 G dd	„ d	eƒZ
d
d„ ZG dd„ deƒZdS )é    N)ÚNSSpeechSynthesizer)Ú*)Ú	AppHelper)ÚPyObjCAppHelperRunLoopStopperé   )ÚVoicec                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚRunLoopStopperzM
    Overrides PyObjCAppHelperRunLoopStopper to terminate after endLoop.
    c                 C   ó
   d| _ d S ©NF©Z
shouldStop©Úself© r   úC/var/www/auris/lib/python3.10/site-packages/pyttsx3/drivers/nsss.pyÚ__init__   ó   
zRunLoopStopper.__init__c                 C   s   t  t| ¡ ¡ S ©N)ÚobjcÚsuperr   Úinitr   r   r   r   r      ó   zRunLoopStopper.initc                 C   r	   ©NTr   r   r   r   r   Ústop   r   zRunLoopStopper.stopN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r      s
    r   c                 C   s   t  ¡  | ¡S r   )ÚNSSpeechDriverÚallocÚinitWithProxy)Úproxyr   r   r   ÚbuildDriver   s   r!   c                   @   s¬   e Zd Zdd„ Zejdd„ ƒZdd„ Zdd„ Zd	d
„ Z	e
dd„ ƒZdd„ Zejdd„ ƒZdd„ Zejdd„ ƒZejdd„ ƒZejdd„ ƒZejdd„ ƒZdd„ Zdd„ ZdS ) r   c                 C   s   d | _ d | _d| _d| _d S )NFÚ )Ú_proxyÚ_ttsÚ
_completedÚ_current_textr   r   r   r   r   $   s   
zNSSpeechDriver.__init__c                 C   sh   z
t  t| ¡ ¡ }W n ty   | }Y nw |r2|| _t ¡  d ¡| _	| j	 
| ¡ | j	 d¡ d| _| S )NéÈ   T)r   r   r   r   ÚAttributeErrorr#   r   r   ZinitWithVoice_r$   ÚsetDelegate_ÚsetRate_r%   )r   r    Z
proxy_attrr   r   r   r   *   s   ÿzNSSpeechDriver.initWithProxyc                 C   s   | j  d ¡ | ` d S r   )r$   r)   r   r   r   r   Údestroy9   s   zNSSpeechDriver.destroyc                 C   s   | j  d¡ d S r
   ©r#   ÚsetBusy)r   Útimerr   r   r   ÚonPumpFirst_=   r   zNSSpeechDriver.onPumpFirst_c                 C   sŒ   t  d| dd d¡ t ¡ }t ¡  ¡ }t ||¡ | 	¡ r?| 
t¡}t d¡}|d ur0| |¡}| t|¡s;| ¡  n| 	¡ st |¡ d S )Ng        zonPumpFirst:Fr   )ZNSTimerZ@scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_Z	NSRunLoopZcurrentRunLoopr   r   r   r   ZaddRunLoopStopper_toRunLoop_Z	shouldRunZlimitDateForMode_ZNSDefaultRunLoopModeZNSDateZdateWithTimeIntervalSinceNow_ZearlierDate_ZrunMode_beforeDate_r   Z removeRunLoopStopperFromRunLoop_)r   ZrunLoopZstopperZnextfireÚsoonr   r   r   Ú	startLoop@   s    
ÿ


ùzNSSpeechDriver.startLoopc                   C   s   t  ¡  d S r   )r   ZstopEventLoopr   r   r   r   ÚendLoopR   s   zNSSpeechDriver.endLoopc                 c   s    | j  d¡ d V  d S r
   r,   r   r   r   r   ÚiterateV   s   €
zNSSpeechDriver.iteratec                 C   s4   | j  d¡ d| _| j  d¡ || _| j |¡ d S )NTzstarted-utterance)r#   r-   r%   Únotifyr&   r$   ZstartSpeakingString_)r   Útextr   r   r   ÚsayZ   s
   zNSSpeechDriver.sayc                 C   s   | j  ¡ rd| _| j ¡  d S r
   )r#   ZisBusyr%   r$   ZstopSpeakingr   r   r   r   r   b   s   
zNSSpeechDriver.stopc              	   C   s8   t | d¡| d¡| d| d¡¡g| d¡| d¡ƒS )NZVoiceIdentifierZ	VoiceNameZVoiceLocaleIdentifierZVoiceLanguageZVoiceGenderZVoiceAge)r   Úget)r   Úattrr   r   r   Ú_toVoiceg   s   þzNSSpeechDriver._toVoicec                    sx   |dkr‡ fdd„t t ¡ ƒD ƒS |dkrˆ j ¡ S |dkr#ˆ j ¡ S |dkr,ˆ j ¡ S |dkr6tdƒ d S td	| ƒ‚)
NZvoicesc                    s   g | ]
}ˆ   t |¡¡‘qS r   )r9   r   ZattributesForVoice_)Ú.0Úvr   r   r   Ú
<listcomp>p   s    ÿz.NSSpeechDriver.getProperty.<locals>.<listcomp>ÚvoiceÚrateÚvolumeÚpitchú.Pitch adjustment not supported when using NSSSúunknown property %s)	Úlistr   ZavailableVoicesr$   r=   r>   r?   ÚprintÚKeyError)r   Únamer   r   r   ÚgetPropertym   s   

ÿ


zNSSpeechDriver.getPropertyc                 C   s”   |dkr"| j  ¡ }| j  ¡ }| j  |¡ | j  |¡ | j  |¡ d S |dkr.| j  |¡ d S |dkr:| j  |¡ d S |dkrDtdƒ d S td| ƒ‚)Nr=   r>   r?   r@   rA   rB   )r$   r?   r>   Z	setVoice_r*   Z
setVolume_rD   rE   )r   rF   ÚvalueZvolr>   r   r   r   ÚsetProperty}   s   

zNSSpeechDriver.setPropertyc                 C   s0   | j  d¡ d| _tj |¡}| j ||¡ d S r   )r#   r-   r%   Ú
FoundationZNSURLZfileURLWithPath_r$   ZstartSpeakingString_toURL_)r   r5   ÚfilenameÚurlr   r   r   Úsave_to_file   s   zNSSpeechDriver.save_to_filec                 C   s0   | j sd}nd}| jjd|d | j d¡ d S )NFTzfinished-utterance)Ú	completed)r%   r#   r4   r-   )r   ÚttsÚsuccessr   r   r   Ú$speechSynthesizer_didFinishSpeaking_–   s
   z3NSSpeechDriver.speechSynthesizer_didFinishSpeaking_c                 C   s@   | j r| j |j|j|j … }nd}| jjd||j|jd d S )NÚUnknownzstarted-word)rF   ÚlocationÚlength)r&   rS   rT   r#   r4   )r   rO   Úrngr5   Zcurrent_wordr   r   r   Ú)speechSynthesizer_willSpeakWord_ofString_ž   s   
ÿz8NSSpeechDriver.speechSynthesizer_willSpeakWord_ofString_N)r   r   r   r   r   Zpython_methodr   r+   r/   r1   Ústaticmethodr2   r3   r6   r   r9   rG   rI   rM   rQ   rV   r   r   r   r   r   "   s.    






r   )r   ZAppKitr   rJ   ZPyObjCToolsr   ZPyObjCTools.AppHelperr   r=   r   r   r!   ZNSObjectr   r   r   r   r   Ú<module>   s   