Skip to content

Commit

Permalink
Removed check_length from validate_input_file() (pytorch#1312)
Browse files Browse the repository at this point in the history
Some audio formats like `gsm` does not have valid frame numbers when opened. But `libsox` can properly handle these audios, so checking if `length > 0` is not necessary and too strict.

(cherry picked from commit 086467a)
  • Loading branch information
prabhat00155 committed Feb 24, 2021
1 parent 5784b81 commit 9372273
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 7 deletions.
2 changes: 1 addition & 1 deletion torchaudio/csrc/sox/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ std::tuple<torch::Tensor, int64_t> apply_effects_fileobj(
/*filetype=*/format.has_value() ? format.value().c_str() : nullptr));

// In case of streamed data, length can be 0
validate_input_file(sf, /*check_length=*/false);
validate_input_file(sf);

// Prepare output buffer
std::vector<sox_sample_t> out_buffer;
Expand Down
2 changes: 1 addition & 1 deletion torchaudio/csrc/sox/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> get_info_fileobj(
/*filetype=*/format.has_value() ? format.value().c_str() : nullptr));

// In case of streamed data, length can be 0
validate_input_file(sf, /*check_length=*/false);
validate_input_file(sf);

return std::make_tuple(
static_cast<int64_t>(sf->signal.rate),
Expand Down
5 changes: 1 addition & 4 deletions torchaudio/csrc/sox/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,13 @@ void SoxFormat::close() {
}
}

void validate_input_file(const SoxFormat& sf, bool check_length) {
void validate_input_file(const SoxFormat& sf) {
if (static_cast<sox_format_t*>(sf) == nullptr) {
throw std::runtime_error("Error loading audio file: failed to open file.");
}
if (sf->encoding.encoding == SOX_ENCODING_UNKNOWN) {
throw std::runtime_error("Error loading audio file: unknown encoding.");
}
if (check_length && sf->signal.length == 0) {
throw std::runtime_error("Error reading audio file: unknown length.");
}
}

void validate_input_tensor(const torch::Tensor tensor) {
Expand Down
2 changes: 1 addition & 1 deletion torchaudio/csrc/sox/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct SoxFormat {

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

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

0 comments on commit 9372273

Please sign in to comment.