Added tons of documentation headers to functions in source files.

This commit is contained in:
Johannes Findeisen 2026-10-27 01:58:27 +01:00
commit 4eb2080933
43 changed files with 3953 additions and 43 deletions

View file

@ -21,6 +21,23 @@ struct xtc_audio_plan {
unsigned int rate;
};
/**
* @brief Imports an audio file as a timeline clip.
*
* Resolves path to an absolute reference, reads the WAV to determine its
* musical length, then transactionally clones the project, adds a clip, stores
* the asset reference, and adds an audio source -> output route when the graph
* has no audio source. The project is unchanged on failure.
*
* @param project Project to modify; must not be NULL.
* @param track Existing destination track ID.
* @param path Source WAV path; resolved with realpath() and must fit the asset path limit.
* @param start Clip start position in project ticks.
* @param out_clip Receives the new clip ID on success; unchanged on failure.
* @return XTC_OK on success, XTC_ERR_INVALID_ARGUMENT for NULL arguments, XTC_ERR_IO
* when the path cannot be resolved or read, XTC_ERR_CAPACITY when the absolute
* path is too long, or a WAV load or project edit error.
*/
xtc_result xtc_project_import_audio(xtc_project *project, uint64_t track, const char *path, uint64_t start,
uint64_t *out_clip)
{
@ -79,6 +96,11 @@ xtc_result xtc_project_import_audio(xtc_project *project, uint64_t track, const
return result;
}
/**
* @brief Releases an audio plan and all samples preloaded by it.
*
* @param plan Plan to destroy; NULL is accepted and does nothing.
*/
void xtc_audio_plan_destroy(xtc_audio_plan *plan)
{
size_t i;
@ -89,6 +111,21 @@ void xtc_audio_plan_destroy(xtc_audio_plan *plan)
free(plan);
}
/**
* @brief Preloads project audio into an immutable render plan.
*
* At most 64 distinct assets and 67,108,864 total mono frames are accepted.
* Missing or invalid assets are skipped and counted as issues rather than
* failing creation. The caller owns the returned plan and must release it with
* xtc_audio_plan_destroy().
*
* @param project Project whose clips are compiled; must not be NULL.
* @param rate Output sample rate in Hz, from 1 to 768000.
* @param out Receives the new plan on success and is set to NULL on failure.
* @return XTC_OK on success, XTC_ERR_INVALID_ARGUMENT for NULL arguments or an
* invalid rate, XTC_ERR_OUT_OF_MEMORY if allocation fails, XTC_ERR_CAPACITY
* for asset or frame limits, or an error from mix preparation or tick conversion.
*/
xtc_result xtc_audio_plan_create(const xtc_project *project, unsigned int rate, xtc_audio_plan **out)
{
xtc_audio_plan *plan;
@ -158,6 +195,14 @@ xtc_result xtc_audio_plan_create(const xtc_project *project, unsigned int rate,
return XTC_OK;
}
/**
* @brief Returns the number of assets that could not be loaded.
*
* @param plan Plan to query; NULL reports zero issues.
* @param path Receives a borrowed path to the first failed asset, or an empty
* string when there is none; may be NULL.
* @return Number of skipped assets; zero when plan is NULL.
*/
size_t xtc_audio_plan_issues(const xtc_audio_plan *plan, const char **path)
{
if (path)
@ -165,6 +210,20 @@ size_t xtc_audio_plan_issues(const xtc_audio_plan *plan, const char **path)
return plan ? plan->issues : 0;
}
/**
* @brief Renders a block of the plan at unity gain with linear rate conversion.
*
* Both buffers are replaced, not mixed into. Rendering performs no allocation,
* file access, or locking. Non-finite output samples are replaced with zero.
*
* @param plan Immutable plan to render; must not be NULL.
* @param position First output sample position on the project timeline.
* @param output Left or mono destination with room for frames samples.
* @param right Optional right destination; must differ from output, or NULL for mono.
* @param frames Number of samples to render, at most 65536.
* @return XTC_OK on success, or XTC_ERR_INVALID_ARGUMENT for NULL output, aliased
* stereo buffers, too many frames, or a position overflow.
*/
xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t position, float *output, float *right,
size_t frames)
{
@ -206,6 +265,17 @@ xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t pos
return XTC_OK;
}
/**
* @brief Renders a mono block of the plan.
*
* Equivalent to xtc_audio_plan_render_stereo() with no right channel.
*
* @param p Immutable plan to render; must not be NULL.
* @param position First output sample position on the project timeline.
* @param out Destination with room for frames samples.
* @param frames Number of samples to render, at most 65536.
* @return XTC_OK on success, or XTC_ERR_INVALID_ARGUMENT for invalid arguments.
*/
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);

View file

@ -15,6 +15,15 @@ struct xtc_recorder {
int finished;
};
/**
* @brief Allocates a bounded recording ring and opens a temporary mono float32 WAV file.
*
* @param path NUL-terminated file or asset path.
* @param rate Sample rate in hertz.
* @param capacity Recording ring capacity in frames, from 1 to 2097152.
* @param out Receives the newly allocated object; caller owns it on success.
* @return XTC_OK on success, or an error code describing the failure.
*/
xtc_result xtc_recorder_create(const char *path, unsigned int rate, size_t capacity, xtc_recorder **out)
{
xtc_recorder *r;
@ -67,6 +76,13 @@ xtc_result xtc_recorder_create(const char *path, unsigned int rate, size_t capac
return XTC_OK;
}
/**
* @brief Enqueues an entire audio block without blocking; overflow sets a sticky error.
*
* @param r Recorder handle.
* @param input Borrowed input sample buffer.
* @param frames Number of audio frames.
*/
void xtc_recorder_push(xtc_recorder *r, const float *input, size_t frames)
{
uint64_t write, read;
@ -85,6 +101,12 @@ void xtc_recorder_push(xtc_recorder *r, const float *input, size_t frames)
__atomic_store_n(&r->write_position, write + frames, __ATOMIC_RELEASE);
}
/**
* @brief Writes queued samples to the temporary WAV file on the control thread.
*
* @param r Recorder handle.
* @return XTC_OK on success, or an error code describing the failure.
*/
xtc_result xtc_recorder_drain(xtc_recorder *r)
{
uint64_t read, write;
@ -108,6 +130,13 @@ xtc_result xtc_recorder_drain(xtc_recorder *r)
return (xtc_result)__atomic_load_n(&r->error, __ATOMIC_ACQUIRE);
}
/**
* @brief Copies the number of accepted frames and returns the sticky recording status.
*
* @param r Recorder handle.
* @param frames Receives the accepted frame count.
* @return XTC_OK on success, or an error code describing the failure.
*/
xtc_result xtc_recorder_status(const xtc_recorder *r, uint64_t *frames)
{
if (!r || !frames)
@ -116,6 +145,12 @@ xtc_result xtc_recorder_status(const xtc_recorder *r, uint64_t *frames)
return (xtc_result)__atomic_load_n(&r->error, __ATOMIC_ACQUIRE);
}
/**
* @brief Drains and finalizes the WAV file, then renames it after producer quiescence.
*
* @param r Recorder handle.
* @return XTC_OK on success, or an error code describing the failure.
*/
xtc_result xtc_recorder_finish(xtc_recorder *r)
{
xtc_result result;
@ -137,6 +172,11 @@ xtc_result xtc_recorder_finish(xtc_recorder *r)
return failed ? XTC_ERR_IO : result;
}
/**
* @brief Releases recorder storage and removes any unfinished temporary file; accepts NULL.
*
* @param r Recorder handle.
*/
void xtc_recorder_destroy(xtc_recorder *r)
{
if (!r)

View file

@ -4,16 +4,34 @@
#include <stdlib.h>
#include <string.h>
/**
* @brief Reads an unsigned 32-bit integer from four little-endian bytes.
*
* @param p Byte buffer containing the little-endian integer.
* @return Decoded 32-bit value.
*/
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;
}
/**
* @brief Reads an unsigned 16-bit integer from two little-endian bytes.
*
* @param p Byte buffer containing the little-endian integer.
* @return Decoded 16-bit value.
*/
static unsigned int u16(const unsigned char *p)
{
return (unsigned int)p[0] | (unsigned int)p[1] << 8U;
}
/**
* @brief Writes an unsigned 32-bit integer as four little-endian bytes.
*
* @param p Destination buffer with room for four bytes.
* @param v Unsigned integer to encode.
*/
static void put32(unsigned char *p, uint32_t v)
{
size_t i;
@ -23,6 +41,14 @@ static void put32(unsigned char *p, uint32_t v)
}
}
/**
* @brief Writes a 44-byte mono float32 WAV header at the current file position.
*
* @param file Open file stream at the required read or write position.
* @param rate Sample rate in hertz.
* @param frames Number of audio frames.
* @return Nonzero if all header bytes were written, zero on a write failure.
*/
int xtc_wave_header(FILE *file, unsigned int rate, uint32_t frames)
{
unsigned char h[44] = {0};
@ -41,6 +67,14 @@ int xtc_wave_header(FILE *file, unsigned int rate, uint32_t frames)
return fwrite(h, 1, sizeof(h), file) == sizeof(h);
}
/**
* @brief Writes mono float32 samples in little-endian order, replacing nonfinite values with zero.
*
* @param file Open file stream at the required read or write position.
* @param input Borrowed input sample buffer.
* @param frames Number of audio frames.
* @return Nonzero if all samples were written, zero on a write failure.
*/
int xtc_wave_write(FILE *file, const float *input, size_t frames)
{
unsigned char bytes[4096];
@ -60,6 +94,13 @@ int xtc_wave_write(FILE *file, const float *input, size_t frames)
return 1;
}
/**
* @brief Loads a bounded PCM or float WAV file into caller-owned mono sample storage.
*
* @param path NUL-terminated file or asset path.
* @param out Receives the operation result.
* @return XTC_OK on success, or an error code describing the failure.
*/
xtc_result xtc_wave_load(const char *path, xtc_wave *out)
{
FILE *f;

View file

@ -10,7 +10,30 @@ typedef struct {
unsigned int rate;
} xtc_wave;
/**
* @brief Loads a bounded PCM or float WAV file into caller-owned mono sample storage.
*
* @param path NUL-terminated file or asset path.
* @param out_wave Receives the wave.
* @return XTC_OK on success, or an error code describing the failure.
*/
xtc_result xtc_wave_load(const char *path, xtc_wave *out_wave);
/**
* @brief Writes a 44-byte mono float32 WAV header at the current file position.
*
* @param file Open file stream at the required read or write position.
* @param rate Sample rate in hertz.
* @param frames Number of audio frames.
* @return Nonzero if all header bytes were written, zero on a write failure.
*/
int xtc_wave_header(FILE *file, unsigned int rate, uint32_t frames);
/**
* @brief Writes mono float32 samples in little-endian order, replacing nonfinite values with zero.
*
* @param file Open file stream at the required read or write position.
* @param input Borrowed input sample buffer.
* @param frames Number of audio frames.
* @return Nonzero if all samples were written, zero on a write failure.
*/
int xtc_wave_write(FILE *file, const float *input, size_t frames);
#endif