Skip to content

Commit

Permalink
Use const reference
Browse files Browse the repository at this point in the history
Summary: Adopt more of const reference in sox source code.

Differential Revision: D46264068

fbshipit-source-id: d474ba1c9d87c924f3b893fba0ff98ecb412cdc8
  • Loading branch information
mthrok committed May 30, 2023
1 parent d974131 commit 393a49f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions torchaudio/csrc/sox/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ std::string to_string(Encoding v) {
}
}

Encoding get_encoding_from_option(const c10::optional<std::string> encoding) {
Encoding get_encoding_from_option(const c10::optional<std::string>& encoding) {
if (!encoding.has_value())
return Encoding::NOT_PROVIDED;
std::string v = encoding.value();
Expand All @@ -74,7 +74,7 @@ Encoding get_encoding_from_option(const c10::optional<std::string> encoding) {
TORCH_CHECK(false, "Internal Error: unexpected encoding value: ", v);
}

BitDepth get_bit_depth_from_option(const c10::optional<int64_t> bit_depth) {
BitDepth get_bit_depth_from_option(const c10::optional<int64_t>& bit_depth) {
if (!bit_depth.has_value())
return BitDepth::NOT_PROVIDED;
int64_t v = bit_depth.value();
Expand Down
4 changes: 2 additions & 2 deletions torchaudio/csrc/sox/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum class Encoding {
};

std::string to_string(Encoding v);
Encoding get_encoding_from_option(const c10::optional<std::string> encoding);
Encoding get_encoding_from_option(const c10::optional<std::string>& encoding);

enum class BitDepth : unsigned {
NOT_PROVIDED = 0,
Expand All @@ -49,7 +49,7 @@ enum class BitDepth : unsigned {
B64 = 64,
};

BitDepth get_bit_depth_from_option(const c10::optional<int64_t> bit_depth);
BitDepth get_bit_depth_from_option(const c10::optional<int64_t>& bit_depth);

std::string get_encoding(sox_encoding_t encoding);

Expand Down
22 changes: 11 additions & 11 deletions torchaudio/csrc/sox/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void validate_input_file(const SoxFormat& sf, const std::string& path) {
"Error loading audio file: unknown encoding.");
}

void validate_input_tensor(const torch::Tensor tensor) {
void validate_input_tensor(const torch::Tensor& tensor) {
TORCH_CHECK(tensor.device().is_cpu(), "Input tensor has to be on CPU.");

TORCH_CHECK(tensor.ndimension() == 2, "Input tensor has to be 2D.");
Expand Down Expand Up @@ -184,7 +184,7 @@ torch::Tensor convert_to_tensor(
return t.contiguous();
}

const std::string get_filetype(const std::string path) {
const std::string get_filetype(const std::string& path) {
std::string ext = path.substr(path.find_last_of(".") + 1);
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
return ext;
Expand Down Expand Up @@ -278,9 +278,9 @@ std::tuple<sox_encoding_t, unsigned> get_save_encoding_for_wav(

std::tuple<sox_encoding_t, unsigned> get_save_encoding(
const std::string& format,
const caffe2::TypeMeta dtype,
const c10::optional<std::string> encoding,
const c10::optional<int64_t> bits_per_sample) {
const caffe2::TypeMeta& dtype,
const c10::optional<std::string>& encoding,
const c10::optional<int64_t>& bits_per_sample) {
const Format fmt = get_format_from_string(format);
const Encoding enc = get_encoding_from_option(encoding);
const BitDepth bps = get_bit_depth_from_option(bits_per_sample);
Expand Down Expand Up @@ -385,7 +385,7 @@ std::tuple<sox_encoding_t, unsigned> get_save_encoding(
}
}

unsigned get_precision(const std::string filetype, caffe2::TypeMeta dtype) {
unsigned get_precision(const std::string& filetype, caffe2::TypeMeta dtype) {
if (filetype == "mp3")
return SOX_UNSPEC;
if (filetype == "flac")
Expand Down Expand Up @@ -425,7 +425,7 @@ unsigned get_precision(const std::string filetype, caffe2::TypeMeta dtype) {
sox_signalinfo_t get_signalinfo(
const torch::Tensor* waveform,
const int64_t sample_rate,
const std::string filetype,
const std::string& filetype,
const bool channels_first) {
return sox_signalinfo_t{
/*rate=*/static_cast<sox_rate_t>(sample_rate),
Expand Down Expand Up @@ -476,10 +476,10 @@ sox_encodinginfo_t get_tensor_encodinginfo(caffe2::TypeMeta dtype) {

sox_encodinginfo_t get_encodinginfo_for_save(
const std::string& format,
const caffe2::TypeMeta dtype,
const c10::optional<double> compression,
const c10::optional<std::string> encoding,
const c10::optional<int64_t> bits_per_sample) {
const caffe2::TypeMeta& dtype,
const c10::optional<double>& compression,
const c10::optional<std::string>& encoding,
const c10::optional<int64_t>& bits_per_sample) {
auto enc = get_save_encoding(format, dtype, encoding, bits_per_sample);
return sox_encodinginfo_t{
/*encoding=*/std::get<0>(enc),
Expand Down
14 changes: 7 additions & 7 deletions torchaudio/csrc/sox/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct SoxFormat {

///
/// Verify that input Tensor is 2D, CPU and either uin8, int16, int32 or float32
void validate_input_tensor(const torch::Tensor);
void validate_input_tensor(const torch::Tensor&);

///
/// Get target dtype for the given encoding and precision.
Expand Down Expand Up @@ -85,13 +85,13 @@ torch::Tensor convert_to_tensor(
const bool channels_first);

/// Extract extension from file path
const std::string get_filetype(const std::string path);
const std::string get_filetype(const std::string& path);

/// Get sox_signalinfo_t for passing a torch::Tensor object.
sox_signalinfo_t get_signalinfo(
const torch::Tensor* waveform,
const int64_t sample_rate,
const std::string filetype,
const std::string& filetype,
const bool channels_first);

/// Get sox_encodinginfo_t for Tensor I/O
Expand All @@ -100,10 +100,10 @@ sox_encodinginfo_t get_tensor_encodinginfo(const caffe2::TypeMeta dtype);
/// Get sox_encodinginfo_t for saving to file/file object
sox_encodinginfo_t get_encodinginfo_for_save(
const std::string& format,
const caffe2::TypeMeta dtype,
const c10::optional<double> compression,
const c10::optional<std::string> encoding,
const c10::optional<int64_t> bits_per_sample);
const caffe2::TypeMeta& dtype,
const c10::optional<double>& compression,
const c10::optional<std::string>& encoding,
const c10::optional<int64_t>& bits_per_sample);

} // namespace torchaudio::sox
#endif

0 comments on commit 393a49f

Please sign in to comment.