-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_render_samples_api.c
142 lines (140 loc) · 4.35 KB
/
test_render_samples_api.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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sointu.h>
#define BPM 100
#define SAMPLE_RATE 44100
#define LENGTH_IN_ROWS 16
#define SAMPLES_PER_ROW SAMPLE_RATE * 4 * 60 / (BPM * 16)
const int su_max_samples = SAMPLES_PER_ROW * LENGTH_IN_ROWS;
int main(int argc, char *argv[])
{
Synth *synth;
float *buffer;
const unsigned char opcodes[] = {SU_ENVELOPE_ID, // MONO
SU_ENVELOPE_ID, // MONO
SU_OUT_ID + 1, // STEREO
SU_ADVANCE_ID}; // MONO
const unsigned char operands[] = {64, 64, 64, 80, 128, // envelope 1
95, 64, 64, 80, 128, // envelope 2
128};
int errcode;
int time;
int samples;
int totalrendered;
int retval;
// initialize Synth
synth = (Synth *)malloc(sizeof(Synth));
memset(synth, 0, sizeof(Synth));
memcpy(synth->Opcodes, opcodes, sizeof(opcodes));
memcpy(synth->Operands, operands, sizeof(operands));
synth->NumVoices = 1;
synth->Polyphony = 0;
synth->RandSeed = 1;
// initialize Buffer
buffer = (float *)malloc(2 * sizeof(float) * su_max_samples);
// triger first voice
synth->SynthWrk.Voices[0].Note = 64;
synth->SynthWrk.Voices[0].Sustain = 1;
totalrendered = 0;
// First check that when we render using su_render with 0 time
// we get nothing done
samples = su_max_samples;
time = 0;
errcode = su_render(synth, buffer, &samples, &time);
if (errcode != 0)
goto fail;
if (samples > 0)
{
printf("su_render rendered samples, despite it should not\n");
goto fail;
}
if (time > 0)
{
printf("su_render advanced time, despite it should not\n");
goto fail;
}
// Then check that when we render using su_render with 0 samples,
// we get nothing done
samples = 0;
time = INT32_MAX;
errcode = su_render(synth, buffer, &samples, &time);
if (errcode != 0)
goto fail;
if (samples > 0)
{
printf("su_render rendered samples, despite it should not\n");
goto fail;
}
if (time > 0)
{
printf("su_render advanced time, despite it should not\n");
goto fail;
}
// Then check that each time we call render, only SAMPLES_PER_ROW
// number of samples are rendered
for (int i = 0; i < 16; i++)
{
// Simulate "small buffers" i.e. render a buffer with 1 sample
// check that buffer full
samples = 1;
time = INT32_MAX;
errcode = su_render(synth, &buffer[totalrendered * 2], &samples, &time);
if (errcode != 0)
goto fail;
totalrendered += samples;
if (samples != 1)
{
printf("su_render should have return 1, as it should have believed buffer is full");
goto fail;
}
if (time != 1)
{
printf("su_render should have advanced the time also by one");
goto fail;
}
samples = SAMPLES_PER_ROW - 1;
time = INT32_MAX;
errcode = su_render(synth, &buffer[totalrendered * 2], &samples, &time);
if (errcode != 0)
goto fail;
totalrendered += samples;
if (samples != SAMPLES_PER_ROW - 1)
{
printf("su_render should have return SAMPLES_PER_ROW - 1, as it should have believed buffer is full");
goto fail;
}
if (time != SAMPLES_PER_ROW - 1)
{
printf("su_render should have advanced the time also by SAMPLES_PER_ROW - 1");
goto fail;
}
if (i == 8)
synth->SynthWrk.Voices[0].Sustain = 0;
}
if (totalrendered != su_max_samples)
{
printf("su_render should have rendered a total of su_max_samples");
goto fail;
}
retval = 0;
finish:
free(synth);
free(buffer);
return retval;
fail:
if (errcode > 0)
{
if ((errcode & 0xFF00) != 0)
printf("FPU stack was not empty on exit\n");
if ((errcode & 0x04) != 0)
printf("FPU zero divide\n");
if ((errcode & 0x01) != 0)
printf("FPU invalid operation\n");
if ((errcode & 0x40) != 0)
printf("FPU stack error\n");
}
retval = 1;
goto finish;
}