From 4e37a5fda20f0878b593b8ba2b9ea46db63743b5 Mon Sep 17 00:00:00 2001
From: 嘉渊 <wangjiaming.wjm@alibaba-inc.com>
Date: 星期四, 11 五月 2023 14:16:28 +0800
Subject: [PATCH] update repo
---
funasr/fileio/sound_scp.py | 25 +++++++++++++++++++++----
1 files changed, 21 insertions(+), 4 deletions(-)
diff --git a/funasr/fileio/sound_scp.py b/funasr/fileio/sound_scp.py
index 459369e..f0ac053 100644
--- a/funasr/fileio/sound_scp.py
+++ b/funasr/fileio/sound_scp.py
@@ -2,9 +2,13 @@
from pathlib import Path
from typing import Union
+import random
import numpy as np
import soundfile
+import librosa
from typeguard import check_argument_types
+
+import torchaudio
from funasr.fileio.read_text import read_2column_text
@@ -30,6 +34,8 @@
dtype=np.int16,
always_2d: bool = False,
normalize: bool = False,
+ dest_sample_rate: int = 16000,
+ speed_perturb: tuple = None,
):
assert check_argument_types()
self.fname = fname
@@ -37,16 +43,27 @@
self.always_2d = always_2d
self.normalize = normalize
self.data = read_2column_text(fname)
+ self.dest_sample_rate = dest_sample_rate
+ self.speed_perturb = speed_perturb
def __getitem__(self, key):
wav = self.data[key]
if self.normalize:
# soundfile.read normalizes data to [-1,1] if dtype is not given
- array, rate = soundfile.read(wav, always_2d=self.always_2d)
- else:
- array, rate = soundfile.read(
- wav, dtype=self.dtype, always_2d=self.always_2d
+ array, rate = librosa.load(
+ wav, sr=self.dest_sample_rate, mono=not self.always_2d
)
+ else:
+ array, rate = librosa.load(
+ 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
--
Gitblit v1.9.1