@@ -168,6 +168,38 @@ def assert_vorbis(self, *args, **kwargs):
168168 else :
169169 raise error
170170
171+ def assert_sphere (self , sample_rate , num_channels , duration ):
172+ """`sox_io_backend.save` can save sph format.
173+
174+ This test takes the same strategy as mp3 to compare the result
175+ """
176+ src_path = self .get_temp_path ('1.reference.wav' )
177+ flc_path = self .get_temp_path ('2.1.torchaudio.sph' )
178+ wav_path = self .get_temp_path ('2.2.torchaudio.wav' )
179+ flc_path_sox = self .get_temp_path ('3.1.sox.sph' )
180+ wav_path_sox = self .get_temp_path ('3.2.sox.wav' )
181+
182+ # 1. Generate original wav
183+ data = get_wav_data ('float32' , num_channels , normalize = True , num_frames = duration * sample_rate )
184+ save_wav (src_path , data , sample_rate )
185+ # 2.1. Convert the original wav to sph with torchaudio
186+ sox_io_backend .save (flc_path , load_wav (src_path )[0 ], sample_rate )
187+ # 2.2. Convert the sph to wav with Sox
188+ # converting to 32 bit because sph file has 24 bit depth which scipy cannot handle.
189+ sox_utils .convert_audio_file (flc_path , wav_path , bit_depth = 32 )
190+ # 2.3. Load
191+ found = load_wav (wav_path )[0 ]
192+
193+ # 3.1. Convert the original wav to sph with SoX
194+ sox_utils .convert_audio_file (src_path , flc_path_sox )
195+ # 3.2. Convert the sph to wav with Sox
196+ # converting to 32 bit because sph file has 24 bit depth which scipy cannot handle.
197+ sox_utils .convert_audio_file (flc_path_sox , wav_path_sox , bit_depth = 32 )
198+ # 3.3. Load
199+ expected = load_wav (wav_path_sox )[0 ]
200+
201+ self .assertEqual (found , expected )
202+
171203
172204@skipIfNoExec ('sox' )
173205@skipIfNoExtension
@@ -262,6 +294,14 @@ def test_vorbis_large(self, sample_rate, num_channels, quality_level):
262294 self.assert_vorbis(sample_rate, num_channels, quality_level, two_hours)
263295 '''
264296
297+ @parameterized .expand (list (itertools .product (
298+ [8000 , 16000 ],
299+ [1 , 2 ],
300+ )), name_func = name_func )
301+ def test_sphere (self , sample_rate , num_channels ):
302+ """`sox_io_backend.save` can save sph format."""
303+ self .assert_sphere (sample_rate , num_channels , duration = 1 )
304+
265305
266306@skipIfNoExec ('sox' )
267307@skipIfNoExtension
0 commit comments