File size: 2,227 Bytes
2640ad4
 
d990151
 
2640ad4
 
 
 
 
 
 
 
 
d990151
2640ad4
 
 
 
 
 
d990151
 
 
 
 
 
2640ad4
 
 
 
 
 
 
 
d990151
 
2640ad4
 
 
 
 
 
d990151
2640ad4
 
 
 
 
d990151
2640ad4
 
 
 
d990151
2640ad4
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
import numpy as np

from scripts.spokenwoz.build import Config
from scripts.utils import bounds, turn_start
from scripts.vad import Segment

CFG = Config()


def test_clip_never_contains_samples_after_fire_time():
    wav = np.arange(CFG.sr * 20, dtype=np.float32)  # sample value == sample index
    for fire_frame in [100, 333, 512]:
        fire = fire_frame * 0.032  # VAD frame grid
        a, b = bounds(1.234, fire, CFG.sr, CFG.preroll_s, CFG.max_clip_s)
        c = wav[a:b]
        assert c[-1] < fire * CFG.sr
        assert c[-1] == round(fire * CFG.sr) - 1


def test_preroll_clamped_at_zero():
    assert bounds(0.1, 1.0, CFG.sr, CFG.preroll_s, CFG.max_clip_s)[0] == 0


def test_clip_capped_at_max_length():
    a, b = bounds(start=5.0, fire=100.0, sr=CFG.sr, preroll_s=CFG.preroll_s, max_clip_s=30.0)
    assert (b - a) / CFG.sr == 30.0 and b == 100.0 * CFG.sr


OWN = [Segment(0.0, 2.0, 2.2), Segment(2.5, 4.0, 4.2), Segment(7.0, 9.0, 9.2), Segment(9.5, 10.0, 10.2)]
NOT_COMPLETE = [False] * 4


def test_turn_start_breaks_on_real_other_segment():
    other = [Segment(4.5, 6.5, 6.7), Segment(9.1, 9.3, 9.5)]  # real reply, then a backchannel
    assert turn_start(OWN, 1, other, [], NOT_COMPLETE, CFG.real_seg_s) == (0.0, "dialogue_start")
    assert turn_start(OWN, 3, other, [], NOT_COMPLETE, CFG.real_seg_s) == (7.0, "other_real_segment")


def test_turn_start_ignores_future_audio():
    # other segment starts in the gap but has lasted < 1 s by own[1].fire -> not yet a real segment
    own = [Segment(0.0, 2.0, 2.2), Segment(2.6, 3.0, 3.2)]
    other = [Segment(2.4, 5.0, 5.2)]
    assert turn_start(own, 1, other, [], [False, False], CFG.real_seg_s) == (0.0, "dialogue_start")


def test_turn_start_breaks_on_short_other_annotated_turn():
    # S turn, other says "yes" (0.5 s, too short to be a real VAD segment), S turn
    other = [Segment(4.5, 5.0, 5.2)]
    start, reason = turn_start(OWN, 2, other, [4.5], NOT_COMPLETE, CFG.real_seg_s)
    assert (start, reason) == (7.0, "other_annotated_turn")


def test_turn_start_breaks_after_previous_complete():
    start, reason = turn_start(OWN, 2, [], [], [False, True, False, False], CFG.real_seg_s)
    assert (start, reason) == (7.0, "prev_complete")