o
    ziM                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlZd dlZd dlZd dlmZ d dlmZ e
G d	d
 d
ZdefddZ		d[dejejejeef dedefddZdd ZdejdedefddZ dejeejj!f fddZ"d\ddZ#ede$fd d!Z%g d"Z&e&fd#ed$ee fd%d&Z'd'd(e&fd)ee d*e(d+ed$ee fd,d-Z)	d]d.ejj!d/e$d0efd1d2Z*ed3ejeef fd4d5Z+d^d7eje,e$ejf d8efd9d:Z-d]d;e.d.ejj!fd<d=Z/d]d>e$d?efd@dAZ0dBZ1dCZ2				'	'	Dd_dEe.dFedGe(dHe(dIef
dJdKZ3	L	M	N	O	P	Q	Rd`dSededTedUedVedWedXefdYdZZ4dS )a    N)contextmanager)	dataclass)Path)Dict)Listflatten)	unflattenc                   @   s4   e Zd ZU dZeed< eed< edefddZdS )Infoz%Shim for torchaudio.info API changes.sample_rate
num_framesreturnc                 C   s   | j | j S N)r   r   )self r   H/home/ubuntu/.local/lib/python3.10/site-packages/audiotools/core/util.pyduration   s   zInfo.durationN)	__name__
__module____qualname____doc__float__annotations__intpropertyr   r   r   r   r   r
      s   
 r
   
audio_pathc                 C   sh   z	t t| }W n   t jjt| }Y t|tr*|d }t|j|j	d}|S t|j
|jd}|S )zShim for torchaudio.info to make 0.7.2 API match 0.8.0.

    Parameters
    ----------
    audio_path : str
        Path to audio file.
    r   )r   r   )
torchaudioinfostrbackendsoundfile_backend
isinstancetupler
   ratelengthr   r   )r   r   signal_infor   r   r   r   !   s   	
r   xndim
batch_sizec                 C   s~   t | s
t | } |dur$| j|ksJ | j|k r$| d} | j|k s|dur=| jd |kr=t| j}||d< | j| } | S )a  Ensures that the input ``x`` is a tensor of specified
    dimensions and batch size.

    Parameters
    ----------
    x : typing.Union[np.ndarray, torch.Tensor, float, int]
        Data that will become a tensor on its way out.
    ndim : int, optional
        How many dimensions should be in the output, by default None
    batch_size : int, optional
        The batch size of the output, by default None

    Returns
    -------
    torch.Tensor
        Modified version of ``x`` as a tensor.
    Nr   )torch	is_tensor	as_tensorr'   	unsqueezeshapelistexpand)r&   r'   r(   r.   r   r   r   ensure_tensor8   s   






r1   c                 C   s    ddl m} t| |r| jS | S )N   AudioSignal) r4   r!   
audio_data)otherr4   r   r   r   
_get_value\   s   
r8   hzn_fftr   c                 C   sx   | j }|  } td|d d|d  }|d | | |d k< | dddf |dddf   }|jddj}|j| S )a[  Closest frequency bin given a frequency, number
    of bins, and a sampling rate.

    Parameters
    ----------
    hz : torch.Tensor
       Tensor of frequencies in Hz.
    n_fft : int
        Number of FFT bins.
    sample_rate : int
        Sample rate of audio.

    Returns
    -------
    torch.Tensor
        Closest bins to the data.
    r      N)dim)r.   r   r*   linspaceabsminindicesreshape)r9   r:   r   r.   freqsclosestclosest_binsr   r   r   	hz_to_bind   s   $
rE   seedc                 C   sZ   | du s	| t ju rt jjjS t| tjt jtfrt j	| S t| t jj	r'| S t
d|  )a;  
    Turn seed into a np.random.RandomState instance.

    Parameters
    ----------
    seed : typing.Union[int, np.random.RandomState] or None
        If seed is None, return the RandomState singleton used by np.random.
        If seed is an int, return a new RandomState instance seeded with seed.
        If seed is already a RandomState instance, return it.
        Otherwise raise ValueError.

    Returns
    -------
    np.random.RandomState
        Random state object.

    Raises
    ------
    ValueError
        If seed is not valid, an error is thrown.
    Nz=%r cannot be used to seed a numpy.random.RandomState instance)nprandommtrand_randr!   numbersIntegralintegerr   RandomState
ValueError)rF   r   r   r   random_state   s   
rP   Fc                 C   s@   t |  tj|  t|  |rdt jj_dt jj_dS dS )a  
    Seeds all random states with the same random seed
    for reproducibility. Seeds ``numpy``, ``random`` and ``torch``
    random generators.
    For full reproducibility, two further options must be set
    according to the torch documentation:
    https://pytorch.org/docs/stable/notes/randomness.html
    To do this, ``set_cudnn`` must be True. It defaults to
    False, since setting it to True results in a performance
    hit.

    Args:
        random_seed (int): integer corresponding to random seed to
        use.
        set_cudnn (bool): Whether or not to set cudnn into determinstic
        mode and off of benchmark mode. Defaults to False.
    TFN)	r*   manual_seedrG   rH   rF   backendscudnndeterministic	benchmark)random_seed	set_cudnnr   r   r   rF      s   


tmpfilesc                 #   s2     fdd}zdV  W n   |   |  dS )a  Utility function for creating a context and closing all temporary files
    once the context is exited. For correct functionality, all temporary file
    handles created inside the context must be appended to the ```tmpfiles```
    list.

    This function is taken wholesale from Scaper.

    Parameters
    ----------
    tmpfiles : list
        List of temporary file handles
    c                     s0    D ]} z|    t| j W q   Y qd S r   )closeosunlinkname)trX   r   r   _close   s   z!_close_temp_files.<locals>._closeNr   )rX   r_   r   r^   r   _close_temp_files   s   

r`   ).wavz.flacz.mp3z.mp4folderextc                 C   sl   t | } t| t|r#dt| v r tjt| dt| v dS | gS g }|D ]}|| d| 7 }q'|S )a8  Finds all audio files in a directory recursively.
    Returns a list.

    Parameters
    ----------
    folder : str
        Folder to look for audio files in, recursively.
    ext : List[str], optional
        Extensions to look for without the ., by default
        ``['.wav', '.flac', '.mp3', '.mp4']``.
    *z**)	recursivez**/*)r   r   endswithr"   glob)rb   rc   filesr&   r   r   r   
find_audio   s   ri   Tr5   sourcesremove_emptyrelative_pathc           
   	   C   s   g }t |}| D ]l}t|}g }|drSt|d0}t|}|D ] }	|r-|	d dkr-q"|	d dkr=t||	d  |	d< ||	 q"W d   n1 sMw   Y  nt||dD ]}	t||	 }	|d|	i qY|t|dd d	 q|S )
ae  Reads audio sources that can either be folders
    full of audio files, or CSV files that contain paths
    to audio files. CSV files that adhere to the expected
    format can be generated by
    :py:func:`audiotools.data.preprocess.create_csv`.

    Parameters
    ----------
    sources : List[str]
        List of audio sources to be converted into a
        list of lists of audio files.
    remove_empty : bool, optional
        Whether or not to remove rows with an empty "path"
        from each CSV file, by default True.

    Returns
    -------
    list
        List of lists of rows of CSV files.
    .csvrpathr5   N)rc   c                 S   s   | d S )Nro   r   )r&   r   r   r   <lambda>*  s    zread_sources.<locals>.<lambda>)key)	r   r   rf   opencsv
DictReaderappendri   sorted)
rj   rk   rl   rc   rh   source_filesfreaderr&   r   r   r   read_sources   s,   

	r{   statelist_of_listspc                 C   s>   | j ttt||d}| t|| }|| | ||fS )a  Choose a single item from a list of lists.

    Parameters
    ----------
    state : np.random.RandomState
        Random state to use when choosing an item.
    list_of_lists : list
        A list of lists from which items will be drawn.
    p : float, optional
        Probabilities of each list, by default None

    Returns
    -------
    typing.Any
        An item from the list of lists.
    )r~   )choicer/   rangelenrandint)r|   r}   r~   
source_idxitem_idxr   r   r   choose_from_list_of_lists.  s   r   newdirc              	   c   s8    t  }zt |  dV  W t | dS t | w )z
    Context manager for switching directories to run a
    function. Useful for when you want to use relative
    paths to different runs.

    Parameters
    ----------
    newdir : typing.Union[Path, str]
        Directory to switch to.
    N)rZ   getcwdchdir)r   curdirr   r   r   r   F  s   
r   cpubatchdevicec                 C   s   t | tr&t| } |  D ]\}}z	||| |< W q   Y qt| } | S t| r2| |} | S t | trPt	t
| D ]}z| | || |< W q=   Y q=| S )a   Moves items in a batch (typically generated by a DataLoader as a list
    or a dict) to the specified device. This works even if dictionaries
    are nested.

    Parameters
    ----------
    batch : typing.Union[dict, list, torch.Tensor]
        Batch, typically generated by a dataloader, that will be moved to
        the device.
    device : str, optional
        Device to move batch to, by default "cpu"

    Returns
    -------
    typing.Union[dict, list, torch.Tensor]
        Batch with all values moved to the specified device.
    )r!   dictr   itemstor	   r*   r+   r/   r   r   )r   r   rq   valir   r   r   prepare_batchZ  s&   
	


r   
dist_tuplec                 C   s:   | d dkr
| d S t |}t|| d }|| dd  S )a  Samples from a distribution defined by a tuple. The first
    item in the tuple is the distribution type, and the rest of the
    items are arguments to that distribution. The distribution function
    is gotten from the ``np.random.RandomState`` object.

    Parameters
    ----------
    dist_tuple : tuple
        Distribution tuple
    state : np.random.RandomState, optional
        Random state, or seed to use, by default None

    Returns
    -------
    typing.Union[float, int, str]
        Draw from the distribution.

    Examples
    --------
    Sample from a uniform distribution:

    >>> dist_tuple = ("uniform", 0, 1)
    >>> sample_from_dist(dist_tuple)

    Sample from a constant distribution:

    >>> dist_tuple = ("const", 0)
    >>> sample_from_dist(dist_tuple)

    Sample from a normal distribution:

    >>> dist_tuple = ("normal", 0, 0.5)
    >>> sample_from_dist(dist_tuple)

    r   constr2   N)rP   getattr)r   r|   dist_fnr   r   r   sample_from_dist  s
   $r   list_of_dictsn_splitsc                    s  ddl m  g }t| }|du rdnd}|du rdn|}tt|| }td||D ]Q}dd | |||  D fd	d
d D }i }| D ])\}	}
t|
t	rtt
 fdd|
D ri j|
dd||	< qKtjjjj|
||	< qK|t| q+|s|d }|S |}|S )a   Collates a list of dictionaries (e.g. as returned by a
    dataloader) into a dictionary with batched values. This routine
    uses the default torch collate function for everything
    except AudioSignal objects, which are handled by the
    :py:func:`audiotools.core.audio_signal.AudioSignal.batch`
    function.

    This function takes n_splits to enable splitting a batch
    into multiple sub-batches for the purposes of gradient accumulation,
    etc.

    Parameters
    ----------
    list_of_dicts : list
        List of dictionaries to be collated.
    n_splits : int
        Number of splits to make when creating the batches (split into
        sub-batches). Useful for things like gradient accumulation.

    Returns
    -------
    dict
        Dictionary containing batched data.
    r2   r3   NFTr   c                 S   s   g | ]}t |qS r   r   ).0dr   r   r   
<listcomp>      zcollate.<locals>.<listcomp>c                    s    i | ]   fd dD qS )c                    s   g | ]}|  qS r   r   )r   dickr   r   r     r   z&collate.<locals>.<dictcomp>.<listcomp>r   )r   )list_of_dicts_r   r   
<dictcomp>  s    zcollate.<locals>.<dictcomp>c                 3   s    | ]}t | V  qd S r   )r!   )r   sr3   r   r   	<genexpr>  s    zcollate.<locals>.<genexpr>)pad_signals)r5   r4   r   r   mathceilr   r   r!   r/   allr   r*   utilsdata_utilscollatedefault_collateru   r	   )r   r   batcheslist_lenreturn_listn_itemsr   dict_of_listsr   r   vr   )r4   r   r   r     s.   

r   i`  )	      whitefig_sizetitleformat_axesformat
font_colorc                 C   s  ddl }ddlm} | du rt} |sdS |du r| }|j|   |j}| |j d }	|	t	 }
|r|j}|D ]z}|
 \}}| \}}| }|dd D ]}|d j|d dd||fdd	d
dd|d|
 dd
}qP| dd }|dd D ]}|d j|dd||fddd
dd|d|
 dd
}q||dd |  |j|  |j|  q8|jddddddd |dur|d j|ddd|
 dd
dddd	}|tdddd dS dS ) a*  Prettifies the spectrogram and waveform plots. A title
    can be inset into the top right corner, and the axes can be
    inset into the figure, allowing the data to take up the entire
    image. Used in

    - :py:func:`audiotools.core.display.DisplayMixin.specshow`
    - :py:func:`audiotools.core.display.DisplayMixin.waveplot`
    - :py:func:`audiotools.core.display.DisplayMixin.wavespec`

    Parameters
    ----------
    fig_size : tuple, optional
        Size of figure, by default (9, 3)
    title : str, optional
        Title to inset in top right, by default None
    fig : matplotlib.figure.Figure, optional
        Figure object, if None ``plt.gcf()`` will be used, by default None
    format_axes : bool, optional
        Format the axes to be inside the figure, by default True
    format : bool, optional
        This formatting can be skipped entirely by passing ``format=False``
        to any of the plotting functions that use this formater, by default True
    font_color : str, optional
        Color of font of axes, by default "white"
    r   Nr;   r)   i  z2.1fr   r   )   zoffset pointslefttop   g      ?)	xyxycoordsxytext
textcoordshavacolorfontsizealphar   )r   r   centerbottomr2   )r   r   rightr   hspacewspace)r2   r2   zaxes fraction   )r   r   r   r   )r   r   r   r   r   r   r   r   blackg      ?)	facecolorr   	edgecolor)
matplotlibmatplotlib.pyplotpyplotDEFAULT_FIG_SIZEgcfset_size_inchesaxesget_size_inchesdpi	BASE_SIZEget_ylimget_xlim
get_yticksannotate
get_xticksmarginsset_axis_offxaxisset_major_locatorNullLocatoryaxissubplots_adjustset_bboxr   )r   r   figr   r   r   r   pltaxspixels
font_scaleaxymin_xminticksr]   r   r   r   format_figure  s   !

r      D  r         ?C2C6chords
max_voices	num_itemsr   min_notemax_note
output_dirc                 C   s  ddl }ddlm} ddlm}	 ||}
||}g }t|D ]7}i }td| }t|D ]#}t|
|}t	d| |}|j
||||dd	}||d
| < q.|| q t|}|jdd t|D ]$\}}|d|  }|jdd | D ]\}}||| d  q{qfttdd |D }dd |D }|D ]}|D ]}||v r|| || j q|| d qq| D ]\}}|	||| d dd q|S )a  
    Generates a toy multitrack dataset of chords, synthesized from sine waves.


    Parameters
    ----------
    max_voices : int, optional
        Maximum number of voices in a chord, by default 8
    sample_rate : int, optional
        Sample rate of audio, by default 44100
    num_items : int, optional
        Number of items to generate, by default 5
    duration : float, optional
        Duration of each item, by default 1.0
    min_note : str, optional
        Minimum note in the dataset, by default "C2"
    max_note : str, optional
        Maximum note in the dataset, by default "C6"
    output_dir : Path, optional
        Directory to save the dataset, by default "chords"

    r   Nr2   r3   r;   )
create_csvg333333?sine)	frequencyr   r   r.   voice_T)exist_oktrack_ra   c                 S   s   g | ]}|  D ]}|qqS r   )keys)r   trackr   r   r   r   r     s    z*generate_chord_dataset.<locals>.<listcomp>c                 S   s   i | ]}|g qS r   r   )r   voicer   r   r   r     s    z*generate_chord_dataset.<locals>.<dictcomp>r5   rm   )loudness)librosar5   r4   data.preprocessr   note_to_midir   rH   r   uniformwave
midi_to_hzru   r   mkdir	enumerater   writer/   setpath_to_file)r   r   r   r   r   r   r   r
  r4   r   min_midimax_miditracksidxr  
num_voices	voice_idxmidinotedursig	track_dir
voice_name
all_voicesvoice_listspathsr   r   r   generate_chord_datasetQ  sN   

r#  )NN)Fr   )r   )NNNTTr   )r   r   r   r   r   r   r   )5rs   rg   r   rK   rZ   rH   typing
contextlibr   dataclassesr   pathlibr   r   r   numpyrG   r*   r   flatten_dictr   r	   r
   r   r   UnionndarrayTensorr   r   r1   r8   rE   rN   rP   rF   r/   r`   AUDIO_EXTENSIONSri   boolr{   r   r   r   r   r"   r   r   r   r   r   r#  r   r   r   r   <module>   s    
$
"
1
"%+8
l