Skip to content

Commit

Permalink
Change sox_io C++ return type to optional (#2416)
Browse files Browse the repository at this point in the history
Summary:
Preparation for upcoming change where load/info function will use fallback
if sox_io backend cannot handle the input.

Pull Request resolved: #2416

Reviewed By: carolineechen

Differential Revision: D36736969

Pulled By: mthrok

fbshipit-source-id: f804cfda3678f13bf0c2f6557a2f82ae42ae3c03
  • Loading branch information
mthrok authored and facebook-github-bot committed May 29, 2022
1 parent 65ab62e commit fd7ace1
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 18 deletions.
5 changes: 4 additions & 1 deletion torchaudio/backend/sox_io_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def info(
return AudioMetaData(*sinfo)
filepath = os.fspath(filepath)
sinfo = torch.ops.torchaudio.sox_io_get_info(filepath, format)
assert sinfo is not None # for TorchScript compatibility
return AudioMetaData(*sinfo)


Expand Down Expand Up @@ -148,9 +149,11 @@ def load(
filepath, frame_offset, num_frames, normalize, channels_first, format
)
filepath = os.fspath(filepath)
return torch.ops.torchaudio.sox_io_load_audio_file(
ret = torch.ops.torchaudio.sox_io_load_audio_file(
filepath, frame_offset, num_frames, normalize, channels_first, format
)
assert ret is not None # for TorchScript compatibility
return ret


@_mod_utils.requires_sox()
Expand Down
5 changes: 3 additions & 2 deletions torchaudio/csrc/pybind/sox/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ auto apply_effects_fileobj(
const std::vector<std::vector<std::string>>& effects,
c10::optional<bool> normalize,
c10::optional<bool> channels_first,
c10::optional<std::string> format) -> std::tuple<torch::Tensor, int64_t> {
c10::optional<std::string> format)
-> c10::optional<std::tuple<torch::Tensor, int64_t>> {
// Prepare the buffer used throughout the lifecycle of SoxEffectChain.
//
// For certain format (such as FLAC), libsox keeps reading the content at
Expand Down Expand Up @@ -110,7 +111,7 @@ auto apply_effects_fileobj(
normalize.value_or(true),
channels_first_);

return std::make_tuple(
return std::forward_as_tuple(
tensor, static_cast<int64_t>(chain.getOutputSampleRate()));
}

Expand Down
3 changes: 2 additions & 1 deletion torchaudio/csrc/pybind/sox/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ auto apply_effects_fileobj(
const std::vector<std::vector<std::string>>& effects,
c10::optional<bool> normalize,
c10::optional<bool> channels_first,
c10::optional<std::string> format) -> std::tuple<torch::Tensor, int64_t>;
c10::optional<std::string> format)
-> c10::optional<std::tuple<torch::Tensor, int64_t>>;

} // namespace torchaudio::sox_effects

Expand Down
7 changes: 4 additions & 3 deletions torchaudio/csrc/pybind/sox/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using namespace torchaudio::sox_utils;
namespace torchaudio::sox_io {

auto get_info_fileobj(py::object fileobj, c10::optional<std::string> format)
-> std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> {
-> c10::optional<MetaDataTuple> {
// Prepare in-memory file object
// When libsox opens a file, it also reads the header.
// When opening a file there are two functions that might touch FILE* (and the
Expand Down Expand Up @@ -63,7 +63,7 @@ auto get_info_fileobj(py::object fileobj, c10::optional<std::string> format)
// In case of streamed data, length can be 0
validate_input_memfile(sf);

return std::make_tuple(
return std::forward_as_tuple(
static_cast<int64_t>(sf->signal.rate),
static_cast<int64_t>(sf->signal.length / sf->signal.channels),
static_cast<int64_t>(sf->signal.channels),
Expand All @@ -77,7 +77,8 @@ auto load_audio_fileobj(
c10::optional<int64_t> num_frames,
c10::optional<bool> normalize,
c10::optional<bool> channels_first,
c10::optional<std::string> format) -> std::tuple<torch::Tensor, int64_t> {
c10::optional<std::string> format)
-> c10::optional<std::tuple<torch::Tensor, int64_t>> {
auto effects = get_effects(frame_offset, num_frames);
return torchaudio::sox_effects::apply_effects_fileobj(
std::move(fileobj),
Expand Down
8 changes: 6 additions & 2 deletions torchaudio/csrc/pybind/sox/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

namespace torchaudio::sox_io {

using MetaDataTuple =
std::tuple<int64_t, int64_t, int64_t, int64_t, std::string>;

auto get_info_fileobj(py::object fileobj, c10::optional<std::string> format)
-> std::tuple<int64_t, int64_t, int64_t, int64_t, std::string>;
-> c10::optional<MetaDataTuple>;

auto load_audio_fileobj(
py::object fileobj,
c10::optional<int64_t> frame_offset,
c10::optional<int64_t> num_frames,
c10::optional<bool> normalize,
c10::optional<bool> channels_first,
c10::optional<std::string> format) -> std::tuple<torch::Tensor, int64_t>;
c10::optional<std::string> format)
-> c10::optional<std::tuple<torch::Tensor, int64_t>>;

void save_audio_fileobj(
py::object fileobj,
Expand Down
2 changes: 1 addition & 1 deletion torchaudio/csrc/sox/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ auto apply_effects_file(
c10::optional<bool> normalize,
c10::optional<bool> channels_first,
const c10::optional<std::string>& format)
-> std::tuple<torch::Tensor, int64_t> {
-> c10::optional<std::tuple<torch::Tensor, int64_t>> {
// Open input file
SoxFormat sf(sox_open_read(
path.c_str(),
Expand Down
2 changes: 1 addition & 1 deletion torchaudio/csrc/sox/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ auto apply_effects_file(
c10::optional<bool> normalize,
c10::optional<bool> channels_first,
const c10::optional<std::string>& format)
-> std::tuple<torch::Tensor, int64_t>;
-> c10::optional<std::tuple<torch::Tensor, int64_t>>;

} // namespace torchaudio::sox_effects

Expand Down
7 changes: 3 additions & 4 deletions torchaudio/csrc/sox/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace torchaudio::sox_utils;
namespace torchaudio {
namespace sox_io {

std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> get_info_file(
c10::optional<MetaDataTuple> get_info_file(
const std::string& path,
const c10::optional<std::string>& format) {
SoxFormat sf(sox_open_read(
Expand All @@ -20,8 +20,7 @@ std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> get_info_file(
/*filetype=*/format.has_value() ? format.value().c_str() : nullptr));

validate_input_file(sf, path);

return std::make_tuple(
return std::forward_as_tuple(
static_cast<int64_t>(sf->signal.rate),
static_cast<int64_t>(sf->signal.length / sf->signal.channels),
static_cast<int64_t>(sf->signal.channels),
Expand Down Expand Up @@ -58,7 +57,7 @@ std::vector<std::vector<std::string>> get_effects(
return effects;
}

std::tuple<torch::Tensor, int64_t> load_audio_file(
c10::optional<std::tuple<torch::Tensor, int64_t>> load_audio_file(
const std::string& path,
const c10::optional<int64_t>& frame_offset,
const c10::optional<int64_t>& num_frames,
Expand Down
7 changes: 5 additions & 2 deletions torchaudio/csrc/sox/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ auto get_effects(
const c10::optional<int64_t>& num_frames)
-> std::vector<std::vector<std::string>>;

std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> get_info_file(
using MetaDataTuple =
std::tuple<int64_t, int64_t, int64_t, int64_t, std::string>;

c10::optional<MetaDataTuple> get_info_file(
const std::string& path,
const c10::optional<std::string>& format);

std::tuple<torch::Tensor, int64_t> load_audio_file(
c10::optional<std::tuple<torch::Tensor, int64_t>> load_audio_file(
const std::string& path,
const c10::optional<int64_t>& frame_offset,
const c10::optional<int64_t>& num_frames,
Expand Down
4 changes: 3 additions & 1 deletion torchaudio/sox_effects/sox_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,6 @@ def apply_effects_file(
if hasattr(path, "read"):
return torchaudio._torchaudio.apply_effects_fileobj(path, effects, normalize, channels_first, format)
path = os.fspath(path)
return torch.ops.torchaudio.sox_effects_apply_effects_file(path, effects, normalize, channels_first, format)
ret = torch.ops.torchaudio.sox_effects_apply_effects_file(path, effects, normalize, channels_first, format)
assert ret is not None
return ret

0 comments on commit fd7ace1

Please sign in to comment.