missing commit in generator.h
[galan.git] / plugins / libarrwindow.c
blobc3812e8afac3c48120328e7355fc0420b212e44f
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>
27 #include "global.h"
28 #include "generator.h"
29 #include "comp.h"
30 #include "control.h"
31 #include "gencomp.h"
33 #include <math.h>
35 #define GENERATOR_CLASS_NAME "arrwindow"
36 #define GENERATOR_CLASS_PATH "Array/Window"
38 #define SIG_INPUT 0
39 #define SIG_OUTPUT 0
41 #define EVT_INPUT 0
42 #define NUM_EVENT_INPUTS 1
44 #define EVT_OUTPUT 0
45 #define NUM_EVENT_OUTPUTS 1
47 typedef struct Data {
48 SAMPLE *window;
49 int lastN;
50 } Data;
52 PRIVATE int init_instance(Generator *g) {
53 Data *data = safe_malloc(sizeof(Data));
54 g->data = data;
56 data->lastN = 0;
57 data->window = NULL;
58 return 1;
61 PRIVATE void destroy_instance(Generator *g) {
63 Data *data = g->data;
65 if( data->window )
66 free( data->window );
68 free(data);
71 PRIVATE void unpickle_instance(Generator *g, ObjectStoreItem *item, ObjectStore *db) {
72 Data *data = safe_malloc(sizeof(Data));
74 g->data = data;
76 data->lastN = 0;
77 data->window = NULL;
80 PRIVATE void pickle_instance(Generator *g, ObjectStoreItem *item, ObjectStore *db) {
81 //Data *data = g->data;
84 PRIVATE void evt_input_handler(Generator *g, AEvent *event) {
85 Data *data = g->data;
86 int i;
87 SAMPLE *out, *in;
88 AEvent send_ev;
90 RETURN_UNLESS( event->kind == AE_NUMARRAY );
91 //RETURN_UNLESS( (event->d.array.len & 1) == 0 );
93 if( event->d.array.len != data->lastN ) {
95 data->lastN = event->d.array.len;
97 if( data->window )
98 free( data->window );
100 data->window = safe_malloc( sizeof( SAMPLE ) * data->lastN );
101 for( i=0; i<data->lastN; i++ )
102 data->window[i] = 0.5 * ( 1 - cos(2*M_PI*i/(data->lastN-1)));
105 in = event->d.array.numbers;
106 // TODO: make this alloca...
107 out = safe_malloc( sizeof( SAMPLE ) * data->lastN );
109 for( i=0; i<data->lastN; i++ )
110 out[i] = data->window[i] * in[i];
113 gen_init_aevent(&send_ev, AE_NUMARRAY, NULL, 0, NULL, 0, event->time);
115 send_ev.d.array.len = data->lastN;
116 send_ev.d.array.numbers = out;
119 gen_send_events(g, EVT_OUTPUT, -1, &send_ev);
121 free(out);
124 PRIVATE ControlDescriptor controls[] = {
125 /* { kind, name, min,max,step,page, size,editable, is_dst,queue_number,
126 init,destroy,refresh,refresh_data }, */
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 NULL, NULL, controls,
134 init_instance, destroy_instance,
135 unpickle_instance, pickle_instance);
137 gen_configure_event_input(k, EVT_INPUT, "Input", evt_input_handler);
138 gen_configure_event_output(k, EVT_OUTPUT, "Output" );
140 gencomp_register_generatorclass(k, FALSE, GENERATOR_CLASS_PATH, NULL, NULL);
143 PUBLIC void init_plugin(void) {
144 setup_class();