aboutsummaryrefslogtreecommitdiff
path: root/tests/test_robust.c
blob: 7dd43a7830fd886dfe011d3638fdae36aee0a501 (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Inputs the model will never see in training but will see in production.
 *
 * The model normalises its input by the window's own standard deviation, so
 * digital silence divides by roughly the epsilon in the normalisation and a
 * constant offset does the same. Those are the cases that produce infinities
 * if the epsilon is dropped or applied in the wrong place, and a whole window
 * of silence is entirely ordinary in a real recording. */

#include "pyseg.h"
#include "test_util.h"

#include <math.h>
#include <stdlib.h>
#include <string.h>

static unsigned rng_state = 987654321u;

static float next_float(void) {
    rng_state = rng_state * 1664525u + 1013904223u;
    return (float)((rng_state >> 8) & 0xFFFF) / 32768.0f - 1.0f;
}

static void check_output(const char *label, const float *probs) {
    for (int t = 0; t < PYSEG_FRAMES; ++t) {
        const float *row = probs + (size_t)t * PYSEG_CLASSES;
        double sum = 0.0;
        for (int i = 0; i < PYSEG_CLASSES; ++i) {
            if (!isfinite(row[i]) || row[i] < 0.0f || row[i] > 1.0f) {
                TEST_ASSERT(0, "%s: frame %d class %d is %g", label, t, i, row[i]);
                return;
            }
            sum += row[i];
        }
        if (fabs(sum - 1.0) > 1e-5) {
            TEST_ASSERT(0, "%s: frame %d sums to %.9f", label, t, sum);
            return;
        }
    }
    TEST_ASSERT(1, "%s", label);
}

int main(void) {
    pyseg_t *v = pyseg_create();
    TEST_ASSERT(v != NULL, "create failed");
    if (!v)
        return TEST_REPORT();

    float *wav = malloc(PYSEG_WINDOW_SAMPLES * sizeof(float));
    float *probs = malloc((size_t)PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float));
    float *active = malloc((size_t)PYSEG_FRAMES * PYSEG_SPEAKERS * sizeof(float));
    int16_t *pcm = malloc(PYSEG_WINDOW_SAMPLES * sizeof(int16_t));

    memset(wav, 0, PYSEG_WINDOW_SAMPLES * sizeof(float));
    pyseg_process_f32(v, wav, probs);
    check_output("digital silence", probs);

    for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i)
        wav[i] = 0.5f;
    pyseg_process_f32(v, wav, probs);
    check_output("constant offset", probs);

    for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i)
        wav[i] = (i & 1) ? 1.0f : -1.0f;
    pyseg_process_f32(v, wav, probs);
    check_output("full-scale square wave", probs);

    for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i)
        wav[i] = next_float() * 4.0f;
    pyseg_process_f32(v, wav, probs);
    check_output("out-of-range noise", probs);

    for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i)
        wav[i] = next_float() * 1e-8f;
    pyseg_process_f32(v, wav, probs);
    check_output("near-denormal noise", probs);

    /* A single impulse in an otherwise silent window: the normalisation
     * amplifies the silence around it by the full dynamic range. */
    memset(wav, 0, PYSEG_WINDOW_SAMPLES * sizeof(float));
    wav[PYSEG_WINDOW_SAMPLES / 2] = 1.0f;
    pyseg_process_f32(v, wav, probs);
    check_output("lone impulse", probs);

    for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i)
        pcm[i] = (int16_t)(i & 1 ? 32767 : -32768);
    pyseg_process_s16(v, pcm, probs);
    check_output("s16 full scale", probs);

    /* The s16 entry point must be the f32 one with a fixed scaling. */
    for (int i = 0; i < PYSEG_WINDOW_SAMPLES; ++i) {
        pcm[i] = (int16_t)((int)(next_float() * 16000.0f));
        wav[i] = (float)pcm[i] * (1.0f / 32768.0f);
    }
    float *from_s16 = malloc((size_t)PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float));
    pyseg_process_s16(v, pcm, from_s16);
    pyseg_process_f32(v, wav, probs);
    TEST_ASSERT(memcmp(from_s16, probs, (size_t)PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float)) == 0,
                "s16 and f32 entry points disagree on identical audio");

    /* Decoding must land inside the powerset and stay consistent with it. */
    pyseg_to_multilabel(probs, active);
    for (int t = 0; t < PYSEG_FRAMES; ++t) {
        const float *a = active + (size_t)t * PYSEG_SPEAKERS;
        int count = 0;
        for (int s = 0; s < PYSEG_SPEAKERS; ++s) {
            if (a[s] != 0.0f && a[s] != 1.0f) {
                TEST_ASSERT(0, "frame %d speaker %d is %g, not a hard decision", t, s, a[s]);
                break;
            }
            count += a[s] != 0.0f;
        }
        if (count > 2) {
            TEST_ASSERT(0, "frame %d has %d speakers, more than the powerset allows", t, count);
            break;
        }
    }
    TEST_ASSERT(1, "multilabel decoding stays inside the powerset");

    TEST_ASSERT(pyseg_powerset_class(-1, -1) == 0, "empty set is not class 0");
    TEST_ASSERT(pyseg_powerset_class(0, -1) == 1, "speaker 0 alone is not class 1");
    TEST_ASSERT(pyseg_powerset_class(2, -1) == 3, "speaker 2 alone is not class 3");
    TEST_ASSERT(pyseg_powerset_class(0, 1) == 4, "speakers 0 and 1 are not class 4");
    TEST_ASSERT(pyseg_powerset_class(1, 2) == 6, "speakers 1 and 2 are not class 6");

    free(from_s16);
    free(pcm);
    free(active);
    free(probs);
    free(wav);
    pyseg_destroy(v);
    pyseg_destroy(NULL);
    return TEST_REPORT();
}