
    /j!                     X    S SK r SSKJr  SSKJr  SSKJrJr  S SK	J
r
Jr   " S S5      rg)	    N   )	FfiHandle)audio_frame_pb2)_ensure_compatible_bufferget_address)AnyUnionc                      \ rS rSrSrSS.S\\\\4   S\	S\	S\	S	\\
\\4   S4   S
S4S jjr\S\	S\	S\	S
S 4S j5       r\S\R"                  S
S 4S j5       rS
\R&                  4S jr\S
\
\\4   4S j5       r\S
\4S j5       r\S
\	4S j5       r\S
\	4S j5       r\S
\	4S j5       r\S
\4S j5       rS
\4S jrS
\4S jr\S\4S j5       r Sr!g)
AudioFrame   z
A class that represents a frame of audio data with specific properties such as sample rate,
number of channels, and samples per channel.

The format of the audio data is 16-bit signed integers (int16) interleaved by channel.
N)userdatadatasample_ratenum_channelssamples_per_channelr   returnc                   [        U[        5      (       a  UR                  S5      n[        U5      nX4-  [        R
                  " [        R                  5      -  n[        U5      nXv:  a  [        S5      eU[        R
                  " [        R                  5      -  S:w  a  [        S5      eXl	        X l
        X0l        X@l        Uc  0 U l        gUU l        g)a  
Initialize an AudioFrame instance.

Args:
    data (Union[bytes, bytearray, memoryview]): The raw audio data, which must be at least
        `num_channels * samples_per_channel * sizeof(int16)` bytes long.
    sample_rate (int): The sample rate of the audio in Hz.
    num_channels (int): The number of audio channels (e.g., 1 for mono, 2 for stereo).
    samples_per_channel (int): The number of samples per channel.

Raises:
    ValueError: If the length of `data` is smaller than the required size.
BzIdata length must be >= num_channels * samples_per_channel * sizeof(int16)r   z/data length must be a multiple of sizeof(int16)N)
isinstance
memoryviewcastr   ctypessizeofc_int16len
ValueError_data_sample_rate_num_channels_samples_per_channel	_userdata)selfr   r   r   r   r   min_sizedata_lens           H/app/agent/.venv/lib/python3.13/site-packages/livekit/rtc/audio_frame.py__init__AudioFrame.__init__   s    , dJ''99S>D(.5fnn8UUt9[  fmmFNN33q8NOO
')$7!'/X    c                     X-  [         R                  " [         R                  5      -  n[        U5      n[	        X@X5      $ )a  
Create a new empty AudioFrame instance with specified sample rate, number of channels,
and samples per channel.

Args:
    sample_rate (int): The sample rate of the audio in Hz.
    num_channels (int): The number of audio channels (e.g., 1 for mono, 2 for stereo).
    samples_per_channel (int): The number of samples per channel.

Returns:
    AudioFrame: A new AudioFrame instance with uninitialized (zeroed) data.
)r   r   r   	bytearrayr   )r   r   r   sizer   s        r%   createAudioFrame.createL   s4     1FMM&..4QQ$\OOr(   
owned_infoc                 j   U R                   nUR                  UR                  -  n[        R                  U-  R                  UR                  5      n[        U5      n[        U R                  R                  5      R                  5         [        XAR                  UR                  UR                  5      $ N)infor   r   r   r   from_addressdata_ptrr*   r   handleiddisposer   r   )r.   r1   r+   cdatar   s        r%   _from_owned_infoAudioFrame._from_owned_info^   s      4#;#;;$&44T]]C*##&&'//1$ 0 0$2C2CTE]E]^^r(   c                     [         R                  " 5       n[        U R                  5      Ul        U R
                  Ul        U R                  Ul        U R                  Ul        U$ r0   )proto_audioAudioFrameBufferInfor   r   r3   r   r   r   )r"   
audio_infos     r%   _proto_infoAudioFrame._proto_infog   sQ     557
)$**5
!%!1!1
"&"3"3
)-)A)A
&r(   c                     U R                   $ )z8
Returns the user data associated with the audio frame.
)r!   r"   s    r%   r   AudioFrame.userdatao   s    
 ~~r(   c                 h    [        U R                  5      R                  S5      R                  S5      $ )z
Returns a memory view of the audio data as 16-bit signed integers.

Returns:
    memoryview: A memory view of the audio data.
r   h)r   r   r   rA   s    r%   r   AudioFrame.datav   s)     $**%**3/44S99r(   c                     U R                   $ )zW
Returns the sample rate of the audio frame.

Returns:
    int: The sample rate in Hz.
)r   rA   s    r%   r   AudioFrame.sample_rate   s        r(   c                     U R                   $ )z
Returns the number of channels in the audio frame.

Returns:
    int: The number of audio channels (e.g., 1 for mono, 2 for stereo).
)r   rA   s    r%   r   AudioFrame.num_channels   s     !!!r(   c                     U R                   $ )zb
Returns the number of samples per channel.

Returns:
    int: The number of samples per channel.
)r    rA   s    r%   r   AudioFrame.samples_per_channel   s     (((r(   c                 4    U R                   U R                  -  $ )zc
Returns the duration of the audio frame in seconds.

Returns:
    float: The duration in seconds.
)r   r   rA   s    r%   durationAudioFrame.duration   s     ''$*:*:::r(   c                    SSK nSSKnUR                  5        nUR                  US5       nUR	                  U R
                  5        UR                  S5        UR                  U R                  5        UR                  U R                  5        SSS5        UR                  5       sSSS5        $ ! , (       d  f       N'= f! , (       d  f       g= f)zy
Convert the audio frame data to a WAV-formatted byte stream.

Returns:
    bytes: The audio data encoded in WAV format.
r   Nwb   )waveioBytesIOopensetnchannelsr   setsampwidthsetframerater   writeframesr   getvalue)r"   rR   rS   wav_filewavs        r%   to_wav_bytesAudioFrame.to_wav_bytes   s     	ZZ\X8T*c  !2!23  #  !1!12

+	 + $$& \** \s#   CA#B0C0
B>	:C
Cc           	      r    SU R                    SU R                   SU R                   SU R                  S S3	$ )Nzrtc.AudioFrame(sample_rate=z, num_channels=z, samples_per_channel=z, duration=z.3f))r   r   r   rM   rA   s    r%   __repr__AudioFrame.__repr__   sO    )$*:*:); < --. /##'#;#;"< =c*!-	
r(   _c                   ^ SSK Jn  SS KmS[        SS4U4S jjnUR	                  UR                  UR                  UR                  UR                  5       5      UR                  UR                  5       5      UR                  UR                  5       5      UR                  UR                  5       5      S.5      UR                  U5      /5      UR                  U5      UR                  U4S j5      S	9$ )
Nr   )core_schemavaluer   r   c                    > [        U [        5      (       a  U $ [        U [        5      (       a  U S   n [        U [        5      (       a'  [        TR	                  U S   5      U S   U S   U S   S9$ [        S5      e)Nr   r   r   r   r   r   r   r   r   zInvalid type for AudioFrame)r   r   tupledict	b64decode	TypeError)rf   base64s    r%   validate_audio_frameEAudioFrame.__get_pydantic_core_schema__.<locals>.validate_audio_frame   s~    %,,%''a%&&!))%-8 %m 4!&~!6(-.C(D	  9::r(   rh   c                    > TR                  U R                  5      R                  S5      U R                  U R                  U R
                  S.$ )Nzutf-8rh   )	b64encoder   decoder   r   r   )instancerm   s    r%   <lambda>9AudioFrame.__get_pydantic_core_schema__.<locals>.<lambda>   s@    ",,X]];BB7K#+#7#7$,$9$9+3+G+G	"r(   )json_schemapython_schemaserialization)pydantic_corere   rm   r   json_or_python_schemachain_schemamodel_fields_schemamodel_field
str_schema
int_schema no_info_plain_validator_function$plain_serializer_function_ser_schema)clsrc   re   rn   rm   s       @r%   __get_pydantic_core_schema__'AudioFrame.__get_pydantic_core_schema__   s    -	; 	; 	;" 00#0033$/$;$;K<R<R<T$U+6+B+B;CYCYC[+\,7,C,CKDZDZD\,]3>3J3J + 6 6 84		  @@AUV &FFG[\%JJ# 1 
 	
r(   )r   r   r   r    r!   )"__name__
__module____qualname____firstlineno____doc__r	   bytesr*   r   intrj   strr   r&   staticmethodr,   r;   OwnedAudioFrameBufferr8   r<   r>   propertyr   r   r   r   r   floatrM   r]   ra   classmethodr   __static_attributes__ r(   r%   r   r      s    15,>E9j01,> ,> 	,>
 !,> S#X,-,> 
,>\ PC Ps P PQ] P P" _[%F%F _< _ _[==  $sCx.   :j : : !S ! ! "c " " )S ) ) ;% ; ;'e '&
# 
 .
c .
 .
r(   r   )r   _ffi_clientr   _protor   r;   _utilsr   r   typingr   r	   r   r   r(   r%   <module>r      s"     " 2 : \
 \
r(   