code: def add_background_noise(
      audio: Union[str, np.ndarray],
      sample_rate: int = DEFAULT_SAMPLE_RATE,
      background_audio: Optional[Union[str, np.ndarray]] = None,
      snr_level_db: float = 10.0,
      seed: Optional[audutils.RNGSeed] = None,
      output_path: Optional[str] = None,
      metadata: Optional[List[Dict[str, Any]]] = None,
  ) -> Tuple[np.ndarray, int]:
      """
      Mixes in a background sound into the audio
  
      @param audio: the path to the audio or a variable of type np.ndarray that
          will be augmented
  
      @param sample_rate: the audio sample rate of the inputted audio
  
      @param background_audio: the path to the background audio or a variable of type
          np.ndarray containing the background audio. If set to `None`, the background
          audio will be white noise
  
      @param snr_level_db: signal-to-noise ratio in dB
  
      @param seed: a NumPy random generator (or seed) such that the results
          remain reproducible
  
      @param output_path: the path in which the resulting audio will be stored. If None,
          the resulting np.ndarray will still be returned
  
      @param metadata: if set to be a list, metadata about the function execution
          including its name, the source & dest duration, sample rates, etc. will be
          appended to the inputted list. If set to None, no metadata will be appended
  
      @returns: the augmented audio array and sample rate
      """
      assert isinstance(
          snr_level_db, (int, float)
      ), "Expected 'snr_level_db' to be a number"
      audio, sample_rate = audutils.validate_and_load_audio(
          audio, sample_rate
      )
  
      if metadata is not None:
          func_kwargs = deepcopy(locals())
          func_kwargs.pop("metadata")
          func_kwargs.pop("seed")
  
      random_generator = audutils.check_random_state(seed)
  
      if background_audio is None:
          background_audio = random_generator.standard_normal(
              audio.shape
          )
      else:
          (
              background_audio,
              background_sr,
          ) = audutils.validate_and_load_audio(
              background_audio, sample_rate
          )
          if background_sr != sample_rate:
              background_audio = resample(
                  torch.tensor(background_audio),
                  background_sr,
                  sample_rate,
              ).numpy()
  
      if metadata is not None:
          func_kwargs["background_duration"] = (
              background_audio.shape[-1] / sample_rate
          )
  
      audio_rms = np.sqrt(np.mean(np.square(audio), axis=-1))
      bg_rms = np.sqrt(np.mean(np.square(background_audio), axis=-1))
      desired_bg_rms = audio_rms / (10 ** (snr_level_db / 20))
  
      if isinstance(bg_rms, np.number) and isinstance(
          desired_bg_rms, np.ndarray
      ):
          desired_bg_rms = desired_bg_rms.mean()
      elif isinstance(bg_rms, np.ndarray) and isinstance(
          desired_bg_rms, np.number
      ):
          bg_rms = bg_rms.mean()
      elif isinstance(bg_rms, np.ndarray) and isinstance(
          desired_bg_rms, np.ndarray
      ):
          bg_rms = bg_rms.reshape((bg_rms.shape[0], 1))
          desired_bg_rms = desired_bg_rms.reshape(
              (desired_bg_rms.shape[0], 1)
          )
          assert bg_rms.shape == desired_bg_rms.shape, (
              "Handling stereo audio and stereo background audio with different "
              "amounts of channels is currently unsupported"
          )
  
      background_audio *= desired_bg_rms / bg_rms
  
      while background_audio.shape[-1] < audio.shape[-1]:
          axis = 0 if background_audio.ndim == 1 else 1
          background_audio = np.concatenate(
              (background_audio, background_audio), axis=axis
          )
  
      background_audio = (
          background_audio[: audio.shape[-1]]
          if background_audio.ndim == 1
          else background_audio[:, : audio.shape[-1]]
      )
  
      aug_audio = audio + background_audio
      if metadata is not None:
          audutils.get_metadata(
              metadata=metadata,
              function_name="add_background_noise",
              dst_audio=aug_audio,
              dst_sample_rate=sample_rate,
              # pyre-fixme[61]: `func_kwargs` may not be initialized here.
              **func_kwargs,
          )
  
      return audutils.ret_and_save_audio(
          aug_audio, output_path, sample_rate
      )
  
imports: from copy import deepcopy
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  from augly.audio import utils
  from augly.utils import DEFAULT_SAMPLE_RATE
  from augly.utils.libsndfile import install_libsndfile
  import librosa
  from torchaudio import sox_effects
  from torchaudio.functional import fftconvolve, resample
label_desc: Use image node_id 1 for calling a global function with example usage: add_background_noise(audio, sample_rate, background_audio, snr_level_db, seed, output_path, metadata) and returns: audutils