Some code semantics refactoring.

This commit is contained in:
Johannes Findeisen 2026-10-27 01:37:21 +01:00
commit 0f04e056b2
42 changed files with 3374 additions and 1357 deletions

View file

@ -1,12 +1,14 @@
#include "xtc/xtc_native.h"
#include <math.h>
#include <stdlib.h>
struct xtc_native {
xtc_native_descriptor descriptor;
void *state;
size_t max_frames;
uint64_t next_position;
};
xtc_result xtc_native_validate(const xtc_native_descriptor *d)
{
size_t i;
@ -14,8 +16,8 @@ xtc_result xtc_native_validate(const xtc_native_descriptor *d)
return XTC_ERR_INVALID_ARGUMENT;
if (d->abi_version != XTC_NATIVE_ABI_VERSION || d->struct_size != sizeof(*d))
return XTC_ERR_UNSUPPORTED;
if (d->kind < XTC_NATIVE_EFFECT || d->kind > XTC_NATIVE_INSTRUMENT || !d->prepare ||
!d->reset || !d->process || !d->destroy || !d->identifier[0])
if (d->kind < XTC_NATIVE_EFFECT || d->kind > XTC_NATIVE_INSTRUMENT || !d->prepare || !d->reset || !d->process ||
!d->destroy || !d->identifier[0])
return XTC_ERR_INVALID_ARGUMENT;
for (i = 0; i < sizeof(d->identifier); ++i) {
unsigned char c = (unsigned char)d->identifier[i];
@ -26,8 +28,8 @@ xtc_result xtc_native_validate(const xtc_native_descriptor *d)
}
return XTC_ERR_INVALID_ARGUMENT;
}
xtc_result xtc_native_create(const xtc_native_descriptor *d, double rate, size_t frames,
float value, xtc_native **out)
xtc_result xtc_native_create(const xtc_native_descriptor *d, double rate, size_t frames, float value, xtc_native **out)
{
xtc_native *p;
xtc_result r;
@ -37,8 +39,7 @@ xtc_result xtc_native_create(const xtc_native_descriptor *d, double rate, size_t
r = xtc_native_validate(d);
if (r != XTC_OK)
return r;
if (!isfinite(rate) || rate <= 0 || rate > 768000 || !frames || frames > 65536 ||
!isfinite(value))
if (!isfinite(rate) || rate <= 0 || rate > 768000 || !frames || frames > 65536 || !isfinite(value))
return XTC_ERR_INVALID_ARGUMENT;
p = calloc(1, sizeof(*p));
if (!p)
@ -55,6 +56,7 @@ xtc_result xtc_native_create(const xtc_native_descriptor *d, double rate, size_t
*out = p;
return XTC_OK;
}
void xtc_native_destroy(xtc_native *p)
{
if (p) {
@ -62,6 +64,7 @@ void xtc_native_destroy(xtc_native *p)
free(p);
}
}
void xtc_native_reset(xtc_native *p)
{
if (p) {
@ -69,21 +72,21 @@ void xtc_native_reset(xtc_native *p)
p->next_position = UINT64_MAX;
}
}
xtc_result xtc_native_process(xtc_native *p, const xtc_native_block *b)
{
size_t i;
xtc_result r;
if (!p || !b || !b->output || b->position > UINT64_MAX - b->frames ||
b->event_count > XTC_MIDI_MAX_EVENTS || (b->event_count && !b->events) ||
(b->event_count && p->descriptor.kind != XTC_NATIVE_INSTRUMENT))
if (!p || !b || !b->output || b->position > UINT64_MAX - b->frames || b->event_count > XTC_MIDI_MAX_EVENTS ||
(b->event_count && !b->events) || (b->event_count && p->descriptor.kind != XTC_NATIVE_INSTRUMENT))
return XTC_ERR_INVALID_ARGUMENT;
if (b->frames > p->max_frames)
return XTC_ERR_CAPACITY;
for (i = 0; i < b->event_count; ++i) {
const xtc_midi_event *e = b->events + i;
if (e->offset >= b->frames || (i && e->offset < b->events[i - 1].offset) ||
e->type < XTC_MIDI_NOTE_ON || e->type > XTC_MIDI_ALL_OFF || e->channel > 15 ||
e->pitch > 127 || e->velocity > 127 || (e->type == XTC_MIDI_NOTE_ON && !e->velocity))
if (e->offset >= b->frames || (i && e->offset < b->events[i - 1].offset) || e->type < XTC_MIDI_NOTE_ON ||
e->type > XTC_MIDI_ALL_OFF || e->channel > 15 || e->pitch > 127 || e->velocity > 127 ||
(e->type == XTC_MIDI_NOTE_ON && !e->velocity))
return XTC_ERR_INVALID_ARGUMENT;
}
if (!b->frames)