code/text formatting and cleaning up whitespace

This commit is contained in:
5684185+vsariola@users.noreply.github.com 2023-09-24 10:47:54 +03:00
parent f35f948118
commit 00850c8001
8 changed files with 96 additions and 71 deletions

View File

@ -167,7 +167,10 @@ wat2wasm --enable-bulk-memory test_chords.wat
#### Examples #### Examples
The folder `examples/code` contains usage examples for Sointu with winmm und dsound playback under Windows and asound playback under Unix. Source code is available in C and x86 assembly (win32, elf32 and elf64 versions). The folder `examples/code` contains usage examples for Sointu with winmm
and dsound playback under Windows and asound playback under Unix. Source
code is available in C and x86 assembly (win32, elf32 and elf64
versions).
To build the examples, use `ninja examples`. To build the examples, use `ninja examples`.

View File

@ -4,20 +4,21 @@
#include <sointu.h> #include <sointu.h>
#include "test_render_samples.h" #include "test_render_samples.h"
void SU_CALLCONV su_render_song(float* buffer) { void SU_CALLCONV su_render_song(float *buffer)
Synth* synth; {
const unsigned char commands[] = { SU_ENVELOPE_ID, // MONO Synth *synth;
const unsigned char commands[] = {SU_ENVELOPE_ID, // MONO
SU_ENVELOPE_ID, // MONO SU_ENVELOPE_ID, // MONO
SU_OUT_ID + 1, // STEREO SU_OUT_ID + 1, // STEREO
SU_ADVANCE_ID };// MONO SU_ADVANCE_ID}; // MONO
const unsigned char values[] = { 64, 64, 64, 80, 128, // envelope 1 const unsigned char values[] = {64, 64, 64, 80, 128, // envelope 1
95, 64, 64, 80, 128, // envelope 2 95, 64, 64, 80, 128, // envelope 2
128}; 128};
int retval; int retval;
int samples; int samples;
int time; int time;
// initialize Synth // initialize Synth
synth = (Synth*)malloc(sizeof(Synth)); synth = (Synth *)malloc(sizeof(Synth));
memset(synth, 0, sizeof(Synth)); memset(synth, 0, sizeof(Synth));
memcpy(synth->Commands, commands, sizeof(commands)); memcpy(synth->Commands, commands, sizeof(commands));
memcpy(synth->Values, values, sizeof(values)); memcpy(synth->Values, values, sizeof(values));

View File

@ -10,23 +10,24 @@
#define SAMPLES_PER_ROW SAMPLE_RATE * 4 * 60 / (BPM * 16) #define SAMPLES_PER_ROW SAMPLE_RATE * 4 * 60 / (BPM * 16)
const int su_max_samples = SAMPLES_PER_ROW * LENGTH_IN_ROWS; const int su_max_samples = SAMPLES_PER_ROW * LENGTH_IN_ROWS;
int main(int argc, char* argv[]) { int main(int argc, char *argv[])
Synth* synth; {
float* buffer; Synth *synth;
const unsigned char commands[] = { SU_ENVELOPE_ID, // MONO float *buffer;
const unsigned char commands[] = {SU_ENVELOPE_ID, // MONO
SU_ENVELOPE_ID, // MONO SU_ENVELOPE_ID, // MONO
SU_OUT_ID + 1, // STEREO SU_OUT_ID + 1, // STEREO
SU_ADVANCE_ID };// MONO SU_ADVANCE_ID}; // MONO
const unsigned char values[] = { 64, 64, 64, 80, 128, // envelope 1 const unsigned char values[] = {64, 64, 64, 80, 128, // envelope 1
95, 64, 64, 80, 128, // envelope 2 95, 64, 64, 80, 128, // envelope 2
128 }; 128};
int errcode; int errcode;
int time; int time;
int samples; int samples;
int totalrendered; int totalrendered;
int retval; int retval;
// initialize Synth // initialize Synth
synth = (Synth*)malloc(sizeof(Synth)); synth = (Synth *)malloc(sizeof(Synth));
memset(synth, 0, sizeof(Synth)); memset(synth, 0, sizeof(Synth));
memcpy(synth->Commands, commands, sizeof(commands)); memcpy(synth->Commands, commands, sizeof(commands));
memcpy(synth->Values, values, sizeof(values)); memcpy(synth->Values, values, sizeof(values));
@ -34,7 +35,7 @@ int main(int argc, char* argv[]) {
synth->Polyphony = 0; synth->Polyphony = 0;
synth->RandSeed = 1; synth->RandSeed = 1;
// initialize Buffer // initialize Buffer
buffer = (float*)malloc(2 * sizeof(float) * su_max_samples); buffer = (float *)malloc(2 * sizeof(float) * su_max_samples);
// triger first voice // triger first voice
synth->SynthWrk.Voices[0].Note = 64; synth->SynthWrk.Voices[0].Note = 64;
synth->SynthWrk.Voices[0].Sustain = 1; synth->SynthWrk.Voices[0].Sustain = 1;
@ -75,12 +76,13 @@ int main(int argc, char* argv[]) {
} }
// Then check that each time we call render, only SAMPLES_PER_ROW // Then check that each time we call render, only SAMPLES_PER_ROW
// number of samples are rendered // number of samples are rendered
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++)
{
// Simulate "small buffers" i.e. render a buffer with 1 sample // Simulate "small buffers" i.e. render a buffer with 1 sample
// check that buffer full // check that buffer full
samples = 1; samples = 1;
time = INT32_MAX; time = INT32_MAX;
errcode = su_render(synth, &buffer[totalrendered*2], &samples, &time); errcode = su_render(synth, &buffer[totalrendered * 2], &samples, &time);
if (errcode != 0) if (errcode != 0)
goto fail; goto fail;
totalrendered += samples; totalrendered += samples;
@ -124,7 +126,8 @@ finish:
free(buffer); free(buffer);
return retval; return retval;
fail: fail:
if (errcode > 0) { if (errcode > 0)
{
if ((errcode & 0xFF00) != 0) if ((errcode & 0xFF00) != 0)
printf("FPU stack was not empty on exit\n"); printf("FPU stack was not empty on exit\n");
if ((errcode & 0x04) != 0) if ((errcode & 0x04) != 0)

View File

@ -1,4 +1,4 @@
#if defined (_WIN32) #if defined(_WIN32)
#define _CRT_SECURE_NO_DEPRECATE #define _CRT_SECURE_NO_DEPRECATE
#include <windows.h> #include <windows.h>
#else #else
@ -21,9 +21,9 @@ float syncBuf[SU_SYNCBUFFER_LENGTH];
float fileSyncBuf[SU_BUFFER_LENGTH]; float fileSyncBuf[SU_BUFFER_LENGTH];
#endif #endif
int main(int argc, char *argv[])
int main(int argc, char* argv[]) { {
FILE* f; FILE *f;
char filename[256]; char filename[256];
int n; int n;
char test_name[] = TEST_NAME; char test_name[] = TEST_NAME;
@ -33,18 +33,19 @@ int main(int argc, char* argv[]) {
float max_diff; float max_diff;
float diff; float diff;
if (argc < 2) { if (argc < 2)
{
fprintf(stderr, "usage: [test] path/to/expected_wave.raw"); fprintf(stderr, "usage: [test] path/to/expected_wave.raw");
return 1; return 1;
} }
#ifdef SU_LOAD_GMDLS #ifdef SU_LOAD_GMDLS
su_load_gmdls(); su_load_gmdls();
#endif #endif
su_render_song(buf); su_render_song(buf);
#if defined (_WIN32) #if defined(_WIN32)
CreateDirectory(actual_output_folder, NULL); CreateDirectory(actual_output_folder, NULL);
#else #else
mkdir(actual_output_folder, 0777); mkdir(actual_output_folder, 0777);
@ -52,19 +53,20 @@ int main(int argc, char* argv[]) {
snprintf(filename, sizeof filename, "%s%s%s", actual_output_folder, test_name, ".raw"); snprintf(filename, sizeof filename, "%s%s%s", actual_output_folder, test_name, ".raw");
f = fopen(filename, "wb"); f = fopen(filename, "wb");
fwrite((void*)buf, sizeof(SUsample), SU_BUFFER_LENGTH, f); fwrite((void *)buf, sizeof(SUsample), SU_BUFFER_LENGTH, f);
fclose(f); fclose(f);
#ifdef SU_SYNC #ifdef SU_SYNC
snprintf(filename, sizeof filename, "%s%s%s", actual_output_folder, test_name, "_syncbuf.raw"); snprintf(filename, sizeof filename, "%s%s%s", actual_output_folder, test_name, "_syncbuf.raw");
f = fopen(filename, "wb"); f = fopen(filename, "wb");
fwrite((void*)syncBuf, sizeof(float), SU_SYNCBUFFER_LENGTH, f); fwrite((void *)syncBuf, sizeof(float), SU_SYNCBUFFER_LENGTH, f);
fclose(f); fclose(f);
#endif #endif
f = fopen(argv[1], "rb"); f = fopen(argv[1], "rb");
if (f == NULL) { if (f == NULL)
{
fprintf(stderr, "No expected waveform found!\n"); fprintf(stderr, "No expected waveform found!\n");
goto fail; goto fail;
} }
@ -73,42 +75,49 @@ int main(int argc, char* argv[]) {
fsize = ftell(f); fsize = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
if (SU_BUFFER_LENGTH * sizeof(SUsample) < fsize) { if (SU_BUFFER_LENGTH * sizeof(SUsample) < fsize)
{
fprintf(stderr, "Sointu rendered shorter wave than expected\n"); fprintf(stderr, "Sointu rendered shorter wave than expected\n");
goto fail; goto fail;
} }
if (SU_BUFFER_LENGTH * sizeof(SUsample) > fsize) { if (SU_BUFFER_LENGTH * sizeof(SUsample) > fsize)
{
fprintf(stderr, "Sointu rendered longer wave than expected\n"); fprintf(stderr, "Sointu rendered longer wave than expected\n");
goto fail; goto fail;
} }
fread((void*)filebuf, fsize, 1, f); fread((void *)filebuf, fsize, 1, f);
fclose(f); fclose(f);
f = NULL; f = NULL;
max_diff = 0.0f; max_diff = 0.0f;
for (n = 0; n < SU_BUFFER_LENGTH; n++) { 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)) { 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"); fprintf(stderr, "Sointu rendered different wave than expected\n");
goto fail; goto fail;
} }
if (diff > max_diff) { if (diff > max_diff)
{
max_diff = diff; max_diff = diff;
} }
} }
if (max_diff > 1e-6) { if (max_diff > 1e-6)
fprintf(stderr, "Warning: Sointu rendered almost correct wave, but a small maximum error of %f\n",max_diff); {
fprintf(stderr, "Warning: Sointu rendered almost correct wave, but a small maximum error of %f\n", max_diff);
} }
#ifdef SU_SYNC #ifdef SU_SYNC
f = fopen(argv[2], "rb"); f = fopen(argv[2], "rb");
if (f == NULL) { if (f == NULL)
{
fprintf(stderr, "No expected sync waveform found!\n"); fprintf(stderr, "No expected sync waveform found!\n");
goto fail; goto fail;
} }
@ -117,25 +126,29 @@ int main(int argc, char* argv[]) {
fsize = ftell(f); fsize = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
if (SU_SYNCBUFFER_LENGTH * sizeof(float) < fsize) { if (SU_SYNCBUFFER_LENGTH * sizeof(float) < fsize)
{
fprintf(stderr, "Sointu rendered shorter sync wave than expected\n"); fprintf(stderr, "Sointu rendered shorter sync wave than expected\n");
goto fail; goto fail;
} }
if (SU_SYNCBUFFER_LENGTH * sizeof(float) > fsize) { if (SU_SYNCBUFFER_LENGTH * sizeof(float) > fsize)
{
fprintf(stderr, "Sointu rendered longer sync wave than expected\n"); fprintf(stderr, "Sointu rendered longer sync wave than expected\n");
goto fail; goto fail;
} }
fread((void*)fileSyncBuf, fsize, 1, f); fread((void *)fileSyncBuf, fsize, 1, f);
fclose(f); fclose(f);
f = NULL; f = NULL;
max_diff = 0.0f; max_diff = 0.0f;
for (n = 0; n < SU_SYNCBUFFER_LENGTH; n++) { for (n = 0; n < SU_SYNCBUFFER_LENGTH; n++)
{
diff = (float)fabs(syncBuf[n] - fileSyncBuf[n]); diff = (float)fabs(syncBuf[n] - fileSyncBuf[n]);
if (diff > 1e-3f || isnan(diff)) { if (diff > 1e-3f || isnan(diff))
{
fprintf(stderr, "Sointu rendered different sync wave than expected\n"); fprintf(stderr, "Sointu rendered different sync wave than expected\n");
goto fail; goto fail;
} }
@ -144,7 +157,8 @@ int main(int argc, char* argv[]) {
return 0; return 0;
fail: fail:
if (f != NULL) { if (f != NULL)
{
fclose(f); fclose(f);
f = NULL; f = NULL;
} }

View File

@ -1,4 +1,6 @@
//go:build ignore
// +build ignore // +build ignore
package main package main
import ( import (

View File

@ -1,4 +1,6 @@
//go:build ignore
// +build ignore // +build ignore
package main package main
import ( import (