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

@ -5,7 +5,12 @@
#include <math.h>
#include <stdlib.h>
#include <string.h>
typedef struct { uint64_t start, end, offset; size_t asset, track; } region;
typedef struct {
uint64_t start, end, offset;
size_t asset, track;
} region;
struct xtc_audio_plan {
xtc_mix_snapshot mix;
xtc_wave waves[64];
@ -15,8 +20,9 @@ struct xtc_audio_plan {
char first_issue[XTC_ASSET_PATH_BYTES];
unsigned int rate;
};
xtc_result xtc_project_import_audio(xtc_project *project, uint64_t track,
const char *path, uint64_t start, uint64_t *out_clip)
xtc_result xtc_project_import_audio(xtc_project *project, uint64_t track, const char *path, uint64_t start,
uint64_t *out_clip)
{
xtc_wave wave = {0};
xtc_project *candidate = NULL;
@ -24,121 +30,183 @@ xtc_result xtc_project_import_audio(xtc_project *project, uint64_t track,
uint64_t length, id = 0;
size_t i, source = SIZE_MAX, output = SIZE_MAX;
xtc_result result;
if (!project || !path || !out_clip) return XTC_ERR_INVALID_ARGUMENT;
if (!project || !path || !out_clip)
return XTC_ERR_INVALID_ARGUMENT;
absolute = realpath(path, NULL);
if (!absolute) return XTC_ERR_IO;
if (strlen(absolute) >= XTC_ASSET_PATH_BYTES) { free(absolute); return XTC_ERR_CAPACITY; }
if (!absolute)
return XTC_ERR_IO;
if (strlen(absolute) >= XTC_ASSET_PATH_BYTES) {
free(absolute);
return XTC_ERR_CAPACITY;
}
result = xtc_wave_load(absolute, &wave);
if (result != XTC_OK) { free(absolute); return result; }
length = ((uint64_t)wave.frames * project->tempo * XTC_TICKS_PER_QUARTER +
(uint64_t)wave.rate * 60000U - 1U) / ((uint64_t)wave.rate * 60000U);
if (result != XTC_OK) {
free(absolute);
return result;
}
length = ((uint64_t)wave.frames * project->tempo * XTC_TICKS_PER_QUARTER + (uint64_t)wave.rate * 60000U - 1U) /
((uint64_t)wave.rate * 60000U);
free(wave.samples);
result = xtc_project_clone(project, &candidate);
if (result == XTC_OK) result = xtc_project_add_clip(candidate, track, "Audio", start, length, &id);
if (result == XTC_OK) result = xtc_project_set_clip_asset(candidate, id, absolute);
if (result == XTC_OK)
result = xtc_project_add_clip(candidate, track, "Audio", start, length, &id);
if (result == XTC_OK)
result = xtc_project_set_clip_asset(candidate, id, absolute);
free(absolute);
if (result == XTC_OK) {
for (i = 0; i < candidate->graph->node_count; ++i) {
xtc_node *n = candidate->graph->nodes + i;
if (n->type == XTC_NODE_AUDIO) source = n->id;
if (n->type == XTC_NODE_OUTPUT) output = n->id;
if (n->type == XTC_NODE_AUDIO)
source = n->id;
if (n->type == XTC_NODE_OUTPUT)
output = n->id;
}
if (source == SIZE_MAX) {
result = xtc_graph_add_audio(candidate->graph, &source);
if (result == XTC_OK && output == SIZE_MAX) result = xtc_graph_add_output(candidate->graph, &output);
if (result == XTC_OK) result = xtc_graph_connect(candidate->graph, source, output);
if (result == XTC_OK && output == SIZE_MAX)
result = xtc_graph_add_output(candidate->graph, &output);
if (result == XTC_OK)
result = xtc_graph_connect(candidate->graph, source, output);
}
}
if (result == XTC_OK) {
xtc_project old = *project; *project = *candidate; *candidate = old;
xtc_project old = *project;
*project = *candidate;
*candidate = old;
*out_clip = id;
}
xtc_project_destroy(candidate);
return result;
}
void xtc_audio_plan_destroy(xtc_audio_plan *plan)
{
size_t i;
if (!plan) return;
for (i = 0; i < plan->assets; ++i) free(plan->waves[i].samples);
if (!plan)
return;
for (i = 0; i < plan->assets; ++i)
free(plan->waves[i].samples);
free(plan);
}
xtc_result xtc_audio_plan_create(const xtc_project *project, unsigned int rate, xtc_audio_plan **out)
{
xtc_audio_plan *plan;
size_t i, total = 0;
xtc_result result = XTC_OK;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
if (!out)
return XTC_ERR_INVALID_ARGUMENT;
*out = NULL;
if (!project || !rate || rate > 768000) return XTC_ERR_INVALID_ARGUMENT;
if (!project || !rate || rate > 768000)
return XTC_ERR_INVALID_ARGUMENT;
plan = calloc(1, sizeof(*plan));
if (!plan) return XTC_ERR_OUT_OF_MEMORY;
if (!plan)
return XTC_ERR_OUT_OF_MEMORY;
plan->rate = rate;
result = xtc_mix_prepare(project, rate, &plan->mix);
if (result != XTC_OK) { free(plan); return result; }
if (result != XTC_OK) {
free(plan);
return result;
}
for (i = 0; i < project->clip_count; ++i) {
const xtc_clip_info *clip = project->clips + i;
size_t a; region *r;
if (!clip->asset_path[0]) continue;
for (a = 0; a < plan->assets; ++a) if (!strcmp(plan->paths[a], clip->asset_path)) break;
size_t a;
region *r;
if (!clip->asset_path[0])
continue;
for (a = 0; a < plan->assets; ++a)
if (!strcmp(plan->paths[a], clip->asset_path))
break;
if (a == plan->assets) {
if (a == 64) { result = XTC_ERR_CAPACITY; break; }
strcpy(plan->paths[a], clip->asset_path); ++plan->assets;
if (a == 64) {
result = XTC_ERR_CAPACITY;
break;
}
strcpy(plan->paths[a], clip->asset_path);
++plan->assets;
result = xtc_wave_load(clip->asset_path, plan->waves + a);
if (result == XTC_ERR_CAPACITY || result == XTC_ERR_OUT_OF_MEMORY) break;
if (result == XTC_ERR_CAPACITY || result == XTC_ERR_OUT_OF_MEMORY)
break;
if (result != XTC_OK) {
++plan->issues;
if (!plan->first_issue[0]) strcpy(plan->first_issue, clip->asset_path);
if (!plan->first_issue[0])
strcpy(plan->first_issue, clip->asset_path);
}
result = XTC_OK;
total += plan->waves[a].frames;
if (total > 67108864U) { result = XTC_ERR_CAPACITY; break; }
if (total > 67108864U) {
result = XTC_ERR_CAPACITY;
break;
}
}
r = plan->regions + plan->count++;
r->asset = a; r->track = xtc_mix_track(&plan->mix, clip->track_id);
r->asset = a;
r->track = xtc_mix_track(&plan->mix, clip->track_id);
result = xtc_project_tick_to_sample(project, clip->start_tick, rate, &r->start);
if (result == XTC_OK) result = xtc_project_tick_to_sample(project, clip->start_tick + clip->length_tick, rate, &r->end);
if (result == XTC_OK) result = xtc_project_tick_to_sample(project, clip->offset_tick, rate, &r->offset);
if (result != XTC_OK) break;
if (result == XTC_OK)
result = xtc_project_tick_to_sample(project, clip->start_tick + clip->length_tick, rate, &r->end);
if (result == XTC_OK)
result = xtc_project_tick_to_sample(project, clip->offset_tick, rate, &r->offset);
if (result != XTC_OK)
break;
}
if (result != XTC_OK) { xtc_audio_plan_destroy(plan); return result; }
*out = plan; return XTC_OK;
if (result != XTC_OK) {
xtc_audio_plan_destroy(plan);
return result;
}
*out = plan;
return XTC_OK;
}
size_t xtc_audio_plan_issues(const xtc_audio_plan *plan, const char **path)
{
if (path) *path = plan ? plan->first_issue : "";
if (path)
*path = plan ? plan->first_issue : "";
return plan ? plan->issues : 0;
}
xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t position, float *output, float *right, size_t frames)
xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t position, float *output, float *right,
size_t frames)
{
size_t i, j;
if (!plan || !output || (right && right == output) || frames > 65536U || position > UINT64_MAX - frames) return XTC_ERR_INVALID_ARGUMENT;
if (!plan || !output || (right && right == output) || frames > 65536U || position > UINT64_MAX - frames)
return XTC_ERR_INVALID_ARGUMENT;
memset(output, 0, frames * sizeof(*output));
if (right) memset(right, 0, frames * sizeof(*right));
if (right)
memset(right, 0, frames * sizeof(*right));
for (i = 0; i < plan->count; ++i) {
const region *r = plan->regions + i;
const xtc_wave *wave = plan->waves + r->asset;
uint64_t begin = position > r->start ? position : r->start;
uint64_t end = position + frames < r->end ? position + frames : r->end;
if (!wave->samples || end <= begin) continue;
if (!wave->samples || end <= begin)
continue;
for (j = (size_t)(begin - position); j < (size_t)(end - position); ++j) {
double source = (double)(position + j - r->start + r->offset) * wave->rate / plan->rate;
size_t index;
double fraction;
if (source >= (double)wave->frames) continue;
index = (size_t)source; fraction = source - (double)index;
float sample = (float)(wave->samples[index] * (1.0 - fraction) +
(index + 1 < wave->frames ? wave->samples[index + 1] : wave->samples[index]) * fraction);
if (source >= (double)wave->frames)
continue;
index = (size_t)source;
fraction = source - (double)index;
float sample =
(float)(wave->samples[index] * (1.0 - fraction) +
(index + 1 < wave->frames ? wave->samples[index + 1] : wave->samples[index]) * fraction);
output[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, right ? 0U : 2U);
if (right) right[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, 1);
if (right)
right[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, 1);
}
}
for (j = 0; j < frames; ++j) {
if (!isfinite(output[j])) output[j] = 0;
if (right && !isfinite(right[j])) right[j] = 0;
if (!isfinite(output[j]))
output[j] = 0;
if (right && !isfinite(right[j]))
right[j] = 0;
}
return XTC_OK;
}
xtc_result xtc_audio_plan_render(const xtc_audio_plan *p, uint64_t position, float *out, size_t frames)
{ return xtc_audio_plan_render_stereo(p, position, out, NULL, frames); }
{
return xtc_audio_plan_render_stereo(p, position, out, NULL, frames);
}

View file

@ -4,6 +4,7 @@
#include <float.h>
#include <string.h>
#include <unistd.h>
struct xtc_recorder {
float *ring;
size_t capacity;
@ -13,39 +14,65 @@ struct xtc_recorder {
char *path, *temporary;
int finished;
};
xtc_result xtc_recorder_create(const char *path, unsigned int rate, size_t capacity, xtc_recorder **out)
{
xtc_recorder *r;
size_t length = 0;
int fd;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
if (!out)
return XTC_ERR_INVALID_ARGUMENT;
*out = NULL;
if (!path || !rate || rate > 768000 || !capacity || capacity > 2097152U) return XTC_ERR_INVALID_ARGUMENT;
while (length < 4096 && path[length]) ++length;
if (!length || length == 4096) return XTC_ERR_INVALID_ARGUMENT;
if (sizeof(float) != 4 || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128) return XTC_ERR_UNSUPPORTED;
if (!__atomic_always_lock_free(sizeof(uint64_t), 0) ||
!__atomic_always_lock_free(sizeof(unsigned int), 0)) return XTC_ERR_UNSUPPORTED;
if (!path || !rate || rate > 768000 || !capacity || capacity > 2097152U)
return XTC_ERR_INVALID_ARGUMENT;
while (length < 4096 && path[length])
++length;
if (!length || length == 4096)
return XTC_ERR_INVALID_ARGUMENT;
if (sizeof(float) != 4 || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128)
return XTC_ERR_UNSUPPORTED;
if (!__atomic_always_lock_free(sizeof(uint64_t), 0) || !__atomic_always_lock_free(sizeof(unsigned int), 0))
return XTC_ERR_UNSUPPORTED;
r = calloc(1, sizeof(*r));
if (!r) return XTC_ERR_OUT_OF_MEMORY;
r->capacity = capacity; r->rate = rate;
if (!r)
return XTC_ERR_OUT_OF_MEMORY;
r->capacity = capacity;
r->rate = rate;
r->ring = malloc(capacity * sizeof(float));
r->path = malloc(length + 1); r->temporary = calloc(length + 12, 1);
if (!r->ring || !r->path || !r->temporary) { xtc_recorder_destroy(r); return XTC_ERR_OUT_OF_MEMORY; }
r->path = malloc(length + 1);
r->temporary = calloc(length + 12, 1);
if (!r->ring || !r->path || !r->temporary) {
xtc_recorder_destroy(r);
return XTC_ERR_OUT_OF_MEMORY;
}
memcpy(r->path, path, length + 1);
memcpy(r->temporary, path, length); memcpy(r->temporary + length, ".tmp.XXXXXX", 12);
memcpy(r->temporary, path, length);
memcpy(r->temporary + length, ".tmp.XXXXXX", 12);
fd = mkstemp(r->temporary);
if (fd < 0) { xtc_recorder_destroy(r); return XTC_ERR_IO; }
if (fd < 0) {
xtc_recorder_destroy(r);
return XTC_ERR_IO;
}
r->file = fdopen(fd, "w+b");
if (!r->file) { close(fd); xtc_recorder_destroy(r); return XTC_ERR_IO; }
if (!xtc_wave_header(r->file, rate, 0)) { xtc_recorder_destroy(r); return XTC_ERR_IO; }
*out = r; return XTC_OK;
if (!r->file) {
close(fd);
xtc_recorder_destroy(r);
return XTC_ERR_IO;
}
if (!xtc_wave_header(r->file, rate, 0)) {
xtc_recorder_destroy(r);
return XTC_ERR_IO;
}
*out = r;
return XTC_OK;
}
void xtc_recorder_push(xtc_recorder *r, const float *input, size_t frames)
{
uint64_t write, read;
size_t i;
if (!r || !frames || __atomic_load_n(&r->error, __ATOMIC_ACQUIRE)) return;
if (!r || !frames || __atomic_load_n(&r->error, __ATOMIC_ACQUIRE))
return;
write = __atomic_load_n(&r->write_position, __ATOMIC_RELAXED);
read = __atomic_load_n(&r->read_position, __ATOMIC_ACQUIRE);
if (!input || frames > r->capacity || frames > r->capacity - (size_t)(write - read) ||
@ -53,53 +80,73 @@ void xtc_recorder_push(xtc_recorder *r, const float *input, size_t frames)
__atomic_store_n(&r->error, (unsigned int)(input ? XTC_ERR_CAPACITY : XTC_ERR_IO), __ATOMIC_RELEASE);
return;
}
for (i = 0; i < frames; ++i) r->ring[(size_t)((write + i) % r->capacity)] = input[i];
for (i = 0; i < frames; ++i)
r->ring[(size_t)((write + i) % r->capacity)] = input[i];
__atomic_store_n(&r->write_position, write + frames, __ATOMIC_RELEASE);
}
xtc_result xtc_recorder_drain(xtc_recorder *r)
{
uint64_t read, write;
if (!r || !r->file || r->finished) return XTC_ERR_INVALID_STATE;
if (!r || !r->file || r->finished)
return XTC_ERR_INVALID_STATE;
read = __atomic_load_n(&r->read_position, __ATOMIC_RELAXED);
write = __atomic_load_n(&r->write_position, __ATOMIC_ACQUIRE);
while (read < write) {
size_t offset = (size_t)(read % r->capacity);
size_t count = r->capacity - offset;
if (count > write - read) count = (size_t)(write - read);
if (count > write - read)
count = (size_t)(write - read);
if (!xtc_wave_write(r->file, r->ring + offset, count)) {
__atomic_store_n(&r->error, (unsigned int)XTC_ERR_IO, __ATOMIC_RELEASE);
return XTC_ERR_IO;
}
read += count; r->written += count;
read += count;
r->written += count;
__atomic_store_n(&r->read_position, read, __ATOMIC_RELEASE);
}
return (xtc_result)__atomic_load_n(&r->error, __ATOMIC_ACQUIRE);
}
xtc_result xtc_recorder_status(const xtc_recorder *r, uint64_t *frames)
{
if (!r || !frames) return XTC_ERR_INVALID_ARGUMENT;
if (!r || !frames)
return XTC_ERR_INVALID_ARGUMENT;
*frames = __atomic_load_n(&r->write_position, __ATOMIC_ACQUIRE);
return (xtc_result)__atomic_load_n(&r->error, __ATOMIC_ACQUIRE);
}
xtc_result xtc_recorder_finish(xtc_recorder *r)
{
xtc_result result;
int failed = 0;
if (!r || !r->file || r->finished) return XTC_ERR_INVALID_STATE;
if (!r || !r->file || r->finished)
return XTC_ERR_INVALID_STATE;
result = xtc_recorder_drain(r);
if (result == XTC_ERR_IO) failed = 1;
if (fseek(r->file, 0, SEEK_SET) || !xtc_wave_header(r->file, r->rate, (uint32_t)r->written) ||
fflush(r->file) || fsync(fileno(r->file))) failed = 1;
if (fclose(r->file)) failed = 1;
if (result == XTC_ERR_IO)
failed = 1;
if (fseek(r->file, 0, SEEK_SET) || !xtc_wave_header(r->file, r->rate, (uint32_t)r->written) || fflush(r->file) ||
fsync(fileno(r->file)))
failed = 1;
if (fclose(r->file))
failed = 1;
r->file = NULL;
if (!failed && rename(r->temporary, r->path)) failed = 1;
if (!failed && rename(r->temporary, r->path))
failed = 1;
r->finished = 1;
return failed ? XTC_ERR_IO : result;
}
void xtc_recorder_destroy(xtc_recorder *r)
{
if (!r) return;
if (r->file) fclose(r->file);
if (r->temporary) unlink(r->temporary);
free(r->temporary); free(r->path); free(r->ring); free(r);
if (!r)
return;
if (r->file)
fclose(r->file);
if (r->temporary)
unlink(r->temporary);
free(r->temporary);
free(r->path);
free(r->ring);
free(r);
}

View file

@ -3,35 +3,63 @@
#include <math.h>
#include <stdlib.h>
#include <string.h>
static uint32_t u32(const unsigned char *p)
{ return (uint32_t)p[0] | (uint32_t)p[1] << 8U | (uint32_t)p[2] << 16U | (uint32_t)p[3] << 24U; }
{
return (uint32_t)p[0] | (uint32_t)p[1] << 8U | (uint32_t)p[2] << 16U | (uint32_t)p[3] << 24U;
}
static unsigned int u16(const unsigned char *p)
{ return (unsigned int)p[0] | (unsigned int)p[1] << 8U; }
{
return (unsigned int)p[0] | (unsigned int)p[1] << 8U;
}
static void put32(unsigned char *p, uint32_t v)
{ size_t i; for (i = 0; i < 4; ++i) { p[i] = (unsigned char)(v & 255U); v >>= 8U; } }
{
size_t i;
for (i = 0; i < 4; ++i) {
p[i] = (unsigned char)(v & 255U);
v >>= 8U;
}
}
int xtc_wave_header(FILE *file, unsigned int rate, uint32_t frames)
{
unsigned char h[44] = {0};
memcpy(h, "RIFF", 4); put32(h + 4, 36U + frames * 4U);
memcpy(h + 8, "WAVEfmt ", 8); put32(h + 16, 16);
h[20] = 3; h[22] = 1; put32(h + 24, rate); put32(h + 28, rate * 4U);
h[32] = 4; h[34] = 32; memcpy(h + 36, "data", 4); put32(h + 40, frames * 4U);
memcpy(h, "RIFF", 4);
put32(h + 4, 36U + frames * 4U);
memcpy(h + 8, "WAVEfmt ", 8);
put32(h + 16, 16);
h[20] = 3;
h[22] = 1;
put32(h + 24, rate);
put32(h + 28, rate * 4U);
h[32] = 4;
h[34] = 32;
memcpy(h + 36, "data", 4);
put32(h + 40, frames * 4U);
return fwrite(h, 1, sizeof(h), file) == sizeof(h);
}
int xtc_wave_write(FILE *file, const float *input, size_t frames)
{
unsigned char bytes[4096];
while (frames) {
size_t i, count = frames > 1024 ? 1024 : frames;
for (i = 0; i < count; ++i) {
uint32_t bits; float value = isfinite(input[i]) ? input[i] : 0.0f;
memcpy(&bits, &value, 4); put32(bytes + i * 4, bits);
uint32_t bits;
float value = isfinite(input[i]) ? input[i] : 0.0f;
memcpy(&bits, &value, 4);
put32(bytes + i * 4, bits);
}
if (fwrite(bytes, 4, count, file) != count) return 0;
input += count; frames -= count;
if (fwrite(bytes, 4, count, file) != count)
return 0;
input += count;
frames -= count;
}
return 1;
}
xtc_result xtc_wave_load(const char *path, xtc_wave *out)
{
FILE *f;
@ -43,73 +71,114 @@ xtc_result xtc_wave_load(const char *path, xtc_wave *out)
size_t i, frames, chunks = 0;
float *samples = NULL;
xtc_result result = XTC_ERR_FORMAT;
if (!path || !out) return XTC_ERR_INVALID_ARGUMENT;
if (!path || !out)
return XTC_ERR_INVALID_ARGUMENT;
memset(out, 0, sizeof(*out));
if (sizeof(float) != 4 || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128)
return XTC_ERR_UNSUPPORTED;
f = fopen(path, "rb");
if (!f) return XTC_ERR_IO;
if (fseek(f, 0, SEEK_END) || (file_size = ftell(f)) < 12 || fseek(f, 0, SEEK_SET)) goto done;
if (file_size > 268435456L) { result = XTC_ERR_CAPACITY; goto done; }
if (fread(h, 1, 12, f) != 12 || memcmp(h, "RIFF", 4) || memcmp(h + 8, "WAVE", 4)) goto done;
if (!f)
return XTC_ERR_IO;
if (fseek(f, 0, SEEK_END) || (file_size = ftell(f)) < 12 || fseek(f, 0, SEEK_SET))
goto done;
if (file_size > 268435456L) {
result = XTC_ERR_CAPACITY;
goto done;
}
if (fread(h, 1, 12, f) != 12 || memcmp(h, "RIFF", 4) || memcmp(h + 8, "WAVE", 4))
goto done;
remaining = u32(h + 4);
if (remaining < 4 || remaining + 8U > (uint64_t)file_size) goto done;
if (remaining < 4 || remaining + 8U > (uint64_t)file_size)
goto done;
remaining -= 4;
while (remaining) {
uint32_t size; uint64_t padded;
if (++chunks > 4096 || remaining < 8 || fread(h, 1, 8, f) != 8) goto done;
size = u32(h + 4); padded = (uint64_t)size + (size & 1U);
uint32_t size;
uint64_t padded;
if (++chunks > 4096 || remaining < 8 || fread(h, 1, 8, f) != 8)
goto done;
size = u32(h + 4);
padded = (uint64_t)size + (size & 1U);
remaining -= 8;
if (padded > 0x7fffffffU || padded > remaining) goto done;
if (padded > 0x7fffffffU || padded > remaining)
goto done;
if (!memcmp(h, "fmt ", 4)) {
if (format || size < 16 || fread(h + 8, 1, 16, f) != 16) goto done;
format = u16(h + 8); channels = u16(h + 10); rate = u32(h + 12);
align = u16(h + 20); bits = u16(h + 22);
if (fseek(f, (long)(padded - 16), SEEK_CUR)) goto done;
if (format || size < 16 || fread(h + 8, 1, 16, f) != 16)
goto done;
format = u16(h + 8);
channels = u16(h + 10);
rate = u32(h + 12);
align = u16(h + 20);
bits = u16(h + 22);
if (fseek(f, (long)(padded - 16), SEEK_CUR))
goto done;
} else {
if (!memcmp(h, "data", 4)) {
if (data_offset) goto done;
data_offset = ftell(f); data_size = size;
if (data_offset)
goto done;
data_offset = ftell(f);
data_size = size;
}
if (padded > 0x7fffffffU || fseek(f, (long)padded, SEEK_CUR)) goto done;
if (padded > 0x7fffffffU || fseek(f, (long)padded, SEEK_CUR))
goto done;
}
remaining -= padded;
}
if ((format != 1 && format != 3) || (channels != 1 && channels != 2) ||
(bits != 16 && bits != 24 && bits != 32) || (format == 3 && bits != 32)) {
result = XTC_ERR_UNSUPPORTED; goto done;
if ((format != 1 && format != 3) || (channels != 1 && channels != 2) || (bits != 16 && bits != 24 && bits != 32) ||
(format == 3 && bits != 32)) {
result = XTC_ERR_UNSUPPORTED;
goto done;
}
if (!rate || rate > 768000 || !data_offset || !data_size ||
align != channels * (bits / 8U) || data_size % align) goto done;
if (!rate || rate > 768000 || !data_offset || !data_size || align != channels * (bits / 8U) || data_size % align)
goto done;
frames = data_size / align;
if (frames > 16777216U) { result = XTC_ERR_CAPACITY; goto done; }
if (frames > 16777216U) {
result = XTC_ERR_CAPACITY;
goto done;
}
samples = malloc(frames * sizeof(*samples));
if (!samples) { result = XTC_ERR_OUT_OF_MEMORY; goto done; }
if (fseek(f, data_offset, SEEK_SET)) goto done;
if (!samples) {
result = XTC_ERR_OUT_OF_MEMORY;
goto done;
}
if (fseek(f, data_offset, SEEK_SET))
goto done;
for (i = 0; i < frames; ++i) {
unsigned int c; double sum = 0;
if (fread(bytes, 1, align, f) != align) goto done;
unsigned int c;
double sum = 0;
if (fread(bytes, 1, align, f) != align)
goto done;
for (c = 0; c < channels; ++c) {
const unsigned char *p = bytes + c * (bits / 8U);
if (format == 3) {
uint32_t value = u32(p); float sample; memcpy(&sample, &value, 4);
if (!isfinite(sample)) goto done;
uint32_t value = u32(p);
float sample;
memcpy(&sample, &value, 4);
if (!isfinite(sample))
goto done;
sum += sample;
} else {
uint32_t value = bits == 16 ? u16(p) : bits == 24 ?
(uint32_t)p[0] | (uint32_t)p[1] << 8U | (uint32_t)p[2] << 16U : u32(p);
uint32_t value = bits == 16 ? u16(p)
: bits == 24 ? (uint32_t)p[0] | (uint32_t)p[1] << 8U | (uint32_t)p[2] << 16U
: u32(p);
int64_t signed_value = value;
if (value & (UINT32_C(1) << (bits - 1U))) signed_value -= INT64_C(1) << bits;
if (value & (UINT32_C(1) << (bits - 1U)))
signed_value -= INT64_C(1) << bits;
sum += (double)signed_value / (double)(INT64_C(1) << (bits - 1U));
}
}
samples[i] = (float)(sum / channels);
}
if (ferror(f)) goto done;
if (ferror(f))
goto done;
result = XTC_OK;
done:
if (fclose(f) && result == XTC_OK) result = XTC_ERR_IO;
if (result == XTC_OK) { out->samples = samples; out->frames = frames; out->rate = rate; }
else free(samples);
if (fclose(f) && result == XTC_OK)
result = XTC_ERR_IO;
if (result == XTC_OK) {
out->samples = samples;
out->frames = frames;
out->rate = rate;
} else
free(samples);
return result;
}

View file

@ -1,8 +1,15 @@
#ifndef XTC_WAVE_INTERNAL_H
#define XTC_WAVE_INTERNAL_H
#include "xtc/xtc_audio.h"
#include <stdio.h>
typedef struct { float *samples; size_t frames; unsigned int rate; } xtc_wave;
typedef struct {
float *samples;
size_t frames;
unsigned int rate;
} xtc_wave;
xtc_result xtc_wave_load(const char *path, xtc_wave *out_wave);
int xtc_wave_header(FILE *file, unsigned int rate, uint32_t frames);
int xtc_wave_write(FILE *file, const float *input, size_t frames);