
    Pi*                        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 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	 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e          Z)d Z* G d d          Z+ G d de+          Z, G d dej-                  Z. G d dej-                  Z/ G d d ej-                  Z0 G d! d"e	          Z1dS )#z-Read audio files using CoreAudio on Mac OS X.    N   )DecodeError)	AudioFilec                 z    t           j                            t           j                            |                     S N)ctypescdllLoadLibraryutilfind_library)names    c/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/audioread/macca.py_load_frameworkr      s(    ;""6;#;#;D#A#ABBB    AudioToolboxCoreFoundationc                     d}t          |           D ]2\  }}t          |           |z
  dz
  dz  }|t          |          |z  z  }3|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shifts        r   multi_char_literalr   P   sZ     C '' " "tUe#a'1,s4yyE!!Jr   ffmtcfmtz#frmlpcm      r   ztyp?zfmt?ic                        e Zd Z fdZ xZS )MacErrorc                     |t           k    rd}n|t          k    rd}nd|z  }t                                          |           d S )Nzunsupported audio typezunsupported formatzerror %i)
ERROR_TYPEERROR_FORMATsuper__init__)selfcodemsg	__class__s      r   r)   zMacError.__init__k   sO    :*CC\!!&CCt#Cr   )__name__
__module____qualname__r)   __classcell__r-   s   @r   r$   r$   j   s8                r   r$   c                 d    | 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$   )errs    r   checkr7   u   s8    
o&'''	smm 
r   c                       e Zd Zd Zd ZdS )CFObjectc                 >    |dk    rt          d          || _        d S )Nr   zobject is zero)
ValueError_obj)r*   objs     r   r)   zCFObject.__init__   s%    !88-...			r   c                 V    t           r!t                               | j                   d S d S r   )_corefoundation	CFReleaser<   r*   s    r   __del__zCFObject.__del__   s0     	1%%di00000	1 	1r   N)r.   r/   r0   r)   rB    r   r   r9   r9      s2          
1 1 1 1 1r   r9   c                   $     e Zd Z fdZd Z xZS )CFURLc                    t           j                            t           j                            |                    }t	          |t
                    s&|                    t          j                              }t          
                    d|t          |          d          }t                                          |           d S )Nr   F)ospathabspath
expanduser
isinstancebytesencodesysgetfilesystemencodingr?   'CFURLCreateFromFileSystemRepresentationr   r(   r)   )r*   filenameurlr-   s      r   r)   zCFURL.__init__   s    7??27#5#5h#?#?@@(E** 	Ds'@'B'BCCHEExX
 
 	r   c                 z    t                               | j                  }t                               |d          }|S )Nr   )r?   CFURLGetStringr<   CFStringGetCStringPtr)r*   cfstrouts      r   __str__zCFURL.__str__   s1    ..ty9933E1==
r   )r.   r/   r0   r)   rX   r1   r2   s   @r   rE   rE      sG                  r   rE   c            
           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/   r0   r   c_doublec_uint_fields_rC   r   r   rZ   rZ      so        	fo.	fm,	fm,	fm,	fm,	fm,	fm,	fm,	fm,
HHHr   rZ   c                   B    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/   r0   r   re   c_void_prf   rC   r   r   rh   rh      s3        	FM*	FM*	FO,HHHr   rh   c                   .    e Zd Zdej        fdedz  fgZdS )AudioBufferListmNumberBuffersmBuffersr   N)r.   r/   r0   r   re   rh   rf   rC   r   r   rn   rn      s+        	FM*	[1_%HHHr   rn   c                       e Zd ZdZd Zed             Zd Zd Ze	d             Z
e	d             Ze	d             Ze	d	             ZddZddZd Zd Zd Zd Z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                     t          |          }	 |                     |          | _        n#  d| _         xY w~d| _        d | _        d | _        |                                  d S )NTF)rE   	_open_urlr<   closed	_file_fmt_client_fmtsetup)r*   rQ   rR   s      r   r)   zExtAudioFile.__init__   sd    Hoo	s++DII	DK

s   , 
6c                     t          j                    }t          t                              |j        t          j        |                               |S )zGGiven a CFURL Python object, return an opened ExtAudioFileRef.
        )r   rl   r7   
_coreaudioExtAudioFileOpenURLr<   byref)clsrR   file_objs      r   rt   zExtAudioFile._open_url   sM     ?$$j,,Hfl8,,
 
 	 	 	 r   c           
          |j         t          k    sJ t          t                              | j        t          t          j        |          t          j	        |                               || _
        dS )zGet the client format description. This describes the
        encoding of the data that the program will read from this
        object.
        N)r\   AUDIO_ID_PCMr7   rz   ExtAudioFileSetPropertyr<   PROP_CLIENT_DATA_FORMATr   sizeofr|   rw   )r*   descs     r   set_client_formatzExtAudioFile.set_client_format   sl    
 ~----j00I.d0C0CL
 
 	 	 	  r   c           
      H   | j         | j         S t                      }t          j        t          j        |                    }t          t                              | j        t          t          j
        |          t          j
        |                               || _         |S )zaGet the file format description. This describes the type of
        data stored on disk.
        )rv   rZ   r   c_intr   r7   rz   ExtAudioFileGetPropertyr<   PROP_FILE_DATA_FORMATr|   )r*   r   sizes      r   get_file_formatzExtAudioFile.get_file_format   s    
 >%>! +,,|FM$//00j00I,fl4.@.@L
 
 	 	 	 r   c                 N    t          |                                 j                  S )z+The number of channels in the audio source.)intr   ra   rA   s    r   channelszExtAudioFile.channels  s!     4''));<<<r   c                 N    t          |                                 j                  S )z"Gets the sample rate of the audio.)r   r   r[   rA   s    r   
sampleratezExtAudioFile.samplerate  s!     4''))5666r   c                 :    t          | j                  | j        z  S )z1Gets the length of the file in seconds (a float).)floatnframesr   rA   s    r   durationzExtAudioFile.duration  s     T\""T_44r   c           
      2   t          j                    }t          j        t          j        |                    }t	          t
                              | j        t          t          j	        |          t          j	        |                               |j
        S )z-Gets the number of frames in the source file.)r   c_longr   r   r7   rz   r   r<   PROP_LENGTHr|   value)r*   lengthr   s      r   r   zExtAudioFile.nframes  su     |FM&1122j00I{FL$6$6V8L8L
 
 	 	 	 |r      c                 "   |                                  }t          j        |          }t          |_        t          t
          z  |_        ||_        |j        |j        z  dz  |_	        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)r   copyr   r\   PCM_IS_SIGNED_INTPCM_IS_PACKEDr]   rb   ra   r^   r_   r`   r   )r*   bitdepthfmtnewfmts       r   rx   zExtAudioFile.setup  s    
 ""$$3'- 	!)"V%;;q@ 	"# & 6v&&&&&r      c           	   #     K   t          j        || j        j        z            }t          j        |          }t                      }d|_        | j        j        |j        d         _	        ||j        d         _
        t          j        |t           j                  |j        d         _        	 t          t                              | j        t          j        |          t          j        |                               |j        dk    sJ |j        d         j
        }|sdS t          j        |j        d         j        t          j        t           j                            }|d|         }|V  )zFGenerates byte strings reflecting the audio data in the file.
        r   r   TN)r   re   rw   r`   create_string_bufferrn   ro   ra   rp   ri   rj   castrl   rk   r7   rz   ExtAudioFileReadr<   r|   POINTERc_char)r*   	blocksizeframesbufbuflistr   datablobs           r   	read_datazExtAudioFile.read_data,  sM      yD,<,KKLL))44!##!". 	+,5)$*KV_$E$E!	*--	6<//g1F1F     )Q....#A&4D ;w/28%~fm<<> >D;DJJJ	r   c                 ~    | j         s5t          t                              | j                             d| _         dS dS )z0Close the audio file and free associated memory.TN)ru   r7   rz   ExtAudioFileDisposer<   rA   s    r   closezExtAudioFile.closeH  s?    { 	*00;;<<<DKKK	 	r   c                 @    t           r|                                  d S d S r   )rz   r   rA   s    r   rB   zExtAudioFile.__del__N  s%     	JJLLLLL	 	r   c                     | S r   rC   rA   s    r   	__enter__zExtAudioFile.__enter__S  s    r   c                 .    |                                   dS )NF)r   )r*   exc_typeexc_valexc_tbs       r   __exit__zExtAudioFile.__exit__V  s    

ur   c                 *    |                                  S r   )r   rA   s    r   __iter__zExtAudioFile.__iter__[  s    ~~r   N)r   )r   )r.   r/   r0   __doc__r)   classmethodrt   r   r   propertyr   r   r   r   rx   r   r   rB   r   r   r   rC   r   r   rr   rr      sE       
 
     [
  
  
   ( = = X= 7 7 X7 5 5 X5   X' ' ' '$   8    
    
         r   rr   )2r   r   r   ctypes.utilrG   rN   
exceptionsr   baser   r   rz   r?   c_char_prU   restyperl   r   argtypesr@   rP   c_boolrT   r{   r   re   r   r   r   r   r   r   r   r   PCM_IS_FLOATPCM_IS_BIG_ENDIANr   r   r&   r'   r4   r$   r7   r9   rE   	StructurerZ   rh   rn   rr   rC   r   r   <module>r      s   4 3       				 



 # # # # # #      C C C _^,,
!/"233 17 % -28/28,2@ % . '-o%6  " O  7 ? \6?FL&-@  7 @ *0  &+1?*;  ' *0
  &+1?FO*L
  ' .4\
 " *_fmV]FOD  " + .4\
 " *_fmV_foF  " + '-l
  #_fov7   $ *0
  &+1?*;
  '
   +*622 ,,V44   ((!!&))  ''
!!&))
    {     1 1 1 1 1 1 1 1    H   &    &"2       &"       f&   `  `  `  `  ` 9 `  `  `  `  ` r   