summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/xas/script.h86
-rw-r--r--src/Makefile4
-rw-r--r--src/script.c211
3 files changed, 299 insertions, 2 deletions
diff --git a/include/xas/script.h b/include/xas/script.h
new file mode 100644
index 0000000..102d421
--- /dev/null
+++ b/include/xas/script.h
@@ -0,0 +1,86 @@
+#ifndef _XAS_SCRIPT_H
+#define _XAS_SCRIPT_H
+
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include <xas/audio.h>
+#include <xas/vox.h>
+#include <xas/bank.h>
+#include <xas/synth.h>
+#include <xas/spatial.h>
+
+enum xas_script_event_type {
+ XAS_SCRIPT_EVENT_OFF,
+ XAS_SCRIPT_EVENT_ON,
+ XAS_SCRIPT_EVENT_SET_POSITION,
+ XAS_SCRIPT_EVENT_SET_GAIN,
+ XAS_SCRIPT_EVENT_SET_BANK_INDEX,
+ XAS_SCRIPT_EVENT_SET_FREQUENCY
+};
+
+typedef struct _xas_script_event xas_script_event;
+
+struct _xas_script_event {
+ enum xas_script_event_type type;
+
+ xas_spatial_object *object;
+
+ struct timeval timestamp;
+
+ union {
+ float gain;
+ xas_spatial_coord point;
+ size_t index;
+ size_t frequency;
+ };
+
+ xas_script_event *next;
+};
+
+typedef struct _xas_script {
+ xas_spatial_scene *scene;
+
+ xas_script_event *first;
+
+ struct timeval timestamp;
+
+ size_t buffer_size,
+ current_index;
+} xas_script;
+
+xas_script *xas_script_new(xas_spatial_scene *scene, size_t buffer_size);
+
+void xas_script_destroy(xas_script *script);
+
+int xas_script_add_event_off(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp);
+
+int xas_script_add_event_on(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp);
+
+int xas_script_add_position_set(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp,
+ xas_spatial_coord point);
+
+int xas_script_add_gain_set(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp,
+ float gain);
+
+int xas_script_add_bank_set(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp,
+ size_t index);
+
+int xas_script_add_frequency_set(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp,
+ size_t frequency);
+
+int xas_script_play(xas_script *script, xas_audio_stream *sink);
+
+#endif /* _XAS_SCRIPT_H */
diff --git a/src/Makefile b/src/Makefile
index 7c26ac9..e8bf54d 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -7,9 +7,9 @@ CC = $(CROSS)cc
CFLAGS += -I$(INCLUDE_PATH)
LDFLAGS +=
-HEADERS = audio.h riff.h mixer.h synth.h vox.h bank.h spatial.h
+HEADERS = audio.h riff.h mixer.h synth.h vox.h bank.h spatial.h script.h
-OBJS = audio.o riff.o mixer.o synth.o vox.o bank.o spatial.o
+OBJS = audio.o riff.o mixer.o synth.o vox.o bank.o spatial.o script.o
VERSION_MAJOR = 0
VERSION_MINOR = 0.1
diff --git a/src/script.c b/src/script.c
new file mode 100644
index 0000000..8c58f7f
--- /dev/null
+++ b/src/script.c
@@ -0,0 +1,211 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include <xas/script.h>
+
+xas_script *xas_script_new(xas_spatial_scene *scene, size_t buffer_size) {
+ xas_script *script;
+
+ if ((script = malloc(sizeof(*script))) == NULL) {
+ goto error_malloc_script;
+ }
+
+ script->scene = scene;
+ script->first = NULL;
+
+ timerclear(&script->timestamp);
+
+ script->buffer_size = buffer_size;
+ script->current_index = 0;
+
+ return script;
+
+error_malloc_script:
+ return NULL;
+}
+
+void xas_script_destroy(xas_script *script) {
+ xas_script_event *event = script->first;
+
+ while (event) {
+ xas_script_event *next = event->next;
+
+ free(event);
+
+ event = next;
+ }
+
+ free(script);
+}
+
+static void add_event(xas_script *script, xas_script_event *ev) {
+ xas_script_event *current = script->first;
+
+ if (script->first == NULL) {
+ script->first = ev;
+
+ return;
+ }
+
+ while (current) {
+ xas_script_event *next = current->next;
+
+ if (next == NULL) {
+ current->next = ev;
+
+ return;
+ }
+
+ if (!timercmp(&current->timestamp, &ev->timestamp, <)
+ && timercmp(&current->timestamp, &next->timestamp, <)) {
+ current->next = ev;
+ ev->next = next;
+
+ return;
+ }
+
+ current = next;
+ }
+}
+
+int xas_script_add_event_off(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp) {
+ xas_script_event *ev;
+
+ if ((ev = malloc(sizeof(*ev))) == NULL) {
+ goto error_malloc_ev;
+ }
+
+ ev->type = XAS_SCRIPT_EVENT_OFF;
+ ev->object = object;
+ ev->timestamp = timestamp;
+ ev->next = NULL;
+
+ add_event(script, ev);
+
+ return 0;
+
+error_malloc_ev:
+ return -1;
+}
+
+int xas_script_add_event_on(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp) {
+ xas_script_event *ev;
+
+ if ((ev = malloc(sizeof(*ev))) == NULL) {
+ goto error_malloc_ev;
+ }
+
+ ev->type = XAS_SCRIPT_EVENT_ON;
+ ev->object = object;
+ ev->timestamp = timestamp;
+ ev->next = NULL;
+
+ add_event(script, ev);
+
+ return 0;
+
+error_malloc_ev:
+ return -1;
+}
+
+int xas_script_add_position_set(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp,
+ xas_spatial_coord point) {
+ xas_script_event *ev;
+
+ if ((ev = malloc(sizeof(*ev))) == NULL) {
+ goto error_malloc_ev;
+ }
+
+ ev->type = XAS_SCRIPT_EVENT_SET_POSITION;
+ ev->object = object;
+ ev->timestamp = timestamp;
+ ev->point = point;
+ ev->next = NULL;
+
+ add_event(script, ev);
+
+ return 0;
+
+error_malloc_ev:
+ return -1;
+}
+
+int xas_script_add_gain_set(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp,
+ float gain) {
+ xas_script_event *ev;
+
+ if ((ev = malloc(sizeof(*ev))) == NULL) {
+ goto error_malloc_ev;
+ }
+
+ ev->type = XAS_SCRIPT_EVENT_ON;
+ ev->object = object;
+ ev->timestamp = timestamp;
+ ev->gain = gain;
+ ev->next = NULL;
+
+ add_event(script, ev);
+
+ return 0;
+
+error_malloc_ev:
+ return -1;
+}
+
+int xas_script_add_bank_set(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp,
+ size_t index) {
+ xas_script_event *ev;
+
+ if ((ev = malloc(sizeof(*ev))) == NULL) {
+ goto error_malloc_ev;
+ }
+
+ ev->type = XAS_SCRIPT_EVENT_SET_BANK_INDEX;
+ ev->object = object;
+ ev->timestamp = timestamp;
+ ev->next = NULL;
+ ev->index = index;
+
+ add_event(script, ev);
+
+ return 0;
+
+error_malloc_ev:
+ return -1;
+}
+
+int xas_script_add_frequency_set(xas_script *script,
+ xas_spatial_object *object,
+ struct timeval timestamp,
+ size_t frequency) {
+ xas_script_event *ev;
+
+ if ((ev = malloc(sizeof(*ev))) == NULL) {
+ goto error_malloc_ev;
+ }
+
+ ev->type = XAS_SCRIPT_EVENT_SET_FREQUENCY;
+ ev->object = object;
+ ev->timestamp = timestamp;
+ ev->next = NULL;
+ ev->frequency = frequency;
+
+ add_event(script, ev);
+
+ return 0;
+
+error_malloc_ev:
+ return -1;
+}
+
+int xas_script_play(xas_script *script, xas_audio_stream *sink);