U
    ¬9%eâ  ã                   @   sˆ   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 dZ
dZG dd	„ d	eƒZG d
d„ deƒZdd„ ZG dd„ de	ƒZdS )zBUses standard-library modules to read AIFF, AIFF-C, and WAV files.é    Né   )ÚDecodeError)Ú	AudioFileé   )r   r   é   é   c                   @   s   e Zd ZdZdS )ÚUnsupportedErrorz%File is not an AIFF, WAV, or Au file.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úP/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/audioread/rawread.pyr       s   r   c                   @   s   e Zd ZdZdS )ÚBitWidthErrorz'The file uses an unsupported bit width.Nr	   r   r   r   r   r   $   s   r   c                 C   sh   t | ƒd dkst‚g }tdt | ƒdƒD ]4}| ||d … }tjdt d|¡žŽ }| |¡ q(d |¡S )z¤Swaps the endianness of the bytestring s, which must be an array
    of shorts (16-bit signed integers). This is probably less efficient
    than it should be.
    r   r   ú<hz>hó    )r   )ÚlenÚAssertionErrorÚrangeÚstructÚpackÚunpackÚappendÚjoin)ÚsÚpartsÚiÚchunkZnewchunkr   r   r   Úbyteswap(   s    r   c                   @   sn   e Zd ZdZdd„ Zdd„ Zdd„ Zedd	„ ƒZed
d„ ƒZ	edd„ ƒZ
ddd„Zdd„ Zdd„ Zdd„ ZdS )ÚRawAudioFilez|An AIFF, WAV, or Au file that can be read by the Python standard
    library modules ``wave``, ``aifc``, and ``sunau``.
    c                 C   sø   t |dƒ| _zt  | j¡| _W n" tjk
r@   | j d¡ Y nX d| _|  ¡  d S zt  | j¡| _W n" tjk
rˆ   | j d¡ Y nX d| _|  ¡  d S zt	  | j¡| _W n" t	jk
rÐ   | j d¡ Y nX d| _|  ¡  d S | j 
¡  tƒ ‚d S )NÚrbr   TF)ÚopenÚ_fhÚaifcÚ_fileÚErrorÚseekÚ_needs_byteswapÚ_checkÚwaveÚsunauÚcloser   )ÚselfÚfilenamer   r   r   Ú__init__:   s4    
zRawAudioFile.__init__c                 C   s    | j  ¡ tkr|  ¡  tƒ ‚dS )zeCheck that the files' parameters allow us to decode it and
        raise an error otherwise.
        N)r%   ÚgetsampwidthÚSUPPORTED_WIDTHSr,   r   ©r-   r   r   r   r)   _   s    zRawAudioFile._checkc                 C   s   | j  ¡  | j ¡  dS )zClose the underlying file.N)r%   r,   r#   r2   r   r   r   r,   g   s    
zRawAudioFile.closec                 C   s
   | j  ¡ S )zNumber of audio channels.)r%   Zgetnchannelsr2   r   r   r   Úchannelsl   s    zRawAudioFile.channelsc                 C   s
   | j  ¡ S )zSample rate in Hz.)r%   Zgetframerater2   r   r   r   Ú
samplerateq   s    zRawAudioFile.sampleratec                 C   s   t | j ¡ ƒ| j S )z)Length of the audio in seconds (a float).)Úfloatr%   Z
getnframesr4   r2   r   r   r   Údurationv   s    zRawAudioFile.durationé   c                 c   sR   | j  ¡ }| j  |¡}|sqNt ||t¡}| jrF| j  ¡ dkrFt|ƒ}|V  q
dS )z/Generates blocks of PCM data found in the file.ZsowtN)	r%   r0   Z
readframesÚaudioopZlin2linÚTARGET_WIDTHr(   Úgetcomptyper   )r-   Zblock_samplesZ	old_widthÚdatar   r   r   Ú	read_data{   s    
zRawAudioFile.read_datac                 C   s   | S ©Nr   r2   r   r   r   Ú	__enter__Œ   s    zRawAudioFile.__enter__c                 C   s   |   ¡  dS )NF)r,   )r-   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__   s    zRawAudioFile.__exit__c                 C   s   |   ¡ S r=   )r<   r2   r   r   r   Ú__iter__”   s    zRawAudioFile.__iter__N)r7   )r
   r   r   r   r/   r)   r,   Úpropertyr3   r4   r6   r<   r>   rB   rC   r   r   r   r   r    6   s   %



r    )r   r$   r8   r   r+   r*   Ú
exceptionsr   Úbaser   r9   r1   r   r   r   r    r   r   r   r   Ú<module>   s   