
    q	hw                      T    S SK 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5      rg)	    N   )	FfiHandle)audio_frame_pb2)get_address)AnyUnionc            
       d   \ rS rSrSrS\\\\4   S\	S\	S\	SS4
S	 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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.
datasample_ratenum_channelssamples_per_channelreturnNc                 
   [        U5      R                  S5      n[        U5      X4-  [        R                  " [        R
                  5      -  :  a  [        S5      e[        U5      [        R                  " [        R
                  5      -  S:w  a  [        S5      e[        U5      [        R                  " [        R
                  5      -  n[        R
                  U-  R                  U5      U l        X l	        X0l
        X@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)
memoryviewcastlenctypessizeofc_int16
ValueErrorfrom_buffer_copy_data_sample_rate_num_channels_samples_per_channel)selfr   r   r   r   ns         I/var/www/html/env/lib/python3.13/site-packages/livekit/rtc/audio_frame.py__init__AudioFrame.__init__   s    ( $$$S)t9|9FMM&..<YYY[  t9v}}V^^449NOOIv~~66nnq(::4@
')$7!    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.createD   s4     1FMM&..4QQ$\OOr$   
owned_infoc                 N   U R                   nUR                  UR                  -  n[        R                  U-  R                  UR                  5      n[        U5      n[        U R                  R                  5        [        XAR                  UR                  UR                  5      $ N)infor   r   r   r   from_addressdata_ptrr&   r   handleidr
   r   )r*   r-   r'   cdatar   s        r!   _from_owned_infoAudioFrame._from_owned_infoV   s}      4#;#;;$&44T]]C*##&&'$ 0 0$2C2CTE]E]^^r$   c                     [         R                  " 5       n[        [        U R                  5      5      Ul        U R                  Ul        U R                  Ul        U R                  Ul        U$ r,   )	proto_audioAudioFrameBufferInfor   r   r   r/   r   r   r   )r   
audio_infos     r!   _proto_infoAudioFrame._proto_info_   sV     557
)*TZZ*@A
!%!1!1
"&"3"3
)-)A)A
&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   r   s    r!   r   AudioFrame.datag   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   r=   s    r!   r   AudioFrame.sample_rateq   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   r=   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   r=   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   r=   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   rK   rL   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   rF   r=   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)
isinstancer
   tupledict	b64decode	TypeError)r_   base64s    r!   validate_audio_frameEAudioFrame.__get_pydantic_core_schema__.<locals>.validate_audio_frame   s~    %,,%''a%&&!))%-8 %m 4!&~!6(-.C(D	  9::r$   ra   c                    > TR                  U R                  5      R                  S5      U R                  U R                  U R
                  S.$ )Nzutf-8ra   )	b64encoder   decoder   r   r   )instancerg   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_corer^   rg   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)clsr\   r^   rh   rg   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   ) __name__
__module____qualname____firstlineno____doc__r   bytesr&   r   intr"   staticmethodr(   r6   OwnedAudioFrameBufferr3   r7   r9   propertyr   r   r   r   floatrF   rV   strrZ   classmethodr   r}   __static_attributes__ r$   r!   r
   r
      sx   $8E9j01$8 $8 	$8
 !$8 
$8L PC Ps P PQ] P P" _[%F%F _< _ _[==  :j : : !S ! ! "c " " )S ) ) ;% ; ;'e '&
# 
 .
c .
 .
r$   r
   )r   _ffi_clientr   _protor   r6   _utilsr   typingr   r   r
   r   r$   r!   <module>r      s"     " 2  M
 M
r$   