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
c340a8d1
"...python/git@developer.sourcefind.cn:zhaoyu6/sglang.git" did not exist on "9b5efe34644de4106a5e6cf666e942f314e600d2"
Commit
c340a8d1
authored
Mar 12, 2018
by
Peter Goldsborough
Committed by
Soumith Chintala
Apr 25, 2018
Browse files
Conform to sox library a bit better
parent
cba11009
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
4 deletions
+13
-4
torchaudio/torch_sox.cpp
torchaudio/torch_sox.cpp
+13
-4
No files found.
torchaudio/torch_sox.cpp
View file @
c340a8d1
...
...
@@ -9,6 +9,9 @@
namespace
torch
{
namespace
audio
{
int
read_audio_file
(
const
std
::
string
&
file_name
,
at
::
Tensor
output
)
{
if
(
sox_init
()
!=
SOX_SUCCESS
)
{
throw
std
::
runtime_error
(
"Error initializing sox library"
);
}
sox_format_t
*
fd
=
sox_open_read
(
file_name
.
c_str
(),
/*signal=*/
nullptr
,
...
...
@@ -25,7 +28,7 @@ int read_audio_file(const std::string& file_name, at::Tensor output) {
throw
std
::
runtime_error
(
"Error reading audio file: unknown length"
);
}
std
::
vector
<
int32
_t
>
buffer
(
buffer_length
);
std
::
vector
<
sox_sample
_t
>
buffer
(
buffer_length
);
const
int64_t
samples_read
=
sox_read
(
fd
,
buffer
.
data
(),
buffer_length
);
if
(
samples_read
==
0
)
{
throw
std
::
runtime_error
(
...
...
@@ -40,6 +43,8 @@ int read_audio_file(const std::string& file_name, at::Tensor output) {
std
::
copy
(
buffer
.
begin
(),
buffer
.
begin
()
+
samples_read
,
data
);
});
sox_quit
();
return
sample_rate
;
}
...
...
@@ -48,12 +53,15 @@ void write_audio_file(
at
::
Tensor
tensor
,
const
std
::
string
&
extension
,
int
sample_rate
)
{
if
(
sox_init
()
!=
SOX_SUCCESS
)
{
throw
std
::
runtime_error
(
"Error initializing sox library"
);
}
if
(
!
tensor
.
is_contiguous
())
{
throw
std
::
runtime_error
(
"Error writing audio file: input tensor must be contiguous"
);
}
// Create sox objects and write into int32_t buffer.
sox_signalinfo_t
signal
;
signal
.
rate
=
sample_rate
;
signal
.
channels
=
tensor
.
size
(
1
);
...
...
@@ -77,7 +85,7 @@ void write_audio_file(
"Error writing audio file: could not open file for writing"
);
}
std
::
vector
<
int32
_t
>
buffer
(
tensor
.
numel
());
std
::
vector
<
sox_sample
_t
>
buffer
(
tensor
.
numel
());
AT_DISPATCH_ALL_TYPES
(
tensor
.
type
(),
"write_audio_buffer"
,
[
&
]
{
auto
*
data
=
tensor
.
data
<
scalar_t
>
();
...
...
@@ -86,8 +94,9 @@ void write_audio_file(
const
auto
samples_written
=
sox_write
(
fd
,
buffer
.
data
(),
buffer
.
size
());
// Free buffer and sox structures.
// Free buffer and sox structures
(before possibly throwing)
.
sox_close
(
fd
);
sox_quit
();
if
(
samples_written
!=
buffer
.
size
())
{
throw
std
::
runtime_error
(
...
...
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