Commit 9cdf26fd authored by Moto Hira's avatar Moto Hira Committed by Facebook GitHub Bot
Browse files

Use const reference (#3389)

Summary:
Pull Request resolved: https://github.com/pytorch/audio/pull/3389

Adopt more of const reference in sox source code.

Differential Revision: D46264068

fbshipit-source-id: 809d34a6e16f621c856d4278ef7ce45a5868a717
parent a81b0ed2
......@@ -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();
......@@ -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();
......
......@@ -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,
......@@ -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);
......
......@@ -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.");
......@@ -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;
......@@ -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);
......@@ -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")
......@@ -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),
......@@ -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),
......
......@@ -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.
......@@ -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
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment