+
    ~j4C                       ^ RI Ht ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RI	H
t
 ^ RIHt ^ RIt^ RIt^ RIHt ^RIHt ^RIHt ^R	IHt R
 R lt ! R R4      t ! R R]P2                  4      t ! R R4      t ! R R4      tR# )    )annotationsN)AsyncIterator)ThreadPoolExecutor)cast)rtc)logger)aio)AudioByteStreamc                    V ^8  d   QhRRRR/# )   mime
str | Nonereturn )formats   "x/Users/mitch_tango/dev/rabbit-r1-livekit/agent/.venv/lib/python3.14/site-packages/livekit/agents/utils/codecs/decoder.py__annotate__r   $   s      Z J     c                    V '       g   R# V P                  4       p RRRRRRRRRRR	R
RR
RRRRRRRRRRRRRR/pVP                  V 4      # )u   Return the libav *container* short‑name for a given MIME‑type.

If *mime* is *None* or not recognised, return *None* so that PyAV will
fall back to auto‑detection.
Nz
audio/mpegmp3z	audio/mp3zaudio/x-mpegz	audio/aacaaczaudio/x-aacz
audio/flacflaczaudio/x-flacz	audio/wavwavz
audio/wavezaudio/x-wavz
audio/opusoggz	audio/oggz
audio/webmwebmz	audio/mp4mp4)lowerget)r   _TABLEs   & r   _mime_to_av_formatr    $   ss     ::<DeUUufUeueUfUF  ::dr   c                  ^    ] tR t^BtRtRtR R ltR R ltRR R lltR R	 lt	R
 R lt
RtR# )StreamBufferzo
A thread-safe buffer that behaves like an IO stream.
Allows writing from one thread and reading from another.
c                   V ^8  d   QhRR/# r   r   Noner   )r   s   "r   r   StreamBuffer.__annotate__J   s      $ r   c                	    \         P                  ! 4       V n        \        P                  ! 4       V n        \        P                  ! V P
                  4      V n        R V n        R V n	        ^ V n
        ^ V n        R# )FN)ioBytesIO_bio	threadingLock_lock	Condition_data_available_eof_closed
_write_pos	_read_posselfs   &r   __init__StreamBuffer.__init__J   sP    JJL	^^%
(224::>	r   c                    V ^8  d   QhRRRR/# r   databytesr   r%   r   )r   s   "r   r   r&   S   s     . .% .D .r   c                Z   V P                   ;_uu_ 4        V P                  P                  V P                  4       V P                  P	                  V4       V P                  P                  4       V n        V P                   P                  4        RRR4       R#   + '       g   i     R# ; i)z.Write data to the buffer from a writer thread.N)r/   r*   seekr2   writetell
notify_all)r5   r:   s   &&r   r>   StreamBuffer.writeS   se    !!!IINN4??+IIOOD!"iinn.DO  ++-	 "!!!s   A:BB*	c                    V ^8  d   QhRRRR/# )r   sizeintr   r;   r   )r   s   "r   r   r&   [   s     , , ,e ,r   c                   V^ 8X  d   R# V P                   ;_uu_ 4         V P                  '       d    RRR4       R# V P                  V P                  ,
          pV^ 8  EdB   V P                  P                  V P                  4       V^ 8  d   V P                  P                  V4      pM$V P                  P                  \        W4      4      pV P                  P                  4       V n        V P                  V P                  8  d   V P                  P                  4       p\        P                  ! V4      V n        V P                  P                  ^ \        P                  4       V P                  P                  4       V n        ^ V n        V'       d   TMRuuRRR4       # V P                  '       d    RRR4       R# V P                   P                  4        EK    + '       g   i     R# ; i)z-Read data from the buffer in a reader thread.r   N)r/   r1   r2   r3   r*   r=   readminr?   _COMPACT_THRESHOLDr(   r)   SEEK_ENDr0   wait)r5   rC   	availabler:   	remainings   &&   r   rF   StreamBuffer.read[   sF   19!!!<<< "!
 !OOdnn<	q=IINN4>>2ax#yy~~i8#yy~~c$.BC%)YY^^%5DN~~)@)@@$(IINN$4	$&JJy$9			q"++6*.))..*:)*#'4S+ "!. 9991 "!4 $$))+5 "!!s.   GGEGG#G5G GG.	c                   V ^8  d   QhRR/# r$   r   )r   s   "r   r   r&   |   s     . .4 .r   c                    V P                   ;_uu_ 4        RV n        V P                   P                  4        RRR4       R#   + '       g   i     R# ; i)z)Signal that no more data will be written.TN)r/   r0   r@   r4   s   &r   	end_inputStreamBuffer.end_input|   s6    !!!DI  ++- "!!!s   "AA	c                   V ^8  d   QhRR/# r$   r   )r   s   "r   r   r&      s      t r   c                	    V P                   ;_uu_ 4        R V n        V P                   P                  4        V P                  P	                  4        RRR4       R#   + '       g   i     R# ; i)TN)r/   r1   r@   r*   closer4   s   &r   rT   StreamBuffer.close   sD    !!!DL  ++-IIOO "!!!s   <AA,	)r*   r1   r/   r0   r-   r3   r2   Ni  P ))__name__
__module____qualname____firstlineno____doc__rH   r6   r>   rF   rP   rT   __static_attributes__r   r   r   r"   r"   B   s.    
 ).,B. r   r"   c                  *    ] tR t^t^ t^t^t^t^tRt	R# )	_WavStater   N)
rW   rX   rY   rZ   RIFF_HEADERCHUNK_HEADERFMT_DATASKIP_CHUNK_DATA	STREAMINGr\   r   r   r   r^   r^      s    KLHOIr   r^   c                      ] tR t^tRtRtR R ltR R ltR R ltR	 R
 lt	R R lt
R R ltR R ltR R ltR R ltR R ltR R ltRtR# )_WavInlineDecoderu  Incremental WAV decoder that runs entirely on the event loop (no thread).

Processes WAV bytes via a state machine:
RIFF_HEADER → CHUNK_HEADER → FMT_DATA/SKIP_CHUNK_DATA → STREAMING.
Once in STREAMING state, subsequent push() calls feed bytes directly to
AudioByteStream → optional resampler → output channel.

Each push() may contain a complete WAV file (with its own headers). When a
new RIFF header is detected while already streaming, the current stream is
flushed and the state machine resets to parse the new file's headers.
   RIFFc               $    V ^8  d   QhRRRRRR/# )r   	output_chzaio.Chan[rtc.AudioFrame]sample_rate
int | Noner   r%   r   )r   s   "r   r   _WavInlineDecoder.__annotate__   s(      +   
	r   c                	    Wn         W n        \        P                  V n        \        4       V n        ^V n        ^ V n        ^ V n	        RV n
        RV n        ^ V n        ^ V n        R# )   N)
_output_ch_sample_rater^   r_   _state	bytearray_hdr_buf_need_skip_remaining_chunk_size_bstream
_resampler_wave_channels
_wave_rate)r5   rh   ri   s   &&&r   r6   _WavInlineDecoder.__init__   s[    
 $'++!
  1559r   c                    V ^8  d   QhRRRR/# r9   r   )r   s   "r   r   rk      s       4 r   c                	F   V P                   \        P                  8X  d]   \        V4      ^8  d:   VR,          V P                  8X  d"   V P                  4        V P                  4        MV P                  V4       R# \        V4      p^ pV\        V4      8  Ed   V P                   \        P                  8X  d   V P                  W#4      pKC  V P                   \        P                  8X  d   V P                  W#4      pKu  V P                   \        P                  8X  d   V P                  W#4      pK  V P                   \        P                  8X  d   V P!                  W#4      pK  V P                   \        P                  8X  g   K  V P                  \#        W#R 4      4       R# R# )   Nr}   NN)rp   r^   rc   len_RIFF_MAGIC_flush_current_reset_state	_push_pcm
memoryviewr_   _consume_riffr`   _consume_chunk_headerra   _consume_fmt_datarb   _consume_skipr;   )r5   r:   bufposs   &&  r   push_WavInlineDecoder.push   s(   ;;)---4yA~$r(d.>.>">##%!!#t$CHn{{i333((2	 6 6600:	 2 22,,S6	 9 99((2	 3 33uSY/0 r   c                   V ^8  d   QhRR/# r$   r   )r   s   "r   r   rk      s      t r   c                	&    V P                  4        R # N)r   r4   s   &r   flush_WavInlineDecoder.flush   s    r   c                   V ^8  d   QhRR/# r$   r   )r   s   "r   r   rk      s     7 7 7r   c                   V P                   e   V P                   P                  4       pV P                  e   V F@  pV P                  P                  V4       F  pV P                  P                  V4       K   	  KB  	  V P                  P                  4        F1  pVP                  ^ 8  g   K  V P                  P                  V4       K3  	  R# V F  pV P                  P                  V4       K   	  R# R# )z@Flush AudioByteStream and resampler for the current WAV segment.N)rv   r   rw   r   rn   send_nowaitsamples_per_channel)r5   rL   frame	resampleds   &   r   r    _WavInlineDecoder._flush_current   s    ==$++-I*&E%)__%9%9%%@	33I> &A ' "__224E001433E: 5 'EOO//6 ' %r   c                   V ^8  d   QhRR/# r$   r   )r   s   "r   r   rk      s     
 
d 
r   c                    \         P                  V n        V P                  P	                  4        ^V n        ^ V n        ^ V n        RV n        RV n	        ^ V n
        ^ V n        R# )z0Reset the state machine to parse a new WAV file.N)r^   r_   rp   rr   clearrs   rt   ru   rv   rw   rx   ry   r4   s   &r   r   _WavInlineDecoder._reset_state   sS    ++
 r   c               $    V ^8  d   QhRRRRRR/# r   r   r   r   rD   r   r   )r   s   "r   r   rk      s!       # # r   c                	8   \        V P                  \        V P                  4      ,
          \        V4      V,
          4      pV P                  P	                  WW#,            4       W#,          p\        V P                  4      V P                  8  d   V# V P                  R ,          R8w  g   V P                  R,          R8w  d#   \        R\        V P                  4      : 24      hV P                  P                  4        ^V n        \        P                  V n
        V# )r~   rf   :   rm   Ns   WAVEz%Invalid WAV file: missing RIFF/WAVE: )rG   rs   r   rr   extend
ValueErrorr;   r   r^   r`   rp   r5   r   r   takes   &&& r   r   _WavInlineDecoder._consume_riff   s    4::DMM 22CHsNCSsz23t}}

*J=='4==+>'+IDU4==EYD\]^^
,,
r   c               $    V ^8  d   QhRRRRRR/# r   r   )r   s   "r   r   rk      s!       # # r   c                	   \        V P                  \        V P                  4      ,
          \        V4      V,
          4      pV P                  P	                  WW#,            4       W#,          p\        V P                  4      V P                  8  d   V# \
        P                  ! R \        V P                  R,          4      4      w  rEV P                  P                  4        WPn	        VR8X  d   WPn        \        P                  V n        V# VR8X  d(   V P                  4        \        P                  V n        V# WPn        \        P                   V n        V# )z<4sINr   Ns   fmt s   data)rG   rs   r   rr   r   structunpackr;   r   ru   r^   ra   rp   _init_streamingrc   rt   rb   )r5   r   r   r   chunk_id
chunk_sizes   &&&   r   r   '_WavInlineDecoder._consume_chunk_header   s    4::DMM 22CHsNCSsz23t}}

*J%}}VU4==;L5MN%w#J#,,DK 
    "#--DK 
 $. #33DK
r   c               $    V ^8  d   QhRRRRRR/# r   r   )r   s   "r   r   rk     s!      Z c c r   c                	   \        V P                  \        V P                  4      ,
          \        V4      V,
          4      pV P                  P	                  WW#,            4       W#,          p\        V P                  4      V P                  8  d   V# \        V P                  R V P                   4      p\        P                  ! RVR,          4      w  rVp\        V4      ^8  d<   \        P                  ! RVR,          4      ^ ,          pV^8w  d   \        RV R24      hV^8w  d   \        RV 24      hW`n
        Wpn        V P                  P                  4        ^V n        \        P                  V n        V# )Nz<HHIr   z<H:      Nz!Unsupported WAV bits per sample: z (only 16-bit PCM supported)zUnsupported WAV audio format: )rG   rs   r   rr   r   r;   ru   r   r   r   rx   ry   r   r^   r`   rp   )	r5   r   r   r   fmtaudio_formatchannelsratebits_per_samples	   &&&      r   r   #_WavInlineDecoder._consume_fmt_data  s1   4::DMM 22CHsNCSsz23t}}

*JDMM"4D$4$456'-}}VSW'E$s8r>$mmD#e*=a@O"$ 77H23  1=l^LMM&
,,
r   c               $    V ^8  d   QhRRRRRR/# r   r   )r   s   "r   r   rk   +  s!       # # r   c                	"   \        V P                  \        V4      V,
          4      pV ;P                  V,          un        W#,          pV P                  ^ 8X  d7   V P                  P	                  4        ^V n        \        P                  V n        V# )r   )	rG   rt   r   rr   r   rs   r^   r`   rp   r   s   &&& r   r   _WavInlineDecoder._consume_skip+  sk    4''SC8$1$MM!DJ#00DK
r   c                   V ^8  d   QhRR/# r$   r   )r   s   "r   r   rk   7  s       r   c                	\   V P                   ^ 8X  d   \        R4      h\        V P                   V P                  R7      V n        V P
                  e\   V P
                  V P                   8w  d?   \        P                  ! V P                   V P
                  V P                  R7      V n        R# R# R# )r   z-Invalid WAV file: data chunk before fmt chunk)ri   num_channelsN)
input_rateoutput_rater   )	ry   r   r
   rx   rv   ro   r   AudioResamplerrw   r4   s   &r   r   !_WavInlineDecoder._init_streaming7  s    ??aLMM'd6I6I
 (T->->$//-Q!00?? --!00DO .R(r   c                    V ^8  d   QhRRRR/# r9   r   )r   s   "r   r   rk   E  s     3 3e 3 3r   c                	~   V P                   f   Q hV P                  eb   V P                   P                  V4       F@  pV P                  P                  V4       F  pV P                  P	                  V4       K   	  KB  	  R # V P                   P                  V4       F  pV P                  P	                  V4       K   	  R # r   )rv   rw   r   rn   r   )r5   r:   r   r   s   &&  r   r   _WavInlineDecoder._push_pcmE  s    }}(((??&++D1!%!5!5e!<IOO//	: "= 2 ++D1++E2 2r   )rv   ru   rr   rs   rn   rw   ro   rt   rp   rx   ry   N)rW   rX   rY   rZ   r[   r   r6   r   r   r   r   r   r   r   r   r   r   r\   r   r   r   re   re      sJ    
 K(27
,43 3r   re   c                      ] tR tRtRtRRR^RR/R R	 lltR
 R ltR R ltR R ltR R lt	R R lt
R R ltR R ltRtR# )AudioStreamDecoderiP  zA class that can be used to decode audio stream into PCM AudioFrames.

Decoders are stateful, and it should not be reused across multiple streams. Each decoder
is designed to decode a single stream.
ri   i  r   r   Nc               $    V ^8  d   QhRRRRRR/# )r   ri   rj   r   r   r   r   )r   s   "r   r   AudioStreamDecoder.__annotate__W  s*     ; ;  ; !	;
 ;r   c               	   Wn         R V n        V^8X  d   RV n        V'       d   VP                  4       MRV n        \	        V P                  4      V n        V P
                  R8H  V n        \        P                  \        P                  ,          ! 4       V n        RV n        RV n        \        P                  ! 4       V n        RV n        RV n        RV n        R# )monostereoNr   F)ro   _layoutr   
_mime_typer    
_av_format_is_wavr	   Chanr   
AudioFramern   r1   _startedasyncioget_event_loop_loop
_input_buf	_executor_wav_decoder)r5   ri   r   r   s   &$$$r   r6   AudioStreamDecoder.__init__W  s     (1#DL,2&,,.,T__=%/((3>>24++-
 0448 7;r   c                    V ^8  d   QhRRRR/# )r   chunkr;   r   r%   r   )r   s   "r   r   r   t  s     J J% JD Jr   c                	   V P                   '       dY   V P                  f&   \        V P                  V P                  4      V n         V P                  P                  V4       RT n        R # V P                  f"   \        4       V n        \        ^RR7      V n        V P                  P!                  V4       V P                  '       g:   RV n        V P"                  P%                  V P                  V P&                  4       R # R #   \         dN    T P                  '       g8   \        P                  ! R4       T P                  P                  4        RT n         R # i ; i)Nzerror decoding WAV audioTAudioDecoder)max_workersthread_name_prefix)r   r   re   rn   ro   r   	Exceptionr1   r   	exceptionrT   r   r   r"   r   r   r>   r   run_in_executor_decode_loop)r5   r   s   &&r   r   AudioStreamDecoder.pusht  s    <<<  ($5dootGXGX$Y!!!&&u- !DM??"*nDO/AR`aDNe$}}} DMJJ&&t~~t7H7HI   |||$$%?@OO))+#'DLs   D E7EEc                   V ^8  d   QhRR/# r$   r   )r   s   "r   r   r     s     $ $4 $r   c                	   V P                   '       dj   V P                  e.   V P                  '       g    V P                  P                  4        V P                  '       g   V P                  P                  4        R # V P                  e   V P                  P                  4        V P                  '       g   V P                  P                  4        R # R #   \         d    \
        P                  ! R4        Li ; i)Nzerror flushing WAV audio)r   r   r1   r   r   r   r   rn   rT   r   rP   r   r4   s   &r   rP   AudioStreamDecoder.end_input  s    <<<  ,T\\\A%%++- <<<%%'??&OO%%'}}}OO!!#  ! A$$%?@As   C !C87C8c                   V ^8  d   QhRR/# r$   r   )r   s   "r   r   r     s     :" :"d :"r   c                	   R pR p \         P                  ! V P                  RV P                  RRRRRRRR	R
RRRRRR/R7      pV;P                  \        \        \         P                  P                  P                  P                  \         P                  P                  P                  P                  ,          4      ,          un        \        VP                  P                  4      ^ 8X  d   \        R4      hVP                  P                  ^ ,          pV P                   f   V P"                  e.   \         P$                  ! RV P"                  V P                   R7      pVP'                  V4       F  pV P(                  '       dM    V P*                  P-                  V P.                  P0                  4       V'       d   VP1                  4        R # R # V'       d   VP3                  V4      pMV.pV F  pV P5                  V4       K  	  K  	  V'       d<   V P(                  '       g*   VP3                  R 4       F  pV P5                  V4       K  	  T P*                  P-                  T P.                  P0                  4       T'       d   TP1                  4        R # R #   \6         d    \8        P:                  ! R4        Lni ; i  T P*                  P-                  T P.                  P0                  4       T'       d   TP1                  4        i i ; i)Nr   	probesize32analyzeduration0fflagsznobuffer+flush_packetsflags	low_delayreorder_queue_size	max_delay	avioflagsdirect)moder   buffer_sizeoptionszno audio stream founds16)r   layoutr   zerror decoding audio)avopenr   r   r   r   rD   	containerFlags	no_buffervalueflush_packetsr   streamsaudior   ro   r   r   decoder1   r   call_soon_threadsafern   rT   resample_emit_av_framer   r   r   )r5   r   	resampleraudio_streamr   framesfs   &      r   r   AudioStreamDecoder._decode_loop  sV   8<	.2	7	"%s6[(#I  OOtR\\''1177",,:L:L:Z:Z:`:``  O 9$$**+q0 !899$,,2215L   ,0H-- D<M<M	 #)),7<<<$ JJ++DOO,A,AB! # &//6F#WFA''*   8 "++D1A''* 2 JJ++DOO,A,AB! 	  	534	5 JJ++DOO,A,AB! s=   E?J J :J J %)J !J=:K  <J==K   A
L
c                    V ^8  d   QhRRRR/# )r   r  zav.AudioFramer   r%   r   )r   s   "r   r   r     s     	
 	
 	
$ 	
r   c           
     	B   V P                   P                  V P                  P                  \        P
                  ! VP                  4       P                  4       \        VP                  P                  4      \        VP                  4      VP                  R 7      4       R# ))r:   r   ri   r   N)r   r  rn   r   r   r   
to_ndarraytobytesr   r   r   rD   ri   samples)r5   r  s   &&r   r	  !AudioStreamDecoder._emit_av_frame  sd    

''OO''NN\\^++- !2!23.$%II		
r   c                   V ^8  d   QhRR/# )r   r   zAsyncIterator[rtc.AudioFrame]r   )r   s   "r   r   r     s      8 r   c                	    V # r   r   r4   s   &r   	__aiter__AudioStreamDecoder.__aiter__  s    r   c                   V ^8  d   QhRR/# )r   r   zrtc.AudioFramer   )r   s   "r   r   r     s     1 1 1r   c                	R   "   V P                   P                  4       G R j  xL
 #  L5ir   )rn   	__anext__r4   s   &r   r  AudioStreamDecoder.__anext__  s     __..0000s   '%'c                   V ^8  d   QhRR/# r$   r   )r   s   "r   r   r     s     E Ed Er   c                	l  "   V P                   '       d   R # V P                  4        RV n         V P                  e   V P                  P                  4        V P                  '       g   R # V P
                    R j  xL
  pK   LDT P                  e    T P                  P                  RRR7       R # R # 5i)NTF)rJ   cancel_futures)r1   rP   r   rT   r   rn   r   shutdown)r5   _s   & r   acloseAudioStreamDecoder.aclose  s     <<<??&OO!!#}}} 	 	!	 >>%NN##t#D &s0   A%B4(B46B:B;B>B4B0B4)r   r1   r   r   r   r   r   r   rn   ro   r   r   )rW   rX   rY   rZ   r[   r6   r   rP   r   r	  r  r  r"  r\   r   r   r   r   r   P  sV    ; #(; $%	;
 ";:J.$ :"x	
1E Er   r   )
__future__r   r   enumr(   r   r+   collections.abcr   concurrent.futuresr   typingr   r   av.containerlivekitr   logr    r	   r  r
   r    r"   IntEnumr^   re   r   r   r   r   <module>r.     sq    #   	   ) 1  	     #<D DN |3 |3~iE iEr   