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
f2da5861
"git@developer.sourcefind.cn:change/sglang.git" did not exist on "439f65809f7c917165cbb962d7a6bb5167ecdcf9"
Unverified
Commit
f2da5861
authored
Jan 06, 2021
by
moto
Committed by
GitHub
Jan 06, 2021
Browse files
Refactor sox source files (#1106)
parent
a7797d5c
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
193 additions
and
210 deletions
+193
-210
torchaudio/csrc/pybind.cpp
torchaudio/csrc/pybind.cpp
+2
-179
torchaudio/csrc/register.cpp
torchaudio/csrc/register.cpp
+3
-7
torchaudio/csrc/sox/effects.cpp
torchaudio/csrc/sox/effects.cpp
+3
-3
torchaudio/csrc/sox/effects.h
torchaudio/csrc/sox/effects.h
+1
-1
torchaudio/csrc/sox/effects_chain.cpp
torchaudio/csrc/sox/effects_chain.cpp
+2
-2
torchaudio/csrc/sox/effects_chain.h
torchaudio/csrc/sox/effects_chain.h
+1
-2
torchaudio/csrc/sox/io.cpp
torchaudio/csrc/sox/io.cpp
+4
-4
torchaudio/csrc/sox/io.h
torchaudio/csrc/sox/io.h
+1
-1
torchaudio/csrc/sox/legacy.cpp
torchaudio/csrc/sox/legacy.cpp
+174
-0
torchaudio/csrc/sox/legacy.h
torchaudio/csrc/sox/legacy.h
+1
-10
torchaudio/csrc/sox/utils.cpp
torchaudio/csrc/sox/utils.cpp
+1
-1
torchaudio/csrc/sox/utils.h
torchaudio/csrc/sox/utils.h
+0
-0
No files found.
torchaudio/csrc/
sox
.cpp
→
torchaudio/csrc/
pybind
.cpp
View file @
f2da5861
#include <torchaudio/csrc/sox.h>
#include <torch/extension.h>
#include <torchaudio/csrc/sox/legacy.h>
#include <algorithm>
#include <cstdint>
#include <stdexcept>
#include <vector>
namespace
torch
{
namespace
audio
{
namespace
{
/// Helper struct to safely close the sox_format_t descriptor.
struct
SoxDescriptor
{
explicit
SoxDescriptor
(
sox_format_t
*
fd
)
noexcept
:
fd_
(
fd
)
{}
SoxDescriptor
(
const
SoxDescriptor
&
other
)
=
delete
;
SoxDescriptor
(
SoxDescriptor
&&
other
)
=
delete
;
SoxDescriptor
&
operator
=
(
const
SoxDescriptor
&
other
)
=
delete
;
SoxDescriptor
&
operator
=
(
SoxDescriptor
&&
other
)
=
delete
;
~
SoxDescriptor
()
{
if
(
fd_
!=
nullptr
)
{
sox_close
(
fd_
);
}
}
sox_format_t
*
operator
->
()
noexcept
{
return
fd_
;
}
sox_format_t
*
get
()
noexcept
{
return
fd_
;
}
private:
sox_format_t
*
fd_
;
};
int64_t
write_audio
(
SoxDescriptor
&
fd
,
at
::
Tensor
tensor
)
{
std
::
vector
<
sox_sample_t
>
buffer
(
tensor
.
numel
());
AT_DISPATCH_ALL_TYPES
(
tensor
.
scalar_type
(),
"write_audio_buffer"
,
[
&
]
{
auto
*
data
=
tensor
.
data_ptr
<
scalar_t
>
();
std
::
copy
(
data
,
data
+
tensor
.
numel
(),
buffer
.
begin
());
});
const
auto
samples_written
=
sox_write
(
fd
.
get
(),
buffer
.
data
(),
buffer
.
size
());
return
samples_written
;
}
void
read_audio
(
SoxDescriptor
&
fd
,
at
::
Tensor
output
,
int64_t
buffer_length
)
{
std
::
vector
<
sox_sample_t
>
buffer
(
buffer_length
);
int
number_of_channels
=
fd
->
signal
.
channels
;
const
int64_t
samples_read
=
sox_read
(
fd
.
get
(),
buffer
.
data
(),
buffer_length
);
if
(
samples_read
==
0
)
{
throw
std
::
runtime_error
(
"Error reading audio file: empty file or read failed in sox_read"
);
}
output
.
resize_
({
samples_read
/
number_of_channels
,
number_of_channels
});
output
=
output
.
contiguous
();
AT_DISPATCH_ALL_TYPES
(
output
.
scalar_type
(),
"read_audio_buffer"
,
[
&
]
{
auto
*
data
=
output
.
data_ptr
<
scalar_t
>
();
std
::
copy
(
buffer
.
begin
(),
buffer
.
begin
()
+
samples_read
,
data
);
});
}
}
// namespace
std
::
tuple
<
sox_signalinfo_t
,
sox_encodinginfo_t
>
get_info
(
const
std
::
string
&
file_name
)
{
SoxDescriptor
fd
(
sox_open_read
(
file_name
.
c_str
(),
/*signal=*/
nullptr
,
/*encoding=*/
nullptr
,
/*filetype=*/
nullptr
));
if
(
fd
.
get
()
==
nullptr
)
{
throw
std
::
runtime_error
(
"Error opening audio file"
);
}
return
std
::
make_tuple
(
fd
->
signal
,
fd
->
encoding
);
}
int
read_audio_file
(
const
std
::
string
&
file_name
,
at
::
Tensor
output
,
bool
ch_first
,
int64_t
nframes
,
int64_t
offset
,
sox_signalinfo_t
*
si
,
sox_encodinginfo_t
*
ei
,
const
char
*
ft
)
{
SoxDescriptor
fd
(
sox_open_read
(
file_name
.
c_str
(),
si
,
ei
,
ft
));
if
(
fd
.
get
()
==
nullptr
)
{
throw
std
::
runtime_error
(
"Error opening audio file"
);
}
// signal info
const
int
number_of_channels
=
fd
->
signal
.
channels
;
const
int
sample_rate
=
fd
->
signal
.
rate
;
const
int64_t
total_length
=
fd
->
signal
.
length
;
// multiply offset and number of frames by number of channels
offset
*=
number_of_channels
;
nframes
*=
number_of_channels
;
if
(
total_length
==
0
)
{
throw
std
::
runtime_error
(
"Error reading audio file: unknown length"
);
}
if
(
offset
>
total_length
)
{
throw
std
::
runtime_error
(
"Offset past EOF"
);
}
// calculate buffer length
int64_t
buffer_length
=
total_length
;
if
(
offset
>
0
)
{
buffer_length
-=
offset
;
}
if
(
nframes
>
0
&&
buffer_length
>
nframes
)
{
buffer_length
=
nframes
;
}
// seek to offset point before reading data
if
(
sox_seek
(
fd
.
get
(),
offset
,
0
)
==
SOX_EOF
)
{
throw
std
::
runtime_error
(
"sox_seek reached EOF, try reducing offset or num_samples"
);
}
// read data and fill output tensor
read_audio
(
fd
,
output
,
buffer_length
);
// L x C -> C x L, if desired
if
(
ch_first
)
{
output
.
transpose_
(
1
,
0
);
}
return
sample_rate
;
}
void
write_audio_file
(
const
std
::
string
&
file_name
,
const
at
::
Tensor
&
tensor
,
sox_signalinfo_t
*
si
,
sox_encodinginfo_t
*
ei
,
const
char
*
file_type
)
{
if
(
!
tensor
.
is_contiguous
())
{
throw
std
::
runtime_error
(
"Error writing audio file: input tensor must be contiguous"
);
}
#if SOX_LIB_VERSION_CODE >= 918272 // >= 14.3.0
si
->
mult
=
nullptr
;
#endif
SoxDescriptor
fd
(
sox_open_write
(
file_name
.
c_str
(),
si
,
ei
,
file_type
,
/*oob=*/
nullptr
,
/*overwrite=*/
nullptr
));
if
(
fd
.
get
()
==
nullptr
)
{
throw
std
::
runtime_error
(
"Error writing audio file: could not open file for writing"
);
}
const
auto
samples_written
=
write_audio
(
fd
,
tensor
);
if
(
samples_written
!=
tensor
.
numel
())
{
throw
std
::
runtime_error
(
"Error writing audio file: could not write entire buffer"
);
}
}
}
// namespace audio
}
// namespace torch
PYBIND11_MODULE
(
_torchaudio
,
m
)
{
PYBIND11_MODULE
(
_torchaudio
,
m
)
{
py
::
class_
<
sox_signalinfo_t
>
(
m
,
"sox_signalinfo_t"
)
py
::
class_
<
sox_signalinfo_t
>
(
m
,
"sox_signalinfo_t"
)
...
...
torchaudio/csrc/register.cpp
View file @
f2da5861
#ifndef TORCHAUDIO_REGISTER_H
#include <torchaudio/csrc/sox/effects.h>
#define TORCHAUDIO_REGISTER_H
#include <torchaudio/csrc/sox/io.h>
#include <torchaudio/csrc/sox/utils.h>
#include <torchaudio/csrc/sox_effects.h>
#include <torchaudio/csrc/sox_io.h>
#include <torchaudio/csrc/sox_utils.h>
TORCH_LIBRARY
(
torchaudio
,
m
)
{
TORCH_LIBRARY
(
torchaudio
,
m
)
{
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
...
@@ -92,4 +89,3 @@ TORCH_LIBRARY(torchaudio, m) {
...
@@ -92,4 +89,3 @@ TORCH_LIBRARY(torchaudio, m) {
"int num_threads) -> int"
);
"int num_threads) -> int"
);
#endif
#endif
}
}
#endif
torchaudio/csrc/sox
_
effects.cpp
→
torchaudio/csrc/sox
/
effects.cpp
View file @
f2da5861
#include <sox.h>
#include <sox.h>
#include <torchaudio/csrc/sox
_
effects.h>
#include <torchaudio/csrc/sox
/
effects.h>
#include <torchaudio/csrc/sox
_
effects_chain.h>
#include <torchaudio/csrc/sox
/
effects_chain.h>
#include <torchaudio/csrc/sox
_
utils.h>
#include <torchaudio/csrc/sox
/
utils.h>
using
namespace
torchaudio
::
sox_utils
;
using
namespace
torchaudio
::
sox_utils
;
...
...
torchaudio/csrc/sox
_
effects.h
→
torchaudio/csrc/sox
/
effects.h
View file @
f2da5861
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
#define TORCHAUDIO_SOX_EFFECTS_H
#define TORCHAUDIO_SOX_EFFECTS_H
#include <torch/script.h>
#include <torch/script.h>
#include <torchaudio/csrc/sox
_
utils.h>
#include <torchaudio/csrc/sox
/
utils.h>
namespace
torchaudio
{
namespace
torchaudio
{
namespace
sox_effects
{
namespace
sox_effects
{
...
...
torchaudio/csrc/sox
_
effects_chain.cpp
→
torchaudio/csrc/sox
/
effects_chain.cpp
View file @
f2da5861
#include <torchaudio/csrc/sox
_
effects_chain.h>
#include <torchaudio/csrc/sox
/
effects_chain.h>
#include <torchaudio/csrc/sox
_
utils.h>
#include <torchaudio/csrc/sox
/
utils.h>
using
namespace
torch
::
indexing
;
using
namespace
torch
::
indexing
;
using
namespace
torchaudio
::
sox_utils
;
using
namespace
torchaudio
::
sox_utils
;
...
...
torchaudio/csrc/sox
_
effects_chain.h
→
torchaudio/csrc/sox
/
effects_chain.h
View file @
f2da5861
...
@@ -2,8 +2,7 @@
...
@@ -2,8 +2,7 @@
#define TORCHAUDIO_SOX_EFFECTS_CHAIN_H
#define TORCHAUDIO_SOX_EFFECTS_CHAIN_H
#include <sox.h>
#include <sox.h>
#include <torch/script.h>
#include <torchaudio/csrc/sox/utils.h>
#include <torchaudio/csrc/sox_utils.h>
namespace
torchaudio
{
namespace
torchaudio
{
namespace
sox_effects_chain
{
namespace
sox_effects_chain
{
...
...
torchaudio/csrc/sox
_
io.cpp
→
torchaudio/csrc/sox
/
io.cpp
View file @
f2da5861
#include <sox.h>
#include <sox.h>
#include <torchaudio/csrc/sox
_
effects.h>
#include <torchaudio/csrc/sox
/
effects.h>
#include <torchaudio/csrc/sox
_
effects_chain.h>
#include <torchaudio/csrc/sox
/
effects_chain.h>
#include <torchaudio/csrc/sox
_
io.h>
#include <torchaudio/csrc/sox
/
io.h>
#include <torchaudio/csrc/sox
_
utils.h>
#include <torchaudio/csrc/sox
/
utils.h>
using
namespace
torch
::
indexing
;
using
namespace
torch
::
indexing
;
using
namespace
torchaudio
::
sox_utils
;
using
namespace
torchaudio
::
sox_utils
;
...
...
torchaudio/csrc/sox
_
io.h
→
torchaudio/csrc/sox
/
io.h
View file @
f2da5861
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
#define TORCHAUDIO_SOX_IO_H
#define TORCHAUDIO_SOX_IO_H
#include <torch/script.h>
#include <torch/script.h>
#include <torchaudio/csrc/sox
_
utils.h>
#include <torchaudio/csrc/sox
/
utils.h>
namespace
torchaudio
{
namespace
torchaudio
{
namespace
sox_io
{
namespace
sox_io
{
...
...
torchaudio/csrc/sox/legacy.cpp
0 → 100644
View file @
f2da5861
#include <torchaudio/csrc/sox/legacy.h>
namespace
torch
{
namespace
audio
{
namespace
{
/// Helper struct to safely close the sox_format_t descriptor.
struct
SoxDescriptor
{
explicit
SoxDescriptor
(
sox_format_t
*
fd
)
noexcept
:
fd_
(
fd
)
{}
SoxDescriptor
(
const
SoxDescriptor
&
other
)
=
delete
;
SoxDescriptor
(
SoxDescriptor
&&
other
)
=
delete
;
SoxDescriptor
&
operator
=
(
const
SoxDescriptor
&
other
)
=
delete
;
SoxDescriptor
&
operator
=
(
SoxDescriptor
&&
other
)
=
delete
;
~
SoxDescriptor
()
{
if
(
fd_
!=
nullptr
)
{
sox_close
(
fd_
);
}
}
sox_format_t
*
operator
->
()
noexcept
{
return
fd_
;
}
sox_format_t
*
get
()
noexcept
{
return
fd_
;
}
private:
sox_format_t
*
fd_
;
};
int64_t
write_audio
(
SoxDescriptor
&
fd
,
at
::
Tensor
tensor
)
{
std
::
vector
<
sox_sample_t
>
buffer
(
tensor
.
numel
());
AT_DISPATCH_ALL_TYPES
(
tensor
.
scalar_type
(),
"write_audio_buffer"
,
[
&
]
{
auto
*
data
=
tensor
.
data_ptr
<
scalar_t
>
();
std
::
copy
(
data
,
data
+
tensor
.
numel
(),
buffer
.
begin
());
});
const
auto
samples_written
=
sox_write
(
fd
.
get
(),
buffer
.
data
(),
buffer
.
size
());
return
samples_written
;
}
void
read_audio
(
SoxDescriptor
&
fd
,
at
::
Tensor
output
,
int64_t
buffer_length
)
{
std
::
vector
<
sox_sample_t
>
buffer
(
buffer_length
);
int
number_of_channels
=
fd
->
signal
.
channels
;
const
int64_t
samples_read
=
sox_read
(
fd
.
get
(),
buffer
.
data
(),
buffer_length
);
if
(
samples_read
==
0
)
{
throw
std
::
runtime_error
(
"Error reading audio file: empty file or read failed in sox_read"
);
}
output
.
resize_
({
samples_read
/
number_of_channels
,
number_of_channels
});
output
=
output
.
contiguous
();
AT_DISPATCH_ALL_TYPES
(
output
.
scalar_type
(),
"read_audio_buffer"
,
[
&
]
{
auto
*
data
=
output
.
data_ptr
<
scalar_t
>
();
std
::
copy
(
buffer
.
begin
(),
buffer
.
begin
()
+
samples_read
,
data
);
});
}
}
// namespace
std
::
tuple
<
sox_signalinfo_t
,
sox_encodinginfo_t
>
get_info
(
const
std
::
string
&
file_name
)
{
SoxDescriptor
fd
(
sox_open_read
(
file_name
.
c_str
(),
/*signal=*/
nullptr
,
/*encoding=*/
nullptr
,
/*filetype=*/
nullptr
));
if
(
fd
.
get
()
==
nullptr
)
{
throw
std
::
runtime_error
(
"Error opening audio file"
);
}
return
std
::
make_tuple
(
fd
->
signal
,
fd
->
encoding
);
}
int
read_audio_file
(
const
std
::
string
&
file_name
,
at
::
Tensor
output
,
bool
ch_first
,
int64_t
nframes
,
int64_t
offset
,
sox_signalinfo_t
*
si
,
sox_encodinginfo_t
*
ei
,
const
char
*
ft
)
{
SoxDescriptor
fd
(
sox_open_read
(
file_name
.
c_str
(),
si
,
ei
,
ft
));
if
(
fd
.
get
()
==
nullptr
)
{
throw
std
::
runtime_error
(
"Error opening audio file"
);
}
// signal info
const
int
number_of_channels
=
fd
->
signal
.
channels
;
const
int
sample_rate
=
fd
->
signal
.
rate
;
const
int64_t
total_length
=
fd
->
signal
.
length
;
// multiply offset and number of frames by number of channels
offset
*=
number_of_channels
;
nframes
*=
number_of_channels
;
if
(
total_length
==
0
)
{
throw
std
::
runtime_error
(
"Error reading audio file: unknown length"
);
}
if
(
offset
>
total_length
)
{
throw
std
::
runtime_error
(
"Offset past EOF"
);
}
// calculate buffer length
int64_t
buffer_length
=
total_length
;
if
(
offset
>
0
)
{
buffer_length
-=
offset
;
}
if
(
nframes
>
0
&&
buffer_length
>
nframes
)
{
buffer_length
=
nframes
;
}
// seek to offset point before reading data
if
(
sox_seek
(
fd
.
get
(),
offset
,
0
)
==
SOX_EOF
)
{
throw
std
::
runtime_error
(
"sox_seek reached EOF, try reducing offset or num_samples"
);
}
// read data and fill output tensor
read_audio
(
fd
,
output
,
buffer_length
);
// L x C -> C x L, if desired
if
(
ch_first
)
{
output
.
transpose_
(
1
,
0
);
}
return
sample_rate
;
}
void
write_audio_file
(
const
std
::
string
&
file_name
,
const
at
::
Tensor
&
tensor
,
sox_signalinfo_t
*
si
,
sox_encodinginfo_t
*
ei
,
const
char
*
file_type
)
{
if
(
!
tensor
.
is_contiguous
())
{
throw
std
::
runtime_error
(
"Error writing audio file: input tensor must be contiguous"
);
}
#if SOX_LIB_VERSION_CODE >= 918272 // >= 14.3.0
si
->
mult
=
nullptr
;
#endif
SoxDescriptor
fd
(
sox_open_write
(
file_name
.
c_str
(),
si
,
ei
,
file_type
,
/*oob=*/
nullptr
,
/*overwrite=*/
nullptr
));
if
(
fd
.
get
()
==
nullptr
)
{
throw
std
::
runtime_error
(
"Error writing audio file: could not open file for writing"
);
}
const
auto
samples_written
=
write_audio
(
fd
,
tensor
);
if
(
samples_written
!=
tensor
.
numel
())
{
throw
std
::
runtime_error
(
"Error writing audio file: could not write entire buffer"
);
}
}
}
// namespace audio
}
// namespace torch
torchaudio/csrc/sox.h
→
torchaudio/csrc/sox
/legacy
.h
View file @
f2da5861
#include <sox.h>
#include <sox.h>
#include <torch/torch.h>
#include <string>
#include <tuple>
#include <vector>
#include <torch/extension.h>
namespace
at
{
struct
Tensor
;
}
// namespace at
namespace
torch
{
namespace
audio
{
namespace
torch
{
namespace
audio
{
...
...
torchaudio/csrc/sox
_
utils.cpp
→
torchaudio/csrc/sox
/
utils.cpp
View file @
f2da5861
#include <c10/core/ScalarType.h>
#include <c10/core/ScalarType.h>
#include <sox.h>
#include <sox.h>
#include <torchaudio/csrc/sox
_
utils.h>
#include <torchaudio/csrc/sox
/
utils.h>
namespace
torchaudio
{
namespace
torchaudio
{
namespace
sox_utils
{
namespace
sox_utils
{
...
...
torchaudio/csrc/sox
_
utils.h
→
torchaudio/csrc/sox
/
utils.h
View file @
f2da5861
File moved
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