Skip to content

Commit fc9b189

Browse files
committed
Address PR feedbacks
1 parent 909610d commit fc9b189

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

torchaudio/backend/sox_io_backend.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ def load(
8787
filepath (path-like object or file-like object):
8888
Source of audio data. When the function is not compiled by TorchScript,
8989
(e.g. ``torch.jit.script``), the following types are accepted;
90-
* ``path-like object``: file path
91-
* ``file-like object``: Any object with ``read`` method that returns ``bytes``.
90+
* ``path-like``: file path
91+
* ``file-like``: Object with ``read(size: int) -> bytes`` method,
92+
which returns byte string of at most ``size`` length.
9293
When the function is compiled by TorchScript, only ``str`` type is allowed.
9394
9495
Note:

torchaudio/csrc/sox/effects.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ std::tuple<torch::Tensor, int64_t> apply_effects_fileobj(
178178
/*signal=*/nullptr,
179179
/*encoding=*/nullptr,
180180
/*filetype=*/format.has_value() ? format.value().c_str() : nullptr));
181-
validate_input_file(sf);
181+
validate_input_file(sf, /*check_length=*/false);
182182

183183
const auto dtype = get_dtype(sf->encoding.encoding, sf->signal.precision);
184184

torchaudio/csrc/sox/effects_chain.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void SoxEffectsChain::addInputTensor(TensorSignal* signal) {
198198
priv->signal = signal;
199199
priv->index = 0;
200200
if (sox_add_effect(sec_, e, &interm_sig_, &in_sig_) != SOX_SUCCESS) {
201-
throw std::runtime_error("Failed to add effect: input_tensor");
201+
throw std::runtime_error("Internal Error: Failed to add effect: input_tensor");
202202
}
203203
}
204204

@@ -207,7 +207,7 @@ void SoxEffectsChain::addOutputBuffer(
207207
SoxEffect e(sox_create_effect(get_tensor_output_handler()));
208208
static_cast<TensorOutputPriv*>(e->priv)->buffer = output_buffer;
209209
if (sox_add_effect(sec_, e, &interm_sig_, &in_sig_) != SOX_SUCCESS) {
210-
throw std::runtime_error("Failed to add effect: output_tensor");
210+
throw std::runtime_error("Internal Error: Failed to add effect: output_tensor");
211211
}
212212
}
213213

@@ -219,7 +219,7 @@ void SoxEffectsChain::addInputFile(sox_format_t* sf) {
219219
sox_effect_options(e, 1, opts);
220220
if (sox_add_effect(sec_, e, &interm_sig_, &in_sig_) != SOX_SUCCESS) {
221221
std::ostringstream stream;
222-
stream << "Failed to add effect: input " << sf->filename;
222+
stream << "Internal Error: Failed to add effect: input " << sf->filename;
223223
throw std::runtime_error(stream.str());
224224
}
225225
}
@@ -230,7 +230,7 @@ void SoxEffectsChain::addOutputFile(sox_format_t* sf) {
230230
static_cast<FileOutputPriv*>(e->priv)->sf = sf;
231231
if (sox_add_effect(sec_, e, &interm_sig_, &out_sig_) != SOX_SUCCESS) {
232232
std::ostringstream stream;
233-
stream << "Failed to add effect: output " << sf->filename;
233+
stream << "Internal Error: Failed to add effect: output " << sf->filename;
234234
throw std::runtime_error(stream.str());
235235
}
236236
}
@@ -266,7 +266,7 @@ void SoxEffectsChain::addEffect(const std::vector<std::string> effect) {
266266

267267
if (sox_add_effect(sec_, e, &interm_sig_, &in_sig_) != SOX_SUCCESS) {
268268
std::ostringstream stream;
269-
stream << "Failed to add effect: \"" << name;
269+
stream << "Internal Error: Failed to add effect: \"" << name;
270270
for (size_t i = 1; i < num_args; ++i) {
271271
stream << " " << effect[i];
272272
}
@@ -403,7 +403,7 @@ void SoxEffectsChain::addInputFileObj(
403403
priv->buffer = buffer;
404404
priv->buffer_size = buffer_size;
405405
if (sox_add_effect(sec_, e, &interm_sig_, &in_sig_) != SOX_SUCCESS) {
406-
throw std::runtime_error("Failed to add effect: input fileobj");
406+
throw std::runtime_error("Internal Error: Failed to add effect: input fileobj");
407407
}
408408
}
409409

torchaudio/csrc/sox/utils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,17 @@ SoxFormat::operator sox_format_t*() const noexcept {
9292
return fd_;
9393
}
9494

95-
void validate_input_file(const SoxFormat& sf) {
95+
void validate_input_file(const SoxFormat& sf, bool check_length) {
9696
if (static_cast<sox_format_t*>(sf) == nullptr) {
9797
throw std::runtime_error("Error loading audio file: failed to open file.");
9898
}
9999
if (sf->encoding.encoding == SOX_ENCODING_UNKNOWN) {
100100
throw std::runtime_error("Error loading audio file: unknown encoding.");
101101
}
102+
103+
if (check_length && sf->signal.length == 0) {
104+
throw std::runtime_error("Error reading audio file: unknown length.");
105+
}
102106
}
103107

104108
void validate_input_tensor(const torch::Tensor tensor) {

torchaudio/csrc/sox/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct SoxFormat {
6767

6868
///
6969
/// Verify that input file is found, has known encoding, and not empty
70-
void validate_input_file(const SoxFormat& sf);
70+
void validate_input_file(const SoxFormat& sf, bool check_length=true);
7171

7272
///
7373
/// Verify that input Tensor is 2D, CPU and either uin8, int16, int32 or float32

0 commit comments

Comments
 (0)