Skip to content

Commit

Permalink
merging into common_utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentqb committed Mar 30, 2020
1 parent dfda00b commit f4fa0ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
39 changes: 0 additions & 39 deletions test/_test.py

This file was deleted.

35 changes: 35 additions & 0 deletions test/common_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import absolute_import, division, print_function, unicode_literals
import os
from shutil import copytree
from contextlib import contextmanager
import backports.tempfile as tempfile
import torch
import torchaudio

TEST_DIR_PATH = os.path.dirname(os.path.realpath(__file__))
BACKENDS = torchaudio._backend._audio_backends


def create_temp_assets_dir():
Expand Down Expand Up @@ -48,3 +51,35 @@ def random_int_tensor(seed, size, low=0, high=2 ** 32, a=22695477, c=1, m=2 ** 3
""" Same as random_float_tensor but integers between [low, high)
"""
return torch.floor(random_float_tensor(seed, size, a, c, m) * (high - low)) + low


@contextmanager
def AudioBackendScope(new_backend):
previous_backend = torchaudio.get_audio_backend()
try:
torchaudio.set_audio_backend(new_backend)
yield
finally:
torchaudio.set_audio_backend(previous_backend)


def filter_backends_with_mp3(backends):
test_dirpath, _ = create_temp_assets_dir()
test_filepath = os.path.join(
test_dirpath, "assets", "steam-train-whistle-daniel_simon.mp3"
)

backends_mp3 = []

for backend in backends:
try:
with AudioBackendScope(backend):
torchaudio.load(test_filepath)
backends_mp3.append(backend)
except RuntimeError:
pass

return backends_mp3


BACKENDS_MP3 = filter_backends_with_mp3(BACKENDS)

0 comments on commit f4fa0ca

Please sign in to comment.