docstr: Test loading and audio signal from a file.
          
docstr_tok: ["Test","loading","and","audio","signal","from","a","file","."]
code: def test_audio_segment_from_file(
      self, tmpdir, num_channels, channel_selectors, sample_rate
  ):
      """Test loading and audio signal from a file."""
      signal_len_sec = 4
      num_samples = signal_len_sec * sample_rate
      num_examples = 10
      rtol, atol = 1e-5, 1e-6
  
      for n in range(num_examples):
          # Create a test vector
          audio_file = os.path.join(tmpdir, f"test_audio_{n:02}.wav")
          samples = np.random.randn(num_samples, num_channels)
          sf.write(audio_file, samples, sample_rate, "float")
  
          for channel_selector in channel_selectors:
              if channel_selector is None:
                  ref_samples = samples
              elif isinstance(channel_selector, int) or isinstance(
                  channel_selector, list
              ):
                  ref_samples = samples[:, channel_selector]
              elif channel_selector == "average":
                  ref_samples = np.mean(samples, axis=1)
              else:
                  raise ValueError(
                      f"Unexpected value of channel_selector {channel_selector}"
                  )
  
              # 1) Load complete audio
              # Reference
              ref_samples = ref_samples.squeeze()
              ref_channels = (
                  1 if ref_samples.ndim == 1 else ref_samples.shape[1]
              )
  
              # UUT
              audio_segment = AudioSegment.from_file(
                  audio_file, channel_selector=channel_selector
              )
  
              # Test
              assert (
                  audio_segment.sample_rate == sample_rate
              ), f"channel_selector {channel_selector}, sample rate not matching: {audio_segment.sample_rate} != {sample_rate}"
              assert (
                  audio_segment.num_channels == ref_channels
              ), f"channel_selector {channel_selector}, num channels not matching: {audio_segment.num_channels} != {ref_channels}"
              assert audio_segment.num_samples == len(
                  ref_samples
              ), f"channel_selector {channel_selector}, num samples not matching: {audio_segment.num_samples} != {len(ref_samples)}"
              assert np.allclose(
                  audio_segment.samples,
                  ref_samples,
                  rtol=rtol,
                  atol=atol,
              ), f"channel_selector {channel_selector}, samples not matching"
  
              # 2) Load a random segment
              offset = 0.45 * np.random.rand() * signal_len_sec
              duration = 0.45 * np.random.rand() * signal_len_sec
  
              # Reference
              start = int(offset * sample_rate)
              end = start + int(duration * sample_rate)
              ref_samples = ref_samples[start:end, ...]
  
              # UUT
              audio_segment = AudioSegment.from_file(
                  audio_file,
                  offset=offset,
                  duration=duration,
                  channel_selector=channel_selector,
              )
  
              # Test
              assert (
                  audio_segment.sample_rate == sample_rate
              ), f"channel_selector {channel_selector}, offset {offset}, duration {duration}, sample rate not matching: {audio_segment.sample_rate} != {sample_rate}"
              assert (
                  audio_segment.num_channels == ref_channels
              ), f"channel_selector {channel_selector}, offset {offset}, duration {duration}, num channels not matching: {audio_segment.num_channels} != {ref_channels}"
              assert audio_segment.num_samples == len(
                  ref_samples
              ), f"channel_selector {channel_selector}, offset {offset}, duration {duration}, num samples not matching: {audio_segment.num_samples} != {len(ref_samples)}"
              assert np.allclose(
                  audio_segment.samples,
                  ref_samples,
                  rtol=rtol,
                  atol=atol,
              ), f"channel_selector {channel_selector}, offset {offset}, duration {duration}, samples not matching"
  
imports: import os
  from collections import namedtuple
  from typing import List, Type, Union
  import librosa
  import matplotlib.pyplot
  import numpy
  import pytest
  import scipy
  import soundfile
  import torch
  from nemo.collections.asr.parts.preprocessing.segment import AudioSegment
  from nemo.collections.asr.parts.utils.audio_utils import SOUND_VELOCITY
  from nemo.collections.asr.parts.utils.audio_utils import calculate_sdr_numpy, convmtx_mc_numpy, db2mag, estimated_coherence, generate_approximate_noise_field, get_segment_start, mag2db, pow2db, rms, select_channels, theoretical_coherence, toeplitz
label_desc: Use image node_id 1 for calling the TestAudioSegment obj's underlying member method code with example usage: obj.test_audio_segment_from_file(tmpdir, num_channels, channel_selectors, sample_rate) without return types