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
948b5a5c
Commit
948b5a5c
authored
Dec 13, 2018
by
David Pollack
Committed by
Soumith Chintala
Dec 25, 2018
Browse files
fixed temp file effects bug for mac
parent
e95c8f5a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
14 deletions
+19
-14
test/__init__.py
test/__init__.py
+0
-0
test/test_sox_effects.py
test/test_sox_effects.py
+6
-4
torchaudio/torch_sox.cpp
torchaudio/torch_sox.cpp
+13
-10
No files found.
test/__init__.py
0 → 100644
View file @
948b5a5c
test/test_sox_effects.py
View file @
948b5a5c
...
@@ -70,18 +70,20 @@ class Test_SoxEffectsChain(unittest.TestCase):
...
@@ -70,18 +70,20 @@ class Test_SoxEffectsChain(unittest.TestCase):
E
.
append_effect_to_chain
(
"rate"
,
[
si_in
.
rate
])
E
.
append_effect_to_chain
(
"rate"
,
[
si_in
.
rate
])
E
.
append_effect_to_chain
(
"channels"
,
[
si_in
.
channels
])
E
.
append_effect_to_chain
(
"channels"
,
[
si_in
.
channels
])
x
,
sr
=
E
.
sox_build_flow_effects
()
x
,
sr
=
E
.
sox_build_flow_effects
()
#print(x.size(), sr)
# The chorus effect will make the output file longer than the input
self
.
assertGreater
(
x
.
size
(
1
)
*
x
.
size
(
0
),
si_in
.
length
)
def
test_synth
(
self
):
def
test_synth
(
self
):
si_in
,
ei_in
=
torchaudio
.
info
(
self
.
test_filepath
)
si_in
,
ei_in
=
torchaudio
.
info
(
self
.
test_filepath
)
len_in_seconds
=
si_in
.
length
/
si_in
.
channels
/
si_in
.
rate
ei_in
.
encoding
=
torchaudio
.
get_sox_encoding_t
(
1
)
ei_in
.
encoding
=
torchaudio
.
get_sox_encoding_t
(
1
)
E
=
torchaudio
.
sox_effects
.
SoxEffectsChain
(
out_encinfo
=
ei_in
,
out_siginfo
=
si_in
)
E
=
torchaudio
.
sox_effects
.
SoxEffectsChain
(
out_encinfo
=
ei_in
,
out_siginfo
=
si_in
)
E
.
set_input_file
(
self
.
test_filepath
)
E
.
set_input_file
(
self
.
test_filepath
)
E
.
append_effect_to_chain
(
"synth"
,
[
"1"
,
"pinknoise"
,
"mix"
])
E
.
append_effect_to_chain
(
"synth"
,
[
str
(
len_in_seconds
)
,
"pinknoise"
,
"mix"
])
E
.
append_effect_to_chain
(
"rate"
,
[
44100
])
E
.
append_effect_to_chain
(
"rate"
,
[
44100
])
E
.
append_effect_to_chain
(
"channels"
,
[
2
])
E
.
append_effect_to_chain
(
"channels"
,
[
2
])
x
,
sr
=
E
.
sox_build_flow_effects
()
x
,
sr
=
E
.
sox_build_flow_effects
()
#print(x.size(), sr
)
self
.
assertEqual
(
si_in
.
length
,
x
.
size
(
0
)
*
x
.
size
(
1
)
)
def
test_gain
(
self
):
def
test_gain
(
self
):
E
=
torchaudio
.
sox_effects
.
SoxEffectsChain
()
E
=
torchaudio
.
sox_effects
.
SoxEffectsChain
()
...
@@ -120,7 +122,7 @@ class Test_SoxEffectsChain(unittest.TestCase):
...
@@ -120,7 +122,7 @@ class Test_SoxEffectsChain(unittest.TestCase):
x_orig
,
_
=
torchaudio
.
load
(
self
.
test_filepath
)
x_orig
,
_
=
torchaudio
.
load
(
self
.
test_filepath
)
offset
=
"10000s"
offset
=
"10000s"
offset_int
=
int
(
offset
[:
-
1
])
offset_int
=
int
(
offset
[:
-
1
])
num_frames
=
"200s"
num_frames
=
"200
00
s"
num_frames_int
=
int
(
num_frames
[:
-
1
])
num_frames_int
=
int
(
num_frames
[:
-
1
])
E
=
torchaudio
.
sox_effects
.
SoxEffectsChain
()
E
=
torchaudio
.
sox_effects
.
SoxEffectsChain
()
E
.
set_input_file
(
self
.
test_filepath
)
E
.
set_input_file
(
self
.
test_filepath
)
...
...
torchaudio/torch_sox.cpp
View file @
948b5a5c
#include <torch/
torch
.h>
#include <torch/
extension
.h>
#include <sox.h>
#include <sox.h>
...
@@ -266,7 +266,7 @@ int build_flow_effects(const std::string& file_name,
...
@@ -266,7 +266,7 @@ int build_flow_effects(const std::string& file_name,
int
tmp_fd
=
mkstemp
(
tmp_name
);
int
tmp_fd
=
mkstemp
(
tmp_name
);
close
(
tmp_fd
);
close
(
tmp_fd
);
sox_format_t
*
output
=
sox_open_write
(
tmp_name
,
target_signal
,
sox_format_t
*
output
=
sox_open_write
(
tmp_name
,
target_signal
,
target_encoding
,
file_type
,
nullptr
,
nullptr
);
target_encoding
,
"wav"
,
nullptr
,
nullptr
);
#else
#else
// create buffer and buffer_size for output in memwrite
// create buffer and buffer_size for output in memwrite
char
*
buffer
;
char
*
buffer
;
...
@@ -331,24 +331,27 @@ int build_flow_effects(const std::string& file_name,
...
@@ -331,24 +331,27 @@ int build_flow_effects(const std::string& file_name,
int
sr
;
int
sr
;
// Read the in-memory audio buffer or temp file that we just wrote.
// Read the in-memory audio buffer or temp file that we just wrote.
#ifdef __APPLE__
#ifdef __APPLE__
/* certain effects will result in a target signal length of 0.
if (target_signal->length > 0) {
if (target_signal->length > 0) {
if (target_signal->channels != output->signal.channels) {
if (target_signal->channels != output->signal.channels) {
//std::cout << "output: " << output->signal.channels << "|" << output->signal.length << "\n";
std::cout << "output: " << output->signal.channels << "|" << output->signal.length << "\n";
//std::cout << "target: " << target_signal->channels << "|" << target_signal->length << "\n";
std::cout << "interm: " << interm_signal.channels << "|" << interm_signal.length << "\n";
std::cout << "target: " << target_signal->channels << "|" << target_signal->length << "\n";
unlink(tmp_name);
unlink(tmp_name);
throw std::runtime_error("unexpected number of audio channels");
throw std::runtime_error("unexpected number of audio channels");
}
}
sr
=
read_audio_file
(
tmp_name
,
otensor
,
ch_first
,
0
,
0
,
&
output
->
signal
,
&
output
->
encoding
,
file_type
);
}
else
{
sr
=
read_audio_file
(
tmp_name
,
otensor
,
ch_first
,
0
,
0
,
target_signal
,
target_encoding
,
file_type
);
}
}
*/
// read_audio_file reads the temporary file and returns the sr and otensor
sr
=
read_audio_file
(
tmp_name
,
otensor
,
ch_first
,
0
,
0
,
target_signal
,
target_encoding
,
"wav"
);
// delete temporary audio file
unlink
(
tmp_name
);
unlink
(
tmp_name
);
#else
#else
// Resize output tensor to desired dimensions, different effects result in output->signal.length,
// Resize output tensor to desired dimensions, different effects result in output->signal.length,
// interm_signal.length and buffer size being inconsistent with the result of the file output.
// interm_signal.length and buffer size being inconsistent with the result of the file output.
// We prioritize in the order: output->signal.length > interm_signal.length > buffer_size
// We prioritize in the order: output->signal.length > interm_signal.length > buffer_size
// Could be related to: https://sourceforge.net/p/sox/bugs/314/
int
nc
,
ns
;
int
nc
,
ns
;
if
(
output
->
signal
.
length
==
0
)
{
if
(
output
->
signal
.
length
==
0
)
{
if
(
interm_signal
.
length
>
(
buffer_size
*
10
))
{
if
(
interm_signal
.
length
>
(
buffer_size
*
10
))
{
...
@@ -382,7 +385,7 @@ int build_flow_effects(const std::string& file_name,
...
@@ -382,7 +385,7 @@ int build_flow_effects(const std::string& file_name,
// free buffer
// free buffer
free
(
buffer
);
free
(
buffer
);
#endif
#endif
// return sample rate, output tensor modified in-place
return
sr
;
return
sr
;
}
}
}
// namespace audio
}
// namespace audio
...
...
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