from typing import List

import numpy as np


def stack_audio_segments(
    audio_segments: List[np.ndarray], sample_rate: int, silence_pad: float = 1.0
) -> np.ndarray:
    """
    Stack / concatenate all the individual audio segments (speaker audios) sequentially to form the complete podcast.
    Additionally, at the end of each speaker's audio, add a small silence audio as buffer between speakers for a more
    natural sounding podcast. You can turn off this feature by setting silence_pad = 0.0
    Args:
        audio_segments: A list of each speaker's audio in order.
        sample_rate: The sample rate of the waveform generated by the model.
        silence_pad: The maximum length of silence to pad at the end of each audio, sampling between 0.0 and this number.

    Returns: The complete podcast as a single, concatenated waveform.

    """
    stacked = []
    rng = np.random.default_rng(42)
    for segment in audio_segments:
        stacked.append(segment)
        if silence_pad > 0.0:
            stacked.append(
                np.zeros(int(rng.uniform(low=0.0, high=silence_pad) * sample_rate))
            )
    return np.concatenate(stacked)
