
    qi                    X    d dl mZ d dlmZ ddlmZ ddgZd
dZ	 	 d	 	 	 	 	 	 	 	 	 ddZy	)    )annotations)AsyncIterator   )
AudioFramecombine_audio_framessine_wave_generatorc                B   t        | t              s| S | st        d      | d   j                  }| d   j                  }d}d}| D ]}  }|j                  |k7  rt        d| d|j                         |j                  |k7  rt        d| d|j                         |t        |j                        z  }||j                  z  } t        |      }d}| D ]<  }|j                  j                  d      }||||t        |      z    |t        |      z  }> t        ||||      S )a6  
    Combines one or more `rtc.AudioFrame` objects into a single `rtc.AudioFrame`.

    This function concatenates the audio data from multiple frames, ensuring that
    all frames have the same sample rate and number of channels. It efficiently
    merges the data by preallocating the necessary memory and copying the frame
    data without unnecessary reallocations.

    Args:
        buffer: A single `rtc.AudioFrame` or a list of `rtc.AudioFrame`
            objects to be combined.

    Returns:
        rtc.AudioFrame: A new `rtc.AudioFrame` containing the combined audio data.

    Raises:
        ValueError: If the buffer is empty.
        ValueError: If frames have differing sample rates.
        ValueError: If frames have differing numbers of channels.

    Example:
        >>> frame1 = rtc.AudioFrame(
        ...     data=b"", sample_rate=48000, num_channels=2, samples_per_channel=1
        ... )
        >>> frame2 = rtc.AudioFrame(
        ...     data=b"", sample_rate=48000, num_channels=2, samples_per_channel=1
        ... )
        >>> combined_frame = combine_audio_frames([frame1, frame2])
        >>> combined_frame.data
        b''
        >>> combined_frame.sample_rate
        48000
        >>> combined_frame.num_channels
        2
        >>> combined_frame.samples_per_channel
        2
    zbuffer is emptyr   zSample rate mismatch: expected z, got z!Channel count mismatch: expected b)datasample_ratenum_channelssamples_per_channel)
isinstancelist
ValueErrorr   r   lenr   r   	bytearraycastr   )	bufferr   r   total_data_lengthtotal_samples_per_channelframer   offset
frame_datas	            C/opt/pipecat/venv/lib/python3.12/site-packages/livekit/rtc/utils.pyr   r      sY   L fd#*++)''K!9))L ! ?+1+fUEVEVDWX  -3L>HZHZG[\  	S_,!U%>%>>!? &'DF "ZZ__S)
2<Vfs:./#j/!"
 !5	     c                 K   	 ddl }|dz  }t        ||z  |z        }|j                  |      |z  }t	        |      D ]i  }|||z  |z  z   }	||j                  d|j                  z  | z  |	z        z  }
|j                  |
dz        }t        |j                         |d|      }| k y# t        $ r t        d      w xY ww)a  
    Generate sine wave audio frames.

    Useful for testing audio pipelines and generating test signals.

    Args:
        freq: Frequency of the sine wave in Hz.
        duration: Duration of the audio in seconds.
        sample_rate: Sample rate in Hz (default: 48000).
        amplitude: Amplitude of the sine wave, range [0.0, 1.0] (default: 0.3).

    Yields:
        AudioFrame: Audio frames containing sine wave data.

    Example:
        >>> import asyncio
        >>> async def generate_audio():
        ...     async for frame in sine_wave_generator(440, 1.0):
        ...         print(f"Generated frame with {frame.samples_per_channel} samples")
        >>> asyncio.run(generate_audio())
    r   NzMnumpy is required for sine_wave_generator. Install it with: pip install numpy
      i  r   )
numpyImportErrorintarangerangesinpiint16r   tobytes)freqdurationr   	amplitudenp	blocksizetotal_framest_frameitsignalsignal_int16r   s                r   r   r   Z   s     6
 r!I;.9<=Lii	"[0G<  
a)mk11RVVAI$4q$899xx/  "	
 
  
[
 	

s   CB* B"C*B??CN)r   zAudioFrame | list[AudioFrame]returnr   )i  g333333?)
r)   floatr*   r5   r   r"   r+   r5   r4   zAsyncIterator[AudioFrame])	
__future__r   typingr   audio_framer   __all__r   r    r   r   <module>r;      s]    "   # "#8
9Ld 	0
00 0 	0
 0r   