Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] use librosa instead of scipy.wav #216

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
requirements = [
"tqdm",
"onnxruntime>=1.12.0",
"python-speech-features>=0.6",
"scipy>=1.5.2",
"librosa>=0.8.0",
]

setup(
Expand Down
32 changes: 15 additions & 17 deletions wespeaker/cli/speaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import argparse
import sys

import librosa
import numpy as np
import onnxruntime as ort
import scipy.io.wavfile as wav
import scipy.signal as sps
from numpy.linalg import norm

from wespeaker.cli.hub import Hub
Expand All @@ -32,21 +31,18 @@ def __init__(self, model_path: str, resample_rate: int = 16000):
self.table = {}

def extract_embedding(self, audio_path: str):
sample_rate, pcm = wav.read(audio_path)
if sample_rate != self.resample_rate:
# resample
number_of_samples = round(
len(pcm) * float(self.resample_rate) / sample_rate)
pcm = sps.resample(pcm, number_of_samples)
pcm, sample_rate = librosa.load(audio_path, sr=self.resample_rate)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要从-1~1,转到-65536~65536,
pcm = pcm * (1<<15)

pcm = pcm * (1 << 15)
# NOTE: produce the same results as with torchaudio.compliance.kaldi
feats = logfbank(pcm,
sample_rate,
nfilt=80,
lowfreq=20,
winlen=0.025, # 25 ms
winstep=0.01, # 10 ms
dither=0,
wintype='hamming')
feats = logfbank(
pcm,
sample_rate,
nfilt=80,
lowfreq=20,
winlen=0.025, # 25 ms
winstep=0.01, # 10 ms
dither=0,
wintype='hamming')
feats = feats - np.mean(feats, axis=0) # CMN
feats = np.expand_dims(feats, axis=0).astype(np.float32)
outputs = self.session.run(None, {"feats": feats})
Expand Down Expand Up @@ -109,7 +105,9 @@ def get_args():
parser.add_argument('--audio_file', help='audio file')
parser.add_argument('--audio_file2',
help='audio file2, for similarity task')
parser.add_argument('--resample_rate', type=int, default=16000,
parser.add_argument('--resample_rate',
type=int,
default=16000,
help='resampling rate')
args = parser.parse_args()
return args
Expand Down