-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_renderer.c
166 lines (139 loc) · 3.62 KB
/
test_renderer.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#if defined(_WIN32)
#define _CRT_SECURE_NO_DEPRECATE
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include TEST_HEADER
SUsample buf[SU_BUFFER_LENGTH];
SUsample filebuf[SU_BUFFER_LENGTH];
#ifdef SU_SYNC
float syncBuf[SU_SYNCBUFFER_LENGTH];
float fileSyncBuf[SU_BUFFER_LENGTH];
#endif
int main(int argc, char *argv[])
{
FILE *f;
char filename[256];
int n;
char test_name[] = TEST_NAME;
char expected_output_folder[] = "expected_output/";
char actual_output_folder[] = "actual_output/";
long fsize;
float max_diff;
float diff;
if (argc < 2)
{
fprintf(stderr, "usage: [test] path/to/expected_wave.raw");
return 1;
}
#ifdef SU_LOAD_GMDLS
su_load_gmdls();
#endif
su_render_song(buf);
#if defined(_WIN32)
CreateDirectory(actual_output_folder, NULL);
#else
mkdir(actual_output_folder, 0777);
#endif
snprintf(filename, sizeof filename, "%s%s%s", actual_output_folder, test_name, ".raw");
f = fopen(filename, "wb");
fwrite((void *)buf, sizeof(SUsample), SU_BUFFER_LENGTH, f);
fclose(f);
#ifdef SU_SYNC
snprintf(filename, sizeof filename, "%s%s%s", actual_output_folder, test_name, "_syncbuf.raw");
f = fopen(filename, "wb");
fwrite((void *)syncBuf, sizeof(float), SU_SYNCBUFFER_LENGTH, f);
fclose(f);
#endif
f = fopen(argv[1], "rb");
if (f == NULL)
{
fprintf(stderr, "No expected waveform found!\n");
goto fail;
}
fseek(f, 0, SEEK_END);
fsize = ftell(f);
fseek(f, 0, SEEK_SET);
if (SU_BUFFER_LENGTH * sizeof(SUsample) < fsize)
{
fprintf(stderr, "Sointu rendered shorter wave than expected\n");
goto fail;
}
if (SU_BUFFER_LENGTH * sizeof(SUsample) > fsize)
{
fprintf(stderr, "Sointu rendered longer wave than expected\n");
goto fail;
}
fread((void *)filebuf, fsize, 1, f);
fclose(f);
f = NULL;
max_diff = 0.0f;
for (n = 0; n < SU_BUFFER_LENGTH; n++)
{
diff = (float)fabs((float)(buf[n] - filebuf[n]) / SU_SAMPLE_RANGE);
if (diff > 1e-3f || isnan(diff))
{
fprintf(stderr, "Sointu rendered different wave than expected\n");
goto fail;
}
if (diff > max_diff)
{
max_diff = diff;
}
}
if (max_diff > 1e-6)
{
fprintf(stderr, "Warning: Sointu rendered almost correct wave, but a small maximum error of %f\n", max_diff);
}
#ifdef SU_SYNC
f = fopen(argv[2], "rb");
if (f == NULL)
{
fprintf(stderr, "No expected sync waveform found!\n");
goto fail;
}
fseek(f, 0, SEEK_END);
fsize = ftell(f);
fseek(f, 0, SEEK_SET);
if (SU_SYNCBUFFER_LENGTH * sizeof(float) < fsize)
{
fprintf(stderr, "Sointu rendered shorter sync wave than expected\n");
goto fail;
}
if (SU_SYNCBUFFER_LENGTH * sizeof(float) > fsize)
{
fprintf(stderr, "Sointu rendered longer sync wave than expected\n");
goto fail;
}
fread((void *)fileSyncBuf, fsize, 1, f);
fclose(f);
f = NULL;
max_diff = 0.0f;
for (n = 0; n < SU_SYNCBUFFER_LENGTH; n++)
{
diff = (float)fabs(syncBuf[n] - fileSyncBuf[n]);
if (diff > 1e-3f || isnan(diff))
{
fprintf(stderr, "Sointu rendered different sync wave than expected\n");
goto fail;
}
}
#endif
return 0;
fail:
if (f != NULL)
{
fclose(f);
f = NULL;
}
return 1;
}