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
hehl2
Torchaudio
Commits
d272eb0f
"tools/vscode:/vscode.git/clone" did not exist on "37d13ae88ec1f92d80a7068ca810111257d71bc9"
Unverified
Commit
d272eb0f
authored
May 24, 2021
by
Denis Kokarev
Committed by
GitHub
May 24, 2021
Browse files
Add file path to io error messages (#1523)
parent
c4a17027
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
52 additions
and
10 deletions
+52
-10
test/torchaudio_unittest/backend/sox_io/info_test.py
test/torchaudio_unittest/backend/sox_io/info_test.py
+11
-0
test/torchaudio_unittest/backend/sox_io/load_test.py
test/torchaudio_unittest/backend/sox_io/load_test.py
+11
-0
test/torchaudio_unittest/backend/sox_io/save_test.py
test/torchaudio_unittest/backend/sox_io/save_test.py
+13
-0
torchaudio/csrc/sox/effects.cpp
torchaudio/csrc/sox/effects.cpp
+2
-2
torchaudio/csrc/sox/io.cpp
torchaudio/csrc/sox/io.cpp
+4
-5
torchaudio/csrc/sox/utils.cpp
torchaudio/csrc/sox/utils.cpp
+7
-2
torchaudio/csrc/sox/utils.h
torchaudio/csrc/sox/utils.h
+4
-1
No files found.
test/torchaudio_unittest/backend/sox_io/info_test.py
View file @
d272eb0f
...
...
@@ -476,3 +476,14 @@ class TestFileObjectHttp(HttpServerMixin, FileObjTestBase, PytorchTestCase):
assert
sinfo
.
num_frames
==
num_frames
assert
sinfo
.
bits_per_sample
==
bits_per_sample
assert
sinfo
.
encoding
==
get_encoding
(
ext
,
dtype
)
@
skipIfNoSox
class
TestInfoNoSuchFile
(
PytorchTestCase
):
def
test_info_fail
(
self
):
"""
When attempted to get info on a non-existing file, error message must contain the file path.
"""
path
=
"non_existing_audio.wav"
with
self
.
assertRaisesRegex
(
RuntimeError
,
"^Error loading audio file: failed to open file {0}$"
.
format
(
path
)):
sox_io_backend
.
info
(
path
)
test/torchaudio_unittest/backend/sox_io/load_test.py
View file @
d272eb0f
...
...
@@ -522,3 +522,14 @@ class TestFileObjectHttp(HttpServerMixin, PytorchTestCase):
assert
sr
==
sample_rate
self
.
assertEqual
(
expected
,
found
)
@
skipIfNoSox
class
TestLoadNoSuchFile
(
PytorchTestCase
):
def
test_load_fail
(
self
):
"""
When attempted to load a non-existing file, error message must contain the file path.
"""
path
=
"non_existing_audio.wav"
with
self
.
assertRaisesRegex
(
RuntimeError
,
"^Error loading audio file: failed to open file {0}$"
.
format
(
path
)):
sox_io_backend
.
load
(
path
)
test/torchaudio_unittest/backend/sox_io/save_test.py
View file @
d272eb0f
import
io
import
os
import
unittest
import
torch
from
torchaudio.backend
import
sox_io_backend
from
parameterized
import
parameterized
...
...
@@ -387,3 +389,14 @@ class TestSaveParams(TempDirMixin, PytorchTestCase):
sox_io_backend
.
save
(
path
,
data
,
8000
)
self
.
assertEqual
(
data
,
expected
)
@
skipIfNoSox
class
TestSaveNonExistingDirectory
(
PytorchTestCase
):
def
test_save_fail
(
self
):
"""
When attempted to save into a non-existing dir, error message must contain the file path.
"""
path
=
os
.
path
.
join
(
"non_existing_directory"
,
"foo.wav"
)
with
self
.
assertRaisesRegex
(
RuntimeError
,
"^Error saving audio file: failed to open file {0}$"
.
format
(
path
)):
sox_io_backend
.
save
(
path
,
torch
.
zeros
(
1
,
1
),
8000
)
torchaudio/csrc/sox/effects.cpp
View file @
d272eb0f
...
...
@@ -101,7 +101,7 @@ std::tuple<torch::Tensor, int64_t> apply_effects_file(
/*encoding=*/
nullptr
,
/*filetype=*/
format
.
has_value
()
?
format
.
value
().
c_str
()
:
nullptr
));
validate_input_file
(
sf
);
validate_input_file
(
sf
,
path
);
const
auto
dtype
=
get_dtype
(
sf
->
encoding
.
encoding
,
sf
->
signal
.
precision
);
...
...
@@ -204,7 +204,7 @@ std::tuple<torch::Tensor, int64_t> apply_effects_fileobj(
/*filetype=*/
format
.
has_value
()
?
format
.
value
().
c_str
()
:
nullptr
));
// In case of streamed data, length can be 0
validate_input_file
(
sf
);
validate_input_
mem
file
(
sf
);
// Prepare output buffer
std
::
vector
<
sox_sample_t
>
out_buffer
;
...
...
torchaudio/csrc/sox/io.cpp
View file @
d272eb0f
...
...
@@ -19,9 +19,7 @@ std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> get_info_file(
/*encoding=*/
nullptr
,
/*filetype=*/
format
.
has_value
()
?
format
.
value
().
c_str
()
:
nullptr
));
if
(
static_cast
<
sox_format_t
*>
(
sf
)
==
nullptr
)
{
throw
std
::
runtime_error
(
"Error opening audio file"
);
}
validate_input_file
(
sf
,
path
);
return
std
::
make_tuple
(
static_cast
<
int64_t
>
(
sf
->
signal
.
rate
),
...
...
@@ -123,7 +121,8 @@ void save_audio_file(
/*overwrite_permitted=*/
nullptr
));
if
(
static_cast
<
sox_format_t
*>
(
sf
)
==
nullptr
)
{
throw
std
::
runtime_error
(
"Error saving audio file: failed to open file."
);
throw
std
::
runtime_error
(
"Error saving audio file: failed to open file "
+
path
);
}
torchaudio
::
sox_effects_chain
::
SoxEffectsChain
chain
(
...
...
@@ -177,7 +176,7 @@ std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> get_info_fileobj(
/*filetype=*/
format
.
has_value
()
?
format
.
value
().
c_str
()
:
nullptr
));
// In case of streamed data, length can be 0
validate_input_file
(
sf
);
validate_input_
mem
file
(
sf
);
return
std
::
make_tuple
(
static_cast
<
int64_t
>
(
sf
->
signal
.
rate
),
...
...
torchaudio/csrc/sox/utils.cpp
View file @
d272eb0f
...
...
@@ -81,15 +81,20 @@ void SoxFormat::close() {
}
}
void
validate_input_file
(
const
SoxFormat
&
sf
)
{
void
validate_input_file
(
const
SoxFormat
&
sf
,
const
std
::
string
&
path
)
{
if
(
static_cast
<
sox_format_t
*>
(
sf
)
==
nullptr
)
{
throw
std
::
runtime_error
(
"Error loading audio file: failed to open file."
);
throw
std
::
runtime_error
(
"Error loading audio file: failed to open file "
+
path
);
}
if
(
sf
->
encoding
.
encoding
==
SOX_ENCODING_UNKNOWN
)
{
throw
std
::
runtime_error
(
"Error loading audio file: unknown encoding."
);
}
}
void
validate_input_memfile
(
const
SoxFormat
&
sf
)
{
return
validate_input_file
(
sf
,
"<in memory buffer>"
);
}
void
validate_input_tensor
(
const
torch
::
Tensor
tensor
)
{
if
(
!
tensor
.
device
().
is_cpu
())
{
throw
std
::
runtime_error
(
"Input tensor has to be on CPU."
);
...
...
torchaudio/csrc/sox/utils.h
View file @
d272eb0f
...
...
@@ -56,7 +56,10 @@ struct SoxFormat {
///
/// Verify that input file is found, has known encoding, and not empty
void
validate_input_file
(
const
SoxFormat
&
sf
);
void
validate_input_file
(
const
SoxFormat
&
sf
,
const
std
::
string
&
path
);
/// Verify that input memory buffer has known encoding, and not empty
void
validate_input_memfile
(
const
SoxFormat
&
sf
);
///
/// Verify that input Tensor is 2D, CPU and either uin8, int16, int32 or float32
...
...
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