嘉渊 před 2 roky
rodič
revize
7f7c23c36f

+ 3 - 1
funasr/datasets/small_datasets/dataset.py

@@ -110,6 +110,7 @@ class ESPnetDataset(Dataset):
             float_dtype: str = "float32",
             float_dtype: str = "float32",
             int_dtype: str = "long",
             int_dtype: str = "long",
             dest_sample_rate: int = 16000,
             dest_sample_rate: int = 16000,
+            speed_perturb: tuple = None,
     ):
     ):
         assert check_argument_types()
         assert check_argument_types()
         if len(path_name_type_list) == 0:
         if len(path_name_type_list) == 0:
@@ -123,6 +124,7 @@ class ESPnetDataset(Dataset):
         self.float_dtype = float_dtype
         self.float_dtype = float_dtype
         self.int_dtype = int_dtype
         self.int_dtype = int_dtype
         self.dest_sample_rate = dest_sample_rate
         self.dest_sample_rate = dest_sample_rate
+        self.speed_perturb = speed_perturb
 
 
         self.loader_dict = {}
         self.loader_dict = {}
         self.debug_info = {}
         self.debug_info = {}
@@ -146,7 +148,7 @@ class ESPnetDataset(Dataset):
             loader_type:  loader_type. sound, npy, text, etc
             loader_type:  loader_type. sound, npy, text, etc
         """
         """
         if loader_type == "sound":
         if loader_type == "sound":
-            loader = SoundScpReader(path, self.dest_sample_rate, normalize=True, always_2d=False)
+            loader = SoundScpReader(path, self.dest_sample_rate, normalize=True, always_2d=False, speed_perturb=self.speed_perturb)
             return AdapterForSoundScpReader(loader, self.float_dtype)
             return AdapterForSoundScpReader(loader, self.float_dtype)
         elif loader_type == "kaldi_ark":
         elif loader_type == "kaldi_ark":
             loader = kaldiio.load_scp(path)
             loader = kaldiio.load_scp(path)

+ 12 - 0
funasr/fileio/sound_scp.py

@@ -2,11 +2,14 @@ import collections.abc
 from pathlib import Path
 from pathlib import Path
 from typing import Union
 from typing import Union
 
 
+import random
 import numpy as np
 import numpy as np
 import soundfile
 import soundfile
 import librosa
 import librosa
 from typeguard import check_argument_types
 from typeguard import check_argument_types
 
 
+import torchaudio
+
 from funasr.fileio.read_text import read_2column_text
 from funasr.fileio.read_text import read_2column_text
 
 
 
 
@@ -32,6 +35,7 @@ class SoundScpReader(collections.abc.Mapping):
         always_2d: bool = False,
         always_2d: bool = False,
         normalize: bool = False,
         normalize: bool = False,
         dest_sample_rate: int = 16000,
         dest_sample_rate: int = 16000,
+        speed_perturb: tuple = None,
     ):
     ):
         assert check_argument_types()
         assert check_argument_types()
         self.fname = fname
         self.fname = fname
@@ -40,6 +44,7 @@ class SoundScpReader(collections.abc.Mapping):
         self.normalize = normalize
         self.normalize = normalize
         self.data = read_2column_text(fname)
         self.data = read_2column_text(fname)
         self.dest_sample_rate = dest_sample_rate
         self.dest_sample_rate = dest_sample_rate
+        self.speed_perturb = speed_perturb
 
 
     def __getitem__(self, key):
     def __getitem__(self, key):
         wav = self.data[key]
         wav = self.data[key]
@@ -53,6 +58,13 @@ class SoundScpReader(collections.abc.Mapping):
                 wav, sr=self.dest_sample_rate, mono=not self.always_2d, dtype=self.dtype
                 wav, sr=self.dest_sample_rate, mono=not self.always_2d, dtype=self.dtype
             )
             )
 
 
+        if self.speed_perturb is not None:
+            speed = random.choice(self.speed_perturb)
+            if speed != 1.0:
+                array, _ = torchaudio.sox_effects.apply_effects_tensor(
+                    array, rate,
+                    [['speed', str(speed)], ['rate', str(rate)]])
+
         return rate, array
         return rate, array
 
 
     def get_path(self, key):
     def get_path(self, key):