o
    %&i                     @   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                   @      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   <C:\wamp64\www\opt\env\Lib\site-packages\audioread/rawread.pyr	           r	   c                   @   r   )BitWidthErrorz'The file uses an unsupported bit width.Nr
   r   r   r   r   r   $   r   r   c                 C   sl   t | d dks
J g }tdt | dD ]}| ||d  }tjdgtd|R  }|| qd|S )zSwaps 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
w d| _|   d S z	t | j| _W n tjyB   | jd Y n
w d| _|   d S z	t	 | j| _W n t	jye   | jd Y n
w d| _|   d S | j
  t )Nrbr   TF)open_fhaifc_fileErrorseek_needs_byteswap_checkwavesunaucloser	   )selffilenamer   r   r   __init__:   s:   
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#   r2   r   r   r   r,   g   s   
zRawAudioFile.closec                 C   
   | j  S )zNumber of audio channels.)r%   Zgetnchannelsr2   r   r   r   channelsl      
zRawAudioFile.channelsc                 C   r3   )zSample rate in Hz.)r%   Zgetframerater2   r   r   r   
samplerateq   r5   zRawAudioFile.sampleratec                 C   s   t | j | j S )z)Length of the audio in seconds (a float).)floatr%   Z
getnframesr6   r2   r   r   r   durationv   s   zRawAudioFile.duration   c                 c   sT    | j  }	 | j |}|sdS t||t}| jr&| j  dkr&t|}|V  q)z/Generates blocks of PCM data found in the file.TZ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)r9   )r   r   r   r   r/   r)   r,   propertyr4   r6   r8   r>   r@   rD   rE   r   r   r   r   r    6   s    %



r    )r   r$   r:   r   r+   r*   
exceptionsr   baser   r;   r1   r	   r   r   r    r   r   r   r   <module>   s   