Ë
    ³ŒjF  ã                  óš   — d Z ddlmZ ddlZddlZddlZddlZdZddd„Z	 	 	 	 	 	 dd„Z	dd„Z
	 	 	 	 	 	 	 	 	 	 dd„Zed	œ	 	 	 	 	 	 	 	 	 dd
„Zy)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                óL  — | syt        j                  «       }t        j                  |d«      5 }|j	                  |«       |j                  d«       |j                  |«       |j                  | «       ddd«       |j                  «       S # 1 sw Y   |j                  «       S xY w)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        úi/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langsmith/_internal/voice/audio.pyÚ
pcm_to_wavr      s|   € ñ ØÜ
�*‰*‹,€CÜ	�‰�3˜Ô	 Ø
�‰˜Ô%Ø
�‰˜ÔØ
�‰˜Ô$Ø
�‰�sÔ÷	 
ð
 �<‰<‹>Ð÷ 
ð
 �<‰<‹>Ðús   ®ABÂB#c                ó®   — g }d}t        | «      D ]B  \  }\  }}|dk(  r|nt        ||«      }|j                  ||f«       |t        |«      dz  |z  z  }ŒD |S )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$   &   si   € ð &(€CØ€HÜ& vÖ.Ñˆ‰>ˆF�DØ !šV‘6¬¨X°vÓ)>ˆØ�
‰
�H˜dÐ#Ô$Ø”S˜“Y !‘^ {Ñ2Ñ2‰ð /ð €Jr   c                ó*   — | t        |«      dz  |z  z   S )Nr   )r   )Útr#   r   s      r   Ú
_chunk_endr'   <   s   € Ø”�D“	˜Q‘ +Ñ-Ñ-Ð-r   c                ó°   ‡— |€yt        | ‰«      }t        |‰«      }t        ˆfd„|D «       d¬«      }t        ˆfd„|D «       d¬«      }t        ||«      |kD  S )z@Return whether natural-play WAV layout exceeds the duration cap.Fc              3  ó>   •K  — | ]  \  }}t        ||‰«      –— Œ y ­w©N©r'   ©Ú.0r&   Údr   s      €r   Ú	<genexpr>z3session_wav_exceeds_duration_cap.<locals>.<genexpr>K   ó   øè ø€ ÐC¹d±d°a¸”J˜q ! [×1¹dùó   ƒr   ©Údefaultc              3  ó>   •K  — | ]  \  }}t        ||‰«      –— Œ y ­wr*   r+   r,   s      €r   r/   z3session_wav_exceeds_duration_cap.<locals>.<genexpr>L   ó   øè ø€ ÐE¹u±t°q¸!”Z  1 k×2¹uùr1   )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   )r8   c               óJ  ‡‡— | s|syt        | ‰«      }t        |‰«      }t        ˆfd„|D «       d¬«      }t        ˆfd„|D «       d¬«      }t        t        j                  t        ||«      ‰z  «      «      Š|�-t        t        j                  |‰z  «      «      }t        ‰|«      Š‰dk  rydˆˆfd„}	 |	|«      }
 |	|«      }t        j                  d	t        d
‰z  «      «      }|
|ddd…<   ||ddd…<   t        j                  «       }t        j                  |d«      5 }|j                  d«       |j                  d«       |j                  ‰«       |j                  |j!                  «       «       ddd«       |j#                  «       S # 1 sw Y   |j#                  «       S xY w)aj  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  ó>   •K  — | ]  \  }}t        ||‰«      –— Œ y ­wr*   r+   r,   s      €r   r/   z+build_stereo_session_wav.<locals>.<genexpr>d   r0   r1   r   r2   c              3  ó>   •K  — | ]  \  }}t        ||‰«      –— Œ y ­wr*   r+   r,   s      €r   r/   z+build_stereo_session_wav.<locals>.<genexpr>e   r5   r1   Nr   c           
     ó  •— t        j                   dt        d‰z  «      «      }| D ]d  \  }}t        |‰z  «      }t        j                   d|d dt        |«      dz  z   «      }t	        t        |«      ‰|z
  «      }|dkD  sŒZ|d | ||||z    Œf |S )NÚhr   r   )ÚarrayÚbytesÚintr   Úmin)	r   Úchanr&   r#   ÚoffsetÚsamplesÚnr   Útotal_sampless	          €€r   Úmono_channelz.build_stereo_session_wav.<locals>.mono_channelm   s™   ø€ ô �{‰{˜3¤ a¨-Ñ&7Ó 8Ó9ˆÛ‰GˆAˆtÜ˜˜[™Ó)ˆFÜ—k‘k # tÐ,B¨a´3°t³9À±>Ñ.BÐ'CÓDˆGÜ”C˜“L -°&Ñ"8Ó9ˆAØ�1‹uØ,3°B°Q¨K��V˜f q™jÑ)ð ð ˆr   rB   é   r   é   r   )r   úlist[tuple[float, bytes]]Úreturnzarray.array)r$   r   rE   ÚmathÚceilrF   rC   rD   r   r	   r
   r   r   r   r   r   Útobytesr   )r6   r7   r   r8   r9   r:   r;   r<   Úmax_samplesrL   ÚleftÚrightÚstereoÚwav_ior   rK   s     `            @r   Úbuild_stereo_session_wavrY   P   sn  ù€ ñ ™|Øä& {°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Ø€Fˆ1ˆ4ˆaˆ4�LØ€Fˆ1ˆ4ˆaˆ4�Lä�Z‰Z‹\€FÜ	�‰�6˜4Ô	  BØ
�‰˜ÔØ
�‰˜ÔØ
�‰˜Ô$Ø
�‰�v—~‘~Ó'Ô(÷	 
!ð
 �?‰?ÓÐ÷ 
!ð
 �?‰?ÓÐús   ÄAF
Æ
F")rN   )r   rD   r   rE   r   rE   rP   rD   )r   rO   r   rE   rP   rO   )r&   Úfloatr#   rD   r   rE   rP   rZ   )
r6   rO   r7   rO   r   rE   r8   úfloat | NonerP   Úbool)
r6   rO   r7   rO   r   rE   r8   r[   rP   rD   )Ú__doc__Ú
__future__r   rC   r   rQ   r
   ÚDEFAULT_MAX_AUDIO_SECONDSr   r$   r'   r=   rY   © r   r   Ú<module>ra      s´   ðñõ #ã Û 	Û Û à#Ð ôð$Ø%ðØ47ðàóó,.ð;Ø*ð;à+ð;ð ð;ð 'ð	;ð
 
ó;ð* *Cñ9Ø*ð9à+ð9ð ð9ð
 'ð9ð ô9r   