diff --git a/test/torchaudio_unittest/backend/sox_io/info_test.py b/test/torchaudio_unittest/backend/sox_io/info_test.py index a2a93648a1..d0b9f24fcb 100644 --- a/test/torchaudio_unittest/backend/sox_io/info_test.py +++ b/test/torchaudio_unittest/backend/sox_io/info_test.py @@ -221,6 +221,21 @@ def test_alaw(self): assert info.bits_per_sample == 8 assert info.encoding == "ALAW" + def test_gsm(self): + """`sox_io_backend.info` can check gsm file correctly""" + duration = 1 + num_channels = 1 + sample_rate = 8000 + path = self.get_temp_path('data.gsm') + sox_utils.gen_audio_file( + path, sample_rate=sample_rate, num_channels=num_channels, + duration=duration) + info = sox_io_backend.info(path) + assert info.sample_rate == sample_rate + assert info.num_channels == num_channels + assert info.bits_per_sample == 0 + assert info.encoding == "GSM" + @skipIfNoExtension class TestInfoOpus(PytorchTestCase): diff --git a/torchaudio/csrc/sox/io.cpp b/torchaudio/csrc/sox/io.cpp index 8bc520feba..729c64666c 100644 --- a/torchaudio/csrc/sox/io.cpp +++ b/torchaudio/csrc/sox/io.cpp @@ -1,7 +1,7 @@ -#include #include #include #include +#include #include using namespace torch::indexing; @@ -10,41 +10,6 @@ using namespace torchaudio::sox_utils; namespace torchaudio { namespace sox_io { -namespace { - -std::string get_encoding(sox_encoding_t encoding) { - switch (encoding) { - case SOX_ENCODING_UNKNOWN: - return "UNKNOWN"; - case SOX_ENCODING_SIGN2: - return "PCM_S"; - case SOX_ENCODING_UNSIGNED: - return "PCM_U"; - case SOX_ENCODING_FLOAT: - return "PCM_F"; - case SOX_ENCODING_FLAC: - return "FLAC"; - case SOX_ENCODING_ULAW: - return "ULAW"; - case SOX_ENCODING_ALAW: - return "ALAW"; - case SOX_ENCODING_MP3: - return "MP3"; - case SOX_ENCODING_VORBIS: - return "VORBIS"; - case SOX_ENCODING_AMR_WB: - return "AMR_WB"; - case SOX_ENCODING_AMR_NB: - return "AMR_NB"; - case SOX_ENCODING_OPUS: - return "OPUS"; - default: - return "UNKNOWN"; - } -} - -} // namespace - std::tuple get_info_file( const std::string& path, c10::optional& format) { diff --git a/torchaudio/csrc/sox/types.cpp b/torchaudio/csrc/sox/types.cpp index 59e9d320c4..d70fe218f0 100644 --- a/torchaudio/csrc/sox/types.cpp +++ b/torchaudio/csrc/sox/types.cpp @@ -100,5 +100,38 @@ BitDepth get_bit_depth_from_option(const c10::optional& bit_depth) { } } +std::string get_encoding(sox_encoding_t encoding) { + switch (encoding) { + case SOX_ENCODING_UNKNOWN: + return "UNKNOWN"; + case SOX_ENCODING_SIGN2: + return "PCM_S"; + case SOX_ENCODING_UNSIGNED: + return "PCM_U"; + case SOX_ENCODING_FLOAT: + return "PCM_F"; + case SOX_ENCODING_FLAC: + return "FLAC"; + case SOX_ENCODING_ULAW: + return "ULAW"; + case SOX_ENCODING_ALAW: + return "ALAW"; + case SOX_ENCODING_MP3: + return "MP3"; + case SOX_ENCODING_VORBIS: + return "VORBIS"; + case SOX_ENCODING_AMR_WB: + return "AMR_WB"; + case SOX_ENCODING_AMR_NB: + return "AMR_NB"; + case SOX_ENCODING_OPUS: + return "OPUS"; + case SOX_ENCODING_GSM: + return "GSM"; + default: + return "UNKNOWN"; + } +} + } // namespace sox_utils } // namespace torchaudio diff --git a/torchaudio/csrc/sox/types.h b/torchaudio/csrc/sox/types.h index f3a337407c..b13510cde1 100644 --- a/torchaudio/csrc/sox/types.h +++ b/torchaudio/csrc/sox/types.h @@ -1,6 +1,7 @@ #ifndef TORCHAUDIO_SOX_TYPES_H #define TORCHAUDIO_SOX_TYPES_H +#include #include namespace torchaudio { @@ -50,6 +51,8 @@ enum class BitDepth : unsigned { BitDepth get_bit_depth_from_option(const c10::optional& bit_depth); +std::string get_encoding(sox_encoding_t encoding); + } // namespace sox_utils } // namespace torchaudio