Skip to content

Commit 58ac6b9

Browse files
committed
Isolate get_tensor_encodinginfo
1 parent 0037959 commit 58ac6b9

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

torchaudio/csrc/sox/utils.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,31 @@ sox_signalinfo_t get_signalinfo(
293293

294294
sox_encodinginfo_t get_tensor_encodinginfo(
295295
const caffe2::TypeMeta dtype) {
296+
sox_encoding_t encoding = [&](){
297+
if (dtype == torch::kUInt8)
298+
return SOX_ENCODING_UNSIGNED;
299+
if (dtype == torch::kInt16)
300+
return SOX_ENCODING_SIGN2;
301+
if (dtype == torch::kInt32)
302+
return SOX_ENCODING_SIGN2;
303+
if (dtype == torch::kFloat32)
304+
return SOX_ENCODING_FLOAT;
305+
throw std::runtime_error("Unsupported dtype.");
306+
}();
307+
unsigned bits_per_sample = [&](){
308+
if (dtype == torch::kUInt8)
309+
return 8;
310+
if (dtype == torch::kInt16)
311+
return 16;
312+
if (dtype == torch::kInt32)
313+
return 32;
314+
if (dtype == torch::kFloat32)
315+
return 32;
316+
throw std::runtime_error("Unsupported dtype.");
317+
}();
296318
return sox_encodinginfo_t{
297-
/*encoding=*/get_encoding("wav", dtype),
298-
/*bits_per_sample=*/get_precision("wav", dtype),
319+
/*encoding=*/encoding,
320+
/*bits_per_sample=*/bits_per_sample,
299321
/*compression=*/HUGE_VAL,
300322
/*reverse_bytes=*/sox_option_default,
301323
/*reverse_nibbles=*/sox_option_default,

0 commit comments

Comments
 (0)