(fmt, dtype)
| 177 | |
| 178 | |
| 179 | def check_audio_decoder_correctness(fmt, dtype): |
| 180 | batch_size = 16 |
| 181 | niterations = 10 |
| 182 | |
| 183 | @pipeline_def(batch_size=batch_size, device_id=0, num_threads=4) |
| 184 | def audio_decoder_pipe(fnames, dtype, downmix=False): |
| 185 | encoded, _ = fn.readers.file(files=fnames) |
| 186 | decoded, _ = fn.decoders.audio(encoded, dtype=dtype, downmix=downmix) |
| 187 | return decoded |
| 188 | |
| 189 | audio_files = get_files(os.path.join("db", "audio", fmt), fmt) |
| 190 | npy_files = [os.path.splitext(fpath)[0] + ".npy" for fpath in audio_files] |
| 191 | pipe = audio_decoder_pipe(audio_files, dtype) |
| 192 | for it in range(niterations): |
| 193 | data = pipe.run() |
| 194 | for s in range(batch_size): |
| 195 | sample_idx = (it * batch_size + s) % len(audio_files) |
| 196 | ref = np.load(npy_files[sample_idx]) |
| 197 | if len(ref.shape) == 1: |
| 198 | ref = np.expand_dims(ref, 1) |
| 199 | arr = np.array(data[0][s]) |
| 200 | assert arr.shape == ref.shape |
| 201 | if fmt == "ogg": |
| 202 | # For OGG Vorbis, we consider errors any value that is off by more than 1 |
| 203 | # TODO(janton): There is a bug in libsndfile that produces underflow/overflow. |
| 204 | # Remove this when the bug is fixed. |
| 205 | # Tuple with two arrays, we just need the first dimension |
| 206 | wrong_values = np.where(np.abs(arr - ref) > 1)[0] |
| 207 | nerrors = len(wrong_values) |
| 208 | assert nerrors <= 1 |
| 209 | # TODO(janton): Uncomment this when the bug is fixed |
| 210 | # np.testing.assert_allclose(arr, ref, atol=1) |
| 211 | else: |
| 212 | np.testing.assert_equal(arr, ref) |
| 213 | |
| 214 | |
| 215 | def test_audio_decoder_correctness(): |
nothing calls this directly
no test coverage detected