missing commit in generator.h
[galan.git] / plugins / librart.c
blobc327e5fe673056ca11abcf792382730b380b02f9
1 /* gAlan - Graphical Audio Language
2 * Copyright (C) 1999 Tony Garnock-Jones
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <stddef.h>
24 #include <gdk/gdk.h>
25 #include <gtk/gtk.h>
26 #include <gmodule.h>
28 #include "global.h"
29 #include "generator.h"
30 #include "comp.h"
31 #include "control.h"
32 #include "gencomp.h"
34 #define POLYPHONY 4
36 typedef struct Data {
37 SAMPLETIME location[POLYPHONY];
38 int num_playing;
39 } Data;
41 #define LOCATION(g) ((SAMPLETIME) ((g)->data))
43 PRIVATE gboolean init_instance(Generator *g) {
44 Data *data = safe_malloc(sizeof(Data));
46 data->num_playing = 0;
47 /* location[] doesn't need initialising because num_playing is zero. */
49 g->data = data;
50 return TRUE;
53 PRIVATE void done_instance(Generator *g) {
54 free(g->data);
57 PRIVATE gboolean output_generator(Generator *g, SAMPLE *buf, int buflen) {
58 Data *data = g->data;
59 SAMPLE tmpbuf[MAXIMUM_REALTIME_STEP];
60 SAMPLETIME range;
61 int i;
63 if (data->num_playing == 0)
64 return FALSE;
66 memset(buf, 0, sizeof(SAMPLE) * buflen);
67 range = gen_get_randomaccess_input_range(g, 0, 0);
69 i = 0;
70 while (i < data->num_playing) {
71 if (gen_read_randomaccess_input(g, 0, 0, data->location[i], tmpbuf, buflen)) {
72 int j;
73 for (j = 0; j < buflen; j++)
74 buf[j] += tmpbuf[j];
77 data->location[i] += buflen;
79 if (data->location[i] >= range) {
80 data->num_playing--;
81 data->location[i] = data->location[data->num_playing];
82 } else {
83 i++;
87 return TRUE;
90 PRIVATE void evt_reset_handler(Generator *g, AEvent *event) {
91 Data *data = g->data;
93 if (data->num_playing >= POLYPHONY) {
94 int i;
96 for (i = 0; i < POLYPHONY - 1; i++) {
97 data->location[i] = data->location[i+1];
100 data->location[POLYPHONY - 1] = 0;
101 } else {
102 data->location[data->num_playing++] = 0;
106 PRIVATE InputSignalDescriptor input_sigs[] = {
107 { "Input", SIG_FLAG_RANDOMACCESS },
108 { NULL, }
111 PRIVATE OutputSignalDescriptor output_sigs[] = {
112 { "Output", SIG_FLAG_REALTIME, { output_generator, } },
113 { NULL, }
116 PRIVATE ControlDescriptor controls[] = {
117 /* { kind, name, min,max,step,page, size,editable, is_dst,queue_number,
118 init,destroy,refresh,refresh_data }, */
119 { CONTROL_KIND_NONE, }
122 PRIVATE void setup_class(void) {
123 GeneratorClass *k = gen_new_generatorclass("rart", FALSE, 1, 0,
124 input_sigs, output_sigs, controls,
125 init_instance, done_instance,
126 (AGenerator_pickle_t) init_instance, NULL);
128 gen_configure_event_input(k, 0, "Reset", evt_reset_handler);
130 gencomp_register_generatorclass(k, FALSE, "Misc/Randomaccess to Realtime Converter", NULL, NULL);
133 PUBLIC void init_plugin(void) {
134 setup_class();