
    q	hU                    h    S SK Jr  S SKrS SKrSSKJrJr  SSKJr	  SSKJ
r  SSKJr   " S S	5      rg)
    )annotationsN   )	FfiHandle	FfiClient)audio_frame_pb2)ffi_pb2)
AudioFramec                      \ rS rSrSr  S         SS jjr\SS j5       r\SS j5       r\SS j5       r	SS jr
SS	 jrSS
 jrSS jrSS jrSrg)AudioSource   al  
Represents a real-time audio source with an internal audio queue.

The `AudioSource` class allows you to push audio frames into a real-time audio
source, managing an internal queue of audio data up to a maximum duration defined
by `queue_size_ms`. It supports asynchronous operations to capture audio frames
and to wait for the playback of all queued audio data.
Nc                F   Xl         X l        U=(       d    [        R                  " 5       U l        [
        R                  " 5       n[        R                  R                  UR                  l        XR                  l        X%R                  l        X5R                  l        [        R                   R#                  U5      nUR                  R$                  U l        [)        U R&                  R*                  R,                  5      U l        SU l        SU l        SU l        SU l        g)a  
Initializes a new instance of the audio source.

Args:
    sample_rate (int): The sample rate of the audio source in Hz.
    num_channels (int): The number of audio channels.
    queue_size_ms (int, optional): The buffer size of the audio queue in milliseconds.
        Defaults to 1000 ms.
    loop (asyncio.AbstractEventLoop, optional): The event loop to use. Defaults to
        `asyncio.get_event_loop()`.
        N)_sample_rate_num_channelsasyncioget_event_loop_loop	proto_ffi
FfiRequestproto_audio_frameAudioSourceTypeAUDIO_SOURCE_NATIVEnew_audio_sourcetypesample_ratenum_channelsqueue_size_msr   instancerequestsource_infor   handleid_ffi_handle_last_capture_q_size_join_handle	_join_fut)selfr   r   r   loopreqresps          J/var/www/html/env/lib/python3.13/site-packages/livekit/rtc/audio_source.py__init__AudioSource.__init__$   s    $ ()5W335
""$$5$E$E$Y$Y!+6(,8)-:*!!))#.**11
$TZZ%6%6%9%9: 8<6:    c                    U R                   $ )z*The sample rate of the audio source in Hz.)r   r)   s    r-   r   AudioSource.sample_rateI   s        r0   c                    U R                   $ )zThe number of audio channels.)r   r2   s    r-   r   AudioSource.num_channelsN   s     !!!r0   c                t    [        U R                  [        R                  " 5       -
  U R                  -   S5      $ )zDThe current duration (in seconds) of audio data queued for playback.r   )maxr&   time	monotonicr%   r2   s    r-   queued_durationAudioSource.queued_durationS   s,     4<<$.."22T5G5GGMMr0   c                    [         R                  " 5       nU R                  R                  UR                  l        [        R                  R                  U5      nU R                  5         g)a  
Clears the internal audio queue, discarding all buffered audio data.

This method immediately removes all audio data currently queued for playback,
effectively resetting the audio source's buffer. Any audio frames that have been
captured but not yet played will be discarded. This is useful in scenarios where
you need to stop playback abruptly or prevent outdated audio data from being played.
N)
r   r   r$   r"   clear_audio_buffersource_handler   r   r   _release_waiter)r)   r+   _s      r-   clear_queueAudioSource.clear_queueX   sN     ""$/3/?/?/F/F,&&s+r0   c                .  ^#    UR                   S:X  d  U R                  R                  (       a  g[        R                  " 5       nU R
                  S:X  a  SOX R
                  -
  nU =R                  UR                   U R                  -  U-
  -  sl        X l        U R                  (       a  U R                  R                  5         U R                  c  U R                  R                  5       U l
        U R                  R                  U R                  U R                  5      U l        [        R                   " 5       nU R                  R"                  UR$                  l        UR$                  R(                  R+                  UR-                  5       5        [.        R0                  R2                  R5                  U R                  S9n [.        R0                  R7                  U5      mUR9                  U4S j5      I Sh  vN n[.        R0                  R2                  R;                  U5        UR$                  R<                  (       a  [?        UR$                  R<                  5      eg Nh! [.        R0                  R2                  R;                  U5        f = f7f)a  
Captures an `AudioFrame` and queues it for playback.

This method is used to push new audio data into the audio source. The audio data
will be processed and queued. If the size of the audio frame exceeds the internal
queue size, the method will wait until there is enough space in the queue to
accommodate the frame. The method returns only when all of the data in the buffer
has been pushed.

Args:
    frame (AudioFrame): The audio frame to capture and queue.

Raises:
    Exception: If there is an error during frame capture.
r   Nr   )r*   c                ^   > U R                   R                  TR                   R                  :H  $ )N)capture_audio_frameasync_id)er,   s    r-   <lambda>+AudioSource.capture_frame.<locals>.<lambda>   s"    !//88D<T<T<]<]]r0   ) samples_per_channelr$   disposedr8   r9   r%   r&   r   r'   cancelr(   r   create_future
call_laterr?   r   r   r"   rE   r>   bufferCopyFrom_proto_infor   r   queue	subscriber   wait_forunsubscribeerror	Exception)r)   framenowelapsedr+   rR   cbr,   s          @r-   capture_frameAudioSource.capture_framef   s    " $$)T-=-=-F-Fnn++s2#>P>P8P11D4D4DDwNN $$&>>!!ZZ557DN JJ11$,,@T@TU""$040@0@0G0G-&&//0A0A0CD""((22

2C	8%%--c2D+0>>], &B $$007!!''B228899 (& $$007s1   GJ7I' <I%=I' A$J%I' '+JJc                |   #    U R                   c  g[        R                  " U R                   5      I Sh  vN   g N7f)z
Waits for the audio source to finish playing out all audio data.

This method ensures that all queued audio data has been played out before returning.
It can be used to synchronize events after audio playback or to ensure that the
audio queue is empty.
N)r(   r   shieldr2   s    r-   wait_for_playoutAudioSource.wait_for_playout   s*      >>!nnT^^,,,s   2<:<c                    U R                   c  g U R                   R                  5       (       d  U R                   R                  S 5        SU l        SU l        S U l         g )Nr   )r(   done
set_resultr%   r&   r2   s    r-   r?   AudioSource._release_waiter   sJ    >>!~~""$$NN%%d+ r0   c                @   #    U R                   R                  5         g7f)zZClose the audio source

This method cleans up resources associated with the audio source.
N)r$   disposer2   s    r-   acloseAudioSource.aclose   s     
 	  "s   )	r$   r!   r(   r'   r%   r   r   r&   r   )i  N)
r   intr   rj   r   rj   r*   z asyncio.AbstractEventLoop | NonereturnNone)rk   rj   )rk   float)rk   rl   )rX   r	   rk   rl   )__name__
__module____qualname____firstlineno____doc__r.   propertyr   r   r:   rA   r\   r`   r?   rh   __static_attributes__ r0   r-   r   r      s     "15#;#; #; 	#;
 /#; 
#;J ! ! " " N N/:b-	#r0   r   )
__future__r   r8   r   _ffi_clientr   r   _protor   r   r   r   audio_framer	   r   ru   r0   r-   <module>rz      s(    #   - 8 ( #[# [#r0   