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d Z
e
dZe
d	Zejej_ejejgej_ejgej_ejej_ejejejejgej_ejej_ejgej_ejej_ejejgej_ejej_ejejejejgej_ejej_ejejejejgej_ejej_ejejejgej_ejej_ejgej_d
d ZedZedZedZ edZ!dZ"dZ#dZ$dZ%edZ&edZ'dZ(G dd deZ)dd Z*G dd dZ+G dd de+Z,G dd dej-Z.G d d! d!ej-Z/G d"d# d#ej-Z0G d$d% d%e	Z1dS )&z-Read audio files using CoreAudio on Mac OS X.    N   )DecodeError)	AudioFilec                 C   s   t jt j| S N)ctypesZcdllZLoadLibraryutilZfind_library)name r	   ^/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/audioread/macca.py_load_framework   s   r   ZAudioToolboxZCoreFoundationc                 C   s>   d}t | D ]\}}t| | d d }|t||> O }q|S )zEmulates character integer literals in C. Given a string "abc",
    returns the value of the C single-quoted literal 'abc'.
    r   r      )	enumeratelenord)charsnumindexcharshiftr	   r	   r
   multi_char_literalP   s
   r   ZffmtZcfmtz#frmZlpcm      r   ztyp?zfmt?ic                       s   e Zd Z fddZ  ZS )MacErrorc                    s4   |t krd}n|tkrd}nd| }t | d S )Nzunsupported audio typezunsupported formatzerror %i)
ERROR_TYPEERROR_FORMATsuper__init__)selfcodemsg	__class__r	   r
   r   k   s   zMacError.__init__)__name__
__module____qualname__r   __classcell__r	   r	   r    r
   r   j   s    r   c                 C   s$   | t krtd| dkrt| dS )z.If err is nonzero, raise a MacError exception.zfile not foundr   N)ERROR_NOT_FOUNDOSErrorr   )errr	   r	   r
   checku   s
   r)   c                   @   s   e Zd Zdd Zdd ZdS )CFObjectc                 C   s   |dkrt d|| _d S )Nr   zobject is zero)
ValueError_obj)r   objr	   r	   r
   r      s   
zCFObject.__init__c                 C   s   t r
t | j d S d S r   )_corefoundation	CFReleaser,   r   r	   r	   r
   __del__   s   zCFObject.__del__N)r"   r#   r$   r   r1   r	   r	   r	   r
   r*      s    r*   c                       s$   e Zd Z fddZdd Z  ZS )CFURLc                    sP   t jt j|}t|ts|t }t	
d|t|d}t | d S )Nr   F)ospathabspath
expanduser
isinstancebytesencodesysgetfilesystemencodingr.   'CFURLCreateFromFileSystemRepresentationr   r   r   r   filenameurlr    r	   r
   r      s   
zCFURL.__init__c                 C   s   t | j}t |d}|S )Nr   )r.   CFURLGetStringr,   CFStringGetCStringPtr)r   Zcfstroutr	   r	   r
   __str__   s   zCFURL.__str__)r"   r#   r$   r   rC   r%   r	   r	   r    r
   r2      s    	r2   c                
   @   sX   e Zd Zdejfdejfdejfdejfdejfdejfdejfdejfd	ejfg	Zd
S )AudioStreamBasicDescriptionmSampleRate	mFormatIDmFormatFlagsmBytesPerPacketmFramesPerPacketmBytesPerFramemChannelsPerFramemBitsPerChannelZ	mReservedN)r"   r#   r$   r   Zc_doublec_uint_fields_r	   r	   r	   r
   rD      s    rD   c                   @   s(   e Zd ZdejfdejfdejfgZdS )AudioBuffermNumberChannelsmDataByteSizemDataN)r"   r#   r$   r   rM   c_void_prN   r	   r	   r	   r
   rO      s
    rO   c                   @   s"   e Zd Zdejfded fgZdS )AudioBufferListmNumberBuffersmBuffersr   N)r"   r#   r$   r   rM   rO   rN   r	   r	   r	   r
   rT      s    
rT   c                   @   s   e Zd ZdZdd Zedd Zdd Zdd	 Ze	d
d Z
e	dd Ze	dd Ze	dd Zd#ddZd$ddZdd Zdd Zdd Zdd Zd d! Zd"S )%ExtAudioFileaj  A CoreAudio "extended audio file". Reads information and raw PCM
    audio data from any file that CoreAudio knows how to decode.

        >>> with ExtAudioFile('something.m4a') as f:
        >>>     print f.samplerate
        >>>     print f.channels
        >>>     print f.duration
        >>>     for block in f:
        >>>         do_something(block)

    c                 C   sH   t |}z| || _W n   d| _ ~d| _d | _d | _|   d S )NTF)r2   	_open_urlr,   closed	_file_fmt_client_fmtsetupr=   r	   r	   r
   r      s   zExtAudioFile.__init__c                 C   s$   t  }tt|jt | |S )zGGiven a CFURL Python object, return an opened ExtAudioFileRef.
        )r   rS   r)   
_coreaudioExtAudioFileOpenURLr,   byref)clsr?   Zfile_objr	   r	   r
   rX      s
   zExtAudioFile._open_urlc              	   C   s:   |j tksJ tt| jtt|t	| || _
dS )zGet the client format description. This describes the
        encoding of the data that the program will read from this
        object.
        N)rF   AUDIO_ID_PCMr)   r]   ExtAudioFileSetPropertyr,   PROP_CLIENT_DATA_FORMATr   sizeofr_   r[   )r   descr	   r	   r
   set_client_format   s   
zExtAudioFile.set_client_formatc              	   C   sR   | j dur| j S t }tt|}tt| jt	t
|t
| || _ |S )zaGet the file format description. This describes the type of
        data stored on disk.
        N)rZ   rD   r   c_intrd   r)   r]   ExtAudioFileGetPropertyr,   PROP_FILE_DATA_FORMATr_   )r   re   sizer	   r	   r
   get_file_format   s   
zExtAudioFile.get_file_formatc                 C      t |  jS )z+The number of channels in the audio source.)intrk   rK   r0   r	   r	   r
   channels     zExtAudioFile.channelsc                 C   rl   )z"Gets the sample rate of the audio.)rm   rk   rE   r0   r	   r	   r
   
samplerate  ro   zExtAudioFile.sampleratec                 C   s   t | j| j S )z1Gets the length of the file in seconds (a float).)floatnframesrp   r0   r	   r	   r
   duration  s   zExtAudioFile.durationc              	   C   s@   t  }t t |}tt| jtt 	|t 	| |j
S )z-Gets the number of frames in the source file.)r   Zc_longrg   rd   r)   r]   rh   r,   PROP_LENGTHr_   value)r   lengthrj   r	   r	   r
   rr     s   zExtAudioFile.nframes   c                 C   sV   |   }t|}t|_ttB |_||_|j|j d |_	d|_
|j	|_| | dS )zSet the client format parameters, specifying the desired PCM
        audio data format to be read from the file. Must be called
        before reading from the file.
        r   r   N)rk   copyra   rF   PCM_IS_SIGNED_INTPCM_IS_PACKEDrG   rL   rK   rH   rI   rJ   rf   )r   ZbitdepthfmtZnewfmtr	   r	   r
   r\     s   
zExtAudioFile.setup   c                 c   s    t || jj }t |}t }d|_| jj|jd _	||jd _
t |t j|jd _	 tt| jt |t | |jdksFJ |jd j
}|sPdS t |jd jt t j}|d| }|V  q/)zFGenerates byte strings reflecting the audio data in the file.
        r   r   TN)r   rM   r[   rJ   Zcreate_string_bufferrT   rU   rK   rV   rP   rQ   castrS   rR   r)   r]   ExtAudioFileReadr,   r_   ZPOINTERZc_char)r   	blocksizeframesbufZbuflistrj   dataZblobr	   r	   r
   	read_data,  s.   


zExtAudioFile.read_datac                 C   s$   | j stt| j d| _ dS dS )z0Close the audio file and free associated memory.TN)rY   r)   r]   ExtAudioFileDisposer,   r0   r	   r	   r
   closeH  s   
zExtAudioFile.closec                 C   s   t r|   d S d S r   )r]   r   r0   r	   r	   r
   r1   N  s   zExtAudioFile.__del__c                 C   s   | S r   r	   r0   r	   r	   r
   	__enter__S  s   zExtAudioFile.__enter__c                 C   s   |    dS )NF)r   )r   exc_typeexc_valexc_tbr	   r	   r
   __exit__V  s   zExtAudioFile.__exit__c                 C   s   |   S r   )r   r0   r	   r	   r
   __iter__[  s   zExtAudioFile.__iter__N)rw   )r|   )r"   r#   r$   __doc__r   classmethodrX   rf   rk   propertyrn   rp   rs   rr   r\   r   r   r1   r   r   r   r	   r	   r	   r
   rW      s,    
	




	
rW   )2r   rx   r   Zctypes.utilr3   r:   
exceptionsr   baser   r   r]   r.   c_char_prA   restyperS   rg   argtypesr/   r<   Zc_boolr@   r^   rb   rM   rh   r~   r   r   ri   rc   rt   ra   ZPCM_IS_FLOATZPCM_IS_BIG_ENDIANry   rz   r   r   r&   r   r)   r*   r2   Z	StructurerD   rO   rT   rW   r	   r	   r	   r
   <module>   sn   







	