
    tKg*                        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j0                  _        ej                   ej2                  ej2                  ej                   gej0                  _        ej"                  ej4                  _        ej                   ej2                  ej                   ej                   gej4                  _        ej"                  ej6                  _        ej                   ej                   ej                   gej6                  _        ej"                  ej8                  _        ej                   gej8                  _        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jZ                        Z. G d dejZ                        Z/ G d d ejZ                        Z0 G d! d"e	      Z1y)#z-Read audio files using CoreAudio on Mac OS X.    N   )DecodeError)	AudioFilec                 z    t         j                  j                  t         j                  j	                  |             S N)ctypescdllLoadLibraryutilfind_library)names    S/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/audioread/macca.py_load_frameworkr      s&    ;;""6;;#;#;D#ABB    AudioToolboxCoreFoundationc                 z    d}t        |       D ]*  \  }}t        |       |z
  dz
  dz  }|t        |      |z  z  }, |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   sN     C 'tUe#a'1,s4yE!! ( 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        |   |       y )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   s4    :*C\!&Ct#Cr   )__name__
__module____qualname__r)   __classcell__r-   s   @r   r$   r$   j   s     r   r$   c                 L    | t         k(  rt        d      | dk7  rt        |       y)z.If err is nonzero, raise a MacError exception.zfile not foundr   N)ERROR_NOT_FOUNDOSErrorr$   )errs    r   checkr7   u   s,    
o&''	sm 
r   c                       e Zd Zd Zd Zy)CFObjectc                 2    |dk(  rt        d      || _        y )Nr   zobject is zero)
ValueError_obj)r*   objs     r   r)   zCFObject.__init__   s    !8-..	r   c                 P    t         r t         j                  | j                         y y r   )_corefoundation	CFReleaser<   r*   s    r   __del__zCFObject.__del__   s    %%dii0 r   N)r.   r/   r0   r)   rB    r   r   r9   r9      s    
1r   r9   c                   $     e Zd Z fdZd Z xZS )CFURLc                 D   t        |t              s#|j                  t        j                               }t
        j                  j                  t
        j                  j                  |            }t        j                  d|t        |      d      }t        | 5  |       y )Nr   F)
isinstancebytesencodesysgetfilesystemencodingospathabspath
expanduserr?   'CFURLCreateFromFileSystemRepresentationr   r(   r)   )r*   filenameurlr-   s      r   r)   zCFURL.__init__   sq    (E*s'@'@'BCH77??277#5#5h#?@EExX
 	r   c                 p    t         j                  | j                        }t         j                  |d      }|S )Nr   )r?   CFURLGetStringr<   CFStringGetCStringPtr)r*   cfstrouts      r   __str__zCFURL.__str__   s-    ..tyy933E1=
r   )r.   r/   r0   r)   rX   r1   r2   s   @r   rE   rE      s    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y
)AudioStreamBasicDescriptionmSampleRate	mFormatIDmFormatFlagsmBytesPerPacketmFramesPerPacketmBytesPerFramemChannelsPerFramemBitsPerChannel	mReservedN)r.   r/   r0   r   c_doublec_uint_fields_rC   r   r   rZ   rZ      su    	foo.	fmm,	fmm,	fmm,	fmm,	fmm,	fmm,	fmm,	fmm,
Hr   rZ   c                   ^    e Zd Zdej                  fdej                  fdej
                  fgZy)AudioBuffermNumberChannelsmDataByteSizemDataN)r.   r/   r0   r   re   c_void_prf   rC   r   r   rh   rh      s-    	FMM*	FMM*	FOO,Hr   rh   c                   6    e Zd Zdej                  fdedz  fgZy)AudioBufferListmNumberBuffersmBuffersr   N)r.   r/   r0   r   re   rh   rf   rC   r   r   rn   rn      s!    	FMM*	[1_%Hr   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y)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        |      }	 | j                  |      | _        ~d| _        d | _        d | _        | j                          y #  d| _         xY w)NTF)rE   	_open_urlr<   closed	_file_fmt_client_fmtsetup)r*   rQ   rR   s      r   r)   zExtAudioFile.__init__   sU    Ho	s+DI 

	DKs   A
 

Ac                     t        j                         }t        t        j	                  |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   s>     ??$j,,HHfll8,
 	 r   c           
          |j                   t        k(  sJ t        t        j	                  | j
                  t        t        j                  |      t        j                  |                   || _
        y)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   sV    
 ~~---j00II.d0CLL
 	  r   c           
      R   | j                   | j                   S t               }t        j                  t        j                  |            }t        t        j                  | 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}    
 >>%>>! +,||FMM$/0j00II,fll4.@LL
 	 r   c                 H    t        | j                         j                        S )z+The number of channels in the audio source.)intr   ra   rA   s    r   channelszExtAudioFile.channels  s     4'');;<<r   c                 H    t        | j                         j                        S )z"Gets the sample rate of the audio.)r   r   r[   rA   s    r   
sampleratezExtAudioFile.samplerate  s     4'')5566r   c                 F    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           
      <   t        j                         }t        j                  t        j                  |            }t	        t
        j                  | 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  sf     ||FMM&12j00II{FLL$6V8L
 	 ||r   c                 *   | j                         }t        j                  |      }t        |_        t        t
        z  |_        ||_        |j                  |j                  z  dz  |_	        d|_
        |j                  |_        | j                  |       y)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 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!                  | j"                  t        j$                  |      t        j$                  |                   |j                  dk(  sJ |j                  d   j                  }|syt        j                  |j                  d   j                  t        j&                  t         j(                              }|d| }| խw)zFGenerates byte strings reflecting the audio data in the file.
        r   r   N)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,  sK     yD,<,<,K,KKL)))4!#!".. 	+,5)$*KKV__$E!*--		6<</g1F  ))Q...##A&44D;;w//288%~~fmm<>D;DJ s   FFc                 |    | j                   s0t        t        j                  | j                               d| _         yy)z0Close the audio file and free associated memory.TN)ru   r7   rz   ExtAudioFileDisposer<   rA   s    r   closezExtAudioFile.closeH  s,    {{*00;<DK r   c                 2    t         r| j                          y y r   )rz   r   rA   s    r   rB   zExtAudioFile.__del__N  s    JJL r   c                     | S r   rC   rA   s    r   	__enter__zExtAudioFile.__enter__S  s    r   c                 $    | j                          y)NF)r   )r*   exc_typeexc_valexc_tbs       r   __exit__zExtAudioFile.__exit__V  s    

r   c                 "    | j                         S r   )r   rA   s    r   __iter__zExtAudioFile.__iter__[  s    ~~r   N)   )i   )r.   r/   r0   __doc__r)   classmethodrt   r   r   propertyr   r   r   r   rx   r   r   rB   r   r   r   rC   r   r   rr   rr      s    
  
 ( = = 7 7 5 5  '$8

 r   rr   )2r   r   r   ctypes.utilrL   rJ   
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    	 
 # C ^,
!"23 17 % % -28//28,,2@ % % . '-oo%6   " OO  7 7 ? \\6??FLL&--@  7 7 @ *0   &+1??*;   ' *0
   &+1??FOO*L
   ' .4\\
 " " *__fmmV]]FOOD  " " + .4\\
 " " *__fmmV__fooF  " " + '-ll
   #__foov7    $ *0
   &+1??*;
   '
 +62 ,V4  (!&)  '
!&)
{ 1 1H &&"2"2 &"" f&& ` 9 ` r   