Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
Torchaudio
Commits
f1ed19ad
Unverified
Commit
f1ed19ad
authored
Jun 05, 2020
by
moto
Committed by
GitHub
Jun 05, 2020
Browse files
Apply module availability guard to soundfile backend (#696)
parent
4d52106f
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
6 deletions
+11
-6
torchaudio/_soundfile_backend.py
torchaudio/_soundfile_backend.py
+11
-6
No files found.
torchaudio/_soundfile_backend.py
View file @
f1ed19ad
import
os
from
typing
import
Any
,
Optional
,
Tuple
,
Union
from
typing
import
Any
,
Optional
,
Tuple
import
torch
from
torch
import
Tensor
from
torchaudio._internal
import
misc_ops
as
_misc_ops
from
torchaudio._internal
import
(
module_utils
as
_mod_utils
,
misc_ops
as
_misc_ops
,
)
if
_mod_utils
.
is_module_available
(
'soundfile'
):
import
soundfile
_subtype_to_precision
=
{
...
...
@@ -46,6 +52,7 @@ class EncodingInfo:
self
.
opposite_endian
=
opposite_endian
@
_mod_utils
.
requires_module
(
'soundfile'
)
def
load
(
filepath
:
str
,
out
:
Optional
[
Tensor
]
=
None
,
normalization
:
Optional
[
bool
]
=
True
,
...
...
@@ -76,8 +83,6 @@ def load(filepath: str,
if
offset
<
0
:
raise
ValueError
(
"Expected positive offset value"
)
import
soundfile
# initialize output tensor
# TODO call libsoundfile directly to avoid numpy
out
,
sample_rate
=
soundfile
.
read
(
...
...
@@ -94,6 +99,7 @@ def load(filepath: str,
return
out
,
sample_rate
@
_mod_utils
.
requires_module
(
'soundfile'
)
def
save
(
filepath
:
str
,
src
:
Tensor
,
sample_rate
:
int
,
precision
:
int
=
16
,
channels_first
:
bool
=
True
)
->
None
:
r
"""See torchaudio.save"""
...
...
@@ -123,14 +129,13 @@ def save(filepath: str, src: Tensor, sample_rate: int, precision: int = 16, chan
precision
=
"PCM_S8"
if
precision
==
8
else
"PCM_"
+
str
(
precision
)
import
soundfile
return
soundfile
.
write
(
filepath
,
src
,
sample_rate
,
precision
)
@
_mod_utils
.
requires_module
(
'soundfile'
)
def
info
(
filepath
:
str
)
->
Tuple
[
SignalInfo
,
EncodingInfo
]:
r
"""See torchaudio.info"""
import
soundfile
sfi
=
soundfile
.
info
(
filepath
)
precision
=
_subtype_to_precision
[
sfi
.
subtype
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment