Skip to content

Commit b423dec

Browse files
Add GSM encoding support to info (#1307)
Cherry-picked e8e1c22 from #1277
1 parent 023200b commit b423dec

File tree

4 files changed

+52
-36
lines changed

4 files changed

+52
-36
lines changed

test/torchaudio_unittest/backend/sox_io/info_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,21 @@ def test_alaw(self):
221221
assert info.bits_per_sample == 8
222222
assert info.encoding == "ALAW"
223223

224+
def test_gsm(self):
225+
"""`sox_io_backend.info` can check gsm file correctly"""
226+
duration = 1
227+
num_channels = 1
228+
sample_rate = 8000
229+
path = self.get_temp_path('data.gsm')
230+
sox_utils.gen_audio_file(
231+
path, sample_rate=sample_rate, num_channels=num_channels,
232+
duration=duration)
233+
info = sox_io_backend.info(path)
234+
assert info.sample_rate == sample_rate
235+
assert info.num_channels == num_channels
236+
assert info.bits_per_sample == 0
237+
assert info.encoding == "GSM"
238+
224239

225240
@skipIfNoExtension
226241
class TestInfoOpus(PytorchTestCase):

torchaudio/csrc/sox/io.cpp

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#include <sox.h>
21
#include <torchaudio/csrc/sox/effects.h>
32
#include <torchaudio/csrc/sox/effects_chain.h>
43
#include <torchaudio/csrc/sox/io.h>
4+
#include <torchaudio/csrc/sox/types.h>
55
#include <torchaudio/csrc/sox/utils.h>
66

77
using namespace torch::indexing;
@@ -10,41 +10,6 @@ using namespace torchaudio::sox_utils;
1010
namespace torchaudio {
1111
namespace sox_io {
1212

13-
namespace {
14-
15-
std::string get_encoding(sox_encoding_t encoding) {
16-
switch (encoding) {
17-
case SOX_ENCODING_UNKNOWN:
18-
return "UNKNOWN";
19-
case SOX_ENCODING_SIGN2:
20-
return "PCM_S";
21-
case SOX_ENCODING_UNSIGNED:
22-
return "PCM_U";
23-
case SOX_ENCODING_FLOAT:
24-
return "PCM_F";
25-
case SOX_ENCODING_FLAC:
26-
return "FLAC";
27-
case SOX_ENCODING_ULAW:
28-
return "ULAW";
29-
case SOX_ENCODING_ALAW:
30-
return "ALAW";
31-
case SOX_ENCODING_MP3:
32-
return "MP3";
33-
case SOX_ENCODING_VORBIS:
34-
return "VORBIS";
35-
case SOX_ENCODING_AMR_WB:
36-
return "AMR_WB";
37-
case SOX_ENCODING_AMR_NB:
38-
return "AMR_NB";
39-
case SOX_ENCODING_OPUS:
40-
return "OPUS";
41-
default:
42-
return "UNKNOWN";
43-
}
44-
}
45-
46-
} // namespace
47-
4813
std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> get_info_file(
4914
const std::string& path,
5015
c10::optional<std::string>& format) {

torchaudio/csrc/sox/types.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,38 @@ BitDepth get_bit_depth_from_option(const c10::optional<int64_t>& bit_depth) {
100100
}
101101
}
102102

103+
std::string get_encoding(sox_encoding_t encoding) {
104+
switch (encoding) {
105+
case SOX_ENCODING_UNKNOWN:
106+
return "UNKNOWN";
107+
case SOX_ENCODING_SIGN2:
108+
return "PCM_S";
109+
case SOX_ENCODING_UNSIGNED:
110+
return "PCM_U";
111+
case SOX_ENCODING_FLOAT:
112+
return "PCM_F";
113+
case SOX_ENCODING_FLAC:
114+
return "FLAC";
115+
case SOX_ENCODING_ULAW:
116+
return "ULAW";
117+
case SOX_ENCODING_ALAW:
118+
return "ALAW";
119+
case SOX_ENCODING_MP3:
120+
return "MP3";
121+
case SOX_ENCODING_VORBIS:
122+
return "VORBIS";
123+
case SOX_ENCODING_AMR_WB:
124+
return "AMR_WB";
125+
case SOX_ENCODING_AMR_NB:
126+
return "AMR_NB";
127+
case SOX_ENCODING_OPUS:
128+
return "OPUS";
129+
case SOX_ENCODING_GSM:
130+
return "GSM";
131+
default:
132+
return "UNKNOWN";
133+
}
134+
}
135+
103136
} // namespace sox_utils
104137
} // namespace torchaudio

torchaudio/csrc/sox/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef TORCHAUDIO_SOX_TYPES_H
22
#define TORCHAUDIO_SOX_TYPES_H
33

4+
#include <sox.h>
45
#include <torch/script.h>
56

67
namespace torchaudio {
@@ -50,6 +51,8 @@ enum class BitDepth : unsigned {
5051

5152
BitDepth get_bit_depth_from_option(const c10::optional<int64_t>& bit_depth);
5253

54+
std::string get_encoding(sox_encoding_t encoding);
55+
5356
} // namespace sox_utils
5457
} // namespace torchaudio
5558

0 commit comments

Comments
 (0)