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
2fd32dd0
Unverified
Commit
2fd32dd0
authored
Jun 08, 2020
by
moto
Committed by
GitHub
Jun 08, 2020
Browse files
List backends dynamically based on availability (#697)
parent
e3247e30
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
22 deletions
+31
-22
test/common_utils.py
test/common_utils.py
+1
-1
torchaudio/_backend.py
torchaudio/_backend.py
+30
-21
No files found.
test/common_utils.py
View file @
2fd32dd0
...
@@ -10,7 +10,7 @@ from torch.testing._internal.common_utils import TestCase
...
@@ -10,7 +10,7 @@ from torch.testing._internal.common_utils import TestCase
import
torchaudio
import
torchaudio
_TEST_DIR_PATH
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
_TEST_DIR_PATH
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
BACKENDS
=
torchaudio
.
_backend
.
_
audio_backends
BACKENDS
=
torchaudio
.
_backend
.
_
BACKENDS
def
get_asset_path
(
*
paths
):
def
get_asset_path
(
*
paths
):
...
...
torchaudio/_backend.py
View file @
2fd32dd0
from
typing
import
Any
from
typing
import
Any
,
Optional
import
platform
from
torchaudio._internal
import
module_utils
as
_mod_utils
from
.
import
_soundfile_backend
,
_sox_backend
_BACKEND
=
None
_BACKENDS
=
{}
from
.
import
_soundfile_backend
,
_sox_backend
if
_mod_utils
.
is_module_available
(
'soundfile'
):
_BACKENDS
[
'soundfile'
]
=
_soundfile_backend
if
_mod_utils
.
is_module_available
(
'torchaudio._torchaudio'
):
_BACKENDS
[
'sox'
]
=
_sox_backend
if
'sox'
in
_BACKENDS
:
_BACKEND
=
'sox'
elif
'soundfile'
in
_BACKENDS
:
_BACKEND
=
'soundfile'
if
platform
.
system
()
==
"Windows"
:
_audio_backend
=
"soundfile"
def
list_audio_backends
():
_audio_backends
=
{
"soundfile"
:
_soundfile_backend
}
return
list
(
_BACKENDS
.
keys
())
else
:
_audio_backend
=
"sox"
_audio_backends
=
{
"sox"
:
_sox_backend
,
"soundfile"
:
_soundfile_backend
}
def
set_audio_backend
(
backend
:
str
)
->
None
:
def
set_audio_backend
(
backend
:
str
)
->
None
:
"""
"""
Specifies the package used to load.
Specifies the package used to load.
Args:
Args:
backend (str): Name of the backend. One of {}.
backend (str): Name of the backend. One of "sox" or "soundfile",
"""
.
format
(
_audio_backends
.
keys
())
based on availability of the system.
global
_audio_backend
"""
if
backend
not
in
_audio_backends
:
if
backend
not
in
_BACKENDS
:
raise
ValueError
(
raise
RuntimeError
(
"Invalid backend '{}'. Options are {}."
.
format
(
backend
,
_audio_backends
.
keys
())
f
'Backend "
{
backend
}
" is not one of '
)
f
'available backends:
{
list_audio_backends
()
}
.'
)
_audio_backend
=
backend
global
_BACKEND
_BACKEND
=
backend
def
get_audio_backend
()
->
str
:
def
get_audio_backend
()
->
Optional
[
str
]
:
"""
"""
Gets the name of the package used to load.
Gets the name of the package used to load.
"""
"""
return
_
audio_backend
return
_
BACKEND
def
_get_audio_backend_module
()
->
Any
:
def
_get_audio_backend_module
()
->
Any
:
"""
"""
Gets the module backend to load.
Gets the module backend to load.
"""
"""
backend
=
get_audio_backend
()
if
_BACKEND
is
None
:
return
_audio_backends
[
backend
]
raise
RuntimeError
(
'Backend is not initialized.'
)
return
_BACKENDS
[
_BACKEND
]
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