
    +h                         S SK rSSKJrJr  SSKJr   S SKrSrSr	S S
KJr   " S S\\5      rg! \
 a  rSrS\ S	3r	 SrCN&SrCff = f)    N   )ConfigMixinregister_to_config)SchedulerMixinT FzCannot import librosa because zB. Make sure to correctly install librosa to be able to install it.)Imagec                   @   \ rS rSrSrSr\       SS\S\S\S\S\S	\S
\4S jj5       rS\S\4S jr	SS\
S\R                  4S jjrS\4S jrSS\S\R                  4S jjrS\4S jrS\S\R"                  4S jrS\R"                  S\R                  4S jrSrg)Mel%   a  
Parameters:
    x_res (`int`):
        x resolution of spectrogram (time).
    y_res (`int`):
        y resolution of spectrogram (frequency bins).
    sample_rate (`int`):
        Sample rate of audio.
    n_fft (`int`):
        Number of Fast Fourier Transforms.
    hop_length (`int`):
        Hop length (a higher number is recommended if `y_res` < 256).
    top_db (`int`):
        Loudest decibel value.
    n_iter (`int`):
        Number of iterations for Griffin-Lim Mel inversion.
zmel_config.jsonx_resy_ressample_raten_fft
hop_lengthtop_dbn_iterc                     XPl         X0l        X@l        X`l        Xpl        U R                  X5        S U l        [        (       d  [        [        5      eg )N)
r   srr   r   r   set_resolutionaudio_librosa_can_be_imported
ValueError_import_error)selfr   r   r   r   r   r   r   s           l/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/deprecated/audio_diffusion/mel.py__init__Mel.__init__:   sH     %
E)
'']++ (    c                     Xl         X l        U R                  U l        U R                   U R                  -  S-
  U l        g)zSet resolution.

Args:
    x_res (`int`):
        x resolution of spectrogram (time).
    y_res (`int`):
        y resolution of spectrogram (frequency bins).
   N)r   r   n_melsr   
slice_size)r   r   r   s      r   r   Mel.set_resolutionP   s2     

jj**t6:r   N
audio_file	raw_audioc           	         Ub)  [         R                  " USU R                  S9u  U l        nOX l        [	        U R                  5      U R
                  U R                  -  :  aj  [        R                  " U R                  [        R                  " U R
                  U R                  -  [	        U R                  5      -
  45      /5      U l        gg)zLoad audio.

Args:
    audio_file (`str`):
        An audio file that must be on disk due to [Librosa](https://librosa.org/) limitation.
    raw_audio (`np.ndarray`):
        The raw audio file as a NumPy array.
NT)monor   )
librosaloadr   r   lenr   r   npconcatenatezeros)r   r$   r%   _s       r   
load_audioMel.load_audio^   s     !#LL$477KMDJ"J tzz?TZZ$//99RXXtzzDOO?[^abfblbl^m?m>o5p(qrDJ :r   returnc                 F    [        U R                  5      U R                  -  $ )zlGet number of slices in audio.

Returns:
    `int`:
        Number of spectograms audio can be sliced into.
)r*   r   r"   r   s    r   get_number_of_slicesMel.get_number_of_slicesp   s     4::$//11r   slicec                 Z    U R                   U R                  U-  U R                  US-   -   $ )zGet slice of audio.

Args:
    slice (`int`):
        Slice number of audio (out of `get_number_of_slices()`).

Returns:
    `np.ndarray`:
        The audio slice as a NumPy array.
r    )r   r"   )r   r6   s     r   get_audio_sliceMel.get_audio_slicey   s+     zz$//E1DOOuqy4QRRr   c                     U R                   $ )zDGet sample rate.

Returns:
    `int`:
        Sample rate of audio.
)r   r3   s    r   get_sample_rateMel.get_sample_rate   s     wwr   c                    [         R                  R                  U R                  U5      U R                  U R
                  U R                  U R                  S9n[         R                  " U[        R                  U R                  S9nX0R                  -   S-  U R                  -  R                  SS5      S-   R                  [        R                  5      n[        R                   " U5      nU$ )zConvert slice of audio to spectrogram.

Args:
    slice (`int`):
        Slice number of audio to convert (out of `get_number_of_slices()`).

Returns:
    `PIL Image`:
        A grayscale image of `x_res x y_res`.
)yr   r   r   r!   )refr      r   g      ?)r(   featuremelspectrogramr8   r   r   r   r!   power_to_dbr+   maxr   clipastypeuint8r   	fromarray)r   r6   Slog_Sbytedataimages         r   audio_slice_to_imageMel.audio_slice_to_image   s     OO**""5)dggTZZTXTcTclplwlw + 
 ##A266$++Fkk)S04;;>DDQLsRZZ[][c[cd)r   rL   c                    [         R                  " UR                  5       SS9R                  UR                  UR
                  45      nUR                  S5      U R                  -  S-  U R                  -
  n[        R                  " U5      n[        R                  R                  R                  X@R                  U R                  U R                  U R                   S9nU$ )zConverts spectrogram to audio.

Args:
    image (`PIL Image`):
        An grayscale image of `x_res x y_res`.

Returns:
    audio (`np.ndarray`):
        The audio as a NumPy array.
rG   )dtypefloatr@   )r   r   r   r   )r+   
frombuffertobytesreshapeheightwidthrF   r   r(   db_to_powerrA   inversemel_to_audior   r   r   r   )r   rL   rK   rJ   rI   r   s         r   image_to_audioMel.image_to_audio   s     ==@HH%,,X]XcXcIde(4;;6<t{{J&''44''PTP[P[ 5 
 r   )
r   r   r   r   r!   r"   r   r   r   r   )   r\   i"V  i   i   P       )NN)r   )__name__
__module____qualname____firstlineno____doc__config_namer   intr   r   strr+   ndarrayr/   r4   r8   r;   r   rM   rZ   __static_attributes__ r   r   r
   r
   %   s   $ $K  ,, , 	,
 , , , , ,*;C ; ;sS sBJJ s$2c 2SS S S # %++ &EKK BJJ r   r
   )numpyr+   configuration_utilsr   r   schedulers.scheduling_utilsr   r(   r   r   	ExceptionePILr   r
   ri   r   r   <module>rp      sb      C :	#M N+~ N  $
(+mn s   / AAA