U
    Ķb                     @   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   Q/var/www/html/UseVoiceDocker/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 )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   <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(   Z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_typeZexc_valZ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=   r?   r@   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   