
    q	h>-                        S SK Jr  S SKrS SKrS SKJr  S SKJrJrJ	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
KJrJr  SSKJr  SSKJr  SSKJr  \ " S S5      5       r\ " S S5      5       r " S S5      rg)    )annotationsN)	dataclass)AnyAsyncIteratorOptional   )	FfiClient	FfiHandle)audio_frame_pb2)ffi_pb2)TrackSource)	RingQueuetask_done_logger)
AudioFrame)Participant)Trackc                  $    \ rS rSr% SrS\S'   Srg)AudioFrameEvent    zmAn event representing a received audio frame.

Attributes:
    frame (AudioFrame): The received audio frame.
r   frame N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r       J/var/www/html/env/lib/python3.13/site-packages/livekit/rtc/audio_stream.pyr   r       s     r   r   c                  *    \ rS rSr% S\S'   S\S'   Srg)NoiseCancellationOptions+   str	module_idzdict[str, Any]optionsr   N)r   r   r   r   r   r   r   r   r    r"   r"   +   s    Nr   r"   c                  "   \ rS rSrSr     S             SS jjr\SSSSSS.               SS	 jj5       r\SSSSSS.             SS
 jj5       rSS jr	SS jr
      SS jrS rSS jrSS jrSS jrSS jrSrg)AudioStream1   a?  An asynchronous audio stream for receiving audio frames from a participant or track.

The `AudioStream` class provides an asynchronous iterator over audio frames received from
a specific track or participant. It allows you to receive audio frames in real-time with
customizable sample rates and channel configurations.
Nr   逻  r   c                   Xl         X@l        XPl        U=(       d    [        R                  " 5       U l        [        R                  R                  R                  U R
                  5      U l
        [        U5      U l        SU l        SU l        Ub"  UR                  U l        UR                   U l        U R
                  R#                  U R%                  5       5      U l        U R&                  R)                  [*        5        SnSU;   a  U R-                  US   US   S9nOU R/                  5       n[1        UR2                  R4                  5      U l        UR8                  U l        g)aE  Initialize an `AudioStream` instance.

Args:
    track (Optional[Track]): The audio track from which to receive audio. If not provided,
        you must specify `participant` and `track_source` in `kwargs`.
    loop (Optional[asyncio.AbstractEventLoop], optional): The event loop to use.
        Defaults to the current event loop.
    capacity (int, optional): The capacity of the internal frame queue. Defaults to 0 (unbounded).
    sample_rate (int, optional): The sample rate for the audio stream in Hz.
        Defaults to 48000.
    num_channels (int, optional): The number of audio channels. Defaults to 1.
    noise_cancellation (Optional[NoiseCancellationOptions], optional):
        If noise cancellation is used, pass a `NoiseCancellationOptions` instance
        created by the noise cancellation module.

Example:
    ```python
    audio_stream = AudioStream(
        track=audio_track,
        sample_rate=44100,
        num_channels=2,
    )

    audio_stream = AudioStream.from_track(
        track=audio_track,
        sample_rate=44100,
        num_channels=2,
    )
    ```
Nparticipanttrack_source)r,   r-   )_track_sample_rate_num_channelsasyncioget_event_loop_loopr	   instancequeue	subscribe
_ffi_queuer   _queue_audio_filter_module_audio_filter_optionsr%   r&   create_task_run_taskadd_done_callbackr   %_create_owned_stream_from_participant_create_owned_streamr
   handleid_ffi_handleinfo_info)	selftrackloopcapacitysample_ratenum_channelsnoise_cancellationkwargsstreams	            r    __init__AudioStream.__init__9   s   P %*')5W335
#,,22<<TZZH9B89L$(!%)")(:(D(DD%);)C)CD&ZZ++DIIK8


$$%56F"??"=1~@V @ F ..0F$V]]%5%56[[
r   )rH   rI   rJ   rK   rL   c               "    [        UUUUSUUUS9$ )a  Create an `AudioStream` from a participant's audio track.

Args:
    participant (Participant): The participant from whom to receive audio.
    track_source (TrackSource.ValueType): The source of the audio track (e.g., microphone, screen share).
    loop (Optional[asyncio.AbstractEventLoop], optional): The event loop to use. Defaults to the current event loop.
    capacity (int, optional): The capacity of the internal frame queue. Defaults to 0 (unbounded).
    sample_rate (int, optional): The sample rate for the audio stream in Hz. Defaults to 48000.
    num_channels (int, optional): The number of audio channels. Defaults to 1.
    noise_cancellation (Optional[NoiseCancellationOptions], optional):
        If noise cancellation is used, pass a `NoiseCancellationOptions` instance
        created by the noise cancellation module.

Returns:
    AudioStream: An instance of `AudioStream` that can be used to receive audio frames.

Example:
    ```python
    audio_stream = AudioStream.from_participant(
        participant=participant,
        track_source=TrackSource.MICROPHONE,
        sample_rate=24000,
        num_channels=1,
    )
    ```
N)r,   r-   rH   rI   rG   rJ   rK   rL   r(   )clsr,   r-   rH   rI   rJ   rK   rL   s           r    from_participantAudioStream.from_participantz   s*    L #%#%1	
 		
r   c          	         [        UUUUUUS9$ )a  Create an `AudioStream` from an existing audio track.

Args:
    track (Track): The audio track from which to receive audio.
    loop (Optional[asyncio.AbstractEventLoop], optional): The event loop to use. Defaults to the current event loop.
    capacity (int, optional): The capacity of the internal frame queue. Defaults to 0 (unbounded).
    sample_rate (int, optional): The sample rate for the audio stream in Hz. Defaults to 48000.
    num_channels (int, optional): The number of audio channels. Defaults to 1.
    noise_cancellation (Optional[NoiseCancellationOptions], optional):
        If noise cancellation is used, pass a `NoiseCancellationOptions` instance
        created by the noise cancellation module.

Returns:
    AudioStream: An instance of `AudioStream` that can be used to receive audio frames.

Example:
    ```python
    audio_stream = AudioStream.from_track(
        track=audio_track,
        sample_rate=44100,
        num_channels=2,
    )
    ```
)rG   rH   rI   rJ   rK   rL   rR   )rS   rG   rH   rI   rJ   rK   rL   s          r    
from_trackAudioStream.from_track   s$    F #%1
 	
r   c                j    [         R                  R                  R                  U R                  5        g N)r	   r4   r5   unsubscriber7   rF   s    r    __del__AudioStream.__del__   s       ,,T__=r   c                8   U R                   c   e[        R                  " 5       nUR                  nU R                   R                  R
                  Ul        U R                  Ul        U R                  Ul
        [        R                  R                  Ul        U R                  b  U R                  Ul        U R"                  b%  [$        R&                  " U R"                  5      Ul        [*        R,                  R/                  U5      nUR                  R0                  $ rZ   )r.   	proto_ffi
FfiRequestnew_audio_streamrC   rA   track_handler/   rJ   r0   rK   proto_audio_frameAudioStreamTypeAUDIO_STREAM_NATIVEtyper9   audio_filter_module_idr:   jsondumpsaudio_filter_optionsr	   r4   requestrN   )rF   reqrb   resps       r    r@    AudioStream._create_owned_stream   s    {{&&&""$//(,(?(?(F(F%'+'8'8$(,(:(:% 1 A A U U$$06:6O6O3%%148JJt?Y?Y4Z1!!))#.$$+++r   c                   [         R                  " 5       nUR                  nUR                  R                  Ul        U R                  Ul        U R                  Ul	        [        R                  R                  Ul        X$l        U R                  b  U R                  Ul        U R"                  b%  [$        R&                  " U R"                  5      Ul        [*        R,                  R/                  U5      nUR                  R0                  $ rZ   )r`   ra   audio_stream_from_participantrC   rA   participant_handler/   rJ   r0   rK   rd   re   rf   rg   r-   r9   rh   r:   ri   rj   rk   r	   r4   rl   rN   )rF   r,   r-   rm   rq   rn   s         r    r?   1AudioStream._create_owned_stream_from_participant   s     ""$(+(I(I%;F;R;R;Y;Y%8484E4E%1595G5G%2->-N-N-b-b%*5A2$$0CGC\C\)@%%1AE**B)> !!))#.11888r   c                $  #     U R                   R                  U R                  5      I S h  vN nUR                  nUR	                  S5      (       aS  UR
                  R                  n[        R                  " U5      n[        U5      nU R                  R                  U5        O2UR	                  S5      (       a  U R                  R                  S 5        OM  [        R                  R                  R                  U R                   5        g  N7f)Nframe_receivedeos)r7   wait_for	_is_eventaudio_stream_eventHasFieldru   r   r   _from_owned_infor   r8   putr	   r4   r5   r[   )rF   eventaudio_eventowned_buffer_infor   s        r    r<   AudioStream._run   s     //224>>BBE>C>V>VK##$455$/$>$>$D$D!"334EF'.&%%e,,%  	  ,,T__= Cs   *DDC"Dc                l   #    U R                   R                  5         U R                  I Sh  vN   g N7f)zAsynchronously close the audio stream.

This method cleans up resources associated with the audio stream and waits for
any pending operations to complete.
N)rC   disposer=   r\   s    r    acloseAudioStream.aclose  s&      	  "jjs   *424c                \    UR                   R                  U R                  R                  :H  $ rZ   )ry   stream_handlerC   rA   )rF   es     r    rx   AudioStream._is_event  s%    ##11T5E5E5L5LLLr   c                    U $ rZ   r   r\   s    r    	__aiter__AudioStream.__aiter__  s    r   c                   #    U R                   R                  5       (       a  [        eU R                  R	                  5       I S h  vN nUc  [        eU$  N7frZ   )r=   doneStopAsyncIterationr8   get)rF   items     r    	__anext__AudioStream.__anext__  sD     ::??$$[[__&&<$$	 's   AAAA)r9   r:   rC   r7   rE   r3   r0   r8   r/   r=   r.   )Nr   r*   r   N)rG   r   rH   #Optional[asyncio.AbstractEventLoop]rI   intrJ   r   rK   r   rL   "Optional[NoiseCancellationOptions]returnNone)r,   r   r-   TrackSource.ValueTyperH   r   rI   r   rJ   r   rK   r   rL   r   r   r(   )rG   r   rH   r   rI   r   rJ   r   rK   r   rL   r   r   r(   )r   r   )r   r   )r,   r   r-   r   r   r   )r   zproto_ffi.FfiEventr   bool)r   zAsyncIterator[AudioFrameEvent])r   r   )r   r   r   r   r   rO   classmethodrT   rW   r]   r@   r?   r<   r   rx   r   r   r   r   r   r    r(   r(   1   s    59 AE?!?! 2?! 	?!
 ?! ?! ??! 
?!B  59 AE.
 !.
 ,	.

 2.
 .
 .
 .
 ?.
 
.
 .
` 
 59 AE)
 )
 2	)

 )
 )
 )
 ?)
 
)
 )
V>,9&96K9	9&> Mr   r(   ) 
__future__r   r1   ri   dataclassesr   typingr   r   r   _ffi_clientr	   r
   _protor   rd   r   r`   _proto.track_pb2r   _utilsr   r   audio_framer   r,   r   rG   r   r   r"   r(   r   r   r    <module>r      so    #   ! / / - 8 ( ) / # $       
r rr   