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

Add float32 type casting for get_samples function #5399

Merged
merged 6 commits into from
Nov 22, 2022
Merged
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
10 changes: 4 additions & 6 deletions nemo/collections/asr/parts/utils/audio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
ChannelSelectorType = Union[int, Iterable[int], str]


def get_samples(audio_file: str, target_sr: int = 16000):
def get_samples(audio_file: str, target_sr: int = 16000, dtype: str = 'float32'):
"""
Read the samples from the given audio_file path. If not specified, the input audio file is automatically
resampled to 16kHz.
Expand All @@ -41,12 +41,10 @@ def get_samples(audio_file: str, target_sr: int = 16000):
Time-series sample data from the given audio file
"""
with sf.SoundFile(audio_file, 'r') as f:
sample_rate = f.samplerate
samples = f.read()
if sample_rate != target_sr:
samples = librosa.core.resample(samples, orig_sr=sample_rate, target_sr=target_sr)
samples = f.read(dtype=dtype)
if f.samplerate != target_sr:
samples = librosa.core.resample(samples, orig_sr=f.samplerate, target_sr=target_sr)
samples = samples.transpose()
del f
return samples


Expand Down