o
    CZŽh\  ã                   @   s0   d dl mZ ddlZddlZG dd„ deƒZdS )é   )Údriveré    Nc                   @   sˆ   e Zd ZdZd!dd„Zdd„ Zdd	„ Zd
d„ Zd"dd„Zdd„ Z	d"dd„Z
dd„ Zdd„ Zdd„ Zdd„ Zd#dd„Zdd„ Zdd „ ZdS )$ÚEngineaz  
    @ivar proxy: Proxy to a driver implementation
    @type proxy: L{DriverProxy}
    @ivar _connects: Array of subscriptions
    @type _connects: list
    @ivar _inLoop: Running an event loop or not
    @type _inLoop: bool
    @ivar _driverLoop: Using a driver event loop or not
    @type _driverLoop: bool
    @ivar _debug: Print exceptions or not
    @type _debug: bool
    NFc                 C   s2   t  t | ¡||¡| _i | _d| _d| _|| _dS )a-  
        Constructs a new TTS engine instance.

        @param driverName: Name of the platform specific driver to use. If
            None, selects the default driver for the operating system.
        @type: str
        @param debug: Debugging output enabled or not
        @type debug: bool
        FTN)r   ZDriverProxyÚweakrefÚproxyÚ	_connectsÚ_inLoopÚ_driverLoopÚ_debug)ÚselfZ
driverNameÚdebug© r   ú=/var/www/auris/lib/python3.10/site-packages/pyttsx3/engine.pyÚ__init__   s
   

zEngine.__init__c              	   K   sJ   | j  |g ¡D ]}z	|di |¤Ž W q ty"   | jr t ¡  Y qw dS )zÌ
        Invokes callbacks for an event topic.

        @param topic: String event name
        @type topic: str
        @param kwargs: Values associated with the event
        @type kwargs: dict
        Nr   )r   ÚgetÚ	Exceptionr
   Ú	tracebackÚ	print_exc)r   ÚtopicÚkwargsÚcbr   r   r   Ú_notify%   s   	€þýzEngine._notifyc                 C   s"   | j  |g ¡}| |¡ ||dœS )aþ  
        Registers a callback for an event topic. Valid topics and their
        associated values:

        started-utterance: name=<str>
        started-word: name=<str>, location=<int>, length=<int>
        finished-utterance: name=<str>, completed=<bool>
        error: name=<str>, exception=<exception>

        @param topic: Event topic name
        @type topic: str
        @param cb: Callback function
        @type cb: callable
        @return: Token to use to unregister
        @rtype: dict
        )r   r   )r   Ú
setdefaultÚappend)r   r   r   Úarrr   r   r   Úconnect5   s   

zEngine.connectc                 C   sV   |d }z| j | }W n
 ty   Y dS w | |d ¡ t|ƒdkr)| j |= dS dS )z‘
        Unregisters a callback for an event topic.

        @param token: Token of the callback to unregister
        @type token: dict
        r   Nr   r   )r   ÚKeyErrorÚremoveÚlen)r   Útokenr   r   r   r   r   Ú
disconnectJ   s   ÿÿzEngine.disconnectc                 C   s   |dkrdS | j  ||¡ dS )a  
        Adds an utterance to speak to the event queue.

        @param text: Text to speak
        @type text: unicode
        @param name: Name to associate with this utterance. Included in
            notifications about this utterance.
        @type name: str
        Nz%Argument value can't be none or empty)r   Úsay)r   ÚtextÚnamer   r   r   r!   Z   s   
z
Engine.sayc                 C   s   | j  ¡  dS )zI
        Stops the current utterance and clears the event queue.
        N)r   Ústop©r   r   r   r   r$   i   s   zEngine.stopc                 C   s   | j  |||¡ dS )aC  
        Adds an utterance to speak to the event queue.

        @param text: Text to speak
        @type text: unicode
        @param filename: the name of file to save.
        @param name: Name to associate with this utterance. Included in
            notifications about this utterance.
        @type name: str
        N)r   Úsave_to_file)r   r"   Úfilenamer#   r   r   r   r&   o   s   zEngine.save_to_filec                 C   s
   | j  ¡ S )zl
        @return: True if an utterance is currently being spoken, false if not
        @rtype: bool
        )r   ÚisBusyr%   r   r   r   r(   |   s   
zEngine.isBusyc                 C   s   | j  |¡S )az  
        Gets the current value of a property. Valid names and values include:

        voices: List of L{voice.Voice} objects supported by the driver
        voice: String ID of the current voice
        rate: Integer speech rate in words per minute
        volume: Floating point volume of speech in the range [0.0, 1.0]

        Numeric values outside the valid range supported by the driver are
        clipped.

        @param name: Name of the property to fetch
        @type name: str
        @return: Value associated with the property
        @rtype: object
        @raise KeyError: When the property name is unknown
        )r   ÚgetProperty)r   r#   r   r   r   r)   ƒ   s   zEngine.getPropertyc                 C   s   | j  ||¡ dS )a8  
        Adds a property value to set to the event queue. Valid names and values
        include:

        voice: String ID of the voice
        rate: Integer speech rate in words per minute
        volume: Floating point volume of speech in the range [0.0, 1.0]

        Numeric values outside the valid range supported by the driver are
        clipped.

        @param name: Name of the property to fetch
        @type name: str
        @param: Value to set for the property
        @rtype: object
        @raise KeyError: When the property name is unknown
        N)r   ÚsetProperty)r   r#   Úvaluer   r   r   r*   —   s   zEngine.setPropertyc                 C   s(   | j rtdƒ‚d| _ d| _| j ¡  dS )zõ
        Runs an event loop until all commands queued up until this method call
        complete. Blocks during the event loop and returns when the queue is
        cleared.

        @raise RuntimeError: When the loop is already running
        úrun loop already startedTN)r   ÚRuntimeErrorr	   r   Ú
runAndWaitr%   r   r   r   r.   «   s
   zEngine.runAndWaitTc                 C   s,   | j rtdƒ‚d| _ || _| j | j¡ dS )a™  
        Starts an event loop to process queued commands and callbacks.

        @param useDriverLoop: If True, uses the run loop provided by the driver
            (the default). If False, assumes the caller will enter its own
            run loop which will pump any events for the TTS engine properly.
        @type useDriverLoop: bool
        @raise RuntimeError: When the loop is already running
        r,   TN)r   r-   r	   r   Ú	startLoop)r   ZuseDriverLoopr   r   r   r/   ¹   s
   
zEngine.startLoopc                 C   s&   | j stdƒ‚| j | j¡ d| _ dS )zh
        Stops a running event loop.

        @raise RuntimeError: When the loop is not running
        úrun loop not startedFN)r   r-   r   ÚendLoopr	   r%   r   r   r   r1   É   s   
zEngine.endLoopc                 C   s*   | j stdƒ‚| jrtdƒ‚| j ¡  dS )zM
        Must be called regularly when using an external event loop.
        r0   z$iterate not valid in driver run loopN)r   r-   r	   r   Úiterater%   r   r   r   r2   Ô   s
   zEngine.iterate)NF)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r!   r$   r&   r(   r)   r*   r.   r/   r1   r2   r   r   r   r   r      s     



r   )Ú r   r   r   Úobjectr   r   r   r   r   Ú<module>   s    