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   E/home/ubuntu/.local/lib/python3.10/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chunk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$   r3   r   r   r   r-   g   s   
zRawAudioFile.closec                 C   
   | j  S )zNumber of audio channels.)r&   getnchannelsr3   r   r   r   channelsl      
zRawAudioFile.channelsc                 C   r4   )zSample rate in Hz.)r&   getframerater3   r   r   r   
samplerateq   r7   zRawAudioFile.sampleratec                 C   s   t | j | j S )z)Length of the audio in seconds (a float).)floatr&   
getnframesr9   r3   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.TsowtN)	r&   r1   
readframesaudiooplin2linTARGET_WIDTHr)   getcomptyper    )r.   block_samples	old_widthdatar   r   r   	read_data{   s   
zRawAudioFile.read_datac                 C   s   | S Nr   r3   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 rH   )rG   r3   r   r   r   __iter__   s   zRawAudioFile.__iter__N)r=   )r   r   r   r   r0   r*   r-   propertyr6   r9   r<   rG   rI   rM   rN   r   r   r   r   r!   6   s    %



r!   )r   r%   r@   r   r,   r+   
exceptionsr   baser   rB   r2   r	   r   r    r!   r   r   r   r   <module>   s   