o
    i                     @   s   d dl Z d dlmZmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlmZmZ d dlmZ d dlmZ d dlmZmZmZmZmZ d d	lmZmZ d d
lmZ G dd deZdS )    N)cached_propertypartial)BytesIO)AnyIteratorListOptional)aformatload_url)pad)Graph)
UINT32_MAX
AudioFrameDtypeFilterSeconds)pyav	soundfile)Infoc                       s   e Zd Z											ddededee deee  d	ee d
ee	 de
dee	 de
de
dee deee  f fddZede	fddZdee fddZdd	ee d
ee	 de
de
fddZdde
defddZ  ZS ) Reader        NFTfileoffsetdurationfiltersdtyperateto_mono
frame_size	cache_url	always_2d
fill_valuebackendsc                    sX  t |tr
t|}n2t |tr<d|v r<tj|dd}|jdv r%|jd}|	r.t	|dd}n|dkr<|d	u r<t	|dd}t
 j|||d
 t | jtrUt| jj|d| j_|d	u r[g n|| _| |||sp| jt|||d d	| _t| jdkrt | jtrt| jj| jd| j_nt| j| j| jj| j| j| jd| _|| _|| _|
| _ || _!d	S )a(  
        Create a Reader object.

        Args:
            file: The audio file, audio url, path to audio file, bytes of audio data, etc.
            offset: The offset of the audio to load.
            duration: The duration of the audio to load.
            filters: The filters to apply to the audio.
            dtype: The data type of the audio frames.
            rate: The sample rate of the audio frames.
            to_mono: Whether to convert the audio frames to mono.
            frame_size: The frame size of the audio frames.
            cache_url: Whether to cache the audio file.
            always_2d: Whether to return 2d ndarrays even if the audio frame is mono.
            fill_value: The fill value to pad the audio to the frame size.
            backends: The backends to use.
        z://F)allow_redirects)i-  i.  i/  i3  i4  LocationT)cacher   N)r"   )r   )r   r   )r   )r   r   	is_planarchannelsr   r   )"
isinstancebytesr   strrequestsheadstatus_codeheadersgetr
   super__init__backendr   r   readr   is_passthroughappendr	   graphlenr   build_graphr   r   r   r&   num_channelsr   r   	_durationr    r!   )selfr   r   r   r   r   r   r   r   r   r    r!   r"   response	__class__ J/home/ubuntu/.local/lib/python3.10/site-packages/audiolab/reader/reader.pyr1      s@   
 


zReader.__init__returnc                 C   s   | j jS )N)r2   r   )r;   r?   r?   r@   r   a   s   zReader.frame_sizec                 c   s
   | j | j| jD ]h}| jd u r8| j}t| j tr|\}}| jd ur+t	|| j
| j}| jr0|n| |fV  q
t|jd td|jd  |jj d }td|jd |D ]}|d d ||| f }| j| |  E d H  qVq
| jd ur| jddE d H  d S d S )N   i   r      Tr   )r2   
load_audior   r:   r6   r   r(   r   r!   r   r   r    squeezeminshapeintr   itemsizerangepushpull)r;   framer   
max_lengthichunkr?   r?   r@   __iter__e   s$   

*
zReader.__iter__c                 C   s`   |d u p|| j k}|o|d u p| j|k}|o|o| jdk }|o$| jtk}|o-t| jdk}|S )NrB   r   )r   r   r9   r   r   r7   r   )r;   r   r   r   passthroughr?   r?   r@   r4   x   s   zReader.is_passthroughr   c                 c   sT    | j j|dD ]}|\}}| jd urt|| j| j}| jr |n| |fV  qd S )NrD   )r6   rM   r!   r   r   r    rF   )r;   r   rN   r   r?   r?   r@   rM      s   
zReader.pull)r   NNNNFNFTNN)NNF)F)__name__
__module____qualname__r   r   r   r   r   r   rI   boolfloatr*   r1   r   r   r   r   rR   r4   rM   __classcell__r?   r?   r=   r@   r      sT    
	

D$r   )r+   	functoolsr   r   ior   typingr   r   r   r   audiolab.avr	   r
   audiolab.av.framer   audiolab.av.graphr   audiolab.av.typingr   r   r   r   r   audiolab.reader.backendr   r   audiolab.reader.infor   r   r?   r?   r?   r@   <module>   s   