Create a legacy audio summary protobuf.

  This behaves as if you were to create an `op` with the same arguments
  (wrapped with constant tensors where appropriate) and then execute
  that summary op in a TensorFlow session.

  Arguments:
    name: A unique name for the generated summary node.
    audio: An `np.array` 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]`.
    sample_rate: An `int` that represents the sample rate, in Hz.
      Must be positive.
    labels: Optional list (or rank-1 `np.array`) of textstrings or UTF-8
      bytestrings whose length is the first dimension of `audio`, where
      `labels[i]` contains arbitrary textual information about
      `audio[i]`. (For instance, this could be some text that a TTS
      system was supposed to produce.) Markdown is supported.
    max_outputs: Optional `int`. At most this many audio clips will be
      emitted. When more than `max_outputs` many clips are provided, the
      first `max_outputs` many clips will be used and the rest silently
      discarded.
    encoding: A constant `str` indicating the desired encoding. You
      can choose any format you like, as long as it's "wav". Please see
      the "API compatibility note" below.
    display_name: Optional name for this summary in TensorBoard, as a
      `str`. Defaults to `name`.
    description: Optional long-form description for this summary, as a
      `str`. Markdown is supported. Defaults to empty.

  Returns:
    A `tf.Summary` protobuf object.

  API compatibility note: The default value of the `encoding`
  argument is _not_ guaranteed to remain unchanged across TensorBoard
  versions. In the future, we will by default encode as FLAC instead of
  as WAV. If the specific format is important to you, please provide a
  file format explicitly.

def pb(name,
       audio,
       sample_rate,
       labels=None,
       max_outputs=3,
       encoding=None,
       display_name=None,
       description=None):
  """Create a legacy audio summary protobuf.

  This behaves as if you were to create an `op` with the same arguments
  (wrapped with constant tensors where appropriate) and then execute
  that summary op in a TensorFlow session.

  Arguments:
    name: A unique name for the generated summary node.
    audio: An `np.array` 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]`.
    sample_rate: An `int` that represents the sample rate, in Hz.
      Must be positive.
    labels: Optional list (or rank-1 `np.array`) of textstrings or UTF-8
      bytestrings whose length is the first dimension of `audio`, where
      `labels[i]` contains arbitrary textual information about
      `audio[i]`. (For instance, this could be some text that a TTS
      system was supposed to produce.) Markdown is supported.
    max_outputs: Optional `int`. At most this many audio clips will be
      emitted. When more than `max_outputs` many clips are provided, the
      first `max_outputs` many clips will be used and the rest silently
      discarded.
    encoding: A constant `str` indicating the desired encoding. You
      can choose any format you like, as long as it's "wav". Please see
      the "API compatibility note" below.
    display_name: Optional name for this summary in TensorBoard, as a
      `str`. Defaults to `name`.
    description: Optional long-form description for this summary, as a
      `str`. Markdown is supported. Defaults to empty.

  Returns:
    A `tf.Summary` protobuf object.

  API compatibility note: The default value of the `encoding`
  argument is _not_ guaranteed to remain unchanged across TensorBoard
  versions. In the future, we will by default encode as FLAC instead of
  as WAV. If the specific format is important to you, please provide a
  file format explicitly.
  """
  # TODO(nickfelt): remove on-demand imports once dep situation is fixed.
  import tensorflow.compat.v1 as tf

  audio = np.array(audio)
  if audio.ndim != 3:
    raise ValueError('Shape %r must have rank 3' % (audio.shape,))
  if encoding is None:
    encoding = 'wav'

  if encoding == 'wav':
    encoding = metadata.Encoding.Value('WAV')
    encoder = functools.partial(encoder_util.encode_wav,
                                samples_per_second=sample_rate)
  else:
    raise ValueError('Unknown encoding: %r' % encoding)

  limited_audio = audio[:max_outputs]
  if labels is None:
    limited_labels = [b''] * len(limited_audio)
  else:
    limited_labels = [tf.compat.as_bytes(label)
                      for label in labels[:max_outputs]]

  encoded_audio = [encoder(a) for a in limited_audio]
  content = np.array([encoded_audio, limited_labels]).transpose()
  tensor = tf.make_tensor_proto(content, dtype=tf.string)

  if display_name is None:
    display_name = name
  summary_metadata = metadata.create_summary_metadata(
      display_name=display_name,
      description=description,
      encoding=encoding)
  tf_summary_metadata = tf.SummaryMetadata.FromString(
      summary_metadata.SerializeToString())

  summary = tf.Summary()
  summary.value.add(tag='%s/audio_summary' % name,
                    metadata=tf_summary_metadata,
                    tensor=tensor)
  return summary