
    i!                     :   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZ dedefd	Zd
edefdZdedefdZdedefdZe
fdedeeef   defdZdeeef   defdZe
fdededefdZe	fdededefdZ G d d      Zdeeeef      deeef   fdZy)zd
Audio Processing Utilities

Provides audio format conversion, resampling, and file I/O operations.
    N)Path)Union   )SAMPLE_RATE_INSAMPLE_RATE_OUTBYTES_PER_SAMPLEBUFFER_SIZE
ulaw_bytesreturnc                 6    t        j                  | t              S )u   
    Convert µ-law encoded audio to 16-bit PCM.

    Args:
        ulaw_bytes: µ-law encoded audio data (8kHz)

    Returns:
        16-bit PCM audio data (8kHz)
    )audioopulaw2linr   )r
   s    2/home/sas/my/fyp/ringai/ringai/audio/processing.pyulaw_to_pcmr      s     J(899    	pcm_bytesc                 6    t        j                  | t              S )u   
    Convert 16-bit PCM audio to µ-law encoding.

    Args:
        pcm_bytes: 16-bit PCM audio data

    Returns:
        µ-law encoded audio data
    )r   lin2ulawr   )r   s    r   pcm_to_ulawr   "   s     I'788r   pcm_8kc                 X    t        j                  | t        dt        t        d      \  }}|S )z
    Resample 8kHz PCM audio to 16kHz for Whisper.

    Args:
        pcm_8k: 8kHz 16-bit PCM audio data

    Returns:
        16kHz 16-bit PCM audio data
       N)r   ratecvr   r   r   )r   pcm_16k_s      r   resample_8k_to_16kr   /   s/     	JGQ Nr   r   c                 X    t        j                  | t        dt        t        d      \  }}|S )z
    Resample 16kHz PCM audio to 8kHz.

    Args:
        pcm_16k: 16kHz 16-bit PCM audio data

    Returns:
        8kHz 16-bit PCM audio data
    r   N)r   r   r   r   r   )r   r   r   s      r   resample_16k_to_8kr   D   s/     	IFA Mr   pcmpathsample_ratec                 L   t        |      }|j                  j                  dd       t        j                  t        |      d      5 }|j                  d       |j                  t               |j                  |       |j                  |        ddd       y# 1 sw Y   yxY w)z
    Save PCM audio to WAV file.

    Args:
        pcm: 16-bit PCM audio data
        path: Output file path
        sample_rate: Sample rate (default: 16kHz)
    T)parentsexist_okwbr   N)r   parentmkdirwaveopenstrsetnchannelssetsampwidthr   setframeratewriteframes)r   r    r!   wfs       r   save_wavr0   Y   s~     :DKKdT2	3t9d	# r

()
$
s	  s   A	BB#c                     t        j                  t        |       d      5 }|j                  |j	                               cddd       S # 1 sw Y   yxY w)zu
    Load PCM audio from WAV file.

    Args:
        path: Input file path

    Returns:
        PCM audio data
    rbN)r(   r)   r*   
readframes
getnframes)r    r/   s     r   load_wavr5   l   s?     
3t9d	# .r}}R]]_-. . .s   A		Ac                 ,    t        |       |t        z  z  S )z
    Calculate duration of PCM audio in seconds.

    Args:
        pcm: PCM audio data
        sample_rate: Sample rate

    Returns:
        Duration in seconds
    )lenr   r   r!   s     r   get_audio_durationr9   z   s     s8{%5566r   c                 D    t        t        |       |t        z  z  dz        S )z
    Calculate duration of PCM audio in milliseconds.

    Args:
        pcm: PCM audio data
        sample_rate: Sample rate

    Returns:
        Duration in milliseconds
    i  )intr7   r   r8   s     r   get_audio_duration_msr<      s#     CK*::;tCDDr   c                   |    e Zd ZdZdefdedefdZdefdZ	de
fd	Zdefd
Zd Zedefd       Zedefd       Zy)AudioBufferz
    Fixed-duration audio buffer for chunk saving.

    Accumulates audio until buffer reaches target duration,
    then allows flushing for saving to disk.
    g      ?duration_secr!   c                 b    || _         || _        t        |t        z  |z        | _        d| _        y)z
        Initialize audio buffer.

        Args:
            duration_sec: Target buffer duration in seconds
            sample_rate: Sample rate of input audio
        r   N)r?   r!   r;   r   target_size_buffer)selfr?   r!   s      r   __init__zAudioBuffer.__init__   s3     )&{-==LMr   r   c                 .    | xj                   |z  c_         y)zAdd PCM audio to bufferNrB   )rC   r   s     r   addzAudioBuffer.add   s    r   r   c                 F    t        | j                        | j                  k\  S )z'Check if buffer has reached target size)r7   rB   rA   rC   s    r   is_fullzAudioBuffer.is_full   s    4<< D$4$444r   c                 t    | j                   d| j                   }| j                   | j                  d | _         |S )zl
        Flush buffer contents.

        Returns:
            Buffered PCM data (up to target_size)
        N)rB   rA   )rC   datas     r   flushzAudioBuffer.flush   s9     ||.d../||D$4$4$67r   c                     d| _         y)zClear buffer contentsr   NrF   rI   s    r   clearzAudioBuffer.clear   s	    r   c                 ,    t        | j                        S )zCurrent buffer size in bytes)r7   rB   rI   s    r   sizezAudioBuffer.size   s     4<<  r   c                 T    t        | j                        | j                  t        z  z  S )z"Current buffer duration in seconds)r7   rB   r!   r   rI   s    r   durationzAudioBuffer.duration   s$     4<< D$4$47G$GHHr   N)__name__
__module____qualname____doc__r   floatr;   rD   bytesrG   boolrJ   rM   rO   propertyrQ   rS    r   r   r>   r>      s     .1^ U s u 5 5	u 	 !c ! ! I% I Ir   r>   input_pathsoutput_pathc                     d}| D ]M  }	 t        j                  t        |      d      5 }||j                  |j	                               z  }ddd       O |rt        ||       yy# 1 sw Y   xY w# t
        $ r Y ww xY w)z
    Merge multiple WAV files into a single file.

    Args:
        input_paths: List of input WAV file paths
        output_path: Output WAV file path
    r   r2   N)r(   r)   r*   r3   r4   	Exceptionr0   )r]   r^   all_pcmr    r/   s        r   merge_audio_filesrb      s     G 	3t9d+ :r2==99: +& : : 		s(   A1#A%A1%A.	*A11	A=<A=)rW   r   r(   ospathlibr   typingr   configr   r   r   r	   rY   r   r   r   r   r*   r;   r0   r5   rX   r9   r<   r>   listrb   r\   r   r   <module>rh      s3     	   
:E 
:e 
:
95 
9U 
9u  * % * ET % uS$Y/ c &.5d# . . 7F 7E 7 7% 7 :H Eu E3 EC E4I 4In'4c4i(8#9 'cSWiHX 'r   