xtc/examples/native_gain.c

64 lines
2.3 KiB
C
Raw Normal View History

/* Compile-time native effect example. No module loading or external SDK. */
#include "xtc/xtc_native.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct {
float gain;
} gain_state;
static xtc_result prepare(double rate, size_t frames, float gain, void **out)
{
gain_state *state = malloc(sizeof(*state));
(void)rate;
(void)frames;
if (!state)
return XTC_ERR_OUT_OF_MEMORY;
state->gain = gain;
*out = state;
return XTC_OK;
}
static void reset(void *state) { (void)state; }
static void destroy(void *state) { free(state); }
static xtc_result process(void *context, const xtc_native_block *block)
{
const gain_state *state = context;
size_t i;
for (i = 0; i < block->frames; ++i)
block->output[i] = block->input ? block->input[i] * state->gain : 0;
return XTC_OK;
}
int main(void)
{
const xtc_native_descriptor descriptor = {XTC_NATIVE_ABI_VERSION,
sizeof(xtc_native_descriptor),
"org.xtc.example-gain",
XTC_NATIVE_EFFECT,
prepare,
reset,
process,
destroy};
xtc_graph *graph = NULL;
xtc_plan *plan = NULL;
size_t effect, output;
float input[4] = {1, 0.5f, -1, 0}, rendered[4];
xtc_result result = xtc_graph_create(2, &graph);
if (result == XTC_OK)
result = xtc_graph_add_native(graph, &descriptor, 0.25f, &effect);
if (result == XTC_OK)
result = xtc_graph_add_output(graph, &output);
if (result == XTC_OK)
result = xtc_graph_connect(graph, effect, output);
if (result == XTC_OK)
result = xtc_graph_compile(graph, 48000, 4, &plan);
if (result == XTC_OK)
result = xtc_plan_process(plan, input, rendered, 4);
if (result == XTC_OK && (rendered[0] != 0.25f || rendered[2] != -0.25f))
result = XTC_ERR_INVALID_STATE;
xtc_plan_destroy(plan);
xtc_graph_destroy(graph);
if (result != XTC_OK) {
fprintf(stderr, "Native example: %s\n", xtc_result_string(result));
return EXIT_FAILURE;
}
puts("Native gain graph: 0.25, 0.125, -0.25, 0");
return EXIT_SUCCESS;
}