summaryrefslogtreecommitdiff
path: root/tests/test_functions_audio.py
blob: b6e6c6a9b29b65e39c453f70211f442723dd331f (plain)
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
"""Tests for the audio primitive."""

import xml.etree.ElementTree as et

import pytest

from blc2.functions.audio import Audio
from blc2.constants import BXW
from blc2.exceptions import LoadError

def test_audio(aws):
    a = Audio(aws)
    
    a.fade_out = 1000
    a.fade_out = 1000
    a.fade_in = 1000
    a.fade_in = 1000
    a.filename = "nonexistant"
    
    assert a.audio_scope == {"nonexistant"}
    assert not a.scope
    assert a.duration == 0
    assert a.actual_duration == 0

    a.filename = "tests/silence.m4a"
    assert a.filename == "tests/silence.m4a"
    assert a.audio_scope == {"tests/silence.m4a"}
    assert a.duration == 2024
    assert a.actual_duration == 3024

    a.fade_out = 500
    assert a.fade_out == 500
    assert a.duration == 2524
    assert a.actual_duration == 3024

    lc, ac, _ = a.render(0)
    assert not lc
    assert len(ac) == 1
    assert ac[0][1:] == ("tests/silence.m4a", 0, 1000, 2524, 500)

    _, ac2, _ = a.render(3000)
    assert ac2 == ac

    _, ac, _ = a.render(5000)
    assert not ac

    with pytest.raises(ValueError):
        _ = Audio(aws, id_=101, fade_in=-10)

    assert 101 not in aws.functions

    fout = a.fade_out
    with pytest.raises(ValueError):
        a.fade_out = -50
    assert a.fade_out == fout

    fin = a.fade_in
    with pytest.raises(ValueError):
        a.fade_in = -50
    assert a.fade_in == fin

    a._set_duration(10000)
    assert a.copy_data(a.get_data()) is None

    a.filename = None
    assert a.duration == 0
    assert a.actual_duration == 0

def test_audio_serialize(aws, test_xml_eq):
    a1 = Audio(aws, id_=123, name="Test Audio 1", fade_out=100, filename="test.wav")
    a2 = Audio(aws, id_=124, name="Test Audio 2", filename=None)

    test = (a1, a2)
    success = [
"""<function type="Audio" xmlns="{}" id="123" name="Test Audio 1" fade-in="0" fade-out="100" filename="test.wav"/>""",
"""<function type="Audio" xmlns="{}" id="124" name="Test Audio 2" fade-in="0" fade-out="0"/>"""
    ]

    for case, s in zip(test, success):
        s = s.replace('\n', "").format(BXW.strip("{}"))
        assert test_xml_eq(case.serialize(), s)

def test_audio_deserialize(aws, test_xml_eq):
    fail = [
        """<function xmlns={} type="asdf"/>""",
        """<asdf/>""",
        """<function xmlns={} type="Audio" id="asdf"/>""",
        """<function xmlns={} type="Audio" id="123" name="Test Audio" fade-out="asdf"/>""",
        """<function xmlns={} type="Audio" id="123" name="Test Audio" fade-in="asdf"/>""",
    ]

    for case in fail:
        case = case.format('"'+BXW.strip("{}")+'"')
        with pytest.raises(LoadError):
            Audio.deserialize(aws, et.fromstring(case))
    
    case = """<function xmlns={} type="Audio" id="123" name="Test Audio" fade-in="123" fade-out="321" filename="test.wav"/>"""
    case = case.format('"'+BXW.strip("{}")+'"')

    s = Audio.deserialize(aws, et.fromstring(case))

    assert test_xml_eq(s.serialize(), case)