
    $HJj6Y                       d Z 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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mZmZmZmZmZmZmZ ddlmZmZ 	 ddlZ ej:                           G d de      Z G d	 d
e      Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z%ee&ej                  f   Z'ee'e(df   Z)ede'df   Z*ee'e'df   Z+ee)e*e+f   Z, G d d      Z- G d de-      Z. G d de/      Z0 G d de0      Z1de2d e-fd!Z3dde2d"e4d e.fd#Z5de2d e-fd$Z6d%e&d e-fd&Z7de2d e.fd'Z8d%e&d e.fd(Z9	 dde2d)e(d eej                  ddf   fd*Z:de2d e-fd+Z;d%e&d e-fd,Z<de2d e.fd-Z=de2d e.fd.Z>de2d e.fd/Z?d%e&d e.fd0Z@d%e&d e.fd1ZAd%e&d e.fd2ZB	 dde2d4e(d)e(d eej                  ddf   fd5ZCde2d e-fd6ZDd%e&d e-fd7ZEde2d e.fd8ZFde2d e.fd9ZGd%e&d e.fd:ZHd%e&d e.fd;ZI	 dde2d4e(d)e(d eej                  ddf   fd<ZJde2d e-fd=ZKd%e&d e-fd>ZLde2d e.fd?ZMde2d e.fd@ZNde2d e.fdAZOd%e&d e.fdBZPd%e&d e.fdCZQd%e&d e.fdDZR	 dde2d4e(d)e(d eej                  ddf   fdEZSde2dFe.d dfdGZTdHe(d ej                  fdIZUde2d e&fdJZVde2fdKZW G dL dM      ZXdNed e(fdOZYdNed ej                  fdPZZddQe(dRe4d efdSZ[ej                  dTdUe!j                  fde2dVedWe(dXe(dYe!d e.fdZZ^ej                  dTdUe!j                  fd%e&dVedWe(dXe(dYe!d e.fd[Z_	 dd4e(dWe(dVed\ej                  d%ed]ee   d eej                  e(df   fd^Zaej                  dTdUd3e!j                  dfde2dVedWe(dXe(d4e(dYe!d)e(d eej                  e(df   fd_Zbej                  dTdUd3e!j                  fd%e&dVedWe(dXe(d4e(dYe!d eej                  e(df   fd`Zc	 ddaeej                  ede&f   dWe(dQe(d4e(d e)f
dbZe G dc ddej                        Zg G de dfeg      Zhej                  ej                  dTdUd3e!j                  dfdgegdhedVedWe(dXe(d4e(dYe!d)e(d eej                  e(df   fdiZj	 	 	 ddje)dkeee(gdf   df   dleee'ge'f   df   dmeedf   d e)f
dnZk ej                         d\ej                  doej                  dpe(dqej                  d e(f
dr       Zm ej                         d\ej                  dse(dte(d e(fdu       Zne!j                  fdvedwe&dxedYe!d eof
dyZpdvedze(d{e(dwe&dxed|e(d}e(d eofd~Zq ej                         dej                  doej                  dej                  de(d df
d       Zr ej                         dej                  d dfd       Zs G d d      Zt G d det      Zu G d det      Zv G d det      Zwdeej                  ede&f   d e&fdZx G d dej                        Zzd e2fdZ{de#d e4fdZ|d ee#   fdZ}de#d e4fdZ~y# e$ r dZY w xY w)z
Python interface to the miniaudio library (https://github.com/dr-soft/miniaudio)

Author: Irmen de Jong (irmen@razorvine.net)
Software license: "MIT software license". See http://opensource.org/licenses/MIT
z1.71    N)Enum)	GeneratorListDictSetOptionalUnionAnyCallable)ffilibc                       e Zd ZdZej
                  Zej                  Zej                  Z
ej                  Zej                  Zy)
FileFormatzAudio file formatN)__name__
__module____qualname____doc__r   ma_encoding_format_unknownUNKNOWNma_encoding_format_wavWAVma_encoding_format_flacFLACma_encoding_format_mp3MP3ma_encoding_format_vorbisVORBIS     S/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/miniaudio.pyr   r   !   s<    ,,G

$
$C&&D

$
$C**Fr   r   c                       e Zd ZdZej
                  Zej                  Zej                  Z
ej                  Zej                  Zej                  Zy)SampleFormatzSample format in memoryN)r   r   r   r   r   ma_format_unknownr   ma_format_u8	UNSIGNED8ma_format_s16SIGNED16ma_format_s24SIGNED24ma_format_s32SIGNED32ma_format_f32FLOAT32r   r   r    r"   r"   +   sG    !##G  I  H  H  HGr   r"   c                   X    e Zd ZdZej
                  Zej                  Zej                  Z
y)
DeviceTypezType of audio deviceN)r   r   r   r   r   ma_device_type_playbackPLAYBACKma_device_type_captureCAPTUREma_device_type_duplexDUPLEXr   r   r    r/   r/   6   s&    **H((G&&Fr   r/   c                   X    e Zd ZdZej
                  Zej                  Zej                  Z
y)
DitherModezHow to dither when convertingN)r   r   r   r   r   ma_dither_mode_noneNONEma_dither_mode_rectangle	RECTANGLEma_dither_mode_triangleTRIANGLEr   r   r    r7   r7   >   s&    '""D,,I**Hr   r7   c                   p    e Zd ZdZej
                  Zej                  Zej                  Z
ej                  Zy)ChannelMixModez#How to mix channels when convertingN)r   r   r   r   r   ma_channel_mix_mode_rectangularRECTANGULARma_channel_mix_mode_simpleSIMPLE"ma_channel_mix_mode_custom_weightsCUSTOMWEIGHTSma_channel_mix_mode_defaultDEFAULTr   r   r    r?   r?   F   s1    -55K++F::M--Gr   r?   c                   x   e Zd ZdZej
                  Zej                  Zej                  Z
ej                  Zej                  Zej                  Zej"                  Zej&                  Zej*                  Zej.                  Zej2                  Zej6                  Zej:                  Zej>                  Z ejB                  Z"y)BackendzGOperating system audio backend to use (only a subset will be available)N)#r   r   r   r   r   ma_backend_wasapiWASAPIma_backend_dsoundDSOUNDma_backend_winmmWINMMma_backend_coreaudio	COREAUDIOma_backend_sndioSNDIOma_backend_audio4AUDIO4ma_backend_ossOSSma_backend_pulseaudio
PULSEAUDIOma_backend_alsaALSAma_backend_jackJACKma_backend_aaudioAAUDIOma_backend_openslOPENSLma_backend_webaudioWEBAUDIOma_backend_customCUSTOMma_backend_nullNULLr   r   r    rI   rI   O   s    Q""F""F  E((I  E""F


C**JDD""F""F&&H""FDr   rI   c                       e Zd ZdZej
                  Zej                  Zej                  Z
ej                  Zej                  Zej                  Zej"                  Zej&                  Zy)ThreadPriorityz3The priority of the worker thread (default=HIGHEST)N)r   r   r   r   r   ma_thread_priority_idleIDLEma_thread_priority_lowestLOWESTma_thread_priority_lowLOWma_thread_priority_normalNORMALma_thread_priority_highHIGHma_thread_priority_highestHIGHESTma_thread_priority_realtimeREALTIMEma_thread_priority_defaultrG   r   r   r    ri   ri   c   s]    =&&D**F

$
$C**F&&D,,G..H,,Gr   ri   c                   X    e Zd ZdZej
                  Zej                  Zej                  Z
y)
SeekOriginzHow to seek() in a sourceN)r   r   r   r   r   ma_seek_origin_startSTARTma_seek_origin_currentCURRENTma_seek_origin_endENDr   r   r    rz   rz   p   s&    #$$E((G

 
 Cr   rz   c                   X    e Zd ZdZ	 ddedededededed	ed
eddfdZ	defdZ
defdZy)SoundFileInfoz-Contains various properties of an audio file.Nnamefile_format	nchannelssample_ratesample_formatduration
num_frames
sub_formatreturnc	                    || _         || _        || _        || _        t	        j
                  t        j                  |j                              j                         | _
        t        |      | _        || _        || _        || _        || _        y N)r   r   r   r   r   stringr   ma_get_format_namevaluedecodesample_format_namewidth_from_formatsample_widthr   r   r   r   )	selfr   r   r   r   r   r   r   r   s	            r    __init__zSoundFileInfo.__init__   s}     	"&*"%**""=#6#67#

&( 	 .m<$ &$r   c                     | j                   j                  }| j                  r|dt        | j                        z   dz   z  } dj                  d| j
                  j                  |dt        |       S )Nz (fmt=)z<{clazz}: '{name}' {fileformatdisplay} {nchannels} ch, {sample_rate} hz, {sample_format.name}, {num_frames} frames={duration:.2f} sec.>)clazzfileformatdisplayr   )r   r   r   strformat	__class__r   vars)r   r   s     r    __str__zSoundFileInfo.__str__   su     ,,11??C,@!@3!FF> 77=vnn--"3 :		
r   c                     t        |       S r   )r   r   s    r    __repr__zSoundFileInfo.__repr__   s    4yr   r   )r   r   r   r   r   r   intr"   floatr   r   r   r   r   r    r   r      s    7 %%  % 	%
 % $% % % % 
%0
 
# r   r   c                   P     e Zd ZdZdededededej                  ddf fd	Z xZ	S )
DecodedSoundFilezRContains various properties and also the PCM frames of a fully decoded audio file.r   r   r   r   samplesr   Nc           	          t        |      |z  }||z  }t        | 	  |t        j                  |||||       || _        y r   )lensuperr   r   r   r   )	r   r   r   r   r   r   r   r   r   s	           r    r   zDecodedSoundFile.__init__   sN     \Y.
+	
 r   )
r   r   r   r   r   r   r"   arrayr   __classcell__r   s   @r    r   r      sN    \  	
 $  
 r   r   c                       e Zd ZdZy)MiniaudioErrorz'When a miniaudio specific error occurs.Nr   r   r   r   r   r   r    r   r      s    1r   r   c                       e Zd ZdZy)DecodeErrorz8When something went wrong during decoding an audio file.Nr   r   r   r    r   r      s    Br   r   filenamer   c                     t         j                  j                  |       d   j                         }|dv rt	        |       S |dk(  rt        |       S |dk(  rt        |       S |dk(  rt        |       S t        d      )z,Fetch some information about the audio file.   z.oggz.vorbis.mp3.flac.wavunsupported file format)	ospathsplitextlowervorbis_get_file_infomp3_get_file_infoflac_get_file_infowav_get_file_infor   )r   exts     r    get_file_infor      sz    
''

8
$Q
'
-
-
/C
!!#H--	 **	!(++	 **
/
00r   convert_to_16bitc                    t         j                  j                  |       d   j                         }|dv rK|rt	        |       S t        |       }|j                  t        j                  k(  rt	        |       S t        d      |dk(  rs|rt        |       S t        |       }|j                  t        j                  k(  rt        |       S |j                  t        j                  k(  rt        |       S t        d      |dk(  r|rt        |       S t        |       }|j                  t        j                  k(  rt        |       S |j                  t        j                   k(  rt#        |       S |j                  t        j                  k(  rt%        |       S t        d      |dk(  r|rt'        |       S t)        |       }|j                  t        j                  k(  rt'        |       S |j                  t        j                   k(  rt+        |       S |j                  t        j                  k(  rt-        |       S t        d      t/        d      )zReads and decodes the whole audio file.
    Miniaudio will attempt to return the sound data in exactly the same format as in the file.
    Unless you set convert_convert_to_16bit to True, then the result is always a 16 bit sample format.
    r   r   z-file has sample format that must be convertedr   r   r   r   )r   r   r   r   vorbis_read_filer   r   r"   r'   r   mp3_read_file_s16r   r-   mp3_read_file_f32flac_read_file_s16r   r+   flac_read_file_s32flac_read_file_f32wav_read_file_s16r   wav_read_file_s32wav_read_file_f32r   )r   r   r   vorbismp3flacwavs          r    	read_filer      s   
 ''

8
$Q
'
-
-
/C
!!#H--)(3F##|'<'<<'11$%TUU	$X..#H-C  L$9$99(22""l&:&::(22$%TUU	%h//%h/D!!\%:%::)(33##|'<'<<)(33##|';';;)(33$%TUU	$X..#H-C  L$9$99(22""l&;&;;(22""l&:&::(22$%TUU
/
00r   c           
      2   t        |       }t        j                  d      5 }t        j                  ||t        j
                        }|st        d      	 t        j                  |      }t        j                  |      }t        j                  |      }t        | t        j                  |j                  |j                  t        j                   ||      t        j"                  |       cddd       S # t        j"                  |       w xY w# 1 sw Y   yxY w)z<Fetch some information about the audio file (vorbis format).int *could not open/decode fileN)_get_filename_bytesr   newr   stb_vorbis_open_filenamerg   r   stb_vorbis_get_info#stb_vorbis_stream_length_in_seconds#stb_vorbis_stream_length_in_samplesr   r   r   channelsr   r"   r'   stb_vorbis_close)r   filenamebyteserrorr   infor   r   s          r    r   r     s    '1M		U--mUCHHM:;;	)**62D>>vFH@@HJ !!  %%   (% 
	$   (% 
	s$   3DA?C3D3D

DDdatac           
      0   t        j                  d      5 }t        j                  | t	        |       |t         j
                        }|st        d      	 t        j                  |      }t        j                  |      }t        j                  |      }t        dt        j                  |j                  |j                  t        j                   ||      t        j"                  |       cddd       S # t        j"                  |       w xY w# 1 sw Y   yxY w)z<Fetch some information about the audio data (vorbis format).r   could not open/decode data<memory>N)r   r   r   stb_vorbis_open_memoryr   rg   r   r   r   r   r   r   r   r   r   r"   r'   r   )r   r   r   r   r   r   s         r    vorbis_get_infor   2  s    		U++D#d)UCHHM:;;	)**62D>>vFH@@HJ !!  %%   (% 
	$   (% 
	s$   =DA?C2D2D		DDc           
         t        |       }t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  ||||      }|dk  rt        d      	 t        j                  |d   ||d   z  dz        }t        d      }|j                  |       t        | |d   |d   t        j                  |      t        j                  |d          cddd       cddd       cddd       S # t        j                  |d          w xY w# 1 sw Y   nxY wddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)zaReads and decodes the whole vorbis audio file. Resulting sample format is 16 bits signed integer.r   short **r   cannot load/decode file   N)r   r   r   r   stb_vorbis_decode_filenamer   buffer_create_int_array	frombytesr   r"   r'   free)r   r   r   r   outputr   r   r   s           r    r   r   I  s&   '1M		Xswww'7;I	338[&

 ?788	 ZZq	:+Ca+GHF'*Gf%#(1+{1~|7L7Lg HHVAY!I I'7'7		  HHVAY!I I'7'7'7			sY   ED=)D(7A DD(/	D=8	ED%	%D((D1-D=4	E=E	EEc           
         t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  | t	        |       |||      }|dk  rt        d      	 t        j                  |d   ||d   z  dz        }t        d      }|j                  |       t        d|d   |d   t        j                  |      t        j                  |d          cddd       cddd       cddd       S # t        j                  |d          w xY w# 1 sw Y   nxY wddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)zaReads and decodes the whole vorbis audio data. Resulting sample format is 16 bits signed integer.r   r   r   cannot load/decode datar   r   N)r   r   r   stb_vorbis_decode_memoryr   r   r   r   r   r   r"   r'   r   )r   r   r   r   num_samplesr   r   s          r    vorbis_readr   _  s$   		Xswww'7;I	22#d)X{F
 !788	 ZZq	;!+Dq+HIF'*Gf%#HQKQ9N9NPW HHVAY!I I'7'7		  HHVAY!I I'7'7'7			sY   ED<3D'6A D
D'.	D<7	E
D$	$D''D0,D<3	E<E	EE
seek_framec           
   #     K   t        |       }t        j                  d      5 }t        j                  ||t        j
                        }|st        d      	 t        j                  |      }t        j                  dd|j                  z        5 }t        j                  dd|j                  z        5 }t        j                  d|      }t        j                  d|      }	|dkD  r&t        j                  ||      }
|
dk  rt        d      	 t        j                  ||j                  |d|j                  z        }t        j                  ||j                  |	d|j                  z        }||z   dk  rnt        j                  ||dz  |j                  z        }t        d      }|j                  |       |dkD  r7t        j                  ||dz  |j                  z        }|j                  |       | 	 d	d	d	       d	d	d	       t        j                  |       	 d	d	d	       y	# 1 sw Y   0xY w# 1 sw Y   4xY w# t        j                  |       w xY w# 1 sw Y   y	xY ww)
zStreams the ogg vorbis audio file as interleaved 16 bit signed integer sample arrays segments.
    This uses a variable unconfigurable chunk size and cannot be used as a generic miniaudio decoder input stream.
    Consider using stream_file() instead.r   r   zshort[]   zshort *r   
can't seekr   N)r   r   r   r   r   rg   r   r   r   caststb_vorbis_seek_frame&stb_vorbis_get_frame_short_interleavedr   r   r   r   )r   r   r   r   r   r   decode_buffer1decode_buffer2decodebuf_ptr1decodebuf_ptr2resultnum_samples1num_samples2r   r   s                  r    vorbis_stream_filer  t  s     (1M		U--mUCHHM:;;!	)**62DD4==$89^SWW4$--/N!$)^!D!$)^!D> 66vzJF{),77#&#M#M~tdmm?S$L $'#M#M~tdmm?S$L $l2a7 ZZ&q(84==(HF 02G%%f-#a'!$*L1,<t}},L"  ))&1!M'  %N9>   (K 
	N N99>   (K 
	se   !I$3I8H>$H23EH&7H2?H>I	I$&H/+H22H;	7H>>III!I$c           	      <   t         j                  dk(  r0t        |       }t        j                  |t
        j                        }n/t        |       }t        j                  |t
        j                        }|st        d      	 |j                  |j                  z  }|j                  dz  }t        | t        j                  |j                   |j                  t#        |      ||j                        t        j$                  |       S # t        j$                  |       w xY w)z:Fetch some information about the audio file (flac format).win32r      )sysplatform_get_filename_bytes_wr   ma_dr_flac_open_file_wr   rg   r   ma_dr_flac_open_filer   totalPCMFrameCount
sampleRatebitsPerSampler   r   r   r   _format_from_widthma_dr_flac_close)r   filenamebyteswr   r   r   r   s         r    r   r     s    
||w.x8)).#((C+H5''sxx@677#**T__<))Q.OOMMOO|,##
 	T"T"s   A-D Dc           	         t        j                  | t        |       t        j                        }|st        d      	 |j                  |j                  z  }|j                  dz  }t        dt        j                  |j                  |j                  t        |      ||j                        t        j                  |       S # t        j                  |       w xY w)z:Fetch some information about the audio data (flac format).r   r	  r   )r   ma_dr_flac_open_memoryr   r   rg   r   r  r  r  r   r   r   r   r  r  )r   r   r   r   s       r    flac_get_infor    s    %%dCIsxx@D677#**T__<))Q.OOMMOO|,##
 	T"T"s   A-C   Cc           
         t        |       }t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  ||||t        j
                        }|st        d      	 t        d      }t        j                  ||d   |d   z  dz        }|j                  |       t        | |d   |d   t        j                  |      t        j                  |t        j
                         cddd       cddd       cddd       S # t        j                  |t        j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z_Reads and decodes the whole flac audio file. Resulting sample format is 32 bits signed integer.unsigned int *ma_dr_flac_uint64 *r      r   N)r   r   r   r   ,ma_dr_flac_open_file_and_read_pcm_frames_s32rg   r   r   r   r   r   r"   r+   ma_dr_flac_freer   r   r   r   r   memoryr   r   s           r    r   r     6   '1M	!	"h1	cgg34
AA8[*chh
 788	2'*GZZ
1(Ca(GHFf%#(1+{1~|7L7Lg 1 541 1	"	"  1 5441 1 1	"	"	"Y   E7E"5EA D##$E	E"	E7#&E			EEE"	E7"E+	'E77F c           
         t        |       }t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  ||||t        j
                        }|st        d      	 t        d      }t        j                  ||d   |d   z  dz        }|j                  |       t        | |d   |d   t        j                  |      t        j                  |t        j
                         cddd       cddd       cddd       S # t        j                  |t        j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z_Reads and decodes the whole flac audio file. Resulting sample format is 16 bits signed integer.r  r  r   r   r   N)r   r   r   r   ,ma_dr_flac_open_file_and_read_pcm_frames_s16rg   r   r   r   r   r   r"   r'   r  r  s           r    r   r     r   r!  c           
         t        |       }t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  ||||t        j
                        }|st        d      	 t        j                  d      }t        j                  ||d   |d   z  dz        }|j                  |       t        | |d   |d   t        j                  |      t        j                  |t        j
                         cddd       cddd       cddd       S # t        j                  |t        j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)VReads and decodes the whole flac audio file. Resulting sample format is 32 bits float.r  r  r   fr   r  N)r   r   r   r   ,ma_dr_flac_open_file_and_read_pcm_frames_f32rg   r   r   r   r   r   r"   r-   r  r  s           r    r   r     s:   '1M	!	"h1	cgg34
AA8[*chh
 788	2kk#&GZZ
1(Ca(GHFf%#(1+{1~|7K7KW 1 541 1	"	"  1 5441 1 1	"	"	"Y   FE,5EA*D--$E	E,	F-&E	EEE,#	F,E5	1FF
c                    t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  | t	        |       |||t         j
                        }|st        d      	 t        d      }t        j                  ||d   |d   z  dz        }|j                  |       t        d|d   |d   t        j                  |      t        j                  |t         j
                         cddd       cddd       cddd       S # t        j                  |t         j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z_Reads and decodes the whole flac audio data. Resulting sample format is 32 bits signed integer.r  r  r   r  r   r   N)r   r   r   .ma_dr_flac_open_memory_and_read_pcm_frames_s32r   rg   r   r   r   r   r   r"   r+   r  r   r   r   r   r  r   r   s          r    flac_read_s32r,    3   	!	"h1	cgg34
CC#d)X{J
 788	2'*GZZ
1(Ca(GHFf%#HQKQ9N9NPW 1 541 1	"	"  1 5441 1 1	"	"	"Y   E6E!?EA D""$E	E!	E6"&E	EEE!	E6!E*	&E66E?c                    t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  | t	        |       |||t         j
                        }|st        d      	 t        d      }t        j                  ||d   |d   z  dz        }|j                  |       t        d|d   |d   t        j                  |      t        j                  |t         j
                         cddd       cddd       cddd       S # t        j                  |t         j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z_Reads and decodes the whole flac audio data. Resulting sample format is 16 bits signed integer.r  r  r   r   r   r   N)r   r   r   .ma_dr_flac_open_memory_and_read_pcm_frames_s16r   rg   r   r   r   r   r   r"   r'   r  r+  s          r    flac_read_s16r1  )  r-  r.  c                    t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  | t	        |       |||t         j
                        }|st        d      	 t        j                  d      }t        j                  ||d   |d   z  dz        }|j                  |       t        d|d   |d   t        j                  |      t        j                  |t         j
                         cddd       cddd       cddd       S # t        j                  |t         j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)	r%  r  r  r   r&  r   r  r   N)r   r   r   .ma_dr_flac_open_memory_and_read_pcm_frames_f32r   rg   r   r   r   r   r   r"   r-   r  r+  s          r    flac_read_f32r4  >  s6   	!	"h1	cgg34
CC#d)X{J
 788	2kk#&GZZ
1(Ca(GHFf%#HQKQ9M9Mw 1 541 1	"	"  1 5441 1 1	"	"	"Y   F E+?EA*D,,$E	E+	F ,&E	EEE+"	F +E4	0F  F	   frames_to_readc              #   (  K   t         j                  dk(  r0t        |       }t        j                  |t
        j                        }n/t        |       }t        j                  |t
        j                        }|st        d      |dkD  r&t        j                  ||      }|dk  rt        d      	 t        j                  d||j                  z        5 }t        j                  d|      }	 t        j                  |||      }	|	dk  rnGt        j                  ||	dz  |j                  z        }
t!        d      }|j#                  |
       | d	 ddd       t        j$                  |       y# 1 sw Y   xY w# t        j$                  |       w xY ww)	zStreams the flac audio file as interleaved 16 bit signed integer sample arrays segments.
    This uses a fixed chunk size and cannot be used as a generic miniaudio decoder input stream.
    Consider using stream_file() instead.r  r   r   r   zma_dr_flac_int16[]zma_dr_flac_int16 *r   N)r
  r  r  r   r  r   rg   r   r  r   ma_dr_flac_seek_to_pcm_framer   r   r   ma_dr_flac_read_pcm_frames_s16r   r   r   r  )r   r7  r   r  r   r   r  decodebufferbuf_ptrr   r   r   s               r    flac_stream_filer=  S  s\     ||w.x8)).#((C+H5''sxx@677A~11$
CQ;l++#WW .4=="@
hh3\BG!@@.' !#L+/DMM2QR+A.!!&) 
 
 	T"
 
 	T"s7   B+F.#E8 A<E,E8 F,E51E8 8FFc           
         t        j                  d      5 }t        j                  dk(  r1t	        |       }t        j                  ||t         j                        }n0t        |       }t        j                  ||t         j                        }|st        d      	 t        j                  |      }||j                  z  }t        | t        j                  |j                   |j                  t"        j$                  ||      t        j&                  |       cddd       S # t        j&                  |       w xY w# 1 sw Y   yxY w)z9Fetch some information about the audio file (mp3 format).ma_dr_mp3 *r  r   N)r   r   r
  r  r  r   ma_dr_mp3_init_file_wrg   r   ma_dr_mp3_init_filer   ma_dr_mp3_get_pcm_frame_countr  r   r   r   r   r"   r'   ma_dr_mp3_uninit)r   r   r  r  r   r   r   s          r    r   r   x  s    		3<<7"28<N..sNCHHMF/9M,,S-JF:;;	&::3?J!CNN2H %%   %- 
 	,   %- 
 	s%   BD6A$D=D6D33D66D?c           
         t        j                  d      5 }t        j                  || t	        |       t         j
                        st        d      	 t        j                  |      }||j                  z  }t        dt        j                  |j                  |j                  t        j                  ||      t        j                  |       cddd       S # t        j                  |       w xY w# 1 sw Y   yxY w)z9Fetch some information about the audio data (mp3 format).r?  r   r   N)r   r   r   ma_dr_mp3_init_memoryr   rg   r   rB  r  r   r   r   r   r"   r'   rC  )r   r   r   r   s       r    mp3_get_inforF    s    		3((dCIsxxH:;;	&::3?J!CNN2H %%   %! 
 	    %! 
 	s$   ;C/A$C6C/C,,C//C8c           	         t        |       }t        j                  d      5 }t        j                  d      5 }t        j                  |||t        j
                        }|st        d      	 t        j                  d      }t        j                  ||d   |j                  z  dz        }|j                  |       t        | |j                  |j                  t        j                  |      t        j                  |t        j
                         cddd       cddd       S # t        j                  |t        j
                         w xY w# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)zUReads and decodes the whole mp3 audio file. Resulting sample format is 32 bits float.ma_dr_mp3_config *ma_dr_mp3_uint64 *r   r&  r   r  N)r   r   r   r   +ma_dr_mp3_open_file_and_read_pcm_frames_f32rg   r   r   r   r   r   r   r  r"   r-   ma_dr_mp3_freer   r   configr   r  r   r   s          r    r   r     s   '1M	%	&&#''3	@@6:sxx
 788	1kk#&GZZ
1(G!(KLFf%#!!$$ vsxx0)3 3	&	&( vsxx0)3 3	&	&	&s;   E 4E,A?D"+$E	E "&EEE	E  E)c           	         t        |       }t        j                  d      5 }t        j                  d      5 }t        j                  |||t        j
                        }|st        d      	 t        d      }t        j                  ||d   |j                  z  dz        }|j                  |       t        | |j                  |j                  t        j                  |      t        j                  |t        j
                         cddd       cddd       S # t        j                  |t        j
                         w xY w# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z^Reads and decodes the whole mp3 audio file. Resulting sample format is 16 bits signed integer.rH  rI  r   r   r   N)r   r   r   r   +ma_dr_mp3_open_file_and_read_pcm_frames_s16rg   r   r   r   r   r   r   r  r"   r'   rK  rL  s          r    r   r     s   '1M	%	&&#''3	@@6:sxx
 788	1'*GZZ
1(G!(KLFf%#!!%% vsxx0)3 3	&	&( vsxx0)3 3	&	&	&s;   E4E,A5D!$E	E&D>>EE
	EEc           	         t        j                  d      5 }t        j                  d      5 }t        j                  | t	        |       ||t         j
                        }|st        d      	 t        j                  d      }t        j                  ||d   |j                  z  dz        }|j                  |       t        d|j                  |j                  t        j                  |      t        j                  |t         j
                         cddd       cddd       S # t        j                  |t         j
                         w xY w# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)	zUReads and decodes the whole mp3 audio data. Resulting sample format is 32 bits float.rH  rI  r   r&  r   r  r   N)r   r   r   -ma_dr_mp3_open_memory_and_read_pcm_frames_f32r   rg   r   r   r   r   r   r   r  r"   r-   rK  r   rM  r   r  r   r   s         r    mp3_read_f32rS    s   	%	&&#''3	BB#d)VZ
 788	1kk#&GZZ
1(G!(KLFf%#!!$$ vsxx0)3 3	&	&( vsxx0)3 3	&	&	&s;   E>E
+A?D!*$E
	E!&EE

E	EE(c           	         t        j                  d      5 }t        j                  d      5 }t        j                  | t	        |       ||t         j
                        }|st        d      	 t        d      }t        j                  ||d   |j                  z  dz        }|j                  |       t        d|j                  |j                  t        j                  |      t        j                  |t         j
                         cddd       cddd       S # t        j                  |t         j
                         w xY w# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z^Reads and decodes the whole mp3 audio data. Resulting sample format is 16 bits signed integer.rH  rI  r   r   r   r   N)r   r   r   -ma_dr_mp3_open_memory_and_read_pcm_frames_s16r   rg   r   r   r   r   r   r   r  r"   r'   rK  rR  s         r    mp3_read_s16rV    s   	%	&&#''3	BB#d)VZ
 788	1'*GZZ
1(G!(KLFf%#!!%% vsxx0)3 3	&	&( vsxx0)3 3	&	&	&s;   E>E +A5D $E 	E&D==E  E		EEc              #     K   t        j                  d      5 }t        j                  dk(  r1t	        |       }t        j                  ||t         j                        }n0t        |       }t        j                  ||t         j                        }|st        d      |dkD  r&t        j                  ||      }|dk  rt        d      	 t        j                  d||j                  z        5 }t        j                  d|      }	 t        j                  |||      }	|	dk  rnGt        j                  ||	dz  |j                  z        }
t!        d      }|j#                  |
       | d	 d	d	d	       t        j$                  |       	 d	d	d	       y	# 1 sw Y   (xY w# t        j$                  |       w xY w# 1 sw Y   y	xY ww)
zStreams the mp3 audio file as interleaved 16 bit signed integer sample arrays segments.
    This uses a fixed chunk size and cannot be used as a generic miniaudio decoder input stream.
    Consider using stream_file() instead.r?  r  r   r   r   zma_dr_mp3_int16[]zma_dr_mp3_int16 *r   N)r   r   r
  r  r  r   r@  rg   r   rA  r   ma_dr_mp3_seek_to_pcm_framer   r   ma_dr_mp3_read_pcm_frames_s16r   r   r   rC  )r   r7  r   r   r  r  r   r;  r<  r   r   r   s               r    mp3_stream_filerZ    s     
	3<<7"28<N..sNCHHMF/9M,,S-JF:;;>44S*EF{!,//	&#^cll%B((#6E"%"C"C^W#K #a' ZZkAo6TUF/2G%%f-!M 
    %; 
 	    %; 
 	N   F?B-F3#F)A<F&F.F3	F?F	FF00F33F<8F?c                    t        j                  d      5 }t        j                  dk(  r1t	        |       }t        j                  ||t         j                        }n0t        |       }t        j                  ||t         j                        }|st        d      	 |j                  |j                  z  }|j                  dz  }|j                  t
        j                  k(  }|j                  }t!        | t"        j$                  |j&                  |j                  t)        ||      ||j                  |      t        j*                  |       cddd       S # t        j*                  |       w xY w# 1 sw Y   yxY w)z9Fetch some information about the audio file (wav format).
ma_dr_wav*r  r   r	  )r   N)r   r   r
  r  r  r   ma_dr_wav_init_file_wrg   r   ma_dr_wav_init_filer   r  r  r  translatedFormatTagMA_DR_WAVE_FORMAT_IEEE_FLOATr   r   r   r   r  ma_dr_wav_uninit)	r   r   r  r  r   r   r   is_float	subformats	            r    r   r   4  s%   		#<<7"28<N..sNCHHMF/9M,,S-JF:;;	&-->H,,1L..#2R2RRH//I "<:&&$	   %5 
	4   %5 
	s%   BE+BE2E+E((E++E4c                 D   t        j                  d      5 }t        j                  || t	        |       t         j
                        st        d      	 |j                  |j                  z  }|j                  dz  }|j                  t        j                  k(  }t        dt        j                  |j                  |j                  t!        ||      ||j                        t        j"                  |       cddd       S # t        j"                  |       w xY w# 1 sw Y   yxY w)z9Fetch some information about the audio data (wav format).r]  r   r	  r   N)r   r   r   ma_dr_wav_init_memoryr   rg   r   r  r  r  r`  ra  r   r   r   r   r  rb  )r   r   r   r   rc  s        r    wav_get_inforg  S  s    		#((dCIsxxH:;;	&-->H,,1L..#2R2RRH "<:&&   %# 
	"   %# 
	s$   ;DBC<D<DDDc           
         t        |       }t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  ||||t        j
                        }|st        d      	 t        d      }t        j                  ||d   |d   z  dz        }|j                  |       t        | |d   |d   t        j                  |      t        j                  |t        j
                         cddd       cddd       cddd       S # t        j                  |t        j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z^Reads and decodes the whole wav audio file. Resulting sample format is 32 bits signed integer.r  ma_dr_wav_uint64 *r   r  r   N)r   r   r   r   +ma_dr_wav_open_file_and_read_pcm_frames_s32rg   r   r   r   r   r   r"   r+   ma_dr_wav_freer  s           r    r   r   i  6   '1M	!	"h1	cgg23z@@8[*chh
 788	1'*GZZ
1(Ca(GHFf%#(1+{1~|7L7Lg vsxx0 431 1	"	"  vsxx0 4331 1 1	"	"	"r!  c           
         t        |       }t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  ||||t        j
                        }|st        d      	 t        d      }t        j                  ||d   |d   z  dz        }|j                  |       t        | |d   |d   t        j                  |      t        j                  |t        j
                         cddd       cddd       cddd       S # t        j                  |t        j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z^Reads and decodes the whole wav audio file. Resulting sample format is 16 bits signed integer.r  ri  r   r   r   N)r   r   r   r   +ma_dr_wav_open_file_and_read_pcm_frames_s16rg   r   r   r   r   r   r"   r'   rk  r  s           r    r   r     rl  r!  c           
         t        |       }t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  ||||t        j
                        }|st        d      	 t        j                  d      }t        j                  ||d   |d   z  dz        }|j                  |       t        | |d   |d   t        j                  |      t        j                  |t        j
                         cddd       cddd       cddd       S # t        j                  |t        j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)zUReads and decodes the whole wav audio file. Resulting sample format is 32 bits float.r  ri  r   r&  r   r  N)r   r   r   r   +ma_dr_wav_open_file_and_read_pcm_frames_f32rg   r   r   r   r   r   r"   r-   rk  r  s           r    r   r     s:   '1M	!	"h1	cgg23z@@8[*chh
 788	1kk#&GZZ
1(Ca(GHFf%#(1+{1~|7K7KW vsxx0 431 1	"	"  vsxx0 4331 1 1	"	"	"r(  c                    t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  | t	        |       |||t         j
                        }|st        d      	 t        d      }t        j                  ||d   |d   z  dz        }|j                  |       t        d|d   |d   t        j                  |      t        j                  |t         j
                         cddd       cddd       cddd       S # t        j                  |t         j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z^Reads and decodes the whole wav audio data. Resulting sample format is 32 bits signed integer.r  ri  r   r  r   r   N)r   r   r   -ma_dr_wav_open_memory_and_read_pcm_frames_s32r   rg   r   r   r   r   r   r"   r+   rk  r+  s          r    wav_read_s32rs    3   	!	"h1	cgg23zBB#d)X{J
 788	1'*GZZ
1(Ca(GHFf%#HQKQ9N9NPW vsxx0 431 1	"	"  vsxx0 4331 1 1	"	"	"r.  c                    t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  | t	        |       |||t         j
                        }|st        d      	 t        d      }t        j                  ||d   |d   z  dz        }|j                  |       t        d|d   |d   t        j                  |      t        j                  |t         j
                         cddd       cddd       cddd       S # t        j                  |t         j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z^Reads and decodes the whole wav audio data. Resulting sample format is 16 bits signed integer.r  ri  r   r   r   r   N)r   r   r   -ma_dr_wav_open_memory_and_read_pcm_frames_s16r   rg   r   r   r   r   r   r"   r'   rk  r+  s          r    wav_read_s16rw    rt  r.  c                    t        j                  d      5 }t        j                  d      5 }t        j                  d      5 }t        j                  | t	        |       |||t         j
                        }|st        d      	 t        j                  d      }t        j                  ||d   |d   z  dz        }|j                  |       t        d|d   |d   t        j                  |      t        j                  |t         j
                         cddd       cddd       cddd       S # t        j                  |t         j
                         w xY w# 1 sw Y   nxY w	 ddd       n# 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)	zUReads and decodes the whole wav audio data. Resulting sample format is 32 bits float.r  ri  r   r&  r   r  r   N)r   r   r   -ma_dr_wav_open_memory_and_read_pcm_frames_f32r   rg   r   r   r   r   r   r"   r-   rk  r+  s          r    wav_read_f32rz    s6   	!	"h1	cgg23zBB#d)X{J
 788	1kk#&GZZ
1(Ca(GHFf%#HQKQ9M9Mw vsxx0 431 1	"	"  vsxx0 4331 1 1	"	"	"r5  c              #     K   t        j                  d      5 }t        j                  dk(  r1t	        |       }t        j                  ||t         j                        }n0t        |       }t        j                  ||t         j                        }|st        d      |dkD  r&t        j                  ||      }|dk  rt        d      	 t        j                  d||j                  z        5 }t        j                  d|      }	 t        j                  |||      }	|	dk  rnGt        j                  ||	dz  |j                  z        }
t!        d      }|j#                  |
       | d	 d	d	d	       t        j$                  |       	 d	d	d	       y	# 1 sw Y   (xY w# t        j$                  |       w xY w# 1 sw Y   y	xY ww)
zStreams the WAV audio file as interleaved 16 bit signed integer sample arrays segments.
    This uses a fixed chunk size and cannot be used as a generic miniaudio decoder input stream.
    Consider using stream_file() instead.r]  r  r   r   r   zma_dr_wav_int16[]zma_dr_wav_int16 *r   N)r   r   r
  r  r  r   r^  rg   r   r_  r   ma_dr_wav_seek_to_pcm_framer   r   ma_dr_wav_read_pcm_frames_s16r   r   r   rb  )r   r7  r   r   r  r  r   r;  r<  r   r   r   s               r    wav_stream_filer~    s     
	#<<7"28<N..sNCHHMF/9M,,S-JF:;;>44S*EF{!,//	&#^cll%B((#6E"%"C"C^W#K #a' ZZkAo6TUF/2G%%f-!M 
    %= 
	    %= 
	r[  soundc           	      4   t        j                  d      5 }t        j                  d      5 }t        j                  |_        |j
                  xs t        j                  |_        |j                  |_	        |j                  |_        |j                  dz  |_        | j                  t        j                                }t        j"                  ||||j$                  |j                  z  t         j&                        st)        d      	 t        j*                  ||j$                  |j,                  j/                                t        j0                  |       	 ddd       ddd       y# t        j0                  |       w xY w# 1 sw Y   ,xY w# 1 sw Y   yxY w)z"Writes the pcm sound to a WAV filema_dr_wav_data_format*r]  r	  zcan't open file for writingN)r   r   r   ma_dr_wav_container_riff	containerr   MA_DR_WAVE_FORMAT_PCMr   r   r   r   r  r   r  encoder
  getfilesystemencoding$ma_dr_wav_init_file_write_sequentialr   rg   IOErrorma_dr_wav_write_pcm_framesr   tobytesrb  )r   r  fmtpwavfilename_bytess        r    wav_write_filer    s+   	)	*c377<3HD44%%B)B)B
**!..2!)B)B)DE77.#u'7'7%//'I388
 788	'**e&&(=(=(?   &# 4I	*	*"   &# 4I3H	*	*s;   FCF9E(FF(E??FF	FFitemsizec                 v    dD ]*  }t        j                   |      }|j                  | k(  s(|c S  t        d      )NBhilqzcannot create array)r   r  
ValueError)r  typecodeas      r    r   r   '  s9    KK!::!H  *
++r   c                     t         j                  j                  |       }t         j                  j                  |      st	        |       |j                  t        j                               S r   )r   r   
expanduserisfileFileNotFoundErrorr  r
  r  r   	filename2s     r    r   r   /  sK    ""8,I77>>)$))C55788r   c                     t         j                  j                  |       }t         j                  j                  |      st	        |       t        j                  d|      S )Nz	wchar_t[])r   r   r  r  r  r   r   r  s     r    r  r  6  sB    ""8,I77>>)$))77;	**r   c                       e Zd ZdZddeee      ddfdZdeee	e
f      fdZdeee	e
f      fdZded	ej                  dee	e
f   fd
Zd Zy)DeviceszCQuery the audio playback and record devices that miniaudio providesNbackendsr   c                    t         j                  | _        t        j                  d      }|rqt        j                  dt	        |            }t        |      D ]  \  }}|j                  ||<    t        j                  |t	        |      t         j                  |      }n4t        j                  t         j                  dt         j                  |      }|t        j                  k7  rt        d|      || _        t        j                  t        j                  | j                  d   j                              j                         | _        y )Nma_context*ma_backend[]r   cannot init context)r   rg   _contextr   r   	enumerater   r   ma_context_init
MA_SUCCESSr   r   ma_get_backend_namebackendr   )r   r  contextbackends_memibr  s          r    r   zDevices.__init__@  s    ''-(77>3x=AL!(+1"#''Q ,((s8}chhPWXF((1chhHFS^^# !6??zz##DMM!$4$<$<=

&( 	r   c           	         t        j                  d      5 }t        j                  d      5 }t        j                  | j                  ||t         j
                  t         j
                        }|t        j                  k7  rt        d|      g }t        |d         D ]  }|d   |   }t        j                  d|j                        }t        j                  |j                        j                         t        j                  |d}|j                  | j!                  t        j                  |             |j#                  |        |cddd       cddd       S # 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z:Get a list of playback devices and some details about themma_device_info**
ma_uint32*cannot get device infosr   ma_device_id *r   typeidN)r   r   r   ma_context_get_devicesr  rg   r  r   ranger  r   r   r   r/   r1   update	_get_infoappend)	r   playback_infosplayback_countr  devsr  ma_device_infodev_idr   s	            r    get_playbackszDevices.get_playbacksQ  s+   WW'(NCGG=
//~~sxxF '$%>GGD>!,-!/!21!5$n&7&7  JJ~':':;BBD&// 
 DNN:+>+>OPD! . +=
 =
(( =
 =
(((#   E0DE	E0E$	 E00E9c           	         t        j                  d      5 }t        j                  d      5 }t        j                  | j                  t         j
                  t         j
                  ||      }|t        j                  k7  rt        d|      g }t        |d         D ]  }|d   |   }t        j                  d|j                        }t        j                  |j                        j                         t        j                  |d}|j                  | j!                  t        j                  |             |j#                  |        |cddd       cddd       S # 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)z9Get a list of capture devices and some details about themr  r  r  r   r  r  N)r   r   r   r  r  rg   r  r   r  r  r   r   r   r/   r3   r  r  r  )	r   capture_infoscapture_countr  r  r  r  r  r   s	            r    get_captureszDevices.get_capturesj  s+   WW'(M377<
//sxx=-F '$%>GGD=+,!.q!1!!4$n&7&7  JJ~':':;BBD&.. 
 DNN:+=+=~NOD! - +<
 <
(( <
 <
(((r  device_typedevice_infoc                 "   t        j                  | j                  |j                  t	        j
                  |j                        t	        j
                  |            }|t         j                  k7  rt        d      g }t        |j                        D ]x  }|j                  |   }|j                  t	        j                  t        j                  |j                              j!                         |j"                  |j$                  d       z d|iS )Nzcan't get device info)r   
samplerater   formats)r   ma_context_get_device_infor  r   r   	addressofr  r  r   r  nativeDataFormatCountnativeDataFormatsr  r   r   r   r   r  r   )r   r  r  r  r  r  data_formats          r    r  zDevices._get_info  s     //MMMM+..)MM+&	
 S^^# !899::;C%77<KNN!jj..{/A/ABfh"-"8"8 + 4 4 < 7##r   c                 B    t        j                  | j                         y r   )r   ma_context_uninitr  r   s    r    __del__zDevices.__del__  s    dmm,r   r   )r   r   r   r   r   r   rI   r   r   r   r
   r  r  r/   r   CDatar  r  r   r   r    r  r  =  s    M$w-!8 D "tDcN3 2d4S>2 2$%$47II$	c3h$4-r   r  sampleformatc                     t         j                  dt         j                  dt         j                  dt         j                  dt         j
                  dt         j                  di}| |v r||    S t        d|       )z>returns the sample width in bytes, of the given sample format.r   r   r      r  zunsupported sample format)r"   r   r%   r'   r)   r+   r-   r   )r  widthss     r    r   r     sp     	aqqqaF vl##
4l
CCr   c           
      *   t         j                  t        d      t         j                  t        d      t         j                  t        d      t         j
                  t        j                  d      i}| |v r||    S t        d| j                  z   dz         )Nr   r   r  r&  z6the requested sample format can not be used directly: z (convert it first))	r"   r%   r   r'   r+   r-   r   r   r   )r  arrayss     r    _array_proto_from_formatr    s     1! 40303ekk#.	F vl##
@


	
	  r   r   rc  c                    |rt         j                  S | dk(  rt         j                  S | dk(  rt         j                  S | dk(  rt         j                  S | dk(  rt         j
                  S | dk(  rt         j                  S t        d|       )Nr   r   r  r  r   zunsupported sample width)r"   r-   r%   r'   r)   r+   r   r   )r   rc  s     r    r  r    s    ###		%%%		$$$		$$$		$$$		###7FFr   r   D  output_formatr   r   ditherc           	         t        |      }t        |      }t        |       }t        j                  d      5 }t        j                  d      5 }	t        j                  |j                  ||      }
|j                  |
_        t        j                  |t        j                  |
      ||	      }|t
        j                  k7  rt        d|      t        j                  |	d   |d   |z  |z        }|j                  |       t        j                  |	d   t        j                          t#        | ||||      cddd       cddd       S # 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)zaConvenience function to decode any supported audio file to raw PCM samples in your chosen format.ma_uint64 *void **zfailed to decode filer   N)r   r  r   r   r   r   ma_decoder_config_initr   
ditherModema_decode_filer  r  r   r   r   ma_freerg   r   )r   r  r   r   r  r   r   r   framesr  decoder_configr  r   s                r    decode_filer    s    %]3L&}5G'1M		63779+=33K
 %+LL!##3==8&&
 S^^#5v>>F1Ivay9'<|'KL&!F1Isxx(imW
 ,>+=		+=+=			s$   EC%E2	EE	
EE#c           	         t        |      }t        |      }t        j                  d      5 }t        j                  d      5 }t	        j
                  |j                  ||      }	|j                  |	_        t	        j                  | t        |       t        j                  |	      ||      }
|
t        j                  k7  rt        d|
      t        j                  |d   |d   |z  |z        }|j                  |       t	        j                  |d   t        j                          t#        d||||      cddd       cddd       S # 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)zkConvenience function to decode any supported audio file in memory to raw PCM samples in your chosen format.r  r  zfailed to decode datar   r   N)r   r  r   r   r   r  r   r  ma_decode_memoryr   r  r  r   r   r   r  rg   r   )r   r  r   r   r  r   r   r  r  r  r  r   s               r    r   r     s    %]3L&}5G		63779+=33K
 %+LL!%%#d)S]]>:FF
 S^^#5v>>F1Ivay9'<|'KL&!F1Isxx(	;w
 ,>+=		+=+=			s$   EC/E1	EE		EE"decoderon_closec              #   |  K   |}t        |      }t        |      }t        | d      }		 t        j                  d|	|z  |z        5 }
t        j
                  d|
      }|xs | }d }|j                  t        j                  k7  rt        j                  |j                        }	 ||	kD  rt        dj                  ||	            d}t        j                  d      5 }	 t        j                  ||||      }|t        j                  k(  r|d   }n(|t        j                  k(  r
	 d d d        nt        d      	 d d d        |dk  rnz|r"|j"                  rt        d      |j"                  t        j$                  |
||z  |z        }t'        j&                  |j(                        }|j+                  |       |xs | }d d d        |r |        t        j,                  |       y # t         $ r}t        d      |d }~ww xY w# 1 sw Y   xY w# 1 sw Y   QxY w# |r |        t        j,                  |       w xY ww)	Ni @  zint8_t[]zvoid *zDwanted to read more frames than storage was allocated for ({} vs {})r   r  z#error in ma_decoder_read_pcm_frameszerror in read callback)r   r  maxr   r   r   	pUserDatarg   from_handler   r   r   ma_decoder_read_pcm_framesr  	MA_AT_ENDr   	Exceptionerror_in_readcallbackr   r   r  r   ma_decoder_uninit)r7  r   r  r  r   r  
_referencer   samples_protoallocated_buffer_framesr;  r<  want_framessourcer   frames_readr  xr   r   s                       r    _samples_stream_generatorr  
  s.     J$]3L,];M!.%8,'WW/);lJ
hhx6G!..A>KF  CHH,):):;!88(^ee')@ 
 
WW]+{U!$!?!?#Wk;" "S^^3)4QJ#s}}4! ,+ #..S"TT	 ' , ?f::%0!778 L*|2Ki2WX++m&<&<=!!&)%,}?? 
R Jg&- % X)*OPVWWX ,+!
 
R Jg&s~   %H<H BHHG$3-H 	H)H5BH=H H<$	G>-G99G>>HH
	HHH  H99H<c                    t        j                  d      }t        j                  |j                  ||      }|j                  |_        t        j                  dk(  r6t        |       }	t        j                  |	t        j                  |      |      }
n5t        |       }t        j                  |t        j                  |      |      }
|
t        j                  k7  rt        d|
      |dkD  r5t        j                  ||      }
|
t        j                  k7  rt        d|
      t!        ||||d      }t#        |      }t%        |      dk(  sJ |S )a  
    Convenience generator function to decode and stream any supported audio file
    as chunks of raw PCM samples in the chosen format.
    If you send() a number into the generator rather than just using next() on it,
    you'll get that given number of frames, instead of the default configured amount.
    This is particularly useful to plug this stream into an audio device callback that
    wants a variable number of frames per call.
    ma_decoder *r  failed to init decoderr   failed to seek to frameN)r   r   r   r  r   r  r
  r  r  ma_decoder_init_file_wr  r   ma_decoder_init_filer  r   ma_decoder_seek_to_pcm_framer  nextr   )r   r  r   r   r7  r  r   r  r  r  r  r   gdummys                 r    stream_filer  E  s#   " ggn%G//YN !'N
||w.x8++CMM.97
 ,H5))3==8'
 2F;;A~11':FS^^#7@@!	='4	A GEu:??Hr   c                    t        j                  d      }t        j                  |j                  ||      }|j                  |_        t        j                  | t        |       t        j                  |      |      }|t        j                  k7  rt        d|      t        |||||       }	t        |	      }
t        |
      dk(  sJ |	S )a  
    Convenience generator function to decode and stream any supported audio file in memory
    as chunks of raw PCM samples in the chosen format.
    If you send() a number into the generator rather than just using next() on it,
    you'll get that given number of frames, instead of the default configured amount.
    This is particularly useful to plug this stream into an audio device callback that
    wants a variable number of frames per call.
    r  r  r   )r   r   r   r  r   r  ma_decoder_init_memoryr   r  r  r   r  r  )r   r  r   r   r7  r  r  r  r  r  r  s              r    stream_memoryr
  s  s      ggn%G//YN !'N''c$i~6F 2F;;!	='4	A GEu:??Hr   pcmdatac                 p     dt         f fd} |       }t        |      }t        |      dk(  sJ |S )a<  
    Convenience generator function to stream raw pcm audio data from memory.
    Usually you don't need to use this as the library provides many other streaming
    options that work on much smaller, encoded, audio data.
    However, in the odd case that you only have already decoded raw pcm data you can use
    this generator as a stream source.

    The data can be provided in ``array`` type or ``bytes``, ``memoryview`` or even a numpy array.
    Be sure to also specify the correct number of channels that the audio data has, and the
    sample with in bytes.
    r   c               3      K   t              t        j                  u rdt              } dxs }d}|t        |       k  r(||z  z  z   }| || xs }|}|t        |       k  r'y y w)Nr   r   r   )r  r   
memoryviewr   )memdatarequired_framesr  
frames_endr7  r   r  r   s       r    _mem_stream_genz.stream_raw_pcm_memory.<locals>._mem_stream_gen  s      =EKK'LW%!$97s7|#/I"="LLJ%,VJ%??RNOF s7|#s   A'A-+A-r   )PlaybackCallbackGeneratorTyper  r   )r  r   r   r7  r  r  r  s   ````   r    stream_raw_pcm_memoryr    s=    $ :     	AGEu:??Hr   c                       e Zd ZdZej
                  ZdZej                  de
deeef   fd       Zde
dedefdZdd	Zdd
ZddZy)StreamableSourcezfBase class for streams of audio data bytes. Can be used as a contextmanager, to properly call close().N	num_bytesr   c                      y)zAoverride this to provide data bytes to the consumer of the streamNr   )r   r  s     r    readzStreamableSource.read  s     	r   offsetoriginc                      y)z
        Override this if the stream supports seeking.
        Note: seek support is sometimes not needed if you give the file type
        to a decoder upfront. You can ignore this method then.
        Fr   )r   r  r  s      r    seekzStreamableSource.seek  s     r   c                      y)z>Override this to properly close the stream and free resources.Nr   r   s    r    closezStreamableSource.close      r   c                     | S r   r   r   s    r    	__enter__zStreamableSource.__enter__      r   c                 $    | j                          y r   r  r   exc_typeexc_valexc_tbs       r    __exit__zStreamableSource.__exit__      

r   r   N)r   r  )r   r   r   r   r   rg   
ffi_handler  abcabstractmethodr   r	   bytesr  r  rz   boolr  r  r"  r*  r   r   r    r  r    sm    pJ c eE:,=&>  3 
 t r   r  c            	           e Zd ZdZdZdZ	 	 ddeded egdf   ddd	dfd
Zded	e	fdZ
ded	efdZddZded	efdZddZeded	eeef   fd       Zy)IceCastClienta~  
    A simple client for IceCast audio streams as miniaudio streamable source.
    If the stream has Icy MetaData, the stream_title attribute will be updated
    with the actual title taken from the metadata.
    You can also provide a callback to be called when a new stream title is available.
    The downloading of the data from the internet is done in a background thread
    and it tries to keep a (small) buffer filled with available data to read.
    You can optionally provide a custom ssl.SSLContext in the ssl_context parameter,
    if you need to change the way SSL connections are configured (certificates, checks, etc).
    i    i   Nurlupdate_stream_titlessl_contextzssl.SSLContextr   c                    || _         d| _        d| _        d| _        d| _        t
        j                  | _        d| _        d| _	        t        j                         | _        || _        t        j                  j!                  |ddi      }t        j                  j#                  ||      5 }|j$                  d	   | _        |j$                  d
   | _        |j$                  d   }| j'                  |      | _        d d d        t        j(                  | j*                  d      | _        | j,                  j/                          y # 1 sw Y   JxY w)Nz??? Fr   icy-metadata1headers)r  	icy-genreicy-nameContent-TypeT)targetdaemon)r4  stream_titlestation_genrestation_name
audio_infor   r   audio_format_stop_stream_buffer	threadingLock_buffer_lock_update_titleurllibrequestRequesturlopenr<  determine_audio_formatThread_download_stream_download_threadstart)r   r4  r5  r6  reqr  stream_formats          r    r   zIceCastClient.__init__  s    !"!&..!%NN,0nn$$S>32G$H^^##C#=!'!<D &z :D"NN>:M $ ; ;M JD	 >
 !* 0 0((!
 	##% >=s   +AEErW  c                     |dk(  rt         j                  S |dk(  rt         j                  S |j                  d      rt         j                  S t         j
                  S )Nz
audio/mpegz
audio/flacz/ogg)r   r   r   endswithr   r   )r   rW  s     r    rQ  z$IceCastClient.determine_audio_format  sL    L(>>!l*??"##F+$$$%%%r   r  c                    t        | j                        |k  r.t        j                  d       t        | j                        |k  r.| j                  5  | j                  d| }| j                  |d | _        |cddd       S # 1 sw Y   yxY w)z%Read a chunk of data from the stream.g?N)r   rH  timesleeprK  )r   r  chunks      r    r  zIceCastClient.read  sk    $,,)+JJsO $,,)+LL),E<<	
3DL s   %BBc                 F    d| _         | j                  j                          y)z5Stop the stream, aborting the background downloading.TN)rG  rT  joinr   s    r    r  zIceCastClient.close  s     ""$r   sizec                 l    d}t        |      |k  r#||j                  |      z  }t        |      |k  r#|S )Nr   )r   r  )r   
fileobjectr`  r  s       r    _readallzIceCastClient._readall  s7    !ftm&&A !ftmr   c                 |   t         j                  j                  | j                  ddi      }t         j                  j	                  |      5 }|j
                  d   | _        |j
                  d   | _        |j
                  d   }|r| j                  |      | _	        |j
                  j                  dd      | _        d	|j
                  v rt        |j
                  d	         }nd
}|rX| j                  st        | j                        | j                   k\  rNt#        j$                  d       | j                  r
	 d d d        y t        | j                        | j                   k\  rN| j'                  ||      }| j(                  5  | xj                  |z  c_        d d d        d| j'                  |d      d
   z  }t+        | j'                  ||      j-                  d      dd      }|rI| j/                  |      }|j                  d      }	|	r%|	| _        | j2                  r| j3                  | |	       | j                  sJn| j                  st        | j                        | j                   k\  rNt#        j$                  d       | j                  r
	 d d d        y t        | j                        | j                   k\  rN|j5                  | j6                        }| j(                  5  | xj                  |z  c_        d d d        | j                  sd d d        y # 1 sw Y   xY w# 1 sw Y   ,xY w# 1 sw Y   y xY w)Nr9  r:  r;  r=  r>  r?  zice-audio-infor8  zicy-metaintr   g?   r       zutf-8replace)errorsStreamTitle)rM  rN  rO  r4  rP  r<  rC  rD  rQ  rF  getrE  r   rG  r   rH  BUFFER_SIZEr[  r\  rc  rK  r   stripparse_metadatarB  rL  r  
BLOCK_SIZE)
r   rV  r  rW  meta_intervalr]  	meta_sizemetadatametarB  s
             r    rS  zIceCastClient._download_stream   s   nn$$TXX7L$M^^##C(F!'!<D &z :D"NN>:M$($?$?$N!$nn001A2FDO. #FNN=$A B !++dll+t/?/??

3,,"# )( dll+t/?/?? !MM&-@E**- + "T]]61%=a%@ @I"fi8>>uE( H
  #228<'+xx'>'0<D-#11 $ 2 24 F) ++, ++dll+t/?/??

3,,"O )(H dll+t/?/?? #KK8E**- + ++G )(& +*, +*S )(s]   C.L2"L2'L2LB$L2AL2"L2>'L2%L&;L2L#	L2&L/	+L22L;rq  c                     i }t        j                  |       } | j                  d      D ].  }|j                  d      \  }}}|s|j	                  d      ||<   0 |S )N;=')htmlunescapesplit	partitionrl  )rq  rr  partkey_r   s         r    rm  zIceCastClient.parse_metadataN  sX    ==*NN3'D NN3/MCE!KK,S	 ( r   )NNr,  )r   r   r   r   rn  rk  r   r   r   r   rQ  r   r0  r  r  rc  rS  staticmethodr   rm  r   r   r    r3  r3    s    	 JK
 GK(,	&& &&<d&BC& &	&
 
&6&C &J &c e %
  ,.\  c3h  r   r3  r  source_formatc                    t        j                  d      }t        j                  |j                  ||      }	|j                  |	_        t        j                  |       | _        |j                  |	_        t        j                  t        j                  t        j                  | j                  t        j                  |	      |      }
|
t        j                  k7  rt        d|
      |dkD  r5t        j                  ||      }
|
t        j                  k7  rt        d|
      dd}t!        ||||d|      }t#        |      }t%        |      dk(  sJ |S )a  
    Convenience function that returns a generator to decode and stream any source of encoded audio data
    (such as a network stream). Stream result is chunks of raw PCM samples in the chosen format.
    If you send() a number into the generator rather than just using next() on it,
    you'll get that given number of frames, instead of the default configured amount.
    This is particularly useful to plug this stream into an audio device callback that
    wants a variable number of frames per call.
    r  r  r   r   Nc                       y r   r   r   r   r    r  zstream_any.<locals>.on_close  s    r   r,  )r   r   r   r  r   r  
new_handler-  encodingFormatma_decoder_init_internal_decoder_read_callback_internal_decoder_seek_callbackr  r  r   r  r  r  r   )r  r  r  r   r   r7  r  r   r  r  r  r  r  r  s                 r    
stream_anyr  Y  s#   $ ggn%G//YN !'Nv.F$1$7$7N!  ++++n%F 2F;;A~11':FS^^#7@@ 	"	='4	A GEu:??Hr   sample_streamprogress_callbackframe_process_methodend_callbackc              #      K   d}	 	 | j                  |      }|r ||      }n|}|r ||       -# t        $ r |r	 |        Y yY yw xY ww)a%  
    Convenience generator function to add callback and processing functionality to another stream.
    You can specify :
    > A callback function that gets called during play and takes an int
    for the number of frames played.

    > A function that can be used to process raw data frames before they are yielded back
    (takes an array.array or bytes, returns an array.array or bytes)
    *Note: if the processing method is slow it will result in audio glitchiness

    > A callback function that gets called when the stream ends playing.
    r   N)sendStopIteration)r  r  r  r  frame_countframes         r    stream_with_callbacksr    sk     $ )K!&&{3E#$8$??$)k !+.   N s    A.6 AAAAr   r  
bytes_readc                    d|d<   |dk  s| j                   st        j                  S t        j                  | j                         }|j
                  n	 |j                  |      }t        j                  ||t        |             t        |      |d<   t        |      dkD  rt        j                  S t        j                  S t        j                  S # t        $ r }||_        Y d }~t        j                  S d }~ww xY wNr   )r  r   MA_ERRORr   r  r  r  memmover   r  r  r  )r  r   r  r  r  r   r  s          r    r  r    s     JqMA~W..||__W../F##+	-;;y)DKKc$i0IJqM%(Y]3>>EE <<  	-+,F((<<	-s   AC 0C 	C9C44C9r  seek_originc                    | j                   st        j                  S |dk(  r#|t        j                  k(  rt        j                  S t        j                  | j                         }	 |j                  |t        |            }|rt        j                  S t        j                  S # t        $ r t        j                  cY S w xY wr  )r  r   r  r}   r  r   r  r  rz   r  MA_BAD_SEEK)r  r  r  r  r  s        r    r  r    s     ||{{c&@&@@~~__W../FVZ%<=>>! ~~ s   %,B" B" "B>=B>from_fmt
sourcedatato_fmtc                     t        |       }t        |      |z  }t        |      }t        ||z        }t        j                  t        j                  |      |j                  || j                  ||j                         |S )zyConvert a raw buffer of pcm samples to another sample format.
    The result is returned as another raw pcm sample buffer)r   r   	bytearrayr   ma_pcm_convertr   from_bufferr   )r  r  r  r  r   r   r   s          r    convert_sample_formatr    sq     %X.Lj/\1K$V,L|k12F Mr   from_numchannelsfrom_samplerateto_numchannelsto_sampleratec                 @   t        |       }t        t        |      |z  |z        }t        |      }t        j                  |||      }	t        |	|z  |z        }
t        j                  t        j                  |
      |	|j                  ||||| j                  ||
       |
S )zConvert audio frames in source sample format with a certain number of channels,
    to another sample format and possibly down/upmixing the number of channels as well.)
r   r   r   r   )ma_calculate_frame_count_after_resamplingr  ma_convert_framesr   r  r   )r  r  r  r  r  r  r  r   r   output_frame_countr   s              r    convert_framesr    s     %X.LS_'77,FGJ$V,LFF
 )L8>IJF Mr   deviceinput
framecountc                     |dk  s| j                   sy t        j                  | j                         }|j                  | |||       y r  )r  r   r  _data_callback)r  r   r  r  callback_devices        r    _internal_data_callbackr    s>     Qf..oof&6&67O""665*Er   c                 ~    | j                   sy t        j                  | j                         }|j                  |        y r   )r  r   r  _stop_callback)r  r  s     r    _internal_stop_callbackr    s0    oof&6&67O""6*r   c            	           e Zd ZddZddZd Zd ZdeddfdZdd	Z	dd
Z
dej                  ddfdZej                  dfdee   dededej                  fdZy)AbstractDevicer   Nc                 b    d | _         d| _        d | _        t        j                  d      | _        y )NFzma_device *)callback_generatorrunningstop_callbackr   r   _devicer   s    r    r   zAbstractDevice.__init__  s)    "&!ww}-r   c                 $    | j                          y r   r%  r   s    r    r  zAbstractDevice.__del__%  r+  r   c                     | S r   r   r   s    r    r"  zAbstractDevice.__enter__(  r#  r   c                 $    | j                          y r   r%  r&  s       r    r*  zAbstractDevice.__exit__+  r+  r   r  c                     | j                   rt        d      || _         t        j                  | j                        }|t        j
                  k7  rt        d|      d| _        y)z[Start playback or capture, using the given callback generator (should already been started)z%can't start an already started devicezfailed to start audio deviceTN)r  r   r   ma_device_startr  r  r  )r   r  r  s      r    rU  zAbstractDevice.start.  sV    "" !HII #5$$T\\2S^^# !?HHr   c                     d| _         | j                  r>t        j                  | j                        }|t        j
                  k7  rt        d|      d| _        y)zHalt playback or capture.Nzfailed to stop audio deviceF)r  r  r   ma_device_stopr  r  r   )r   r  s     r    stopzAbstractDevice.stop;  sF    "&<<''5F'$%BFKKr   c                     	 | j                          | j                  &t        j                  | j                         d| _        d| _        y# t        $ r Y Ew xY w)z
        Halt playback or capture and close down the device.
        If you use the device as a context manager, it will be closed automatically.
        N)r  r   r  r   ma_device_uninitr  r   s    r    r  zAbstractDevice.closeD  sQ    
	IIK <<#  .DL!  		s   A 	AAr  c                 Z    | j                   rd| _        | j                          d| _        yy)zlCalled when the device is stopped (i.e. device disconnect or manual stop) Doesn't work consistently however.FN)r  r  r  )r   r  s     r    r  zAbstractDevice._stop_callbackR  s+     DL &*D# r   r8  r  thread_prioapp_namec                 @   t        j                         }|j                  |_        t	        j
                  d      }|rq|j                         | _        t	        j                  | j                        |j                  _
        t	        j                  | j                        |j                  _        |rt	        j
                  dt        |            }t        |      D ]  \  }}|j                  ||<    t        j                  |t        |      t	        j                   |      |      }	|	t         j"                  k7  rt%        d|	      |S t        j                  t        j&                  dt	        j                   |      |      }	|	t         j"                  k7  rt%        d|	      |S )Nr  r  r  r   )r   ma_context_config_initr   threadPriorityr   r   r  _context_app_namer  pulsepApplicationNamejackpClientNamer   r  r  r  r  r   rg   )
r   r  r  r  context_configr  r  r  r  r  s
             r    _make_contextzAbstractDevice._make_contextY  sH    335(3(9(9%''-(%-__%6D"47OO&&5N  1 /2ood>T>T.UN+77>3x=AL!(+1"#''Q ,((c(mS]]>-JGF '$%:FCC  ((!S]]>:GF '$%:FCCr   r,  )r   r   r   r   r  r"  r*  GeneratorTypesrU  r  r  r   r  r  ri   ru   r   rI   r   r  r   r   r    r  r    s    . 4 "+SYY +4 + '5&<&<	w- $ 	
 
r   r  c                       e Zd ZdZej
                  ddddddej                  df	ded	ed
edede	e
j                  df   dedeee      dededdf fdZdeddf fdZde
j                  de
j                  de
j                  deddf
dZ xZS )CaptureDevicezEAn audio device provided by miniaudio, for audio capture (recording).r   r     Nr   r8  input_formatr   r   buffersize_msec	device_idcallback_periodsr  r  r  r   c
                    t         |           || _        t        |      | _        || _        || _        || _        t        j                  |       | _
        t        j                  t        j                        | _        | j                  | j                  _        | j
                  | j                  j                   _        | j                  j$                  | j                  j                   _        |xs t        j&                  | j                  j                   _        | j                  | j                  _        | j                  | j                  _        t        j.                  | j                  _        t        j2                  | j                  _        || j                  _        d | _        | j;                  |xs g ||	      | _        t        j>                  | j<                  t        j@                  | j                        | jB                        }
|
t        jD                  k7  rtG        d|
      | jB                  jH                  jJ                  t        jL                  k(  r|rtN        j&                  |vrtG        d      t        jP                  t        jR                  | jB                  jH                  jJ                              jU                         | _%        y Nzfailed to init devicezno suitable audio backend found)+r   r   r   r   r   r   r   r  r   r  _ffi_handler   ma_device_config_initr2   
_devconfigr  capturer   r   rg   	pDeviceIDperiodSizeInMillisecondsr  r  dataCallbackr  stopCallbackperiodsr  r  r  ma_device_initr  r  r  r   pContextr  rf   rI   r   r  r   )r   r  r   r   r  r  r  r  r  r  r  r   s              r    r   zCaptureDevice.__init__~  s    	"-l;"&.>>$/33C4N4NO%)%5%5"+/>>()-):):&,5,A)373G3G0$($4$4!'*'B'B$'*'B'B$"2"&**8>r;Q##MM3==94<<
 S^^# !8&AA<<  ((C,?,??GLL8$%FGGzz##DLL$9$9$A$AB

&( 	r   r  c                 "    t         |   |      S )zStart the audio device: capture (recording) begins.
        The recorded audio data is sent to the given callback generator as raw bytes.
        (it should already be started before)r   rU  r   r  r   s     r    rU  zCaptureDevice.start  s     w}/00r   r  r   r  r  c                    | j                   r[| j                  | j                  z  |z  }t        |      }t	        j
                  |||       	 | j                   j                  |       y y # t        $ r
 d | _         Y y t        $ r	 d | _          w xY wr   )	r  r   r   r  r   r  r  r  r  )r   r  r   r  r  buffer_sizer   s          r    r  zCaptureDevice._data_callback  s     ""++dnn<zIK[)DKKe[1'',,T2 # ! *.' *.'s   A) )B;B)r   r   r   r   r"   r'   ri   ru   r   r	   r   r  r   r   rI   r   r   CaptureCallbackGeneratorTyperU  r  r   r   s   @r    r  r  {  s    O &2%:%: ",0 !,0&4&<&<)") ) 	)
 ) D)) ) 4=)) $) ) 
)V1(D 1 1ii),;>99RU	r   r  c                       e Zd ZdZej
                  ddddddej                  df	ded	ed
edede	e
j                  df   dedeee      dededdf fdZdeddf fdZde
j                  de
j                  de
j                  deddf
dZ xZS )PlaybackDevicez:An audio device provided by miniaudio, for audio playback.r   r  r  Nr   r8  r  r   r   r  r  r  r  r  r  r   c
                    t         |           || _        t        |      | _        || _        || _        || _        t        j                  |       | _
        t        j                  t        j                        | _        | j                  | j                  _        | j
                  | j                  j                   _        | j                  j$                  | j                  j                   _        |xs t        j&                  | j                  j                   _        | j                  | j                  _        | j                  | j                  _        t        j.                  | j                  _        t        j2                  | j                  _        || j                  _        d | _        | j;                  |xs g ||	      | _        t        j>                  | j<                  t        j@                  | j                        | jB                        }
|
t        jD                  k7  rtG        d|
      | jB                  jH                  jJ                  t        jL                  k(  r|rtN        j&                  |vrtG        d      t        jP                  t        jR                  | jB                  jH                  jJ                              jU                         | _%        y r  )+r   r   r   r   r   r   r   r  r   r  r  r   r  r0   r  r  playbackr   r   rg   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  rf   rI   r   r  r   )r   r  r   r   r  r  r  r  r  r  r  r   s              r    r   zPlaybackDevice.__init__  s    	#-m<"&.>>$/33C4O4OP%)%5%5",0NN  )*.++*;*;  '-6-B#((  *373G3G0$($4$4!'*'B'B$'*'B'B$"2"&**8>r;Q##MM3==94<<
 S^^# !8&AA<<  ((C,?,??GLL8$%FGGzz##DLL$9$9$A$AB

&( 	r   r  c                 "    t         |   |      S )av  Start the audio device: playback begins. The audio data is provided by the given callback generator.
        The generator gets sent the required number of frames and should yield the sample data
        as raw bytes, a memoryview, an array.array, or as a numpy array with shape (numframes, numchannels).
        The generator should already be started before passing it in.r  r  s     r    rU  zPlaybackDevice.start      
 w}/00r   r  r   r  r  c                 r   | j                   r	 | j                   j                  |      }t	        |      }|r[t        |      || j                  z  | j                  z  kD  rd | _         t        d      t        j                  ||t        |             y y y # t        $ r
 d | _         Y y t        $ r	 d | _          w xY w)Nz.number of frames from callback exceeds maximum)r  r  r  r  _bytes_from_generator_samplesr   r   r   r   r   r  )r   r  r   r  r  r   samples_bytess          r    r  zPlaybackDevice._data_callback  s     ""1166zB :'BM}%
T5F5F(F(WW.2D+(H  FM3}3EF  # ! *.' *.'s   B B6%B6)r   r   r   r   r"   r'   ri   ru   r   r	   r   r  r   r   rI   r   r   r  rU  r  r   r   s   @r    r  r    s    D '3&;&; ",0 !,0&4&<&<*#* * 	*
 * D)* * 4=)* $* * 
*X1(E 1$ 1GiiG),G;>99GRUG	Gr   r  c                   b    e Zd ZdZej
                  dej
                  dddddddej                  dfded	ed
edededede	e
j                  df   de	e
j                  df   dedeee      dededdf fdZdeddf fdZde
j                  de
j                  de
j                  deddf
dZ xZS )DuplexStreamz-Joins a capture device and a playback device.r   r  r  Nr   r8  playback_formatplayback_channelscapture_formatcapture_channelsr   r  playback_device_idcapture_device_idr  r  r  r  r   c                    t         |           || _        || _        t	        |      | _        || _        || _        || _        || _	        t        j                  |       | _        t        j                  t        j                        | _        | j                  | j                   _        | j                  | j                   j$                  _        | j                  j(                  | j                   j$                  _        |xs t        j,                  | j                   j$                  _        | j                  | j                   j0                  _        | j                  j(                  | j                   j0                  _        |xs t        j,                  | j                   j0                  _        | j                  | j                   _        | j                  | j                   _        t        j6                  | j                   _        t        j:                  | j                   _        |	| j                   _        d | _         | jC                  |
xs g ||      | _"        t        jF                  | jD                  t        jH                  | j                         | jJ                        }|t        jL                  k7  rtO        d|      | jJ                  jP                  jR                  t        jT                  k(  r|
rtV        j,                  |
vrtO        d      t        jX                  t        jZ                  | jJ                  jP                  jR                              j]                         | _)        y r  )/r   r   r  r  r   r   r  r  r   r  r   r  r  r   r  r4   r  r  r  r   r   r   rg   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  rf   rI   r   r  r   )r   r  r  r  r  r   r  r  r  r  r  r  r  r  r   s                 r    r   zDuplexStream.__init__  sX    	,.-n= 0!2&.>>$/33C4M4MN%)%5%5",0,B,B  )*.*>*>*D*D  '-?-K388  *+/+@+@()-)<)<)B)B&,=,I)373G3G0$($4$4!'*'B'B$'*'B'B$"2"&**8>r;Q##MM3==94<<
 S^^# !8&AA<<  ((C,?,??GLL8$%FGGzz##DLL$9$9$A$AB

&( 	r   r  c                 "    t         |   |      S )a   Start the audio device: playback and capture begin.
        The audio data for playback is provided by the given callback generator, which is sent the
        recorded audio data at the same time.
        (it should already be started before passing it in)r  r  s     r    rU  zDuplexStream.startB  r  r   r  r   r  r  c                 z   | j                   | j                  z  |z  }t        |      }t        j                  |||       | j
                  rK	 | j
                  j                  |      }|r,t        |      }t        j                  ||t        |             y y y # t        $ r
 d | _        Y y t        $ r	 d | _         w xY wr   )r   r  r  r   r  r  r  r  r  r  r   )	r   r  r   r  r  r  in_dataout_datar  s	            r    r  zDuplexStream._data_callbackI  s     ''$*?*??*LK(GUK0""2277@  =h GFM3}3EF  # ! *.' *.'s   B B:)B:)r   r   r   r   r"   r'   ri   ru   r   r	   r   r  r   r   rI   r   r   DuplexCallbackGeneratorTyperU  r  r   r   s   @r    r  r    s:   7 )5(=(=!"'3'<'< ! "5948 !,0&4&<&<1%1 1 %	1
 1 1 1 "#))T/21 !D11 1 4=)1 $1 1 
1f1(C 1 1GiiG),G;>99GRUG	Gr   r  r   c                 .   t        | t        j                        rt        |       j                  d      S t        | t              r | j                  dk7  r| j                  d      S t
        r*t        | t
        j                        r| j                         S | S )NBr   )
isinstancer   r  r   r  numpyndarrayr  )r   s    r    r  r  ]  sp     '5;;''"'',,	GZ	(W-=-=-B||C  	:gu}}5  Nr   c                   j    e Zd ZdZ	 ddedededededdfd	Zej                  fd
ede
e   fdZddZy)WavFileReadStreamzaAn IO stream that reads as a .wav file, and which gets its pcm samples from the provided producerpcm_sample_genr   r   r  
max_framesr   Nc                    || _         || _        || _        || _        || _        t        |      | _        ||z  | j                  z  xs t        j                  | _	        d| _
        t        j                  d      }t        j                  |_        t        j                   |_        ||_        ||_        | j                  dz  |_        t        j                  d      }t        j                  d      }t        j                  d      }	|dkD  r,t        j(                  |	|||||z  t        j*                         n't        j,                  |	|||t        j*                         t        j.                  |	       t1        t        j2                  |d   |d               | _        t        j6                  |d   t        j*                         y )Nr   r  r	  zvoid**size_t *r]  )
sample_genr   r   r   r  r   r   r
  maxsize	max_bytes
bytes_doner   r   r   r  r  r  r   r  r  &ma_dr_wav_init_memory_write_sequentialrg   ma_dr_wav_init_memory_writerb  r0  r   bufferedrk  )
r   r  r   r   r  r  r  r   datasizer  s
             r    r   zWavFileReadStream.__init__m  s\    )&"#$-m<$y043D3DDTgg./44..
 $ --1wwx 77:&ww|$>66dHc:	+A388 ++D$#sxxPT"cjja(1+>?47CHH-r   amountc                    | j                   | j                  k\  s| j                  syt        | j                        |k  rM	 t        | j                        }| xj                  t        |      z  c_        t        | j                        |k  rM| j                  d| }| j                  |d | _        | xj                   t        |      z  c_         |S # t        $ r t        j                  | _         Y cw xY w)z3Read up to the given amount of bytes from the file.r   N)
r  r  r  r   r  r  r  r  r
  r  )r   r  r   r  s       r    r  zWavFileReadStream.read  s    ??dnn,DOO$-- 6)Ht/
 !>w!GG $-- 6) w'fg.3v;& ! "%++s    C C0/C0c                      y)zClose the fileNr   r   s    r    r  zWavFileReadStream.close  r   r   r   r,  )r   r   r   r   r  r   r"   r   r
  r  r   r0  r  r  r   r   r    r  r  j  so    k ".5". ". 	".
 $". ". 
".H "% 3 % "r   r  c                  l    t        j                  t        j                               j	                         S )z@Returns the version string of the underlying miniaudio C library)r   r   r   ma_version_stringr   r   r   r    lib_versionr    s#    ::c++-.5577r   r  c                 R    t        t        j                  | j                              S )z~Determines whether or not the given backend is available by the compilation environment for the underlying miniaudio C library)r1  r   ma_is_backend_enabledr   r  s    r    is_backend_enabledr"    s    ))'--899r   c            	      Z   t        j                  d      5 } t        j                  d      5 }t        j                  |d|       }|t        j                  k7  rt        d      t        d |d| d    D              cddd       cddd       S # 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)zkReturns the set of available backends by the compilation environment for the underlying miniaudio C libraryr  zma_backend[30]   z can't determine enabled backendsc              3   2   K   | ]  }t        |        y wr   )rI   ).0r  s     r    	<genexpr>z'get_enabled_backends.<locals>.<genexpr>  s     >'=!71:'=s   r   N)r   r   r   ma_get_enabled_backendsr  r   set)countr  r  s      r    get_enabled_backendsr+    s    		sww/?'@H,,Xr5AS^^# !CDD>xE!H'=>>	 (A'@		'@'@			s#   B!AB9	B!B	B!!B*c                 R    t        t        j                  | j                              S )z@Determines whether or not loopback mode is support by a backend.)r1  r   ma_is_loopback_supportedr   r!  s    r    is_loopback_supportedr.    s    ,,W]];<<r   )Fr  )r6  r   r   )r   )NNN)r   __version__r.  r
  r   ior   urllib.requestrM  inspectr[  rw  rI  enumr   typingr   r   r   r   r   r	   r
   r   
_miniaudior   r   r  ImportErrorinit_miniaudior   r"   r/   r7   r?   rI   ri   rz   r0  
FramesTyper   r  r  r  r  r   r   r  r   r   r   r   r1  r   r   r   r   r   r  r   r  r   r   r   r,  r1  r4  r=  r   rF  r   r   rS  rV  rZ  r   rg  r   r   r   rs  rw  rz  r~  r  r   r   r  r  r   r  r  r'   r9   r  r   r  r  r  r
  r  r  ABCr  r3  r   r  r  
def_externr  r  r  r  r  r  r  r  r  r  r  r  	RawIOBaser  r  r"  r+  r.  r   r   r    <module>r<     s
     
 	 	        M M M     + + 4  ' '+ +.T .d (
-T 
-! ! 5%++%&
 )*c4*? @ (z4)?@ '
J(DE ! !) )X} 2	Y 		. 	1C 1M 151 51t 51@P 51p)3 )= )0)% )M ). s  '7  , e   0  , &',),)",)u{{D$&',)^# # #4# #- #*2 2)9 2,2 2)9 2,2 2)9 2,2 2"2 2*2 2"2 2*2 2"2 2, BC"#"##&"#;>"#u{{D$&'"#J& & &6&u & &*1 1(8 141 1(8 141u 1!1 121u 1!1 14 BC#&#&#&#&;>#&u{{D$&'#&L& & &>&u & &,1 1(8 1,1 1(8 1,1 1(8 1,1u 1!1 1*1u 1!1 1*1u 1!1 1, BC$&$&#&$&;>$&u{{D$&'$&N'S ')9 'd ',, , ,9# 9% 9+C +a- a-HDL DS D< EKK  GS GD G\ G& #/"7"7#


 
 	

 
 
> #/"7"7#



 
 	

 
 
D $(8'8'8'  8' YY	8'
 8' x 8' u{{C%&8'z #/"7"7#+++ + 	+
 + + + u{{C%&+` #/"7"7#
  	
   u{{C%&L 	"5;;
E12"" " 	"
 #"Jsww <~$ ~F !+ 2 2"."7"7#///  / 	/
 / / / / u{{C%&/h =ALP*.	0XseTk2D89  *z)A BD HI $'	
 #B YY #		69GJyy $ YY #25 . $	  	
 .  	
    D FIIF"yyF14FHKF	F F +CII +$ + +Z ZzBN BJIG^ IGXNG> NGb
5;;
E12


: :@8S 8
: :D :
?c'l ?=7 =t =EE  Es   W8 8XX