
    ik                     x   d dl mZ d dlmZmZ d dlmZ d dlm	Z	 d dl
Zd dlmZmZmZ dZdddd	d
dddddddddZdej$                  dededej$                  fdZee   edej,                  fdededej.                  fdZ	 d#deeef   ded ede	j4                  fd!Z	 d#deeef   ded ede	j4                  fd"Zy)$    )Path)OptionalUnionN)base_load_modelget_model_pathload_configi>  
cohere_asrfireredasr2glmasr
sensevoicevoxtralvoxtral_realtimevibevoice_asr	qwen3_asrcanary	moonshinemmsgranite_speechqwen2_audio)r	   r
   glmr   r   r   	vibevoicer   r   r   r   r   r   audioorig_sr	target_srreturnc                 |    ddl m} t        j                  ||      }||z  }||z  }|j	                  | ||d      }|S )Nr   )signaledge)padtype)scipyr   npgcdresample_poly)r   r   r   r   r"   updown	resampleds           h/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/mlx_audio/stt/utils.pyresample_audior(      sG    
&&)
$C	c	Bc>D$$UBf$EI    Ffilesrdtypec                     ddl m}  || d      \  }}||k7  rt        |||      }t        j                  ||      j                  d      S )a?  
    Open an audio file and read as mono waveform, resampling as necessary

    Parameters
    ----------
    file: str
        The audio file to open

    sr: int
        The sample rate to resample the audio if necessary

    Returns
    -------
    A NumPy array containing the audio waveform, in float32 dtype.
    r   )readT)	always_2d)r,      )axis)mlx_audio.audio_ior.   r(   mxarraymean)r*   r+   
from_stdinr,   
audio_readr   sample_rates          r'   
load_audior9   '   sM    * 6#DD9E;buk2688E',,!,44r)   
model_pathlazystrictc           	      ,    t        d| dt        ||d|S )a  
    Load and initialize an STT model from a given path.

    Args:
        model_path: The path or HuggingFace repo to load the model from.
        lazy: If False, evaluate model parameters immediately.
        strict: If True, raise an error if any weights are missing.
        **kwargs: Additional keyword arguments (revision, force_download).

    Returns:
        nn.Module: The loaded and initialized model.
    stt)r:   categorymodel_remappingr;   r<    )r   MODEL_REMAPPINGr:   r;   r<   kwargss       r'   
load_modelrE   D   s/      '  r)   c                      t        | f||d|S )aY  
    Load a speech-to-text model from a local path or HuggingFace repository.

    This is the main entry point for loading STT models. It automatically
    detects the model type and initializes the appropriate model class.

    Args:
        model_path: The local path or HuggingFace repo ID to load from.
        lazy: If False, evaluate model parameters immediately.
        strict: If True, raise an error if any weights are missing.
        **kwargs: Additional keyword arguments:
            - revision (str): HuggingFace revision/branch to use
            - force_download (bool): Force re-download of model files

    Returns:
        nn.Module: The loaded and initialized model.

    Example:
        >>> from mlx_audio.stt import load
        >>> model = load("mlx-community/whisper-tiny-asr-fp16")
        >>> result = model.generate(audio)
    )r;   r<   )rE   rC   s       r'   loadrG   ]   s    2 jEtFEfEEr)   )FF)pathlibr   typingr   r   mlx.corecorer3   mlx.nnnnnumpyr!   mlx_audio.utilsr   r   r   SAMPLE_RATErB   ndarrayintr(   strfloat32Dtyper9   boolModulerE   rG   rA   r)   r'   <module>rX      s(    "    H H  * & ""** s s rzz  jj	5
55 88	5< FKc4i (,>BYY4 FKFc4i F(,F>BFYYFr)   