-
Notifications
You must be signed in to change notification settings - Fork 221
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 chunked sph file processing #367
Merged
pzelasko
merged 7 commits into
lhotse-speech:master
from
videodanchik:feature/chunked-sph-file-load
Aug 14, 2021
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5179291
add chunked sph file processing
videodanchik e02f6ad
Merge branch 'master' into feature/chunked-sph-file-load
videodanchik 4e5890f
add check to subprocess run
videodanchik 40e8fda
move back soundfile local import
videodanchik 0b889c7
add missing sph file handling in recording creation
videodanchik bdf4ef2
Merge branch 'master' into feature/chunked-sph-file-load
videodanchik 6e35b57
Merge branch 'master' into feature/chunked-sph-file-load
pzelasko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
from typing import Any, Callable, Dict, Iterable, List, Mapping, NamedTuple, Optional, Sequence, Tuple, Union | ||
|
||
import numpy as np | ||
import soundfile as sf | ||
from tqdm.auto import tqdm | ||
|
||
from lhotse.augmentation import AudioTransform, Resample, Speed | ||
|
@@ -231,8 +232,7 @@ def from_file( | |
else: | ||
try: | ||
# Try to parse the file using pysoundfile first. | ||
import soundfile | ||
info = soundfile.info(str(path)) | ||
info = sf.info(str(path)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a missing case for using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, good catch. |
||
except: | ||
# Try to parse the file using audioread as a fallback. | ||
info = audioread_info(str(path)) | ||
|
@@ -815,8 +815,13 @@ def read_audio( | |
duration=duration, | ||
force_opus_sampling_rate=force_opus_sampling_rate, | ||
) | ||
elif isinstance(path_or_fd, (str, Path)) and str(path_or_fd).lower().endswith('.sph'): | ||
return read_sph( | ||
path_or_fd, | ||
offset=offset, | ||
duration=duration | ||
) | ||
try: | ||
import soundfile as sf | ||
with sf.SoundFile(path_or_fd) as sf_desc: | ||
sampling_rate = sf_desc.samplerate | ||
if offset > 0: | ||
|
@@ -1102,3 +1107,44 @@ def parse_channel_from_ffmpeg_output(ffmpeg_stderr: bytes) -> str: | |
f"Could not determine the number of channels for OPUS file from the following ffmpeg output " | ||
f"(shown as bytestring due to avoid possible encoding issues):\n{str(ffmpeg_stderr)}" | ||
) | ||
|
||
|
||
def sph_info(path: Pathlike) -> LibsndfileCompatibleAudioInfo: | ||
samples, sampling_rate = read_sph(path) | ||
return LibsndfileCompatibleAudioInfo( | ||
channels=samples.shape[0], | ||
frames=samples.shape[1], | ||
samplerate=sampling_rate, | ||
duration=samples.shape[1] / sampling_rate | ||
) | ||
|
||
|
||
def read_sph( | ||
sph_path: Pathlike, | ||
offset: Seconds = 0.0, | ||
duration: Optional[Seconds] = None | ||
) -> Tuple[np.ndarray, int]: | ||
""" | ||
Reads SPH files using sph2pipe in a shell subprocess. | ||
Unlike audioread, correctly supports offsets and durations for reading short chunks. | ||
|
||
:return: a tuple of audio samples and the sampling rate. | ||
""" | ||
|
||
sph_path = Path(sph_path) | ||
|
||
# Construct the sph2pipe command depending on the arguments passed. | ||
cmd = f'sph2pipe -f wav -p -t {offset}:' | ||
|
||
if duration is not None: | ||
cmd += f'{round(offset + duration, 5)}' | ||
# Add the input specifier after offset and duration. | ||
cmd += f' {sph_path}' | ||
|
||
# Actual audio reading. | ||
proc = BytesIO(run(cmd, shell=True, stdout=PIPE, stderr=PIPE).stdout) | ||
videodanchik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with sf.SoundFile(proc) as sf_desc: | ||
audio, sampling_rate = sf_desc.read(dtype=np.float32), sf_desc.samplerate | ||
audio = audio.reshape(1, -1) if sf_desc.channels == 1 else audio.T | ||
|
||
return audio, sampling_rate |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please stick to a local import, otherwise building the docs is going to fail (the import tries to load libsndfile.so into memory which is not available on read-the-docs servers).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry, will move it back