o
    i)                     @   s  d dl Z d dlT d dlmZ d dlmZ G dd deZG dd deZG d	d
 d
eZ	G dd deZ
G dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZg dZdS )     N)*)Enum)Sequencec                       s\   e Zd Zddedee f fddZdd Zed	efd
dZed	ee fddZ	  Z
S )
KoalaError Nmessagemessage_stackc                    s0   t  | || _|d u rt | _d S || _d S N)super__init___messagelist_message_stack)selfr   r   	__class__ B/home/ubuntu/.local/lib/python3.10/site-packages/pvkoala/_koala.pyr      s   zKoalaError.__init__c                 C   sJ   | j }t| jdkr#|d7 }tt| jD ]}|d|| j| f 7 }q|S )Nr   :z

  [%d] %s)r   lenr   range)r   r   ir   r   r   __str__   s   zKoalaError.__str__returnc                 C      | j S r	   )r   r   r   r   r   r   !      zKoalaError.messagec                 C   r   r	   )r   r   r   r   r   r   %   r   zKoalaError.message_stack)r   N)__name__
__module____qualname__strr   r   r   propertyr   r   __classcell__r   r   r   r   r      s    r   c                   @      e Zd ZdS )KoalaMemoryErrorNr   r   r   r   r   r   r   r$   *       r$   c                   @   r#   )KoalaIOErrorNr%   r   r   r   r   r'   .   r&   r'   c                   @   r#   )KoalaInvalidArgumentErrorNr%   r   r   r   r   r(   2   r&   r(   c                   @   r#   )KoalaStopIterationErrorNr%   r   r   r   r   r)   6   r&   r)   c                   @   r#   )KoalaKeyErrorNr%   r   r   r   r   r*   :   r&   r*   c                   @   r#   )KoalaInvalidStateErrorNr%   r   r   r   r   r+   >   r&   r+   c                   @   r#   )KoalaRuntimeErrorNr%   r   r   r   r   r,   B   r&   r,   c                   @   r#   )KoalaActivationErrorNr%   r   r   r   r   r-   F   r&   r-   c                   @   r#   )KoalaActivationLimitErrorNr%   r   r   r   r   r.   J   r&   r.   c                   @   r#   )KoalaActivationThrottledErrorNr%   r   r   r   r   r/   N   r&   r/   c                   @   r#   )KoalaActivationRefusedErrorNr%   r   r   r   r   r0   R   r&   r0   c                   @   s  e Zd ZdZG dd deZejeeje	ej
eejeejeejeejeejeejeejeejeiZG dd deZdededed	d
fddZ de!e" d	e!e" fddZ#dddZ$dddZ%e&d	e"fddZ'e&d	e"fddZ(e&d	e"fddZ)e&d	efddZ*d	e!e fddZ+d
S )Koalaz<
    Python binding for Koala noise-suppression engine.
    c                   @   s<   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdS )zKoala.PicovoiceStatusesr                           	   
      N)r   r   r   SUCCESSOUT_OF_MEMORYIO_ERRORINVALID_ARGUMENTSTOP_ITERATION	KEY_ERRORINVALID_STATERUNTIME_ERRORACTIVATION_ERRORACTIVATION_LIMIT_REACHEDACTIVATION_THROTTLEDACTIVATION_REFUSEDr   r   r   r   PicovoiceStatuses[   s    rI   c                   @   r#   )zKoala.CKoalaNr%   r   r   r   r   CKoalaw   r&   rJ   
access_key
model_pathlibrary_pathr   Nc                 C   s>  t |trt|dkrtdtj|std| tj|s'td| t	|}|j
}tg|_d|_|dd |j| _tttttg| j_| j| j_|j| _ttg| j_d| j_|j}tttt| jg|_| j|_t| j | _|| | t| j}|| jjur| j| d|  d	|j| _t| jg| j_d| j_|j}t| jtt g|_| j|_t  }	|| j|	}|| jjur| !  | j| d
|  d	|	j"| _#|j$| _%t| jtt&tt&g| j%_| j| j%_|j'| _(t| jg| j(_| j| j(_|) | _*|+ | _,|j-}
g |
_t|
_|
 .d| _/dS )a  
        Constructor.

        :param access_key: AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
        :param model_path: Absolute path to the file containing model parameters.
        :param library_path: Absolute path to Koala's dynamic library.
        r   z*`access_key` should be a non-empty string.z"Could not find model file at `%s`.z/Could not find Koala's dynamic library at `%s`.Npythonutf-8zInitialization failedr   r   zFailed to get delay samples)0
isinstancer    r   r(   ospathexistsr'   cdllLoadLibrary
pv_set_sdkc_char_pargtypesrestypeencodepv_get_error_stack_get_error_stack_funcPOINTERc_intrI   pv_free_error_stack_free_error_stack_funcpv_koala_initrJ   _handlebyrefr=   _PICOVOICE_STATUS_TO_EXCEPTION_get_error_stackpv_koala_delete_delete_funcpv_koala_delay_samplec_int32deletevalue_delay_samplepv_koala_process_process_funcc_shortpv_koala_reset_reset_funcpv_sample_rate_sample_ratepv_koala_frame_length_frame_lengthpv_koala_versiondecode_version)r   rK   rL   rM   libraryset_sdk_func	init_funcstatusdelay_sample_funcdelay_sampleversion_funcr   r   r   r   z   sz   





zKoala.__init__pcmc                 C   sv   t || jkrtdt || jf t| j }|| }| }| | j||}|| jjur7| j| d| 	 dt
|S )a  
        Processes a frame of audio and returns delayed enhanced audio.

        :param pcm: A frame of audio samples. The number of samples per frame can be attained by calling
        `.frame_length`. The incoming audio needs to have a sample rate equal to `.sample_rate` and be 16-bit
        linearly-encoded. Koala operates on single-channel audio. Consecutive calls to `.process()` must provide
        consecutive frames of audio from the same source, unless `.reset()` has been called in between.

        :return: A frame of enhanced audio samples, stored as a sequence of 16-bit linearly-encoded integers.
        The output is not directly the enhanced version of the input PCM, but corresponds to samples that were given in
        previous calls to `.process()`. The delay in samples between the start time of the input frame and the start
        time of the output frame can be attained from `.delay_sample`.
        z@Length of input frame %d does not match required frame length %dzProcessing failedrP   )r   frame_lengthr(   rp   ro   rc   rI   r=   re   rf   r   )r   r   
frame_typeenhanced_pcmr}   r   r   r   process   s   
zKoala.processc                 C   s2   |  | j}|| jjur| j| d|  ddS )z
        Resets Koala into a state as if it had just been newly created.
        Call this function in between calls to `process` that do not provide consecutive frames of audio.
        zReset failedrP   N)rr   rc   rI   r=   re   rf   )r   r}   r   r   r   reset   s   zKoala.resetc                 C   s   |  | j dS )z%Releases resources acquired by Koala.N)rh   rc   r   r   r   r   rk     s   zKoala.deletec                 C   r   )z)Audio sample rate accepted by `.process`.)rt   r   r   r   r   sample_rate     zKoala.sample_ratec                 C   r   )z9Number of audio samples per frame expected by `.process`.)rv   r   r   r   r   r     r   zKoala.frame_lengthc                 C   r   )z
        Delay in samples. If the input and output of consecutive calls to `.process()` are viewed as two contiguous
        streams of audio data, this delay specifies the time shift between the input and output stream.)rm   r   r   r   r   r     s   zKoala.delay_samplec                 C   r   )zVersion.)ry   r   r   r   r   version  r   zKoala.versionc                 C   sx   t t }t }| t|t|}|| jjur | j| ddt }t	|j
D ]}||| d q(| | |S )NzUnable to get Koala error state)r   rO   )r^   rX   r_   r]   rd   rI   r=   re   r   r   rl   appendrx   ra   )r   message_stack_refmessage_stack_depthr}   r   r   r   r   r   rf      s   

zKoala._get_error_stack)r   N),r   r   r   __doc__r   rI   r>   r$   r?   r'   r@   r(   rA   r)   rB   r*   rC   r+   rD   r,   rE   r-   rF   r.   rG   r/   rH   r0   re   	StructurerJ   r    r   r   intr   r   rk   r!   r   r   r   r   rf   r   r   r   r   r1   V   sJ    
[
 
r1   )r1   r-   r.   r0   r/   r   r'   r(   r+   r*   r$   r,   r)   )rR   ctypesenumr   typingr   	Exceptionr   r$   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   objectr1   __all__r   r   r   r   <module>   s&    [