
    :i/                     &   S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKrS SKrS SKJrJrJrJr  \(       a  S SKJr  \R,                  " \5      r\" 5       (       a0  S SKr\R6                  " 5       r\" S\ V s0 s H  o U _M     sn 5      rO\" SS	S	05      r\" 5       (       a  S SKr\R<                  R?                  5        Vs/ s H  oR@                  RC                  5       PM     snr" " S
 S5      r#S\$\RJ                  -  S\$\RJ                  -  4S jr&S\RJ                  S\RJ                  4S jr'S\RJ                  S\RJ                  S\RJ                  4S jr(\S\)S\)S\$S\$S\)S\RJ                  4S j5       r*gs  sn f s  snf )    N)Enum)cache)Path)TYPE_CHECKING)assert_soundfile_installedassert_soxr_installedis_soundfile_installedis_soxr_installed)RawAudioAudioFormatnonec                   B   \ rS rSrS\R
                  S\S\SS4S jrS\4S jr	SS	 jr
\S\4S
 j5       r\SS\S\SS 4S jj5       r\SS\S\SS 4S jj5       r\SS\S\SS 4S jj5       r\SS\S\SS 4S jj5       rSS\S\S\4S jjr\SS j5       rS\SS4S jrSrg)Audio*   audio_arraysampling_rateformatreturnNc                 H    Xl         X l        X0l        U R                  5         g)zInitialize an Audio instance with audio data, sampling rate, and format.

Args:
    audio_array: The audio data as a numpy array.
    sampling_rate: The sampling rate of the audio in Hz.
    format: The format of the audio file.
N)r   r   r   _check_valid)selfr   r   r   s       N/home/james-whalen/.local/lib/python3.13/site-packages/mistral_common/audio.py__init__Audio.__init__+   s!     '*    c                     SU R                    S[        U R                  5      U R                   -  S SU R                  R                   3$ )NzAudio - sampling_rate=z Hz, duration=z.2fz	s, shape=)r   lenr   shaper   s    r   __repr__Audio.__repr__8   sU    $T%7%7$8 9D,,-0B0BB3G H%%++,.	
r   c                 z   [        U R                  [        R                  5      (       d   [	        [        R                  5      5       eU R                  R
                  S:X  d   SU R                  R
                  < 35       e[        5         U R                  [        ;   d   SU R                  < S[        < 35       eg )N   zself.audio_array.ndim=zself.format= not in EXPECTED_FORMAT_VALUES=)	
isinstancer   npndarraytypendimr   r   EXPECTED_FORMAT_VALUESr   s    r   r   Audio._check_valid?   s    $**BJJ77Ibjj9II7$$)F.Dd.>.>.C.C-E+FF)"${{44hFfOeNg6hh4r   c                 R    U R                   R                  S   U R                  -  nU$ )zcCalculate the duration of the audio in seconds.

Returns:
   The duration of the audio in seconds.
r   )r   r   r   )r   durations     r   r-   Audio.durationE   s*     **003d6H6HHr   urlstrictc                     [         R                  " U 5      nUR                  5         [        R	                  UR
                  US9$ ! [         R                   a  n[        SU  35      UeSnAf[         a  n[        SU  S35      UeSnAff = f)zCreate an Audio instance from a URL.

Args:
    url: The URL of the audio file.
    strict: Whether to strictly enforce mono audio.

Returns:
    An instance of the Audio class.
r0   z#Failed to download audio from URL: Nz*Failed to create Audio instance from URL: z .)	requestsgetraise_for_statusr   
from_bytescontentRequestException
ValueError	Exception)r/   r0   responsees       r   from_urlAudio.from_urlP   s    	Z||C(H%%'##H$4$4V#DD(( 	QB3%HIqP 	ZI#bQRXYY	Zs$   AA BA))B6BBaudio_base64c                    [        5         [        R                  " SU 5      (       a  U R                  S5      S   n  [        R
                  " U 5      n[        R                  X!S9$ ! [         a  n[        S5      UeSnAff = f)zCreate an Audio instance from a base64 encoded string.

Args:
    audio_base64: The base64 encoded audio data.
    strict: Whether to strictly enforce mono audio. Defaults to True.

Returns:
    An instance of the Audio class.
z^data:audio/\w+;base64,,r#   zHbase64 decoding failed. Please check the input string is a valid base64.Nr2   )
r   rematchsplitbase64	b64decoder:   r9   r   r6   )r?   r0   audio_bytesr<   s       r   from_base64Audio.from_base64d   s     	#$88.=='--c215L	p **<8K ;;  	pghnoo	ps   A% %
B /A;;B filec                 f   [        5         [        U [        5      (       a  U R                  S5      (       a  U SS n [	        U 5      R                  5       (       d  [        SU < S35      e[        U S5       nUR                  5       nSSS5        [        R                  WUS9$ ! , (       d  f       N"= f)zCreate an Audio instance from an audio file.

Args:
    file: Path to the audio file.
    strict: Whether to strictly enforce mono audio. Defaults to True.

Returns:
    An instance of the Audio class.
zfile://   Nzfile=z does not existrbr2   )r   r%   str
startswithr   existsFileNotFoundErroropenreadr   r6   )rJ   r0   frG   s       r   	from_fileAudio.from_file{   s     	#$dC  T__Y%?%?8DDz  ""#utg_$=>>$&&(K  F;; s   5B""
B0rG   c                    [         R                  " U 5       n[        R                  " U5       nUR	                  SS9nUR
                  nUR                  nSSS5        SSS5        [        W5      nUR                  R                  5       nWR                  S:w  a/  U(       a  [        SUR                  < 35      eUR                  SS9n[        UWUS9$ ! , (       d  f       N= f! , (       d  f       N= f)zCreate an Audio instance from bytes.

Args:
    audio_bytes: The audio data as bytes.
    strict: Whether to strictly enforce mono audio. Defaults to True.

Returns:
    An instance of the Audio class.
float32)dtypeNr#   zaudio_array.ndim=axis)r   r   r   )ioBytesIOsf	SoundFilerS   
samplerater   r   valuelowerr)   r9   meanr   )	rG   r0   
audio_filerT   r   r   audio_formatformat_enumr   s	            r   r6   Audio.from_bytes   s     ZZ$
j)Qff9f5 ! xx	 * % ",/""((*q  $5K$4$4#6!788)..A.6MRXYY *) %$s"   C&(CC&
C#	C&&
C4prefixc                    [        5         U[        ;   d   SU< S[        < 35       e[        R                  " 5        n[        R
                  " X0R                  U R                  UR                  5       S9  UR                  S5        [        R                  " UR                  5       5      R                  S5      nSSS5        U(       a  SUR                  5        SW 3nW$ ! , (       d  f       N-= f)	zConvert the audio data to a base64 encoded string.

Args:
    format: The format to encode the audio in.
    prefix: Whether to add a data prefix to the base64 encoded string.

Returns:
    The base64 encoded audio data.
zformat=r$   )r   r   zutf-8Nzdata:audio/z;base64,)r   r*   r\   r]   r^   writer   r   upperseekrE   	b64encoderS   decoderb   )r   r   rh   rd   
base64_strs        r   	to_base64Audio.to_base64   s     	#$//^GF9<\E[D]1^^/ZZ\ZHHZ!1!143E3Eflln]OOA))*//*;<CCGLJ  &v||~&6hzlKJ \s   A=C!!
C/c                 <   [        U R                  [        5      (       a  [        R	                  U R                  5      $ [        U R                  [
        5      (       a  [        R                  U R                  5      $ [        S[        U R                  5       35      e)zCreate an Audio instance from a RawAudio object.

Args:
    audio: The RawAudio object containing audio data.

Returns:
    An instance of the Audio class.
zUnsupported audio data type: )	r%   databytesr   r6   rN   rH   r9   r(   )audios    r   from_raw_audioAudio.from_raw_audio   sm     ejj%((##EJJ//

C(($$UZZ00<T%**=M<NOPPr   new_sampling_ratec                     U R                   U:X  a  g[        5         [        R                  " U R                  U R                   USS9U l        Xl         g)zzResample audio data to a new sampling rate.

Args:
    new_sampling_rate: The new sampling rate to resample the audio to.
NHQ)quality)r   r   soxrresampler   )r   rx   s     r   r}   Audio.resample   sG     !22==)9)94;M;MO`jno.r   )r   r   r   )r   N)T)F)ru   r   r   r   )__name__
__module____qualname____firstlineno__r&   r'   intrN   r   r    r   propertyfloatr-   staticmethodboolr=   rH   rU   rt   r6   rp   rv   r}   __static_attributes__ r   r   r   r   *   sF   BJJ s C TX 
# 
i %   Zc Z4 Z7 Z Z& <# <t <w < <, < <T <W < <. Z Zt Zw Z Z: T c , Q Q /# /$ /r   r   freqr   c                    SnSnS[         R                  " S5      -  nSU -  S-  n[        U [         R                  5      (       aX  [        U[         R                  5      (       d   [	        U5      5       eX:  nU[         R                  " X   U-  5      U-  -   XE'   U$ X:  a  U[         R                  " X-  5      U-  -   nU$ )zConvert frequency from hertz to mels using the "slaney" mel-scale.

Args:
    freq: The frequency, or multiple frequencies, in hertz (Hz).

Returns:
    The frequencies on the mel scale.
     @@      .@      ;@皙@      @      i@)r&   logr%   r'   r(   )r   min_log_hertzmin_log_mellogstepmels
log_regions         r   hertz_to_melr      s     MKRVVC[ G:D$

##$

++7T$Z7+*
&0@=0P)QT[)[[ K 
	RVVD$89GCCKr   r   c                     SnSn[         R                  " S5      S-  nSU -  S-  nX:  nU[         R                  " X0U   U-
  -  5      -  XE'   U$ )zConvert frequency from mels to hertz using the "slaney" mel-scale.

Args:
    mels: The frequency, or multiple frequencies, in mels.

Returns:
    The frequencies in hertz.
r   r   r   r   r   r   )r&   r   exp)r   r   r   r   r   r   s         r   mel_to_hertzr      s^     MKffSkD G4<#D$J$rvvgj9IK9W.X'YYDKr   	fft_freqsfilter_freqsc                 T   [         R                  " U5      n[         R                  " US5      [         R                  " U S5      -
  nUSS2SS24   * USS -  nUSS2SS24   USS -  n[         R                  " [         R                  " S5      [         R
                  " XE5      5      nU$ )a$  Creates a triangular filter bank.

Adapted from *torchaudio* and *librosa*.

Args:
    fft_freqs: Discrete frequencies of the FFT bins in Hz.
    filter_freqs: Center frequencies of the triangular filters to create, in Hz.

Returns:
    array of shape `(num_frequency_bins, num_mel_filters)`
r   r#   N   )r&   diffexpand_dimsmaximumzerosminimum)r   r   filter_diffslopesdown_slopes	up_slopesfilter_banks          r   _create_triangular_filter_bankr     s     '','K^^L!,r~~i/KKF!SbS&>/K$44Kq!"uAB/I jj!bjj6XYKr   num_frequency_binsnum_mel_binsmin_frequencymax_frequencyr   c                    U S:  a  [        SU  S35      eX#:  a  [        SU SU 35      e[        U5      n[        U5      n[        R                  " XVUS-   5      n[	        U5      n[        R                  " SUS-  U 5      n	[        X5      n
SUSUS-    USU -
  -  nU
[        R                  " US5      -  n
U
R                  SS	9S
:H  R                  5       (       a  [        SU SU  S35      eU
$ )a"  Create a Mel filter bank matrix for converting frequency bins to the Mel scale.

This function generates a filter bank matrix that can be used to transform a
spectrum represented in frequency bins to the Mel scale. The Mel scale is a
perceptual scale of pitches judged by listeners to be equal in distance from one another.

Args:
    num_frequency_bins: The number of frequency bins in the input spectrum.
    num_mel_bins: The number of desired Mel bins in the output.
    min_frequency: The minimum frequency (in Hz) to consider.
    max_frequency: The maximum frequency (in Hz) to consider.
    sampling_rate: The sampling rate of the audio signal.

Returns:
    A filter bank matrix of shape (num_mel_bins, num_frequency_bins)
    that can be used to project frequency bin energies onto Mel bins.
r   zRequire num_frequency_bins: z >= 2zRequire min_frequency: z <= max_frequency: r   g       @NrZ   g        zNAt least one mel filter has all zero values. The value for `num_mel_filters` (z?) may be set too high. Or, the value for `num_frequency_bins` (z) may be set too low.)	r9   r   r&   linspacer   r   r   maxany)r   r   r   r   r   mel_minmel_max	mel_freqsr   r   mel_filtersenorms               r   mel_filter_bankr   &  s*   2 A78J7K5QRR$2=/ATUbTcdee =)G=)GGlQ.>?I	*L A}13EFI0IK <L1$45]l8SSTE2>>%++KQ3&++--00<~ > ###8	:
 	
 r   )+rE   r\   loggingrB   enumr   	functoolsr   pathlibr   typingr   numpyr&   r3   mistral_common.importsr   r   r	   r
   &mistral_common.protocol.instruct.chunkr   	getLoggerr   logger	soundfiler^   available_formatsr   r|   __members__valuesra   rb   r*   r   r   r'   r   r   r   r   r   )format_namevs   00r   <module>r      s    	  	         ?			8	$ ,,. }Sd&eSdKK'?Sd&efK}vv&67K3>3J3J3Q3Q3ST3Sa''--/3ST z/ z/zurzz) ebjj.@ 0rzz bjj &bjj 

 WYWaWa ( 555 5 	5
 5 ZZ5 5O 'f Us   F	
#F