
    qiO                     F    d Z ddlZddlZddlZddlmZ dZ G d de      Zy)a  SoX-based audio resampler stream implementation.

This module provides an audio resampler that uses the SoX ResampleStream library
for very high quality audio sample rate conversion.

When to use the SOXRStreamAudioResampler:
1. For real-time processing scenarios
2. When dealing with very long audio signals
3. When processing audio in chunks or streams
4. When you need to reuse the same resampler configuration multiple times, as it saves initialization overhead

    N)BaseAudioResamplerg?c                   T    e Zd ZdZd ZdedefdZd ZdedefdZ	de
deded	e
fd
Zy)SOXRStreamAudioResamplera  Audio resampler implementation using the SoX ResampleStream library.

    This resampler uses the SoX ResampleStream library configured for very high
    quality (VHQ) resampling, providing excellent audio quality at the cost
    of additional computational overhead.
    It keeps an internal history which avoids clicks at chunk boundaries.

    Notes:
        - Only supports mono audio (1 channel).
        - Input must be 16-bit signed PCM audio as raw bytes.
    c                 <    d| _         d| _        d| _        d| _        y)zxInitialize the resampler.

        Args:
            **kwargs: Additional keyword arguments (currently unused).
        Nr   )_in_rate	_out_rate_last_resample_time_soxr_stream)selfkwargss     `/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/audio/resamplers/soxr_stream_resampler.py__init__z!SOXRStreamAudioResampler.__init__+   s"     '+'+*+ 8<    in_rateout_ratec                     || _         || _        t        j                         | _        t	        j
                  ||ddd      | _        y )N   VHQint16)r   r   num_channelsqualitydtype)r   r   timer	   soxrResampleStreamr
   r   r   r   s      r   _initializez$SOXRStreamAudioResampler._initialize6   s=    !#'99;  //hQU\
r   c                     t        j                          }|| j                  z
  }|t        kD  r&| j                  r| j                  j	                          || _        y )N)r   r	   CLEAR_STREAM_AFTER_SECSr
   clear)r   current_timetime_since_last_resamples      r   _maybe_clear_internal_statez4SOXRStreamAudioResampler._maybe_clear_internal_state>   sJ    yy{#/$2J2J#J #&==  !!'')#/ r   c           
          | j                   | j                  ||       n| j                          | j                  |k7  s| j                  |k7  r+t        d| j                   d| j                   d| d|       y )NzPSOXRStreamAudioResampler cannot be reused with different sample rates: expected z->z, got )r
   r   r#   r   r   
ValueErrorr   s      r   _maybe_initialize_sox_streamz5SOXRStreamAudioResampler._maybe_initialize_sox_streamG   s    $Wh/,,.==G#t~~'A MM?"T^^,<F7)2hZY  (Br   audioreturnc                   K   ||k(  r|S | j                  ||       t        j                  |t        j                        }| j                  j                  |      }|j                  t        j                        j                         }|S w)a^  Resample audio data using soxr.ResampleStream resampler library.

        Args:
            audio: Input audio data as raw bytes (16-bit signed integers).
            in_rate: Original sample rate in Hz.
            out_rate: Target sample rate in Hz.

        Returns:
            Resampled audio data as raw bytes (16-bit signed integers).
        )r   )r&   np
frombufferr   r
   resample_chunkastypetobytes)r   r'   r   r   
audio_dataresampled_audioresults          r   resamplez!SOXRStreamAudioResampler.resampleS   sq      hL))'8<]]59
++:::F ''199;s   B	BN)__name__
__module____qualname____doc__r   floatr   r#   intr&   bytesr2    r   r   r   r      sX    
	=
5 
E 
0
C 
3 
E C 3 5 r   r   )	r6   r   numpyr*   r   -pipecat.audio.resamplers.base_audio_resamplerr   r   r   r:   r   r   <module>r=      s-       L G1 Gr   