diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-13 20:03:39 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-13 20:13:28 +0200 |
| commit | 64c3a1b528a88b4ae21bf3cbcff2537200477806 (patch) | |
| tree | 85da00748d7bbb5036aeb5a7a4b60b5abf75aeea /tests/test_slide.c | |
| download | pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.tar.gz pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.zip | |
Init
Diffstat (limited to 'tests/test_slide.c')
| -rw-r--r-- | tests/test_slide.c | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/tests/test_slide.c b/tests/test_slide.c new file mode 100644 index 0000000..a0fc8ca --- /dev/null +++ b/tests/test_slide.c @@ -0,0 +1,212 @@ +/* The sliding-window driver against the aggregation pyannote performs, and the + * binarizer against its own definition. + * + * The aggregate is not just the model run repeatedly: window placement, + * rounding onto the frame grid, the Hamming weights and the dropped tail all + * have to match, and each is a place where an off-by-one produces output that + * looks entirely reasonable and is shifted by a frame. */ + +#include "pyseg.h" +#include "test_util.h" + +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +typedef struct { + unsigned num_samples; + unsigned frames; + double step; + float *samples; + float *tracks; /* [frames][PYSEG_TRACKS] */ +} fixture; + +static int load(const char *path, fixture *f) { + FILE *fp = fopen(path, "rb"); + if (!fp) { + fprintf(stderr, "cannot open %s\n", path); + return -1; + } + char magic[4]; + if (fread(magic, 1, 4, fp) != 4 || memcmp(magic, "PSGS", 4) != 0 || + fread(&f->num_samples, 4, 1, fp) != 1 || fread(&f->frames, 4, 1, fp) != 1 || + fread(&f->step, 8, 1, fp) != 1) { + fprintf(stderr, "%s: not a matching fixture\n", path); + fclose(fp); + return -1; + } + f->samples = malloc((size_t)f->num_samples * sizeof(float)); + f->tracks = malloc((size_t)f->frames * PYSEG_TRACKS * sizeof(float)); + const int ok = f->samples && f->tracks && + fread(f->samples, sizeof(float), f->num_samples, fp) == f->num_samples && + fread(f->tracks, sizeof(float), (size_t)f->frames * PYSEG_TRACKS, fp) == + (size_t)f->frames * PYSEG_TRACKS; + fclose(fp); + if (!ok) { + fprintf(stderr, "%s: truncated\n", path); + return -1; + } + return 0; +} + +static double compare(const float *got, const float *want, size_t n) { + double worst = 0.0; + for (size_t i = 0; i < n; ++i) { + const double d = fabs((double)got[i] - want[i]); + if (d > worst) + worst = d; + } + return worst; +} + +static void check_slide(const fixture *f, int threads) { + pyseg_slide_params p; + pyseg_slide_params_default(&p); + p.step = f->step; + p.threads = threads; + + const size_t frames = pyseg_slide_frames(f->num_samples, p.step); + TEST_ASSERT(frames == f->frames, "threads=%d: %zu frames, reference has %u", threads, frames, + f->frames); + if (frames != f->frames) + return; + + float *tracks = malloc(frames * PYSEG_TRACKS * sizeof(float)); + const size_t got = pyseg_slide(f->samples, f->num_samples, &p, tracks); + TEST_ASSERT(got == frames, "threads=%d: slide returned %zu", threads, got); + + if (got == frames) { + const double worst = compare(tracks, f->tracks, frames * PYSEG_TRACKS); + fprintf(stderr, "slide threads=%d: %zu frames, max |track diff| %.3e\n", threads, frames, + worst); + TEST_ASSERT(worst <= 1e-5, "threads=%d: max |track diff| %.3e", threads, worst); + } + free(tracks); +} + +/* A hand-built score sequence, so the expected segments can be reasoned about + * rather than captured. */ +static void check_binarize(void) { + enum { N = 40 }; + float scores[N * PYSEG_TRACKS]; + memset(scores, 0, sizeof scores); + for (int f = 10; f < 20; ++f) + scores[f * PYSEG_TRACKS] = 1.0f; + for (int f = 22; f < 25; ++f) + scores[f * PYSEG_TRACKS] = 1.0f; + + pyseg_binarize_params p; + pyseg_binarize_params_default(&p); + + pyseg_segment out[8]; + size_t n = pyseg_binarize(scores, N, PYSEG_TRACK_SPEECH, &p, out, 8); + TEST_ASSERT(n == 2, "two runs of ones gave %zu segments", n); + if (n == 2) { + const double half = 0.5 * PYSEG_FRAME_SIZE / PYSEG_SAMPLE_RATE; + TEST_ASSERT(fabs(out[0].start - (pyseg_frame_time(10) + half)) < 1e-9, + "first segment starts at %.6f", out[0].start); + TEST_ASSERT(fabs(out[0].end - (pyseg_frame_time(20) + half)) < 1e-9, + "first segment ends at %.6f", out[0].end); + TEST_ASSERT(fabs(out[1].start - (pyseg_frame_time(22) + half)) < 1e-9, + "second segment starts at %.6f", out[1].start); + } + + /* The gap between the two runs is two frames, about 34 ms. */ + p.min_duration_off = 0.1; + n = pyseg_binarize(scores, N, PYSEG_TRACK_SPEECH, &p, out, 8); + TEST_ASSERT(n == 1, "a 34 ms gap survived a 100 ms fill: %zu segments", n); + + p.min_duration_off = 0.0; + p.min_duration_on = 0.1; + n = pyseg_binarize(scores, N, PYSEG_TRACK_SPEECH, &p, out, 8); + TEST_ASSERT(n == 1, "a 51 ms segment survived a 100 ms minimum: %zu segments", n); + + /* Counting without writing must agree with writing. */ + pyseg_binarize_params_default(&p); + TEST_ASSERT(pyseg_binarize(scores, N, PYSEG_TRACK_SPEECH, &p, NULL, 0) == 2, + "counting pass disagrees with the writing pass"); + TEST_ASSERT(pyseg_binarize(scores, N, PYSEG_TRACK_SPEECH, &p, out, 1) == 2, + "an undersized buffer did not report the full count"); + + /* A track that is high from the first frame to the last is one segment. */ + for (int f = 0; f < N; ++f) + scores[f * PYSEG_TRACKS + PYSEG_TRACK_OVERLAP] = 1.0f; + n = pyseg_binarize(scores, N, PYSEG_TRACK_OVERLAP, &p, out, 8); + TEST_ASSERT(n == 1, "an always-active track gave %zu segments", n); + + TEST_ASSERT(pyseg_binarize(scores, N, PYSEG_TRACKS, &p, out, 8) == 0, + "an out-of-range track was accepted"); +} + +/* Frame counts read off pyannote's Inference for the same inputs. The formula + * involves two roundings and a tail-window rule, so a table of answers taken + * from the reference is worth more than a second derivation of the formula. */ +static void check_frame_counts(void) { + static const struct { + size_t samples; + double step; + size_t frames; + } cases[] = { + {720000, 1.0, 2668}, {640000, 2.0, 2371}, {592000, 0.5, 2194}, + {400000, 3.0, 1482}, {144000, 1.0, 534}, {160000, 1.0, 594}, + }; + for (size_t i = 0; i < sizeof cases / sizeof *cases; ++i) { + const size_t got = pyseg_slide_frames(cases[i].samples, cases[i].step); + TEST_ASSERT(got == cases[i].frames, "%zu samples at step %g: %zu frames, expected %zu", + cases[i].samples, cases[i].step, got, cases[i].frames); + } + TEST_ASSERT(pyseg_slide_frames(0, 1.0) == 0, "an empty recording gave frames"); + TEST_ASSERT(pyseg_slide_frames(160000, 0.0) == 0, "a zero step gave frames"); +} + +int main(int argc, char **argv) { + if (argc < 2) { + fprintf(stderr, "usage: test_slide <slide_golden.bin>\n"); + return 2; + } + fixture f; + if (load(argv[1], &f) != 0) + return 2; + + check_frame_counts(); + + check_slide(&f, 1); + check_slide(&f, 4); + check_binarize(); + + /* The 16-bit entry point must agree with the float one on audio that came + * from 16-bit samples in the first place, which the fixture did. */ + { + int16_t *pcm = malloc(f.num_samples * sizeof(int16_t)); + float *from_s16 = malloc((size_t)f.frames * PYSEG_TRACKS * sizeof(float)); + for (unsigned i = 0; i < f.num_samples; ++i) + pcm[i] = (int16_t)lrintf(f.samples[i] * 32768.0f); + pyseg_slide_params p; + pyseg_slide_params_default(&p); + p.step = f.step; + TEST_ASSERT(pyseg_slide_s16(pcm, f.num_samples, &p, from_s16) == f.frames, + "the 16-bit slide failed"); + const double worst = compare(from_s16, f.tracks, (size_t)f.frames * PYSEG_TRACKS); + TEST_ASSERT(worst <= 1e-5, "16-bit slide differs by %.3e", worst); + free(from_s16); + free(pcm); + } + + /* Recordings shorter than one window are padded, not rejected. */ + float *tiny = calloc(PYSEG_SAMPLE_RATE, sizeof(float)); + for (int i = 0; i < PYSEG_SAMPLE_RATE; ++i) + tiny[i] = 0.1f * sinf((float)i * 0.01f); + const size_t tiny_frames = pyseg_slide_frames(PYSEG_SAMPLE_RATE, 1.0); + TEST_ASSERT(tiny_frames == PYSEG_SAMPLE_RATE / PYSEG_FRAME_STEP + 1, + "a one-second recording gave %zu frames", tiny_frames); + float *tiny_tracks = malloc(tiny_frames * PYSEG_TRACKS * sizeof(float)); + TEST_ASSERT(pyseg_slide(tiny, PYSEG_SAMPLE_RATE, NULL, tiny_tracks) == tiny_frames, + "a one-second recording failed"); + free(tiny_tracks); + free(tiny); + + free(f.samples); + free(f.tracks); + return TEST_REPORT(); +} |
