Skip to content

Commit 6bcadb9

Browse files
authored
Add GSM format support to sox_io's save function (#1304)
Cherry picked from commit 490a53e (#1275)
1 parent 5799599 commit 6bcadb9

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

test/torchaudio_unittest/backend/sox_io/save_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ def test_save_amr_nb(self, test_mode, bit_rate):
310310
self.assert_save_consistency(
311311
"amr-nb", compression=bit_rate, num_channels=1, test_mode=test_mode)
312312

313+
@nested_params(
314+
["path", "fileobj", "bytesio"],
315+
)
316+
def test_save_gsm(self, test_mode):
317+
self.assert_save_consistency(
318+
"gsm", test_mode=test_mode)
319+
313320
@parameterized.expand([
314321
("wav", "PCM_S", 16),
315322
("mp3", ),

torchaudio/backend/sox_io_backend.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def save(
195195
When ``filepath`` argument is file-like object, this argument is required.
196196
197197
Valid values are ``"wav"``, ``"mp3"``, ``"ogg"``, ``"vorbis"``, ``"amr-nb"``,
198-
``"amb"``, ``"flac"`` and ``"sph"``.
198+
``"amb"``, ``"flac"``, ``"sph"`` and ``"gsm"``.
199199
encoding (str, optional): Changes the encoding for the supported formats.
200200
This argument is effective only for supported formats, such as ``"wav"``, ``""amb"``
201201
and ``"sph"``. Valid values are;
@@ -291,6 +291,9 @@ def save(
291291
``"amr-nb"``
292292
Bitrate ranging from 4.75 kbit/s to 12.2 kbit/s. Default: 4.75 kbit/s
293293
294+
``"gsm"``
295+
Lossy Speech Compression, CPU intensive.
296+
294297
Note:
295298
To save into formats that ``libsox`` does not handle natively, (such as ``"mp3"``,
296299
``"flac"``, ``"ogg"`` and ``"vorbis"``), your installation of ``torchaudio`` has

torchaudio/csrc/sox/types.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Format get_format_from_string(const std::string& format) {
2020
return Format::AMB;
2121
if (format == "sph")
2222
return Format::SPHERE;
23+
if (format == "gsm")
24+
return Format::GSM;
2325
std::ostringstream stream;
2426
stream << "Internal Error: unexpected format value: " << format;
2527
throw std::runtime_error(stream.str());

torchaudio/csrc/sox/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ enum class Format {
1515
AMR_WB,
1616
AMB,
1717
SPHERE,
18+
GSM,
1819
};
1920

2021
Format get_format_from_string(const std::string& format);

torchaudio/csrc/sox/utils.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,14 @@ std::tuple<sox_encoding_t, unsigned> get_save_encoding(
378378
throw std::runtime_error(
379379
"sph does not support encoding: " + encoding.value());
380380
}
381+
case Format::GSM:
382+
if (enc != Encoding::NOT_PROVIDED)
383+
throw std::runtime_error("gsm does not support `encoding` option.");
384+
if (bps != BitDepth::NOT_PROVIDED)
385+
throw std::runtime_error(
386+
"gsm does not support `bits_per_sample` option.");
387+
return std::make_tuple<>(SOX_ENCODING_GSM, 16);
388+
381389
default:
382390
throw std::runtime_error("Unsupported format: " + format);
383391
}
@@ -409,6 +417,8 @@ unsigned get_precision(const std::string filetype, caffe2::TypeMeta dtype) {
409417
if (filetype == "amr-nb") {
410418
return 16;
411419
}
420+
if (filetype == "gsm")
421+
return 16;
412422
throw std::runtime_error("Unsupported file type: " + filetype);
413423
}
414424

0 commit comments

Comments
 (0)