missing commit in generator.h
[galan.git] / plugins / libxfade.c
blob1ca59c418ca4ece166ec20d4564b7b34956ca0e0
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 GENERATOR_CLASS_NAME "xfade"
35 #define GENERATOR_CLASS_PATH "Levels/Cross-fader"
37 #define SIG_LEFT 0
38 #define SIG_RIGHT 1
40 #define SIG_OUTPUT 0
42 #define EVT_AMOUNT 0
43 #define NUM_EVENT_INPUTS 1
45 #define NUM_EVENT_OUTPUTS 0
47 typedef struct Data {
48 gdouble amount;
49 } Data;
51 PRIVATE gboolean init_instance(Generator *g) {
52 Data *data = safe_malloc(sizeof(Data));
53 g->data = data;
55 data->amount = 0;
57 return TRUE;
60 PRIVATE void destroy_instance(Generator *g) {
61 free(g->data);
64 PRIVATE void unpickle_instance(Generator *g, ObjectStoreItem *item, ObjectStore *db) {
65 Data *data = safe_malloc(sizeof(Data));
66 g->data = data;
68 data->amount = objectstore_item_get_double(item, "xfade_amount", 0);
71 PRIVATE void pickle_instance(Generator *g, ObjectStoreItem *item, ObjectStore *db) {
72 Data *data = g->data;
73 objectstore_item_set_double(item, "xfade_amount", data->amount);
76 PRIVATE gboolean output_generator(Generator *g, SAMPLE *buf, int buflen) {
77 Data *data = g->data;
78 SAMPLE bufl[MAXIMUM_REALTIME_STEP];
79 SAMPLE bufr[MAXIMUM_REALTIME_STEP];
80 gdouble lfact, rfact;
81 int i;
82 gboolean resl, resr;
84 resl = gen_read_realtime_input(g, SIG_LEFT, -1, bufl, buflen);
85 resr = gen_read_realtime_input(g, SIG_RIGHT, -1, bufr, buflen);
87 if (!resl && !resr)
88 return FALSE;
90 if (!resl)
91 memset(bufl, 0, sizeof(SAMPLE) * buflen);
93 if (!resr)
94 memset(bufr, 0, sizeof(SAMPLE) * buflen);
96 if (data->amount > 0) {
97 rfact = 1;
98 lfact = 1 - data->amount;
99 } else {
100 lfact = 1;
101 rfact = 1 + data->amount;
104 for (i = 0; i < buflen; i++)
105 buf[i] = bufl[i] * lfact + bufr[i] * rfact;
106 return TRUE;
109 PRIVATE void evt_amount_handler(Generator *g, AEvent *event) {
110 ((Data *) g->data)->amount = MAX(-1, MIN(1, event->d.number));
113 PRIVATE InputSignalDescriptor input_sigs[] = {
114 { "Left", SIG_FLAG_REALTIME },
115 { "Right", SIG_FLAG_REALTIME },
116 { NULL, }
119 PRIVATE OutputSignalDescriptor output_sigs[] = {
120 { "Output", SIG_FLAG_REALTIME, { output_generator, } },
121 { NULL, }
124 PRIVATE ControlDescriptor controls[] = {
125 { CONTROL_KIND_KNOB, "fade", -1,1,0.01,0.01, 0,TRUE, 1,EVT_AMOUNT,
126 NULL,NULL, control_double_updater, (gpointer) offsetof(Data, amount) },
127 { CONTROL_KIND_NONE, }
130 PRIVATE void setup_class(void) {
131 GeneratorClass *k = gen_new_generatorclass(GENERATOR_CLASS_NAME, FALSE,
132 NUM_EVENT_INPUTS, NUM_EVENT_OUTPUTS,
133 input_sigs, output_sigs, controls,
134 init_instance, destroy_instance,
135 unpickle_instance, pickle_instance);
137 gen_configure_event_input(k, EVT_AMOUNT, "Fade", evt_amount_handler);
139 gencomp_register_generatorclass(k, FALSE, GENERATOR_CLASS_PATH, NULL, NULL);
142 PUBLIC void init_plugin(void) {
143 setup_class();