1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/* gAlan - Graphical Audio Language
* Copyright (C) 1999 Tony Garnock-Jones
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include "global.h"
#include "generator.h"
#include "comp.h"
#include "control.h"
#include "gencomp.h"
#define SEQUENCE_LENGTH 32
#define NUM_PATTERNS 16
#define GENERATOR_CLASS_NAME "trigseq"
#define GENERATOR_CLASS_PATH "Pattern/Trigger Sequence"
#define EVT_SEL_EDIT 0 /* input */
#define EVT_SEL_PLAY 1 /* input */
#define EVT_STEP 2 /* input */
#define EVT_OUTPUT 0 /* output */
/* Forward refs. */
PRIVATE void init_pattern(Control *control);
PRIVATE void done_pattern(Control *control);
PRIVATE void refresh_pattern(Control *control);
#define TRIGSEQ_CONTROL_PATTERN 0
PRIVATE ControlDescriptor trigseq_controls[] = {
{ CONTROL_KIND_USERDEF, "pattern", 0,0,0,0, 0,FALSE, 0,0, init_pattern, done_pattern, refresh_pattern },
{ CONTROL_KIND_NONE, }
};
typedef struct Data {
int edit, play;
gboolean pattern[NUM_PATTERNS][SEQUENCE_LENGTH];
} Data;
PRIVATE gboolean clipseq[SEQUENCE_LENGTH];
PRIVATE int init_instance(Generator *g) {
Data *data = safe_malloc(sizeof(Data));
int i, j;
g->data = data;
data->edit = data->play = 0;
for (i = 0; i < NUM_PATTERNS; i++)
for (j = 0; j < SEQUENCE_LENGTH; j++)
data->pattern[i][j] = FALSE;
return 1;
}
PRIVATE void destroy_instance(Generator *g) {
free(g->data);
}
PRIVATE void unpickle_instance(Generator *g, ObjectStoreItem *item, ObjectStore *db) {
Data *data = safe_malloc(sizeof(Data));
ObjectStoreDatum *array;
int i, j;
g->data = data;
data->edit = objectstore_item_get_integer(item, "trigseq_edit", 0);
data->play = objectstore_item_get_integer(item, "trigseq_play", 0);
array = objectstore_item_get(item, "trigseq_patterns");
for (i = 0; i < NUM_PATTERNS; i++)
for (j = 0; j < SEQUENCE_LENGTH; j++)
data->pattern[i][j] =
objectstore_datum_integer_value(
objectstore_datum_array_get(array, i * SEQUENCE_LENGTH + j)
);
}
PRIVATE void pickle_instance(Generator *g, ObjectStoreItem *item, ObjectStore *db) {
Data *data = g->data;
ObjectStoreDatum *array = objectstore_datum_new_array(NUM_PATTERNS * SEQUENCE_LENGTH);
int i, j;
objectstore_item_set_integer(item, "trigseq_edit", data->edit);
objectstore_item_set_integer(item, "trigseq_play", data->play);
objectstore_item_set(item, "trigseq_patterns", array);
for (i = 0; i < NUM_PATTERNS; i++)
for (j = 0; j < SEQUENCE_LENGTH; j++)
objectstore_datum_array_set(array, i * SEQUENCE_LENGTH + j,
objectstore_datum_new_integer(data->pattern[i][j]));
}
PRIVATE void evt_sel_edit_handler(Generator *g, AEvent *event) {
Data *data = g->data;
data->edit = GEN_DOUBLE_TO_INT(event->d.number) % NUM_PATTERNS;
gen_update_controls(g, TRIGSEQ_CONTROL_PATTERN);
}
PRIVATE void evt_sel_play_handler(Generator *g, AEvent *event) {
((Data *) g->data)->play = GEN_DOUBLE_TO_INT(event->d.number) % NUM_PATTERNS;
}
PRIVATE void evt_step_handler(Generator *g, AEvent *event) {
Data *data = g->data;
int step = GEN_DOUBLE_TO_INT(event->d.number) % SEQUENCE_LENGTH;
if (data->pattern[data->play][step]) {
gen_init_aevent(event, AE_NUMBER, NULL, 0, NULL, 0, event->time);
event->d.number = step;
gen_send_events(g, EVT_OUTPUT, -1, event);
}
}
PRIVATE void toggled_handler(GtkWidget *widget, gpointer userdata) {
int step = (int) userdata;
Control *c = gtk_object_get_data(GTK_OBJECT(widget), "Control");
Data *data = c->g->data;
if (c->events_flow) {
data->pattern[data->edit][step] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
gen_update_controls(c->g, TRIGSEQ_CONTROL_PATTERN);
}
}
PRIVATE void do_copy(Control *c, guint action, GtkWidget *widget) {
Data *data = c->g->data;
int i, end, offset;
switch( action ) {
case 0:
i=0; end=SEQUENCE_LENGTH;
break;
case 1:
i=0; end=SEQUENCE_LENGTH/2;
break;
case 2:
i=SEQUENCE_LENGTH/2; end=SEQUENCE_LENGTH;
break;
case 3:
i=0; end=SEQUENCE_LENGTH/4;
break;
case 4:
i=SEQUENCE_LENGTH/4; end=SEQUENCE_LENGTH*2/4;
break;
case 5:
i=SEQUENCE_LENGTH*2/4; end=SEQUENCE_LENGTH*3/4;
break;
case 6:
i=SEQUENCE_LENGTH*3/4; end=SEQUENCE_LENGTH*4/4;
break;
default:
i=0; end=SEQUENCE_LENGTH;
break;
}
for( offset=i; i<end; i++ ) {
clipseq[i-offset] = data->pattern[data->edit][i];
}
}
PRIVATE void do_paste(Control *c, guint action, GtkWidget *widget) {
Data *data = c->g->data;
int i, end, offset;
switch( action ) {
case 0:
i=0; end=SEQUENCE_LENGTH;
break;
case 1:
i=0; end=SEQUENCE_LENGTH/2;
break;
case 2:
i=SEQUENCE_LENGTH/2; end=SEQUENCE_LENGTH;
break;
case 3:
i=0; end=SEQUENCE_LENGTH/4;
break;
case 4:
i=SEQUENCE_LENGTH/4; end=SEQUENCE_LENGTH*2/4;
break;
case 5:
i=SEQUENCE_LENGTH*2/4; end=SEQUENCE_LENGTH*3/4;
break;
case 6:
i=SEQUENCE_LENGTH*3/4; end=SEQUENCE_LENGTH*4/4;
break;
default:
i=0; end=SEQUENCE_LENGTH;
break;
}
for( offset=i; i<end; i++ ) {
data->pattern[data->edit][i] = clipseq[i-offset];
}
if (c->events_flow) {
gen_update_controls(c->g, TRIGSEQ_CONTROL_PATTERN);
}
}
PRIVATE GtkItemFactoryEntry popup_items[] = {
/* Path, accelerator, callback, extra-numeric-argument, kind */
{ "/Full", NULL, NULL, 0, "<Branch>" },
{ "/Full/Copy", NULL, do_copy, 0, NULL },
{ "/Full/Paste", NULL, do_paste, 0, NULL },
{ "/1st Half", NULL, NULL, 0, "<Branch>" },
{ "/1st Half/Copy", NULL, do_copy, 1, NULL },
{ "/1st Half/Paste", NULL, do_paste, 1, NULL },
{ "/2nd Half", NULL, NULL, 0, "<Branch>" },
{ "/2nd Half/Copy", NULL, do_copy, 2, NULL },
{ "/2nd Half/Paste", NULL, do_paste, 2, NULL },
{ "/1st Quarter", NULL, NULL, 0, "<Branch>" },
{ "/1st Quarter/Copy", NULL, do_copy, 3, NULL },
{ "/1st Quarter/Paste", NULL, do_paste, 3, NULL },
{ "/2nd Quarter", NULL, NULL, 0, "<Branch>" },
{ "/2nd Quarter/Copy", NULL, do_copy, 4, NULL },
{ "/2nd Quarter/Paste", NULL, do_paste, 4, NULL },
{ "/3rd Quarter", NULL, NULL, 0, "<Branch>" },
{ "/3rd Quarter/Copy", NULL, do_copy, 5, NULL },
{ "/3rd Quarter/Paste", NULL, do_paste, 5, NULL },
{ "/4th Quarter", NULL, NULL, 0, "<Branch>" },
{ "/4th Quarter/Copy", NULL, do_copy, 6, NULL },
{ "/4th Quarter/Paste", NULL, do_paste, 6, NULL },
};
PRIVATE void kill_popup(GtkWidget *popup, GtkItemFactory *ifact) {
gtk_object_unref(GTK_OBJECT(ifact));
}
PRIVATE GtkWidget *build_popup(Control *c) {
GtkItemFactory *ifact;
GtkWidget *result;
int nitems = sizeof(popup_items) / sizeof(popup_items[0]);
ifact = gtk_item_factory_new(GTK_TYPE_MENU, "<trigseq-popup>", NULL);
gtk_item_factory_create_items(ifact, nitems, popup_items, c);
result = gtk_item_factory_get_widget(ifact, "<trigseq-popup>");
gtk_signal_connect(GTK_OBJECT(result), "destroy", GTK_SIGNAL_FUNC(kill_popup), ifact);
return result;
}
PRIVATE void popup_handler(GtkWidget *widget, Control *c) {
//Data *data = c->g->data;
GtkWidget *popup = build_popup(c);
gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL, 1, 0);
}
PRIVATE void init_pattern(Control *control) {
GtkWidget *hb;
int i;
GtkWidget **widgets = safe_malloc(sizeof(GtkWidget *) * SEQUENCE_LENGTH);
GtkWidget *menu;
hb = gtk_hbox_new(FALSE, 5);
for (i = 0; i < SEQUENCE_LENGTH; i++) {
GtkWidget *b = gtk_toggle_button_new();
gtk_object_set_data(GTK_OBJECT(b), "Control", control);
gtk_signal_connect(GTK_OBJECT(b), "toggled", GTK_SIGNAL_FUNC(toggled_handler), (gpointer) i);
gtk_box_pack_start(GTK_BOX(hb), b, FALSE, FALSE, 0);
gtk_widget_set_usize(b, 12, 16);
gtk_widget_show(b);
widgets[i] = b;
}
menu = gtk_button_new_with_label( "m" );
gtk_signal_connect( GTK_OBJECT(menu), "clicked",
GTK_SIGNAL_FUNC( popup_handler ), control );
gtk_box_pack_start(GTK_BOX(hb), menu, FALSE, FALSE, 0);
gtk_widget_show(menu);
control->widget = hb;
control->data = widgets;
}
PRIVATE void done_pattern(Control *control) {
free(control->data);
}
PRIVATE void refresh_pattern(Control *control) {
Data *data = control->g->data;
GtkWidget **widgets = control->data;
int i;
for (i = 0; i < SEQUENCE_LENGTH; i++) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets[i]),
data->pattern[data->edit][i]);
}
}
PRIVATE void setup_class(void) {
GeneratorClass *k = gen_new_generatorclass(GENERATOR_CLASS_NAME, FALSE, 3, 1,
NULL, NULL, trigseq_controls,
init_instance, destroy_instance,
unpickle_instance, pickle_instance);
gen_configure_event_input(k, EVT_SEL_EDIT, "Edit Seq", evt_sel_edit_handler);
gen_configure_event_input(k, EVT_SEL_PLAY, "Play Seq", evt_sel_play_handler);
gen_configure_event_input(k, EVT_STEP, "Step", evt_step_handler);
gen_configure_event_output(k, EVT_OUTPUT, "Output");
gencomp_register_generatorclass(k, FALSE, GENERATOR_CLASS_PATH, NULL, NULL);
}
PUBLIC void init_plugin_trigseq(void) {
setup_class();
}
|