Skip to content

Commit 393a49f

Browse files
committed
Use const reference
Summary: Adopt more of const reference in sox source code. Differential Revision: D46264068 fbshipit-source-id: d474ba1c9d87c924f3b893fba0ff98ecb412cdc8
1 parent d974131 commit 393a49f

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

torchaudio/csrc/sox/types.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ std::string to_string(Encoding v) {
5757
}
5858
}
5959

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

77-
BitDepth get_bit_depth_from_option(const c10::optional<int64_t> bit_depth) {
77+
BitDepth get_bit_depth_from_option(const c10::optional<int64_t>& bit_depth) {
7878
if (!bit_depth.has_value())
7979
return BitDepth::NOT_PROVIDED;
8080
int64_t v = bit_depth.value();

torchaudio/csrc/sox/types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ enum class Encoding {
3838
};
3939

4040
std::string to_string(Encoding v);
41-
Encoding get_encoding_from_option(const c10::optional<std::string> encoding);
41+
Encoding get_encoding_from_option(const c10::optional<std::string>& encoding);
4242

4343
enum class BitDepth : unsigned {
4444
NOT_PROVIDED = 0,
@@ -49,7 +49,7 @@ enum class BitDepth : unsigned {
4949
B64 = 64,
5050
};
5151

52-
BitDepth get_bit_depth_from_option(const c10::optional<int64_t> bit_depth);
52+
BitDepth get_bit_depth_from_option(const c10::optional<int64_t>& bit_depth);
5353

5454
std::string get_encoding(sox_encoding_t encoding);
5555

torchaudio/csrc/sox/utils.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void validate_input_file(const SoxFormat& sf, const std::string& path) {
9393
"Error loading audio file: unknown encoding.");
9494
}
9595

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

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

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

279279
std::tuple<sox_encoding_t, unsigned> get_save_encoding(
280280
const std::string& format,
281-
const caffe2::TypeMeta dtype,
282-
const c10::optional<std::string> encoding,
283-
const c10::optional<int64_t> bits_per_sample) {
281+
const caffe2::TypeMeta& dtype,
282+
const c10::optional<std::string>& encoding,
283+
const c10::optional<int64_t>& bits_per_sample) {
284284
const Format fmt = get_format_from_string(format);
285285
const Encoding enc = get_encoding_from_option(encoding);
286286
const BitDepth bps = get_bit_depth_from_option(bits_per_sample);
@@ -385,7 +385,7 @@ std::tuple<sox_encoding_t, unsigned> get_save_encoding(
385385
}
386386
}
387387

388-
unsigned get_precision(const std::string filetype, caffe2::TypeMeta dtype) {
388+
unsigned get_precision(const std::string& filetype, caffe2::TypeMeta dtype) {
389389
if (filetype == "mp3")
390390
return SOX_UNSPEC;
391391
if (filetype == "flac")
@@ -425,7 +425,7 @@ unsigned get_precision(const std::string filetype, caffe2::TypeMeta dtype) {
425425
sox_signalinfo_t get_signalinfo(
426426
const torch::Tensor* waveform,
427427
const int64_t sample_rate,
428-
const std::string filetype,
428+
const std::string& filetype,
429429
const bool channels_first) {
430430
return sox_signalinfo_t{
431431
/*rate=*/static_cast<sox_rate_t>(sample_rate),
@@ -476,10 +476,10 @@ sox_encodinginfo_t get_tensor_encodinginfo(caffe2::TypeMeta dtype) {
476476

477477
sox_encodinginfo_t get_encodinginfo_for_save(
478478
const std::string& format,
479-
const caffe2::TypeMeta dtype,
480-
const c10::optional<double> compression,
481-
const c10::optional<std::string> encoding,
482-
const c10::optional<int64_t> bits_per_sample) {
479+
const caffe2::TypeMeta& dtype,
480+
const c10::optional<double>& compression,
481+
const c10::optional<std::string>& encoding,
482+
const c10::optional<int64_t>& bits_per_sample) {
483483
auto enc = get_save_encoding(format, dtype, encoding, bits_per_sample);
484484
return sox_encodinginfo_t{
485485
/*encoding=*/std::get<0>(enc),

torchaudio/csrc/sox/utils.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct SoxFormat {
5353

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

5858
///
5959
/// Get target dtype for the given encoding and precision.
@@ -85,13 +85,13 @@ torch::Tensor convert_to_tensor(
8585
const bool channels_first);
8686

8787
/// Extract extension from file path
88-
const std::string get_filetype(const std::string path);
88+
const std::string get_filetype(const std::string& path);
8989

9090
/// Get sox_signalinfo_t for passing a torch::Tensor object.
9191
sox_signalinfo_t get_signalinfo(
9292
const torch::Tensor* waveform,
9393
const int64_t sample_rate,
94-
const std::string filetype,
94+
const std::string& filetype,
9595
const bool channels_first);
9696

9797
/// Get sox_encodinginfo_t for Tensor I/O
@@ -100,10 +100,10 @@ sox_encodinginfo_t get_tensor_encodinginfo(const caffe2::TypeMeta dtype);
100100
/// Get sox_encodinginfo_t for saving to file/file object
101101
sox_encodinginfo_t get_encodinginfo_for_save(
102102
const std::string& format,
103-
const caffe2::TypeMeta dtype,
104-
const c10::optional<double> compression,
105-
const c10::optional<std::string> encoding,
106-
const c10::optional<int64_t> bits_per_sample);
103+
const caffe2::TypeMeta& dtype,
104+
const c10::optional<double>& compression,
105+
const c10::optional<std::string>& encoding,
106+
const c10::optional<int64_t>& bits_per_sample);
107107

108108
} // namespace torchaudio::sox
109109
#endif

0 commit comments

Comments
 (0)