#include #include #include #include #include xas_seq *xas_seq_new(xas_spatial_scene *scene, size_t buffer_size) { xas_seq *seq; if ((seq = malloc(sizeof(*seq))) == NULL) { goto error_malloc_seq; } seq->scene = scene; seq->first = NULL; seq->last = NULL; seq->buffer_size = buffer_size; return seq; error_malloc_seq: return NULL; } void xas_seq_destroy(xas_seq *seq) { xas_seq_event *ev = seq->first; while (ev) { xas_seq_event *next = ev->next; free(ev); ev = next; } free(seq); } static int event_add(xas_seq *seq, xas_seq_event *ev) { if (seq->first == NULL) { seq->first = ev; seq->last = ev; } else { seq->last->next = ev; seq->last = ev; } ev->next = NULL; return 0; } static xas_seq_event *event_tail(xas_seq_event *head) { if (head == NULL) { return NULL; } while (head->next) { head = head->next; } return head; } static inline int event_lt(xas_seq_event *a, xas_seq_event *b) { if (timercmp(&a->timestamp, &b->timestamp, <)) { return 1; } else if (!timercmp(&a->timestamp, &b->timestamp, !=)) { if (a->type < b->type) { return 1; } } return 0; } static xas_seq_event *event_list_partition(xas_seq_event *head, xas_seq_event *end, xas_seq_event **head_new, xas_seq_event **end_new) { xas_seq_event *pivot = end, *prev = NULL, *cur = head, *tail = pivot; while (cur != pivot) { if (event_lt(cur, pivot)) { if (*head_new == NULL) { *head_new = cur; } prev = cur; cur = cur->next; } else { xas_seq_event *tmp; if (prev) { prev->next = cur->next; } tmp = cur->next; cur->next = NULL; tail->next = cur; tail = cur; cur = tmp; } } if (*head_new == NULL) { *head_new = pivot; } *end_new = tail; return pivot; } static xas_seq_event *event_list_sort_recur(xas_seq_event *head, xas_seq_event *end) { xas_seq_event *head_new = NULL, *end_new = NULL, *pivot; if (head == NULL || head == end) { return head; } pivot = event_list_partition(head, end, &head_new, &end_new); if (head_new != pivot) { xas_seq_event *tmp = head_new; while (tmp->next != pivot) { tmp = tmp->next; } tmp->next = NULL; head_new = event_list_sort_recur(head_new, tmp); tmp = event_tail(head_new); tmp->next = pivot; } pivot->next = event_list_sort_recur(pivot->next, end_new); return head_new; } static void event_sort(xas_seq *seq) { seq->first = event_list_sort_recur(seq->first, seq->last); seq->last = event_tail(seq->first); } int xas_seq_add_event_off(xas_seq *seq, xas_object *object, struct timeval timestamp, size_t index) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_OFF; ev->object = object; ev->timestamp = timestamp; ev->index = index; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_event_on(xas_seq *seq, xas_object *object, struct timeval timestamp, size_t index) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_ON; ev->object = object; ev->timestamp = timestamp; ev->index = index; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_set_position(xas_seq *seq, xas_object *object, struct timeval timestamp, xas_spatial_coord point) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_SET_POSITION; ev->object = object; ev->timestamp = timestamp; ev->point = point; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_set_heading(xas_seq *seq, xas_object *object, struct timeval timestamp, xas_spatial_coord heading) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_SET_HEADING; ev->object = object; ev->timestamp = timestamp; ev->heading = heading; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_set_speed(xas_seq *seq, xas_object *object, struct timeval timestamp, float speed) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_SET_SPEED; ev->object = object; ev->timestamp = timestamp; ev->speed = speed; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_set_gain(xas_seq *seq, xas_object *object, struct timeval timestamp, float gain) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_ON; ev->object = object; ev->timestamp = timestamp; ev->gain = gain; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_set_entry(xas_seq *seq, xas_object *object, struct timeval timestamp, size_t index, size_t entry) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_SET_ENTRY; ev->object = object; ev->timestamp = timestamp; ev->index = index; ev->entry = entry; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_set_flags(xas_seq *seq, xas_object *object, struct timeval timestamp, size_t index, int flags) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_SET_FLAGS; ev->object = object; ev->timestamp = timestamp; ev->index = index; ev->flags = flags; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_set_type(xas_seq *seq, xas_object *object, struct timeval timestamp, size_t index, int type) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_SET_TYPE; ev->object = object; ev->timestamp = timestamp; ev->index = index; ev->value = type; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_set_frequency(xas_seq *seq, xas_object *object, struct timeval timestamp, size_t index, size_t frequency) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_SET_FREQUENCY; ev->object = object; ev->timestamp = timestamp; ev->index = index; ev->frequency = frequency; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_phrase(xas_seq *seq, xas_object *object, struct timeval timestamp, const char *phrase) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_SPEECH; ev->object = object; ev->timestamp = timestamp; ev->phrase = phrase; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } int xas_seq_add_stop(xas_seq *seq, struct timeval timestamp) { xas_seq_event *ev; if ((ev = malloc(sizeof(*ev))) == NULL) { goto error_malloc_ev; } ev->type = XAS_SEQ_EVENT_STOP; ev->object = NULL; ev->timestamp = timestamp; ev->flags = 0; if (event_add(seq, ev) < 0) { goto error_event_add; } return 0; error_event_add: free(ev); error_malloc_ev: return -1; } static int event_trigger(xas_spatial_scene *scene, xas_seq_event *ev) { switch (ev->type) { case XAS_SEQ_EVENT_SET_POSITION: xas_spatial_scene_position_object(scene, ev->object, ev->point); break; case XAS_SEQ_EVENT_SET_HEADING: return xas_object_set_heading(ev->object, ev->index, ev->heading); case XAS_SEQ_EVENT_SET_SPEED: return xas_object_set_speed(ev->object, ev->index, ev->speed); case XAS_SEQ_EVENT_SET_GAIN: return xas_object_set_gain(ev->object, ev->index, ev->gain); case XAS_SEQ_EVENT_SET_FREQUENCY: return xas_object_set_freq(ev->object, ev->index, ev->frequency); case XAS_SEQ_EVENT_SET_TYPE: return xas_object_set_type(ev->object, ev->index, ev->value); case XAS_SEQ_EVENT_SET_ENTRY: return xas_object_set_entry(ev->object, ev->index, ev->entry); case XAS_SEQ_EVENT_SET_FLAGS: return xas_object_set_flags(ev->object, ev->index, ev->flags); case XAS_SEQ_EVENT_SPEECH: xas_vox_say((xas_vox *)ev->object, ev->phrase); xas_vox_generate((xas_vox *)ev->object); break; case XAS_SEQ_EVENT_OFF: return xas_object_stop(ev->object, ev->index); case XAS_SEQ_EVENT_ON: return xas_object_start(ev->object, ev->index); case XAS_SEQ_EVENT_STOP: break; } return 0; } static inline void timerupdate(struct timeval *tv, suseconds_t interval, size_t frame) { suseconds_t usec = frame * interval; tv->tv_sec = usec / 1000000; tv->tv_usec = usec % 1000000; } int xas_seq_play(xas_seq *seq, xas_audio_stream *sink) { xas_seq_event *ev; xas_audio_stream *source; size_t frame = 0, sample_rate = seq->scene->format.sample_rate, buffer_size = seq->buffer_size; suseconds_t interval = 1000000 / (sample_rate / buffer_size); int16_t *samples; if ((source = xas_spatial_scene_stream_new(seq->scene, seq->buffer_size)) == NULL) { goto error_spatial_scene_stream_new; } event_sort(seq); ev = seq->first; while (ev) { struct timeval tv; ssize_t readlen; timerupdate(&tv, interval, frame); while (ev && !timercmp(&tv, &ev->timestamp, <)) { if (ev->type == XAS_SEQ_EVENT_STOP) { goto done; } if (event_trigger(seq->scene, ev) < 0) { goto error_event_trigger; } ev = ev->next; } if ((readlen = xas_audio_stream_read(source, (void **)&samples, buffer_size)) < 0) { goto error_audio_stream_io; } else if (readlen == 0) { break; } if (xas_audio_stream_write(sink, samples, readlen) < 0) { goto error_audio_stream_io; } frame++; } done: xas_audio_stream_destroy(source); return frame * buffer_size; error_audio_stream_io: error_event_trigger: xas_audio_stream_destroy(source); error_spatial_scene_stream_new: return -1; }