
    q	h                        S SK Jr  S SKrS SKJr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      r    SS jrg)    )annotationsN)Enumunique   )audio_frame_pb2)	FfiClient	FfiHandle)ffi_pb2)get_address)
AudioFramec                  (    \ rS rSrSrSrSrSrSrSr	g)	AudioResamplerQuality   quicklowmediumhigh	very_high N)
__name__
__module____qualname____firstlineno__QUICKLOWMEDIUMHIGH	VERY_HIGH__static_attributes__r       M/var/www/html/env/lib/python3.13/site-packages/livekit/rtc/audio_resampler.pyr   r      s    E
CFDIr    r   c                  f    \ rS rSrSrS\R                  S.         S
S jjrSS jrSS jr	Sr
g	)AudioResampler   a  
A class for resampling audio data from one sample rate to another.

`AudioResampler` provides functionality to resample audio data from an input sample rate to an output
sample rate using the Sox resampling library. It supports multiple channels and configurable resampling quality.
r   )num_channelsqualityc                  Xl         X l        X0l        [        R                  " 5       nXR
                  l        X%R
                  l        X5R
                  l        [        U5      UR
                  l
        [        R                  R                  UR
                  l        [        R                  R                  UR
                  l        SUR
                  l        ["        R$                  R'                  U5      nUR
                  R(                  (       a  [+        UR
                  R(                  5      e[-        UR
                  R.                  R0                  R2                  5      U l        g)a  
Initialize an `AudioResampler` instance for resampling audio data.

Args:
    input_rate (int): The sample rate of the input audio data (in Hz).
    output_rate (int): The desired sample rate of the output audio data (in Hz).
    num_channels (int, optional): The number of audio channels (e.g., 1 for mono, 2 for stereo). Defaults to 1.
    quality (AudioResamplerQuality, optional): The quality setting for the resampler. Can be one of the
        `AudioResamplerQuality` enum values: `QUICK`, `LOW`, `MEDIUM`, `HIGH`, `VERY_HIGH`. Higher quality settings
        result in better audio quality but require more processing power. Defaults to `AudioResamplerQuality.MEDIUM`.

Raises:
    Exception: If there is an error creating the resampler.
r   N)_input_rate_output_rate_num_channels	proto_ffi
FfiRequestnew_sox_resampler
input_rateoutput_rater%   _to_proto_qualityquality_recipeproto_audio_frameSoxResamplerDataTypeSOXR_DATATYPE_INT16Iinput_data_typeoutput_data_typeflagsr   instancerequesterror	Exceptionr	   	resamplerhandleid_ffi_handle)selfr.   r/   r%   r&   reqresps          r!   __init__AudioResampler.__init__   s
   , &')""$+5(,7)-9*/@/I, 22GG 	- 22GG 	. '(#!!))#.!!''D228899$T%;%;%E%E%L%L%O%OPr    c           
        [        U[        5      (       a  UOUR                  R                  S5      n[        R
                  " 5       nU R                  R                  UR                  l	        [        [        U5      5      UR                  l        [        U5      UR                  l        [        R                   R#                  U5      nUR                  R$                  (       a  ['        UR                  R$                  5      eUR                  R(                  (       d  / $ [*        R,                  UR                  R                  -  R/                  UR                  R(                  5      n[        U5      n[1        UU R2                  U R4                  [        U5      U R4                  [*        R6                  " [*        R8                  5      -  -  5      /$ )a  
Push audio data into the resampler and retrieve any available resampled data.

This method accepts audio data, resamples it according to the configured input and output rates,
and returns any resampled data that is available after processing the input.

Args:
    data (bytearray | AudioFrame): The audio data to resample. This can be a `bytearray` containing
        raw audio bytes in int16le format or an `AudioFrame` object.

Returns:
    list[AudioFrame]: A list of `AudioFrame` objects containing the resampled audio data.
        The list may be empty if no output data is available yet.

Raises:
    Exception: If there is an error during resampling.
b)
isinstance	bytearraydatacastr+   r,   r?   r=   push_sox_resamplerresampler_handler   
memoryviewdata_ptrlensizer   r8   r9   r:   r;   
output_ptrctypesc_int8from_addressr   r)   r*   sizeofc_int16)r@   rI   bdatarA   rB   cdataoutput_datas          r!   pushAudioResampler.pushN   sR   $ #4339L""$262B2B2I2I/*5j6G*H'&)%j#!!))#.""((D3399::&&11I!8!8!=!==KK##..
  &!!""K T%7%7&--:W%WX	
 	
r    c           
     T   [         R                  " 5       nU R                  R                  UR                  l        [        R                  R                  U5      nUR                  R                  (       d  / $ [        R                  UR                  R                  -  R                  UR                  R                  5      n[        U5      n[        UU R                   U R"                  [%        U5      U R"                  [        R&                  " [        R(                  5      -  -  5      /$ )a  
Flush any remaining audio data through the resampler and retrieve the resampled data.

This method should be called when no more input data will be provided to ensure that all internal
buffers are processed and all resampled data is output.

Returns:
    list[AudioFrame]: A list of `AudioFrame` objects containing the remaining resampled audio data after flushing.
        The list may be empty if no output data remains.

Raises:
    Exception: If there is an error during flushing.
)r+   r,   r?   r=   flush_sox_resamplerrL   r   r8   r9   rQ   rR   rS   rP   rT   rH   r   r)   r*   rO   rU   rV   )r@   rA   rB   rX   rY   s        r!   flushAudioResampler.flush|   s     ""$373C3C3J3J0!!))#.''22I!9!9!>!>>LL$$//
  &!!""K T%7%7&--:W%WX	
 	
r    )r?   r(   r*   r)   N)
r.   intr/   r`   r%   r`   r&   r   returnNone)rI   zbytearray | AudioFramera   list[AudioFrame])ra   rc   )r   r   r   r   __doc__r   r   rC   rZ   r^   r   r   r    r!   r#   r#      s\     )>)E)E.Q.Q .Q
 .Q '.Q 
.Q`,
\!
r    r#   c                   U [         R                  :X  a  [        R                  R                  $ U [         R
                  :X  a  [        R                  R                  $ U [         R                  :X  a  [        R                  R                  $ U [         R                  :X  a  [        R                  R                  $ U [         R                  :X  a  [        R                  R                  $ g )N)r   r   r2   SoxQualityRecipeSOXR_QUALITY_QUICKr   SOXR_QUALITY_LOWr   SOXR_QUALITY_MEDIUMr   SOXR_QUALITY_HIGHr   SOXR_QUALITY_VERYHIGH)r&   s    r!   r0   r0      s     '--- 11DDD	)--	- 11BBB	)00	0 11EEE	)..	. 11CCC	)33	3 11GGG 
4r    )r&   r   ra   z,proto_audio_frame.SoxQualityRecipe.ValueType)
__future__r   rR   enumr   r   _protor   r2   _ffi_clientr   r	   r
   r+   _utilsr   audio_framer   strr   r#   r0   r   r    r!   <module>rs      s_    "   8 - (  # C  G
 G
TH"H1Hr    