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
1fe0a40c
"tests/git@developer.sourcefind.cn:OpenDAS/apex.git" did not exist on "81f8ba7952712c80e260cdb80e3646caf0a94811"
Unverified
Commit
1fe0a40c
authored
Dec 01, 2020
by
moto
Committed by
GitHub
Dec 01, 2020
Browse files
Clean up handling of optional args in C++ with c10:optional (#1043)
parent
fb3ef9ba
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
29 additions
and
26 deletions
+29
-26
torchaudio/csrc/sox_effects.cpp
torchaudio/csrc/sox_effects.cpp
+6
-5
torchaudio/csrc/sox_effects.h
torchaudio/csrc/sox_effects.h
+2
-2
torchaudio/csrc/sox_io.cpp
torchaudio/csrc/sox_io.cpp
+17
-15
torchaudio/csrc/sox_io.h
torchaudio/csrc/sox_io.h
+4
-4
No files found.
torchaudio/csrc/sox_effects.cpp
View file @
1fe0a40c
...
...
@@ -91,8 +91,8 @@ c10::intrusive_ptr<TensorSignal> apply_effects_tensor(
c10
::
intrusive_ptr
<
TensorSignal
>
apply_effects_file
(
const
std
::
string
path
,
std
::
vector
<
std
::
vector
<
std
::
string
>>
effects
,
c
onst
bool
normalize
,
c
onst
bool
channels_first
)
{
c
10
::
optional
<
bool
>&
normalize
,
c
10
::
optional
<
bool
>&
channels_first
)
{
// Open input file
SoxFormat
sf
(
sox_open_read
(
path
.
c_str
(),
...
...
@@ -121,16 +121,17 @@ c10::intrusive_ptr<TensorSignal> apply_effects_file(
chain
.
run
();
// Create tensor from buffer
bool
channels_first_
=
channels_first
.
value_or
(
true
);
auto
tensor
=
convert_to_tensor
(
/*buffer=*/
out_buffer
.
data
(),
/*num_samples=*/
out_buffer
.
size
(),
/*num_channels=*/
chain
.
getOutputNumChannels
(),
dtype
,
normalize
,
channels_first
);
normalize
.
value_or
(
true
)
,
channels_first
_
);
return
c10
::
make_intrusive
<
TensorSignal
>
(
tensor
,
chain
.
getOutputSampleRate
(),
channels_first
);
tensor
,
chain
.
getOutputSampleRate
(),
channels_first
_
);
}
}
// namespace sox_effects
...
...
torchaudio/csrc/sox_effects.h
View file @
1fe0a40c
...
...
@@ -18,8 +18,8 @@ c10::intrusive_ptr<torchaudio::sox_utils::TensorSignal> apply_effects_tensor(
c10
::
intrusive_ptr
<
torchaudio
::
sox_utils
::
TensorSignal
>
apply_effects_file
(
const
std
::
string
path
,
std
::
vector
<
std
::
vector
<
std
::
string
>>
effects
,
c
onst
bool
normalize
=
true
,
c
onst
bool
channels_first
=
true
);
c
10
::
optional
<
bool
>&
normalize
,
c
10
::
optional
<
bool
>&
channels_first
);
}
// namespace sox_effects
}
// namespace torchaudio
...
...
torchaudio/csrc/sox_io.cpp
View file @
1fe0a40c
...
...
@@ -49,30 +49,32 @@ c10::intrusive_ptr<SignalInfo> get_info(const std::string& path) {
c10
::
intrusive_ptr
<
TensorSignal
>
load_audio_file
(
const
std
::
string
&
path
,
const
int64_t
frame_offset
,
const
int64_t
num_frames
,
const
bool
normalize
,
const
bool
channels_first
)
{
if
(
frame_offset
<
0
)
{
c10
::
optional
<
int64_t
>&
frame_offset
,
c10
::
optional
<
int64_t
>&
num_frames
,
c10
::
optional
<
bool
>&
normalize
,
c10
::
optional
<
bool
>&
channels_first
)
{
const
auto
offset
=
frame_offset
.
value_or
(
0
);
if
(
offset
<
0
)
{
throw
std
::
runtime_error
(
"Invalid argument: frame_offset must be non-negative."
);
}
if
(
num_frames
==
0
||
num_frames
<
-
1
)
{
const
auto
frames
=
num_frames
.
value_or
(
-
1
);
if
(
frames
==
0
||
frames
<
-
1
)
{
throw
std
::
runtime_error
(
"Invalid argument: num_frames must be -1 or greater than 0."
);
}
std
::
vector
<
std
::
vector
<
std
::
string
>>
effects
;
if
(
num_
frames
!=
-
1
)
{
std
::
ostringstream
offset
,
frames
;
offset
<<
frame_
offset
<<
"s"
;
frames
<<
"+"
<<
num_
frames
<<
"s"
;
if
(
frames
!=
-
1
)
{
std
::
ostringstream
os_
offset
,
os_
frames
;
os_
offset
<<
offset
<<
"s"
;
os_
frames
<<
"+"
<<
frames
<<
"s"
;
effects
.
emplace_back
(
std
::
vector
<
std
::
string
>
{
"trim"
,
offset
.
str
(),
frames
.
str
()});
}
else
if
(
frame_
offset
!=
0
)
{
std
::
ostringstream
offset
;
offset
<<
frame_
offset
<<
"s"
;
effects
.
emplace_back
(
std
::
vector
<
std
::
string
>
{
"trim"
,
offset
.
str
()});
std
::
vector
<
std
::
string
>
{
"trim"
,
os_
offset
.
str
(),
os_
frames
.
str
()});
}
else
if
(
offset
!=
0
)
{
std
::
ostringstream
os_
offset
;
os_
offset
<<
offset
<<
"s"
;
effects
.
emplace_back
(
std
::
vector
<
std
::
string
>
{
"trim"
,
os_
offset
.
str
()});
}
return
torchaudio
::
sox_effects
::
apply_effects_file
(
...
...
torchaudio/csrc/sox_io.h
View file @
1fe0a40c
...
...
@@ -25,10 +25,10 @@ c10::intrusive_ptr<SignalInfo> get_info(const std::string& path);
c10
::
intrusive_ptr
<
torchaudio
::
sox_utils
::
TensorSignal
>
load_audio_file
(
const
std
::
string
&
path
,
c
onst
int64_t
frame_offset
=
0
,
c
onst
int64_t
num_frames
=
-
1
,
c
onst
bool
normalize
=
true
,
c
onst
bool
channels_first
=
true
);
c
10
::
optional
<
int64_t
>&
frame_offset
,
c
10
::
optional
<
int64_t
>&
num_frames
,
c
10
::
optional
<
bool
>&
normalize
,
c
10
::
optional
<
bool
>&
channels_first
);
void
save_audio_file
(
const
std
::
string
&
file_name
,
...
...
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