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utilfind_library)name r   C/home/ubuntu/.local/lib/python3.10/site-packages/audioread/macca.py_load_framework   s   r   AudioToolbox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   ffmtcfmtz#frm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
   r1   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	CFReleaser4   r%   r   r   r   __del__   s   zCFObject.__del__N)r*   r+   r,   r$   r9   r   r   r   r   r2      s    r2   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getfilesystemencodingr6   'CFURLCreateFromFileSystemRepresentationr   r#   r$   r%   filenameurlr(   r   r   r$      s   
zCFURL.__init__c                 C   s   t | j}t |d}|S )Nr   )r6   CFURLGetStringr4   CFStringGetCStringPtr)r%   cfstroutr   r   r   __str__   s   zCFURL.__str__)r*   r+   r,   r$   rL   r-   r   r   r(   r   r:      s    	r:   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mBitsPerChannel	mReservedN)r*   r+   r,   r   c_doublec_uint_fields_r   r   r   r   rM      s    rM   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   rX   c_void_prY   r   r   r   r   rZ      s
    rZ   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   rX   rZ   rY   r   r   r   r   r_      s    
r_   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)r:   	_open_urlr4   closed	_file_fmt_client_fmtsetuprE   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   r^   r1   
_coreaudioExtAudioFileOpenURLr4   byref)clsrG   file_objr   r   r   rc      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)rO   AUDIO_ID_PCMr1   rh   ExtAudioFileSetPropertyr4   PROP_CLIENT_DATA_FORMATr   sizeofrj   rf   )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)re   rM   r   c_intrp   r1   rh   ExtAudioFileGetPropertyr4   PROP_FILE_DATA_FORMATrj   )r%   rq   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.)intrw   rT   r8   r   r   r   channels     zExtAudioFile.channelsc                 C   rx   )z"Gets the sample rate of the audio.)ry   rw   rN   r8   r   r   r   
samplerate  r{   zExtAudioFile.sampleratec                 C   s   t | j| j S )z1Gets the length of the file in seconds (a float).)floatnframesr|   r8   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_longrs   rp   r1   rh   rt   r4   PROP_LENGTHrj   value)r%   lengthrv   r   r   r   r~     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)rw   copyrm   rO   PCM_IS_SIGNED_INTPCM_IS_PACKEDrP   rU   rT   rQ   rR   rS   rr   )r%   bitdepthfmtnewfmtr   r   r   rg     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   rX   rf   rS   create_string_bufferr_   r`   rT   ra   r[   r\   castr^   r]   r1   rh   ExtAudioFileReadr4   rj   POINTERc_char)r%   	blocksizeframesbufbuflistrv   data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)rd   r1   rh   ExtAudioFileDisposer4   r8   r   r   r   closeH  s   
zExtAudioFile.closec                 C   s   t r|   d S d S r   )rh   r   r8   r   r   r   r9   N  s   zExtAudioFile.__del__c                 C   s   | S r   r   r8   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   r8   r   r   r   __iter__[  s   zExtAudioFile.__iter__N)r   )r   )r*   r+   r,   __doc__r$   classmethodrc   rr   rw   propertyrz   r|   r   r~   rg   r   r   r9   r   r   r   r   r   r   r   rb      s,    
	




	
rb   )2r   r   r   ctypes.utilr;   rB   
exceptionsr   baser   r   rh   r6   c_char_prI   restyper^   rs   argtypesr7   rD   c_boolrH   ri   rn   rX   rt   r   r   r   ru   ro   r   rm   PCM_IS_FLOATPCM_IS_BIG_ENDIANr   r   r!   r"   r.   r    r1   r2   r:   	StructurerM   rZ   r_   rb   r   r   r   r   <module>   sn   







	