mirror of
https://github.com/vsariola/sointu.git
synced 2025-07-18 21:14:31 -04:00
Change the sointu.h api to return -1, 0 or n>0 depending if buffer is full and/or row ended.
test_render_samples_api.c was added to test the api. bridge.go was modified to reflect that there is no need to check for row manually; su_render_samples already returns the information if a row has ended.
This commit is contained in:
@ -151,4 +151,8 @@ regression_test(test_chords "ENVELOPE;VCO_SINE")
|
||||
regression_test(test_speed "ENVELOPE;VCO_SINE")
|
||||
|
||||
regression_test(test_render_samples ENVELOPE "" test_render_samples.c)
|
||||
target_link_libraries(test_render_samples sointu)
|
||||
target_link_libraries(test_render_samples sointu)
|
||||
|
||||
add_executable(test_render_samples_api test_render_samples_api.c)
|
||||
target_link_libraries(test_render_samples_api sointu)
|
||||
add_test(test_render_samples_api test_render_samples_api)
|
||||
|
@ -34,7 +34,7 @@ void CALLCONV su_render(float* buffer) {
|
||||
memcpy(synthState->Commands, commands, sizeof(commands));
|
||||
memcpy(synthState->Values, values, sizeof(values));
|
||||
synthState->RandSeed = 1;
|
||||
synthState->RowLen = INT32_MAX;
|
||||
synthState->SamplesPerRow = INT32_MAX;
|
||||
synthState->NumVoices = 1;
|
||||
synthState->Synth.Voices[0].Note = 64;
|
||||
retval = su_render_samples(synthState, su_max_samples / 2, buffer);
|
||||
|
92
tests/test_render_samples_api.c
Normal file
92
tests/test_render_samples_api.c
Normal file
@ -0,0 +1,92 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "../include/sointu.h"
|
||||
|
||||
#define BPM 100
|
||||
#define SAMPLE_RATE 44100
|
||||
#define TOTAL_ROWS 16
|
||||
#define SAMPLES_PER_ROW SAMPLE_RATE * 4 * 60 / (BPM * 16)
|
||||
const int su_max_samples = SAMPLES_PER_ROW * TOTAL_ROWS;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
SynthState* synthState;
|
||||
float* buffer;
|
||||
const unsigned char commands[] = { su_envelope_id, // MONO
|
||||
su_envelope_id, // MONO
|
||||
su_out_id + 1, // STEREO
|
||||
su_advance_id };// MONO
|
||||
const unsigned char values[] = { 64, 64, 64, 80, 128, // envelope 1
|
||||
95, 64, 64, 80, 128, // envelope 2
|
||||
128 };
|
||||
int remaining, remainingOut;
|
||||
int retval;
|
||||
synthState = (SynthState*)malloc(sizeof(SynthState));
|
||||
buffer = (float*)malloc(2 * sizeof(float) * su_max_samples);
|
||||
memset(synthState, 0, sizeof(SynthState));
|
||||
memcpy(synthState->Commands, commands, sizeof(commands));
|
||||
memcpy(synthState->Values, values, sizeof(values));
|
||||
synthState->RandSeed = 1;
|
||||
synthState->NumVoices = 1;
|
||||
synthState->Synth.Voices[0].Note = 64;
|
||||
remaining = su_max_samples;
|
||||
// First check that when RowLen = 0, we render nothing and remaining does not change
|
||||
synthState->SamplesPerRow = 0;
|
||||
if (su_render_samples(synthState, remaining, buffer) != remaining)
|
||||
{
|
||||
printf("su_render_samples rendered samples despite number of samples per row being 0");
|
||||
goto fail;
|
||||
}
|
||||
// Then check that each time we call render, only SAMPLES_PER_ROW
|
||||
// number of samples are rendered
|
||||
synthState->SamplesPerRow = SAMPLES_PER_ROW;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
// Simulate "small buffers" i.e. render a buffer with 1 sample
|
||||
// check that buffer full
|
||||
remainingOut = su_render_samples(synthState, 1, &buffer[2 * (su_max_samples - remaining)]);
|
||||
if (remainingOut != -1)
|
||||
{
|
||||
printf("su_render_samples should have return -1, as it should have believed buffer is full");
|
||||
goto fail;
|
||||
}
|
||||
if (synthState->RowTick != 1)
|
||||
{
|
||||
printf("su_render_samples RowTick should be at 1 after rendering 1 tick of a row");
|
||||
goto fail;
|
||||
}
|
||||
remaining--; // we rendered just one sample
|
||||
remainingOut = su_render_samples(synthState, remaining, &buffer[2 * (su_max_samples - remaining)]);
|
||||
if (remainingOut != remaining - SAMPLES_PER_ROW + 1)
|
||||
{
|
||||
printf("su_render_samples did not render SAMPLES_PER_ROW, despite rowLen being SAMPLES_PER_ROW");
|
||||
goto fail;
|
||||
}
|
||||
if (synthState->RowTick != 0)
|
||||
{
|
||||
printf("The row should be have been reseted");
|
||||
goto fail;
|
||||
}
|
||||
remaining = remainingOut;
|
||||
if (i == 8)
|
||||
synthState->Synth.Voices[0].Release++;
|
||||
}
|
||||
if (remaining != 0) {
|
||||
printf("The buffer should be full and row finished");
|
||||
goto fail;
|
||||
}
|
||||
// Finally, now that there is no more buffer remaining, should return -1
|
||||
if (su_render_samples(synthState, remaining, &buffer[2 * (su_max_samples - remaining)]) != -1)
|
||||
{
|
||||
printf("su_render_samples should have ran out of buffer and thus return -1");
|
||||
goto fail;
|
||||
}
|
||||
retval = 0;
|
||||
finish:
|
||||
free(synthState);
|
||||
free(buffer);
|
||||
return retval;
|
||||
fail:
|
||||
retval = 1;
|
||||
goto finish;
|
||||
}
|
Reference in New Issue
Block a user