class_docstr: Encode audio with a supported file encoding.
  
  Args:
      opts: The global options.
class_docstr_tok: ["Encode","audio","with","a","supported","file","encoding",".","Args",":","opts",":","The","global","options","."]
docstr: Serialize the input as file bytes.
  
  Args:
      data: A tensor of the form:
          {
              "samples": {"audio": tensor of shape [num_channels, sequence_length]},
              "metadata": {"audio_fps": the audio framerate.}
          }
  
  Returns:
      The transformed data.
docstr_tok: ["Serialize","the","input","as","file","bytes",".","Args",":","data",":","A","tensor","of","the","form",":","{","``","samples","''",":","{","``","audio","''",":","tensor","of","shape","[","num_channels",",","sequence_length","]","}",",","``","metadata","''",":","{","``","audio_fps","''",":","the","audio","framerate",".","}","}","Returns",":","The","transformed","data","."]
code: def __call__(
      self,
      data: Dict[
          str, Union[Dict[str, torch.Tensor], torch.Tensor, int]
      ],
  ) -> Dict[str, Union[Dict[str, torch.Tensor], torch.Tensor, int]]:
      """
      Serialize the input as file bytes.
  
      Args:
          data: A tensor of the form:
              {
                  "samples": {"audio": tensor of shape [num_channels, sequence_length]},
                  "metadata": {"audio_fps": the audio framerate.}
              }
  
      Returns:
          The transformed data.
      """
      x = data["samples"]["audio"]
      audio_fps = data["metadata"]["audio_fps"]
      if x.dim() == 2:
          # @x is [C, N] in shape. Convert to mono.
          if x.shape[0] in (1, 2):
              x = x.mean(dim=0)
          else:
              raise ValueError(
                  f"Expected x.shape[0] to be 1 or 2, got {x.shape}"
              )
      else:
          raise ValueError(
              f"Expected x.dim() == 2, got shape {x.shape}"
          )
  
      if self.format == "wav":
          file_bytes = _stream_to_wav(x, self.encoding_dtype, audio_fps)
          buf = np.frombuffer(file_bytes, dtype=np.uint8)
          # Convert to int32 so we can use negative values as padding.
          # The copy operation is required to avoid a warning about non-writable
          # tensors.
          buf = torch.from_numpy(buf.copy()).to(dtype=torch.int32)
          data["samples"]["audio"] = buf
  
      elif self.format == "mp3":
          if x.dim() == 1:
              x = x.reshape(1, -1)
          with tempfile.NamedTemporaryFile("rb+", suffix=".mp3") as f:
              # NOTE: If we instead save to a io.BytesIO() object, this
              # function will write an error message which cannot be
              # suppressed (even by contextlib.redirect_stdout). The only way
              # to avoid this appears to be to save to a file ending in
              # ".mp3". See also https://github.com/pytorch/audio/issues/2099
              torchaudio.save(f.name, x, audio_fps)
              byte_values = f.read()
          buf = np.frombuffer(byte_values, dtype=np.uint8)
          # Convert to int32 so we can use negative values as padding.
          # The copy operation is required to avoid a warning about non-writable
          # tensors.
          buf = torch.from_numpy(buf.copy()).to(dtype=torch.int32)
          data["samples"]["audio"] = buf
  
      else:
          raise NotImplementedError(
              f"Format {self.format} not implemented. Only 'wav' and 'mp3' are supported."
          )
  
      return data
  
file_path: ml-cvnets/data/transforms/audio_bytes.py
imports: import argparse
  import io
  import tempfile
  from typing import Dict, Union
  import numpy
  import torch
  import torchaudio
  from data.transforms import TRANSFORMATIONS_REGISTRY, BaseTransformation
label_desc: Use image node_id 2 for calling the TorchaudioSave obj's underlying member method code with example usage: obj.__call__(data) and returns: data