o
    zi                     @   sB   d dl Z d dlZd dlmZ ddlmZ dd ZG dd dZdS )	    Nwraps   )utilc                    s   t   fdd}|S )zDecorator for formatting figures produced by the code below.
    See :py:func:`audiotools.core.util.format_figure` for more.

    Parameters
    ----------
    func : Callable
        Plotting function that is decorated by this function.

    c                     sh   t tjj }i }t| D ]\}}||v r"|| |||< q | i | tjdi | d S )N )	inspect	signaturer   format_figure
parameterskeyslistitemspop)argskwargsf_keysf_kwargskvfuncr   K/home/ubuntu/.local/lib/python3.10/site-packages/audiotools/core/display.pywrapper   s   
zformat_figure.<locals>.wrapperr   )r   r   r   r   r   r	      s   
r	   c                
   @   s   e Zd Ze				ddedededefd	d
ZeddefddZeddefddZ			ddedede
je
jef fddZ	ddede
je
jef fddZdS )DisplayMixinFtimelinear   preemphasisx_axisy_axisn_melsc           
      K   s   ddl }ddl}|  }d|_|r|  |j }|j|d}	|dkr6d||	d
  }	|	|	 8 }	|jj|	 d jddf|||jd| dS )	a  Displays a spectrogram, using ``librosa.display.specshow``.

        Parameters
        ----------
        preemphasis : bool, optional
            Whether or not to apply preemphasis, which makes high
            frequency detail easier to see, by default False
        x_axis : str, optional
            How to label the x axis, by default "time"
        y_axis : str, optional
            How to label the y axis, by default "linear"
        n_mels : int, optional
            If displaying a mel spectrogram with ``y_axis = "mel"``,
            this controls the number of mels, by default 128.
        kwargs : dict, optional
            Keyword arguments to :py:func:`audiotools.core.util.format_figure`.
        r   N)	ref_valuemel   gh㈵>)axis)r   r   sr)librosalibrosa.displayclone	stft_datar   	magnitudemaxlog_magnitudemel_spectrogramclamplog10displayspecshownumpymeansample_rate)
selfr   r   r   r    r   r&   signalreflog_magr   r   r   r1   "   s(   

zDisplayMixin.specshowc                 K   sj   ddl }ddl}| jd jdd}|  }t|jdrdnd}t|j|}||f|| j	d| dS )a3  Displays a waveform plot, using ``librosa.display.waveshow``.

        Parameters
        ----------
        x_axis : str, optional
            How to label the x axis, by default "time"
        kwargs : dict, optional
            Keyword arguments to :py:func:`audiotools.core.util.format_figure`.
        r   N)dimwaveshowwaveplot)r   r%   )
r&   r'   
audio_datar3   cpur2   hasattrr0   getattrr4   )r5   r   r   r&   r<   plot_fnwave_plot_fnr   r   r   r;   V   s   zDisplayMixin.waveplotc                 K   sv   ddl m} ddlm} |dd}||dddf  | j|d ||ddddf  | jdd|i| dS )	a>  Displays a waveform plot, using ``librosa.display.waveshow``.

        Parameters
        ----------
        x_axis : str, optional
            How to label the x axis, by default "time"
        kwargs : dict, optional
            Keyword arguments to :py:func:`audiotools.core.display.DisplayMixin.specshow`.
        r   N)GridSpec   r   )r   r   r   )matplotlib.pyplotpyplotmatplotlib.gridspecrB   subplotr;   r1   )r5   r   r   pltrB   gsr   r   r   wavespeck   s   
zDisplayMixin.wavespecNr1   tagstepr@   c           
      K   s   ddl m} | jd   }| j}||||| |durDt|tr(t	| |}|
 }	|  |di | ||dd|	| dS dS )a[  Writes a signal and its spectrogram to Tensorboard. Will show up
        under the Audio and Images tab in Tensorboard.

        Parameters
        ----------
        tag : str
            Tag to write signal to (e.g. ``clean/sample_0.wav``). The image will be
            written to the corresponding ``.png`` file (e.g. ``clean/sample_0.png``).
        writer : SummaryWriter
            A SummaryWriter object from PyTorch library.
        step : int, optional
            The step to write the signal to, by default None
        plot_fn : typing.Union[typing.Callable, str], optional
            How to create the image. Set to ``None`` to avoid plotting, by default "specshow"
        kwargs : dict, optional
            Keyword arguments to :py:func:`audiotools.core.display.DisplayMixin.specshow` or
            whatever ``plot_fn`` is set to.
        r   N)r   r   wavpngr   )rD   rE   r<   detachr=   r4   	add_audio
isinstancestrr?   figureclf
add_figurereplace)
r5   rK   writerrL   r@   r   rH   r<   r4   figr   r   r   write_audio_to_tb   s   

zDisplayMixin.write_audio_to_tb
image_pathc                 K   sR   ddl m} t|trt| |}|  |di | |j|ddd |  dS )a  Save AudioSignal spectrogram (or whatever ``plot_fn`` is set to) to
        a specified file.

        Parameters
        ----------
        image_path : str
            Where to save the file to.
        plot_fn : typing.Union[typing.Callable, str], optional
            How to create the image. Set to ``None`` to avoid plotting, by default "specshow"
        kwargs : dict, optional
            Keyword arguments to :py:func:`audiotools.core.display.DisplayMixin.specshow` or
            whatever ``plot_fn`` is set to.
        r   Ntight)bbox_inches
pad_inchesr   )rD   rE   rQ   rR   r?   rT   savefigclose)r5   rZ   r@   r   rH   r   r   r   
save_image   s   

zDisplayMixin.save_image)Fr   r   r   )r   )Nr1   )r1   )__name__
__module____qualname__r	   boolrR   intr1   r;   rJ   typingUnionCallablerY   r`   r   r   r   r   r   !   sD    3
+r   )r   rf   	functoolsr    r   r	   r   r   r   r   r   <module>   s    