-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathamfm_test.c
112 lines (82 loc) · 2.38 KB
/
amfm_test.c
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
/* Copyright (c) 2018 Peter Teichman */
#ifdef ROTO_TEST
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "greatest.h"
#include "amfm.h"
TEST test_fill_sinemod() {
int16_t vals[256] = {0};
fill_sinemod(vals, -127, 127, 0);
// Check our two zero crossings.
ASSERT_EQ(0, vals[0]);
ASSERT_EQ(0, vals[128]);
// And our peaks.
ASSERT_EQ(127, vals[64]);
ASSERT_EQ(-127, vals[192]);
for (int i = 0; i < 256; i++) {
ASSERTm("underflow", vals[i] >= -127);
ASSERTm("overflow", vals[i] <= 127);
}
PASS();
}
TEST test_fill_sinemod_zeros() {
int16_t vals[256] = {0};
fill_sinemod(vals, 0, 0, 0);
for (int i = 0; i < 256; i++) {
ASSERT_EQ(0, vals[i]);
}
PASS();
}
TEST test_fill_sinemod_phase() {
int16_t vals[256] = {0};
// Shift the sine by a quarter turn: now it's a cosine.
fill_sinemod(vals, -127, 127, (1 << 15) / 4);
// Check our two zero crossings.
ASSERT_EQ(0, vals[64]);
ASSERT_EQ(0, vals[192]);
// And our peaks.
ASSERT_EQ(127, vals[0]);
ASSERT_EQ(-127, vals[128]);
for (int i = 0; i < 256; i++) {
ASSERTm("underflow", vals[i] >= -127);
ASSERTm("overflow", vals[i] <= 127);
}
PASS();
}
TEST test_fill_sinemod_amplitude() {
int16_t vals[256] = {0};
fill_sinemod(vals, 0, 127, 0);
for (int i = 0; i < 256; i++) {
ASSERTm("underflow", vals[i] >= 0);
ASSERTm("overflow", vals[i] <= 127);
}
PASS();
}
TEST test_amfm_update() {
// Make sure a constant input results in a constant output.
int srcdst_len = 8;
int16_t src[8] = {1024, 1014, 1024, 1024, 1024, 1024, 1024, 1024};
int16_t dst[8] = {0};
int16_t ringbuf[512] = {0};
int ringbuf_len = 512;
int16_t volume[257] = {0};
for (int i = 0; i < 257; i++) {
volume[i] = (1 << 15) - 1;
}
int16_t offset[257] = {0};
fill_sinemod(offset, 0, (int16_t)(44.1 * 256), 0); // 0ms -> ~1ms delay (44100 / 1000)
offset[256] = offset[0];
int wp = 0;
uint32_t phase = 0;
amfm_update(dst, src, srcdst_len, ringbuf, ringbuf_len, &wp, volume, offset, 1 << 24, &phase);
PASS();
}
GREATEST_SUITE(amfm_suite) {
RUN_TEST(test_fill_sinemod);
RUN_TEST(test_fill_sinemod_zeros);
RUN_TEST(test_fill_sinemod_phase);
RUN_TEST(test_fill_sinemod_amplitude);
RUN_TEST(test_amfm_update);
}
#endif