U
    9%e*                     @   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utilfind_library)name r
   N/var/www/html/Darija-Ai-API/env/lib/python3.8/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n| 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 r   )_corefoundation	CFReleaser-   r   r
   r
   r   __del__   s    zCFObject.__del__N)r#   r$   r%   r   r2   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 |ts|t }tjtj|}t	
d|t|d}t | d S )Nr   F)
isinstancebytesencodesysgetfilesystemencodingospathabspath
expanduserr/   '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   rD   r&   r
   r
   r!   r   r3      s   	r3   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   rE      s   rE   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   rN   c_void_prO   r
   r
   r
   r   rP      s   rP   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   rN   rP   rO   r
   r
   r
   r   rU      s   
rU   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   sN   t |}z| || _W n   d| _ Y nX ~d| _d | _d | _|   d S )NTF)r3   	_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   rT   r*   
_coreaudioExtAudioFileOpenURLr-   byref)clsr@   Zfile_objr
   r
   r   rY      s     zExtAudioFile._open_urlc              	   C   s:   |j tkst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)rG   AUDIO_ID_PCMAssertionErrorr*   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k	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)r[   rE   r   c_intrf   r*   r^   ExtAudioFileGetPropertyr-   PROP_FILE_DATA_FORMATr`   )r   rg   sizer
   r
   r   get_file_format   s    
  zExtAudioFile.get_file_formatc                 C   s   t |  jS )z+The number of channels in the audio source.)intrm   rL   r1   r
   r
   r   channels  s    zExtAudioFile.channelsc                 C   s   t |  jS )z"Gets the sample rate of the audio.)rn   rm   rF   r1   r
   r
   r   
samplerate  s    zExtAudioFile.sampleratec                 C   s   t | j| j S )z1Gets the length of the file in seconds (a float).)floatnframesrp   r1   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_longri   rf   r*   r^   rj   r-   PROP_LENGTHr`   value)r   lengthrl   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)rm   copyrb   rG   PCM_IS_SIGNED_INTPCM_IS_PACKEDrH   rM   rL   rI   rJ   rK   rh   )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st|jd j
}|sqt |jd jt t j}|d| }|V  qZdS )zFGenerates byte strings reflecting the audio data in the file.
        r   r   N)r   rN   r\   rK   create_string_bufferrU   rV   rL   rW   rQ   rR   castrT   rS   r*   r^   ExtAudioFileReadr-   r`   rc   ZPOINTERZc_char)r   	blocksizeframesbufZbuflistrl   dataZblobr
   r
   r   	read_data,  s,    

  
zExtAudioFile.read_datac                 C   s    | j stt| j d| _ dS )z0Close the audio file and free associated memory.TN)rZ   r*   r^   ExtAudioFileDisposer-   r1   r
   r
   r   closeH  s    zExtAudioFile.closec                 C   s   t r|   d S r   )r^   r   r1   r
   r
   r   r2   N  s    zExtAudioFile.__del__c                 C   s   | S r   r
   r1   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   r1   r
   r
   r   __iter__[  s    zExtAudioFile.__iter__N)rw   )r|   )r#   r$   r%   __doc__r   classmethodrY   rh   rm   propertyro   rp   rs   rr   r]   r   r   r2   r   r   r   r
   r
   r
   r   rX      s*   
	



	

rX   )2r   rx   r   ctypes.utilr9   r7   
exceptionsr   baser   r   r^   r/   c_char_prB   restyperT   ri   Zargtypesr0   r=   Zc_boolrA   r_   rd   rN   rj   r   r   r   rk   re   rt   rb   ZPCM_IS_FLOATZPCM_IS_BIG_ENDIANry   rz   r   r   r'   r   r*   r+   r3   Z	StructurerE   rP   rU   rX   r
   r
   r
   r   <module>   sl   







	