-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.py
executable file
·102 lines (81 loc) · 3.75 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
# Test script for Moodbar
# Copyright (C) 2018 Johannes Sasongko <sasongko@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import shlex
import subprocess
import tempfile
import unittest
import numpy as np
GST_LAUNCH = shlex.split(os.environ.get('GST_LAUNCH', 'gst-launch-1.0'))
MOODBAR_EXE_DEFAULT = './moodbar'
MOODBAR = shlex.split(os.environ.get('MOODBAR', MOODBAR_EXE_DEFAULT))
def create_dummy_audio(path: str) -> None:
"""Create audio file for testing.
The file contains low, medium, and high frequency audio (in that order), and
should show up in Moodbar as red, green, and blue equal-length segments.
The file uses Vorbis codec in Ogg container.
This function requires gst-launch-1.0 to be present in PATH.
"""
cmd = GST_LAUNCH + [
'audiotestsrc', 'freq=100', 'num-buffers=100', 'volume=0.4', '!', 'concat', 'name=c',
'!', 'vorbisenc', 'bitrate=32000', '!', 'oggmux',
'!', 'filesink', 'location='+path.replace('\\', '/'),
'audiotestsrc', 'freq=2000', 'num-buffers=100', 'volume=0.2', '!', 'c.',
'audiotestsrc', 'freq=4000', 'num-buffers=100', 'volume=0.1', '!', 'c.',
]
subprocess.check_call(cmd, stdout=subprocess.DEVNULL)
def call_moodbar(inpath: str, outpath: str):
cmd = MOODBAR + ['-o', outpath, inpath]
try:
return subprocess.check_call(cmd)
except FileNotFoundError as e:
if e.filename == MOODBAR_EXE_DEFAULT:
raise FileNotFoundError("Could not find moodbar executable. "
"Make sure you run this test from the build directory.") from e
raise
class MoodbarTest(unittest.TestCase):
def assertArrayAlmostEqual(self, first, second, delta, msg=None):
if not np.all(np.abs(first - second) < delta):
if msg is None:
msg = f"{first} != {second} within {delta} delta"
raise AssertionError(msg)
def test_unicode(self):
"""Test that non-Latin paths work."""
with tempfile.TemporaryDirectory(prefix='moodbar-test.') as tmpdir:
audiopath = os.path.join(tmpdir, '你好.ogg')
create_dummy_audio(audiopath)
moodpath = os.path.join(tmpdir, '안녕.mood')
call_moodbar(audiopath, moodpath)
def test_colors(self):
"""Test that colors are close enough to the expected ones."""
with tempfile.TemporaryDirectory(prefix='moodbar-test.') as tmpdir:
audiopath = os.path.join(tmpdir, 'test.ogg')
create_dummy_audio(audiopath)
moodpath = os.path.join(tmpdir, 'test.mood')
call_moodbar(audiopath, moodpath)
mood = np.fromfile(moodpath, dtype=np.uint8)
mood = mood.reshape(-1, 3)
length = mood.shape[0]
red = mood[length*1//12 : length*3//12].mean(0)
green = mood[length*5//12 : length*7//12].mean(0)
blue = mood[length*9//12 : length*11//12].mean(0)
delta = [30, 30, 30]
self.assertArrayAlmostEqual(red, [255, 0, 0], delta)
self.assertArrayAlmostEqual(green, [0, 255, 0], delta)
self.assertArrayAlmostEqual(blue, [0, 0, 255], delta)
if __name__ == '__main__':
unittest.main()