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)ctypescdllLoadLibraryutilZfind_library)name r   :C:\wamp64\www\opt\env\Lib\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   r3   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getfilesystemencodingr0   'CFURLCreateFromFileSystemRepresentationr   r   r   r   filenameurlr"   r   r   r      s   
zCFURL.__init__c                 C   s   t | j}t |d}|S )Nr   )r0   CFURLGetStringr.   CFStringGetCStringPtr)r   Zcfstroutr   r   r   __str__   s   zCFURL.__str__)r$   r%   r&   r   rE   r'   r   r   r"   r   r4      s    	r4   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   c_doublec_uint_fields_r   r   r   r   rF      s    rF   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   rP   c_void_prQ   r   r   r   r   rR      s
    rR   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   rP   rR   rQ   r   r   r   r   rW      s    
rW   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)r4   	_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   rV   r+   
_coreaudioExtAudioFileOpenURLr.   byref)clsrA   Zfile_objr   r   r   r[      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)rH   AUDIO_ID_PCMr+   r`   ExtAudioFileSetPropertyr.   PROP_CLIENT_DATA_FORMATr   sizeofrb   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)r]   rF   r   c_intrg   r+   r`   ExtAudioFileGetPropertyr.   PROP_FILE_DATA_FORMATrb   )r   rh   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.)intrn   rM   r2   r   r   r   channels     zExtAudioFile.channelsc                 C   ro   )z"Gets the sample rate of the audio.)rp   rn   rG   r2   r   r   r   
samplerate  rr   zExtAudioFile.sampleratec                 C   s   t | j| j S )z1Gets the length of the file in seconds (a float).)floatnframesrs   r2   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   c_longrj   rg   r+   r`   rk   r.   PROP_LENGTHrb   value)r   lengthrm   r   r   r   ru     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)rn   copyrd   rH   PCM_IS_SIGNED_INTPCM_IS_PACKEDrI   rN   rM   rJ   rK   rL   ri   )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   rP   r^   rL   create_string_bufferrW   rX   rM   rY   rS   rT   castrV   rU   r+   r`   ExtAudioFileReadr.   rb   POINTERc_char)r   	blocksizeframesbufZbuflistrm   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)r\   r+   r`   ExtAudioFileDisposer.   r2   r   r   r   closeH  s   
zExtAudioFile.closec                 C   s   t r|   d S d S r   )r`   r   r2   r   r   r   r3   N  s   zExtAudioFile.__del__c                 C   s   | S r   r   r2   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   r2   r   r   r   __iter__[  s   zExtAudioFile.__iter__N)r{   )r   )r$   r%   r&   __doc__r   classmethodr[   ri   rn   propertyrq   rs   rv   ru   r_   r   r   r3   r   r   r   r   r   r   r   rZ      s,    
	




	
rZ   )2r   r|   r   Zctypes.utilr5   r<   
exceptionsr   baser   r   r`   r0   c_char_prC   restyperV   rj   argtypesr1   r>   c_boolrB   ra   re   rP   rk   r   r   r   rl   rf   rx   rd   ZPCM_IS_FLOATZPCM_IS_BIG_ENDIANr}   r~   r   r   r(   r   r+   r,   r4   	StructurerF   rR   rW   rZ   r   r   r   r   <module>   sn   







	