U
    VcN                     @   s>   d Z ddlZddlmZ ddlmZ ddlmZ d	ddZ	dS )
a  Audio summaries and TensorFlow operations to create them, V2 versions.

An audio summary stores a rank-2 string tensor of shape `[k, 2]`, where
`k` is the number of audio clips recorded in the summary. Each row of
the tensor is a pair `[encoded_audio, label]`, where `encoded_audio` is
a binary string whose encoding is specified in the summary metadata, and
`label` is a UTF-8 encoded Markdown string describing the audio clip.
    N)tf2)metadata)lazy_tensor_creator   c              
      s   t tdd  dkr ddlm  |dkr,d}|dkr@td| tjd|tjdd}|g}t tj	j
d	dpztj	j}	|	| d
|d>\}
}tj fdd}tj	j|
|||dW  5 Q R  S Q R X dS )a  Write an audio summary.

    Arguments:
      name: A name for this summary. The summary tag used for TensorBoard will
        be this name prefixed by any active name scopes.
      data: A `Tensor` representing audio data with shape `[k, t, c]`,
        where `k` is the number of audio clips, `t` is the number of
        frames, and `c` is the number of channels. Elements should be
        floating-point values in `[-1.0, 1.0]`. Any of the dimensions may
        be statically unknown (i.e., `None`).
      sample_rate: An `int` or rank-0 `int32` `Tensor` that represents the
        sample rate, in Hz. Must be positive.
      step: Explicit `int64`-castable monotonic step value for this summary. If
        omitted, this defaults to `tf.summary.experimental.get_step()`, which must
        not be None.
      max_outputs: Optional `int` or rank-0 integer `Tensor`. At most this
        many audio clips will be emitted at each step. When more than
        `max_outputs` many clips are provided, the first `max_outputs`
        many clips will be used and the rest silently discarded.
      encoding: Optional constant `str` for the desired encoding. Only "wav"
        is currently supported, but this is not guaranteed to remain the
        default, so if you want "wav" in particular, set this explicitly.
      description: Optional long-form description for this summary, as a
        constant `str`. Markdown is supported. Defaults to empty.

    Returns:
      True on success, or false if no summary was emitted because no default
      summary writer was available.

    Raises:
      ValueError: if a default writer exists, but no step was provided and
        `tf.summary.experimental.get_step()` is None.
    audioNr   )gen_audio_opsZwavzUnknown encoding: %rZWAV)Zdisplay_namedescriptionencodingsummary_scopeZaudio_summary)valuesc                     s   t jd t j d  } tjjd}t j|| t jdd t 	t j
 dd dk fddd	d  t d
gt j
| dd d }t jt  |gdS )Nr   )sample_rateZencode_each_audio)Zdtypename)inputr   c                      s    S N r   Zencoded_audior   H/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/plugins/audio/summary_v2.py<lambda>s       z,audio.<locals>.lazy_tensor.<locals>.<lambda>c                   S   s   t g t jS r   )tfZconstantstringr   r   r   r   r   t   r       )a)r   	debuggingZassert_rankZassert_non_negative	functoolspartialZ
encode_wavZmap_fnr   ZcondshapeZtileZ	transposestack)Zlimited_audioZ	encode_fnZlimited_labelsZ	audio_opsdatamax_outputsr   r   r   lazy_tensorb   s(     
zaudio.<locals>.lazy_tensor)tagZtensorstepr   )getattrr   Ztensorflow.python.opsr   
ValueErrorr   Zcreate_summary_metadataEncodingValuesummaryZexperimentalr
   r   ZLazyTensorCreatorwrite)r   r    r   r$   r!   r	   r   Zsummary_metadatainputsr
   r#   _r"   r   r   r   r       s2    *
   r   )Nr   NN)
__doc__r   Ztensorboard.compatr   r   Ztensorboard.plugins.audior   Ztensorboard.utilr   r   r   r   r   r   <module>   s   
    