"vscode:/vscode.git/clone" did not exist on "b25f81e0ea098f84fb8f8c4f238937b18db1d953"
Unverified Commit 490a53e5 authored by Prabhat Roy's avatar Prabhat Roy Committed by GitHub
Browse files

Add GSM format support to sox_io's save function (#1275)

parent fa71c5e2
...@@ -310,6 +310,13 @@ class SaveTest(SaveTestBase): ...@@ -310,6 +310,13 @@ class SaveTest(SaveTestBase):
self.assert_save_consistency( self.assert_save_consistency(
"amr-nb", compression=bit_rate, num_channels=1, test_mode=test_mode) "amr-nb", compression=bit_rate, num_channels=1, test_mode=test_mode)
@nested_params(
["path", "fileobj", "bytesio"],
)
def test_save_gsm(self, test_mode):
self.assert_save_consistency(
"gsm", test_mode=test_mode)
@parameterized.expand([ @parameterized.expand([
("wav", "PCM_S", 16), ("wav", "PCM_S", 16),
("mp3", ), ("mp3", ),
......
...@@ -195,7 +195,7 @@ def save( ...@@ -195,7 +195,7 @@ def save(
When ``filepath`` argument is file-like object, this argument is required. When ``filepath`` argument is file-like object, this argument is required.
Valid values are ``"wav"``, ``"mp3"``, ``"ogg"``, ``"vorbis"``, ``"amr-nb"``, Valid values are ``"wav"``, ``"mp3"``, ``"ogg"``, ``"vorbis"``, ``"amr-nb"``,
``"amb"``, ``"flac"`` and ``"sph"``. ``"amb"``, ``"flac"``, ``"sph"`` and ``"gsm"``.
encoding (str, optional): Changes the encoding for the supported formats. encoding (str, optional): Changes the encoding for the supported formats.
This argument is effective only for supported formats, cush as ``"wav"``, ``""amb"`` This argument is effective only for supported formats, cush as ``"wav"``, ``""amb"``
and ``"sph"``. Valid values are; and ``"sph"``. Valid values are;
...@@ -291,6 +291,9 @@ def save( ...@@ -291,6 +291,9 @@ def save(
``"amr-nb"`` ``"amr-nb"``
Bitrate ranging from 4.75 kbit/s to 12.2 kbit/s. Default: 4.75 kbit/s Bitrate ranging from 4.75 kbit/s to 12.2 kbit/s. Default: 4.75 kbit/s
``"gsm"``
Lossy Speech Compression, CPU intensive.
Note: Note:
To save into formats that ``libsox`` does not handle natively, (such as ``"mp3"``, To save into formats that ``libsox`` does not handle natively, (such as ``"mp3"``,
``"flac"``, ``"ogg"`` and ``"vorbis"``), your installation of ``torchaudio`` has ``"flac"``, ``"ogg"`` and ``"vorbis"``), your installation of ``torchaudio`` has
......
...@@ -20,6 +20,8 @@ Format get_format_from_string(const std::string& format) { ...@@ -20,6 +20,8 @@ Format get_format_from_string(const std::string& format) {
return Format::AMB; return Format::AMB;
if (format == "sph") if (format == "sph")
return Format::SPHERE; return Format::SPHERE;
if (format == "gsm")
return Format::GSM;
std::ostringstream stream; std::ostringstream stream;
stream << "Internal Error: unexpected format value: " << format; stream << "Internal Error: unexpected format value: " << format;
throw std::runtime_error(stream.str()); throw std::runtime_error(stream.str());
......
...@@ -15,6 +15,7 @@ enum class Format { ...@@ -15,6 +15,7 @@ enum class Format {
AMR_WB, AMR_WB,
AMB, AMB,
SPHERE, SPHERE,
GSM,
}; };
Format get_format_from_string(const std::string& format); Format get_format_from_string(const std::string& format);
......
...@@ -378,6 +378,14 @@ std::tuple<sox_encoding_t, unsigned> get_save_encoding( ...@@ -378,6 +378,14 @@ std::tuple<sox_encoding_t, unsigned> get_save_encoding(
throw std::runtime_error( throw std::runtime_error(
"sph does not support encoding: " + encoding.value()); "sph does not support encoding: " + encoding.value());
} }
case Format::GSM:
if (enc != Encoding::NOT_PROVIDED)
throw std::runtime_error("gsm does not support `encoding` option.");
if (bps != BitDepth::NOT_PROVIDED)
throw std::runtime_error(
"gsm does not support `bits_per_sample` option.");
return std::make_tuple<>(SOX_ENCODING_GSM, 16);
default: default:
throw std::runtime_error("Unsupported format: " + format); throw std::runtime_error("Unsupported format: " + format);
} }
...@@ -409,6 +417,8 @@ unsigned get_precision(const std::string filetype, caffe2::TypeMeta dtype) { ...@@ -409,6 +417,8 @@ unsigned get_precision(const std::string filetype, caffe2::TypeMeta dtype) {
if (filetype == "amr-nb") { if (filetype == "amr-nb") {
return 16; return 16;
} }
if (filetype == "gsm")
return 16;
throw std::runtime_error("Unsupported file type: " + filetype); throw std::runtime_error("Unsupported file type: " + filetype);
} }
......
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