ó
    üÞ jF  ã                  ó¨   • S r SSKJr  SSKrSSKrSSKrSSKrSrSSS jjr      SS jr	SS jr
          SS jr\S	.         SS
 jjrg)uÉ  WAV reconstruction for the voice integrations.

* ``pcm_to_wav`` â€” wrap already-merged PCM16 bytes in a WAV container. Used by the
  Pipecat processor (whose ``AudioBufferProcessor`` emits merged stereo audio).
* ``build_stereo_session_wav`` â€” reconstruct one stereo conversation WAV from the
  timestamped PCM16 chunks each side recorded (L=user, R=agent), laid out at
  natural play time so bursts don't overlap. Used by the Track-B ``EventSession``.
é    )ÚannotationsNiX  c                ód  • U (       d  g[         R                  " 5       n[        R                  " US5       nUR	                  U5        UR                  S5        UR                  U5        UR                  U 5        SSS5        UR                  5       $ ! , (       d  f       UR                  5       $ = f)zÎWrap raw PCM16 bytes in a WAV container.

For already-merged PCM (e.g. the output of Pipecat's ``AudioBufferProcessor``,
where ``num_channels=2`` is user-left / bot-right). Returns ``b""`` for empty
input.
ó    Úwbé   N)	ÚioÚBytesIOÚwaveÚopenÚsetnchannelsÚsetsampwidthÚsetframerateÚwriteframesÚgetvalue)ÚpcmÚsample_rateÚnum_channelsÚbufÚwfs        ÚY/var/www/html/gaurav/venv/lib/python3.13/site-packages/langsmith/_internal/voice/audio.pyÚ
pcm_to_wavr      s   € ö ØÜ
�*Š*‹,€CÜ	�Š�3˜Ô	 Ø
�‰˜Ô%Ø
�‰˜ÔØ
�‰˜Ô$Ø
�‰�sÔ÷	 
ð
 �<‰<‹>Ð÷ 
Ô	ð
 �<‰<‹>Ðús   µABÂ
B/c                ó®   • / nSn[        U 5       HA  u  nu  pVUS:X  a  UO
[        X55      nUR                  X645        U[        U5      S-  U-  -  nMC     U$ )u=  Rewrite receipt timestamps into natural-play timestamps.

Receipt times reflect when bytes arrived from the source, not when they
play. The agent channel especially arrives in bursts faster than realtime â€”
multiple chunks can land within a few ms of each other, and placing them at
receipt time makes them overlap and overwrite each other (you hear scrambled
tail-ends). The correct natural play time for a chunk is the LATER of where
the previous chunk ended and when this chunk arrived, which preserves real
gaps between bursts and keeps consecutive bursts contiguous.
ç        r   r   )Ú	enumerateÚmaxÚappendÚlen)Úchunksr   ÚoutÚcur_timeÚiÚt_recvÚdatas          r   Ú_layout_chunks_to_play_timer$   &   sc   € ð &(€CØ€HÜ& vÖ.Ñˆ‰>ˆFØ !›V‘6¬¨XÓ)>ˆØ�
‰
�HÐ#Ô$Ø”S˜“Y !‘^ {Ñ2Ñ2Šñ /ð €Jr   c                ó*   • U [        U5      S-  U-  -   $ )Nr   )r   )Útr#   r   s      r   Ú
_chunk_endr'   <   s   € Ø”�D“	˜Q‘ +Ñ-Ñ-Ð-r   c                ó¨   ^• Uc  g[        U T5      n[        UT5      n[        U4S jU 5       SS9n[        U4S jU 5       SS9n[        Xg5      U:„  $ )z@Return whether natural-play WAV layout exceeds the duration cap.Fc              3  ó>   >#   • U H  u  p[        XT5      v •  M     g 7f©N©r'   ©Ú.0r&   Údr   s      €r   Ú	<genexpr>Ú3session_wav_exceeds_duration_cap.<locals>.<genexpr>K   ó   øé € ÐC¹d±d°a”J˜q [×1Ð1ºdùó   ƒr   ©Údefaultc              3  ó>   >#   • U H  u  p[        XT5      v •  M     g 7fr*   r+   r,   s      €r   r/   r0   L   ó   øé € ÐE¹u±t°q”Z  k×2Ð2ºuùr2   )r$   r   )Úuser_chunksÚagent_chunksr   Úmax_duration_secondsÚuserÚagentÚuser_endÚ	agent_ends     `     r   Ú session_wav_exceeds_duration_capr>   @   s[   ø€ ð Ñ#ØÜ& {°KÓ@€DÜ'¨°kÓB€EÜÔC¹dÓCÈSÑQ€HÜÔE¹uÓEÈsÑS€IÜˆxÓ#Ð&:Ñ:Ð:r   )r9   c               ól  ^^• U (       d  U(       d  g[        U T5      n[        UT5      n[        U4S jU 5       SS9n[        U4S jU 5       SS9n[        [        R                  " [        Xg5      T-  5      5      mUb.  [        [        R                  " UT-  5      5      n[        TU5      mTS::  a  gSUU4S jjn	U	" U5      n
U	" U5      n[        R                  " S	[        S
T-  5      5      nX¬SSS2'   X¼SSS2'   [        R                  " 5       n[        R                  " US5       nUR                  S5        UR                  S5        UR                  T5        UR                  UR!                  5       5        SSS5        UR#                  5       $ ! , (       d  f       UR#                  5       $ = f)aV  Reconstruct a duration-capped stereo WAV from timestamped PCM16 chunks.

Left channel = user, right channel = agent. Both channels are laid out at
natural play time (see ``_layout_chunks_to_play_time``). Gaps between bursts
are silence; overlap between user and agent (during a barge-in) is preserved
because they live on different channels.
r   c              3  ó>   >#   • U H  u  p[        XT5      v •  M     g 7fr*   r+   r,   s      €r   r/   Ú+build_stereo_session_wav.<locals>.<genexpr>d   r1   r2   r   r3   c              3  ó>   >#   • U H  u  p[        XT5      v •  M     g 7fr*   r+   r,   s      €r   r/   rA   e   r6   r2   Nr   c           
     ó$  >• [         R                   " S[        ST-  5      5      nU  He  u  p#[        UT-  5      n[         R                   " SUS S[        U5      S-  -   5      n[	        [        U5      TU-
  5      nUS:”  d  M\  US U XXF-   & Mg     U$ )NÚhr   r   )ÚarrayÚbytesÚintr   Úmin)	r   Úchanr&   r#   ÚoffsetÚsamplesÚnr   Útotal_sampless	          €€r   Úmono_channelÚ.build_stereo_session_wav.<locals>.mono_channelm   s“   ø€ ô �{Š{˜3¤ a¨-Ñ&7Ó 8Ó9ˆÛ‰GˆAÜ˜˜[™Ó)ˆFÜ—k’k # tÐ,B¨a´3°t³9À±>Ñ.BÐ'CÓDˆGÜ”C˜“L -°&Ñ"8Ó9ˆAØ�1�uØ,3°B°Q¨K�˜f™jÒ)ñ ð ˆr   rD   é   r   é   r   )r   úlist[tuple[float, bytes]]Úreturnzarray.array)r$   r   rG   ÚmathÚceilrH   rE   rF   r   r	   r
   r   r   r   r   r   Útobytesr   )r7   r8   r   r9   r:   r;   r<   r=   Úmax_samplesrN   ÚleftÚrightÚstereoÚwav_ior   rM   s     `            @r   Úbuild_stereo_session_wavr\   P   sp  ù€ ö ž|Øä& {°KÓ@€DÜ'¨°kÓB€EäÔC¹dÓCÈSÑQ€HÜÔE¹uÓEÈsÑS€IÜœŸ	š	¤# hÓ":¸[Ñ"HÓIÓJ€MØÑ'Üœ$Ÿ)š)Ð$8¸;Ñ$FÓGÓHˆÜ˜M¨;Ó7ˆØ˜ÓØ÷ð ñ ˜Ó€DÙ˜Ó€Eô �[Š[˜œe A¨Ñ$5Ó6Ó7€FØˆ1ˆ4ˆaˆ4�LØˆ1ˆ4ˆaˆ4�Lä�ZŠZ‹\€FÜ	�Š�6˜4Ô	  BØ
�‰˜ÔØ
�‰˜ÔØ
�‰˜Ô$Ø
�‰�v—~‘~Ó'Ô(÷	 
!ð
 �?‰?ÓÐ÷ 
!Ô	 ð
 �?‰?ÓÐús   Ä+AFÆ
F3)rQ   )r   rF   r   rG   r   rG   rS   rF   )r   rR   r   rG   rS   rR   )r&   Úfloatr#   rF   r   rG   rS   r]   )
r7   rR   r8   rR   r   rG   r9   úfloat | NonerS   Úbool)
r7   rR   r8   rR   r   rG   r9   r^   rS   rF   )Ú__doc__Ú
__future__r   rE   r   rT   r
   ÚDEFAULT_MAX_AUDIO_SECONDSr   r$   r'   r>   r\   © r   r   Ú<module>rd      s´   ðñõ #ã Û 	Û Û à#Ð öð$Ø%ðØ47ðàôô,.ð;Ø*ð;à+ð;ð ð;ð 'ð	;ð
 
ô;ð* *Cñ9Ø*ð9à+ð9ð ð9ð
 'ð9ð ö9r   