def mask2timing(
        silence_mask: (np.ndarray, torch.Tensor),
        time_offset: float = 0.0,
        second_per_unit: Optional[float] = None,
        min_start: Optional[float] = None,
        max_end: Optional[float] = None
) -> (Tuple[np.ndarray, np.ndarray], None):
    """
    Return array of start timestamps and array of end timestamps corresponding to where ``silence_mask`` is ``True``.
    """
    if silence_mask is None or not silence_mask.any() or not len(silence_mask):
        return
    assert silence_mask.ndim == 1
    if isinstance(silence_mask, torch.Tensor):
        silence_mask = silence_mask.cpu().numpy().copy()
    elif not isinstance(silence_mask, np.ndarray):
        raise NotImplementedError(f'Expected torch.Tensor or numpy.ndarray, but got {type(silence_mask)}')

    mask = np.concatenate(([False], silence_mask, [False]))
    silent_starts = np.logical_and(~mask[:-2], mask[1:-1]).nonzero()[0]
    silent_ends = (np.logical_and(mask[1:-1], ~mask[2:]).nonzero()[0] + 1)
    clipped = False
    if second_per_unit is None:
        silent_starts = silent_starts / TOKENS_PER_SECOND
        silent_ends = silent_ends / TOKENS_PER_SECOND
    else:
        silent_starts = silent_starts * second_per_unit
        silent_ends = silent_ends * second_per_unit
    if time_offset:
        silent_starts += time_offset
        silent_ends += time_offset
    if min_start is not None and silent_starts[0] < min_start:
        silent_starts.clip(min_start, None, silent_starts)
        clipped = True
    if max_end is not None and silent_ends[-1] > max_end:
        silent_ends.clip(None, max_end, silent_ends)
        clipped = True
    if clipped and (invalid_mask := silent_starts >= silent_ends).any():
        if invalid_mask.all():
            return
        valid_mask = ~invalid_mask
        silent_starts = silent_starts[valid_mask]
        silent_ends = silent_ends[valid_mask]
    return silent_starts, silent_ends

    Return array of start timestamps and array of end timestamps corresponding to where ``silence_mask`` is ``True``.
    
stable_whisper/stabilization/utils.py