a
    )ºDfâ  ã                   @   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   ú^/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/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   sl   t | ƒd dksJ ‚g }tdt | ƒdƒD ]8}| ||d … }tjdgt d|¡¢R Ž }| |¡ 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   z<hz>hó    )ÚlenÚ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y>   | j d¡ Y n0 d| _|  ¡  d S zt  | j¡| _W n  tjy„   | j d¡ Y n0 d| _|  ¡  d S zt	  | j¡| _W n  t	jyÊ   | j d¡ Y n0 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v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!   r0   r   r   r   r*   g   s    
zRawAudioFile.closec                 C   s
   | j  ¡ S )zNumber of audio channels.)r#   Zgetnchannelsr0   r   r   r   Úchannelsl   s    zRawAudioFile.channelsc                 C   s
   | j  ¡ S )zSample rate in Hz.)r#   Zgetframerater0   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
getnframesr2   r0   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#   r.   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   r0   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:   r0   r   r   r   Ú__iter__”   s    zRawAudioFile.__iter__N)r5   )r
   r   r   r   r-   r'   r*   Úpropertyr1   r2   r4   r:   r<   r@   rA   r   r   r   r   r   6   s   %



r   )r   r"   r6   r   r)   r(   Ú
exceptionsr   Úbaser   r7   r/   r   r   r   r   r   r   r   r   Ú<module>   s   