Пример #1
0
def _read_audio(
        cut: Cut,
        suppress_errors: bool = False,
        recording_field: Optional[str] = None) -> Optional[torch.Tensor]:
    """
    Loads audio data from cut, or returns None if there was an error
    and ``suppress_errors`` was set to ``True``.
    """
    with suppress_and_warn(
            AudioLoadingError,
            DurationMismatchError,
            NonPositiveEnergyError,
            enabled=suppress_errors,
    ):
        if recording_field is None:
            audio = cut.load_audio()
        else:
            attr = getattr(cut, recording_field)
            assert isinstance(
                attr, Recording
            ), f"Expected 'getattr(cut, {recording_field})' to yield Recording, got {type(attr)}"
            audio = cut.load_custom(recording_field)
        assert audio.shape[
            0] == 1, f"Expected single-channel audio in cut:\n{cut}"
        return torch.from_numpy(audio[0])
Пример #2
0
 def generate_cut(cuts: CutSet):
     for cut in cuts:
         with suppress_and_warn(AudioLoadingError,
                                DurationMismatchError):
             yield cut.compute_features(
                 extractor=self.feature_extractor,
                 augment_fn=self.augment_fn,
             )
Пример #3
0
def _read_audio(cut: Cut,
                suppress_errors: bool = False) -> Optional[torch.Tensor]:
    """
    Loads audio data from cut, or returns None if there was an error
    and ``suppress_errors`` was set to ``True``.
    """
    with suppress_and_warn(AudioLoadingError,
                           DurationMismatchError,
                           enabled=suppress_errors):
        return torch.from_numpy(cut.load_audio()[0])
Пример #4
0
    def write(self, manifest: MonoCut) -> bool:
        """
        Converts a Cut to a dict, pickles it, and then stores into a tarfile.

        :param manifest: the manifest to be written.
        :return: bool indicating whether the writing was successful.
        """
        with suppress_and_warn(Exception, enabled=self.fault_tolerant):
            cut = manifest.move_to_memory(
                audio_format=self.audio_format,
                load_audio=self.load_audio,
                load_features=self.load_features,
                load_custom=self.load_custom,
            )
            data = pickle.dumps(cut.to_dict())
            self.writer.write({"__key__": cut.id, "data": data})
            return True
        # Will get here if an exception happened.
        return False
Пример #5
0
 def __getitem__(self, cuts: CutSet) -> Dict[str, Any]:
     if self.collate:
         audio, audio_lens = collate_audio(cuts)
         return {
             "cuts": cuts,
             "audio": audio,
             "audio_lens": audio_lens,
         }
     else:
         remain_cuts = []
         remain_audios = []
         for c in cuts:
             with suppress_and_warn(AudioLoadingError,
                                    DurationMismatchError):
                 remain_audios.append(c.load_audio())
                 remain_cuts.append(c)
         return {
             "cuts": CutSet.from_cuts(remain_cuts),
             "audio": remain_audios
         }