| 1 |
/* Copyright (c) 2002 Yves Degoyon. */ |
| 2 |
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL */ |
| 3 |
/* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ |
| 4 |
/* */ |
| 5 |
/* exciter : a graphical object which enables */ |
| 6 |
/* to schedule bang events on a time scale */ |
| 7 |
/* */ |
| 8 |
/* This program is free software; you can redistribute it and/or */ |
| 9 |
/* modify it under the terms of the GNU General Public License */ |
| 10 |
/* as published by the Free Software Foundation; either version 2 */ |
| 11 |
/* of the License, or (at your option) any later version. */ |
| 12 |
/* */ |
| 13 |
/* See file LICENSE for further informations on licensing terms. */ |
| 14 |
/* */ |
| 15 |
/* This program is distributed in the hope that it will be useful, */ |
| 16 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 17 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 18 |
/* GNU General Public License for more details. */ |
| 19 |
/* */ |
| 20 |
/* You should have received a copy of the GNU General Public License */ |
| 21 |
/* along with this program; if not, write to the Free Software */ |
| 22 |
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 23 |
/* */ |
| 24 |
/* Based on PureData by Miller Puckette and others. */ |
| 25 |
/* */ |
| 26 |
/* Made while listening to : */ |
| 27 |
/* */ |
| 28 |
/* "You're a maniac" */ |
| 29 |
/* "To fullfill the emptyness" */ |
| 30 |
/* Mecano ( no, not the spanish ones !!! ) - Escape The Human Myth */ |
| 31 |
/* ---------------------------------------------------------------------------- */ |
| 32 |
|
| 33 |
|
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <math.h> |
| 38 |
#include <ctype.h> |
| 39 |
#include <time.h> |
| 40 |
#include <sys/time.h> |
| 41 |
#include "m_pd.h" |
| 42 |
#include "m_imp.h" |
| 43 |
#include "g_canvas.h" |
| 44 |
|
| 45 |
#include "exciter.h" |
| 46 |
|
| 47 |
#ifdef _WIN32 |
| 48 |
#include <io.h> |
| 49 |
#else |
| 50 |
#include <unistd.h> |
| 51 |
#endif |
| 52 |
|
| 53 |
/* needed to create a exciter from PD's menu |
| 54 |
void canvas_objtext(t_glist *gl, int xpos, int ypos, int selected, t_binbuf *b); |
| 55 |
void canvas_startmotion(t_canvas *x); |
| 56 |
*/ |
| 57 |
|
| 58 |
#define DEFAULT_EXCITER_WIDTH 200 |
| 59 |
#define DEFAULT_EXCITER_HEIGHT 200 |
| 60 |
#define DEFAULT_EXCITER_GRAIN 0.1 |
| 61 |
#define DEFAULT_EXCITER_NBEVENTS 8 |
| 62 |
|
| 63 |
#define EXCITER_PIXEL_GRAIN 5 |
| 64 |
|
| 65 |
static char *exciter_version = "exciter: a bang-events sequencer, version 0.5 ([email protected])"; |
| 66 |
|
| 67 |
t_widgetbehavior exciter_widgetbehavior; |
| 68 |
static t_class *exciter_class; |
| 69 |
static int excitercount=0; |
| 70 |
|
| 71 |
static int guidebug=0; |
| 72 |
static int pointsize = 5; |
| 73 |
|
| 74 |
#define SYS_VGUI2(a,b) if (guidebug) \ |
| 75 |
post(a,b);\ |
| 76 |
sys_vgui(a,b) |
| 77 |
|
| 78 |
#define SYS_VGUI3(a,b,c) if (guidebug) \ |
| 79 |
post(a,b,c);\ |
| 80 |
sys_vgui(a,b,c) |
| 81 |
|
| 82 |
#define SYS_VGUI4(a,b,c,d) if (guidebug) \ |
| 83 |
post(a,b,c,d);\ |
| 84 |
sys_vgui(a,b,c,d) |
| 85 |
|
| 86 |
#define SYS_VGUI5(a,b,c,d,e) if (guidebug) \ |
| 87 |
post(a,b,c,d,e);\ |
| 88 |
sys_vgui(a,b,c,d,e) |
| 89 |
|
| 90 |
#define SYS_VGUI6(a,b,c,d,e,f) if (guidebug) \ |
| 91 |
post(a,b,c,d,e,f);\ |
| 92 |
sys_vgui(a,b,c,d,e,f) |
| 93 |
|
| 94 |
#define SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \ |
| 95 |
post(a,b,c,d,e,f,g);\ |
| 96 |
sys_vgui(a,b,c,d,e,f,g) |
| 97 |
|
| 98 |
#define SYS_VGUI8(a,b,c,d,e,f,g,h) if (guidebug) \ |
| 99 |
post(a,b,c,d,e,f,g,h);\ |
| 100 |
sys_vgui(a,b,c,d,e,f,g,h) |
| 101 |
|
| 102 |
#define SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \ |
| 103 |
post(a,b,c,d,e,f,g,h,i);\ |
| 104 |
sys_vgui(a,b,c,d,e,f,g,h,i) |
| 105 |
|
| 106 |
#define SYS_VGUI13(a,b,c,d,e,f,g,h,i,j,k,l,m) if (guidebug) \ |
| 107 |
post(a,b,c,d,e,f,g,h,i,j,k,l,m);\ |
| 108 |
sys_vgui(a,b,c,d,e,f,g,h,i,j,k,l,m) |
| 109 |
|
| 110 |
/* drawing functions */ |
| 111 |
|
| 112 |
/* draw an event */ |
| 113 |
static void exciter_draw_gem(t_exciter *x, t_glist *glist, t_int ix, t_int iy) |
| 114 |
{ |
| 115 |
t_int polyray = ( x->x_height / x->x_nbevents) / 2; |
| 116 |
t_canvas *canvas=glist_getcanvas(glist); |
| 117 |
|
| 118 |
SYS_VGUI13(".x%lx.c create polygon %d %d %d %d %d %d %d %d -outline #000000 -fill #FFFFFF -tags %xEVENT%.4d%.4d\n", |
| 119 |
canvas, |
| 120 |
text_xpix(&x->x_obj, glist) + ix*EXCITER_PIXEL_GRAIN, |
| 121 |
text_ypix(&x->x_obj, glist) + x->x_height - (iy+1)*x->x_height/x->x_nbevents + 2*polyray, |
| 122 |
text_xpix(&x->x_obj, glist) + ix*EXCITER_PIXEL_GRAIN + polyray, |
| 123 |
text_ypix(&x->x_obj, glist) + x->x_height - (iy+1)*x->x_height/x->x_nbevents + polyray, |
| 124 |
text_xpix(&x->x_obj, glist) + ix*EXCITER_PIXEL_GRAIN + 2*polyray, |
| 125 |
text_ypix(&x->x_obj, glist) + x->x_height - (iy+1)*x->x_height/x->x_nbevents, |
| 126 |
text_xpix(&x->x_obj, glist) + ix*EXCITER_PIXEL_GRAIN + polyray, |
| 127 |
text_ypix(&x->x_obj, glist) + x->x_height - (iy+1)*x->x_height/x->x_nbevents - polyray, |
| 128 |
x, ix, iy); |
| 129 |
} |
| 130 |
|
| 131 |
/* delete an event */ |
| 132 |
static void exciter_delete_gem(t_exciter *x, t_glist *glist, t_int ix, t_int iy) |
| 133 |
{ |
| 134 |
t_canvas *canvas=glist_getcanvas(glist); |
| 135 |
|
| 136 |
SYS_VGUI5(".x%lx.c delete %xEVENT%.4d%.4d\n", canvas, x, ix, iy ); |
| 137 |
} |
| 138 |
|
| 139 |
static void exciter_draw_update(t_exciter *x, t_glist *glist) |
| 140 |
{ |
| 141 |
int ei, gi; |
| 142 |
|
| 143 |
t_canvas *canvas=glist_getcanvas(glist); |
| 144 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 145 |
{ |
| 146 |
for ( gi=0; gi<(x->x_width/EXCITER_PIXEL_GRAIN); gi++ ) |
| 147 |
{ |
| 148 |
if( *(x->x_sbangs+ei*(x->x_width/EXCITER_PIXEL_GRAIN)+gi) == 1 ) |
| 149 |
{ |
| 150 |
exciter_draw_gem(x, glist, gi, ei ); |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
exciter_delete_gem(x, glist, gi, ei ); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
static void exciter_draw_new(t_exciter *x, t_glist *glist) |
| 161 |
{ |
| 162 |
t_canvas *canvas=glist_getcanvas(glist); |
| 163 |
t_int ei; |
| 164 |
|
| 165 |
SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -outline #000000 -fill #902181 -tags %xLINE\n", |
| 166 |
canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist), |
| 167 |
text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist)+x->x_height, |
| 168 |
x); |
| 169 |
SYS_VGUI5(".x%lx.c create text %d %d -font -*-courier-bold--normal--10-* -text \"0 s\" -tags %xLOWERCAPTION\n", |
| 170 |
canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist) + x->x_height + 10, x ); |
| 171 |
SYS_VGUI6(".x%lx.c create text %d %d -font -*-courier-bold--normal--10-* -text \"%.2f s\" -tags %xHIGHERCAPTION\n", |
| 172 |
canvas, text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height + 10, |
| 173 |
x->x_width/EXCITER_PIXEL_GRAIN*x->x_timegrain , x); |
| 174 |
SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -outline #000000 -fill #000000 -tags %xIN\n", |
| 175 |
canvas, text_xpix(&x->x_obj, glist), |
| 176 |
text_ypix(&x->x_obj, glist) - 2, |
| 177 |
text_xpix(&x->x_obj, glist) + 5, |
| 178 |
text_ypix(&x->x_obj, glist) , |
| 179 |
x); |
| 180 |
if ( x->x_nbevents > 1 ) |
| 181 |
{ |
| 182 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 183 |
{ |
| 184 |
SYS_VGUI8(".x%lx.c create rectangle %d %d %d %d -outline #000000 -fill #000000 -tags %xOUT%d\n", |
| 185 |
canvas, text_xpix(&x->x_obj, glist) + ( ei * (x->x_width - 5) )/ (x->x_nbevents-1), |
| 186 |
text_ypix(&x->x_obj, glist) + x->x_height, |
| 187 |
text_xpix(&x->x_obj, glist) + ( ei * (x->x_width - 5) )/ (x->x_nbevents-1) + 5, |
| 188 |
text_ypix(&x->x_obj, glist) + x->x_height + 2, |
| 189 |
x, ei); |
| 190 |
} |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
SYS_VGUI8(".x%lx.c create rectangle %d %d %d %d -outline #000000 -fill #000000 -tags %xOUT%d\n", |
| 195 |
canvas, text_xpix(&x->x_obj, glist), |
| 196 |
text_ypix(&x->x_obj, glist) + x->x_height, |
| 197 |
text_xpix(&x->x_obj, glist) + 5, |
| 198 |
text_ypix(&x->x_obj, glist) + x->x_height + 2, |
| 199 |
x, 0); |
| 200 |
} |
| 201 |
canvas_fixlinesfor( canvas, (t_text*)x ); |
| 202 |
} |
| 203 |
|
| 204 |
static void exciter_draw_move(t_exciter *x, t_glist *glist) |
| 205 |
{ |
| 206 |
t_canvas *canvas=glist_getcanvas(glist); |
| 207 |
t_int polyray = ( x->x_height / x->x_nbevents) / 2; |
| 208 |
t_int ei, gi; |
| 209 |
|
| 210 |
SYS_VGUI7(".x%lx.c coords %xLINE %d %d %d %d \n", |
| 211 |
canvas, x, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist), |
| 212 |
text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist)+x->x_height |
| 213 |
); |
| 214 |
SYS_VGUI5(".x%lx.c coords %xLOWERCAPTION %d %d\n", |
| 215 |
canvas, x, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist) + x->x_height + 10 ); |
| 216 |
SYS_VGUI5(".x%lx.c coords %xHIGHERCAPTION %d %d\n", |
| 217 |
canvas, x, text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height + 10); |
| 218 |
SYS_VGUI7(".x%lx.c coords %xIN %d %d %d %d\n", |
| 219 |
canvas, x, text_xpix(&x->x_obj, glist), |
| 220 |
text_ypix(&x->x_obj, glist) - 2, |
| 221 |
text_xpix(&x->x_obj, glist) + 5, |
| 222 |
text_ypix(&x->x_obj, glist) |
| 223 |
); |
| 224 |
if ( x->x_nbevents > 1 ) |
| 225 |
{ |
| 226 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 227 |
{ |
| 228 |
SYS_VGUI8(".x%lx.c coords %xOUT%d %d %d %d %d\n", |
| 229 |
canvas, x, ei, text_xpix(&x->x_obj, glist) + ( ei * (x->x_width - 5) )/ (x->x_nbevents-1), |
| 230 |
text_ypix(&x->x_obj, glist) + x->x_height, |
| 231 |
text_xpix(&x->x_obj, glist) + ( ei * (x->x_width - 5) )/ (x->x_nbevents-1) + 5, |
| 232 |
text_ypix(&x->x_obj, glist) + x->x_height + 2 |
| 233 |
); |
| 234 |
} |
| 235 |
} |
| 236 |
else |
| 237 |
{ |
| 238 |
SYS_VGUI8(".x%lx.c coords %xOUT%d %d %d %d %d\n", |
| 239 |
canvas, x, 0, text_xpix(&x->x_obj, glist), |
| 240 |
text_ypix(&x->x_obj, glist) + x->x_height, |
| 241 |
text_xpix(&x->x_obj, glist) + 5, |
| 242 |
text_ypix(&x->x_obj, glist) + x->x_height + 2 |
| 243 |
); |
| 244 |
} |
| 245 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 246 |
{ |
| 247 |
for ( gi=0; gi<(x->x_width/EXCITER_PIXEL_GRAIN); gi++ ) |
| 248 |
{ |
| 249 |
SYS_VGUI13(".x%lx.c coords %xEVENT%.4d%.4d %d %d %d %d %d %d %d %d\n", |
| 250 |
canvas, x, gi, ei, |
| 251 |
text_xpix(&x->x_obj, glist) + gi*EXCITER_PIXEL_GRAIN, |
| 252 |
text_ypix(&x->x_obj, glist) + x->x_height - (ei+1)*x->x_height/x->x_nbevents + 2*polyray, |
| 253 |
text_xpix(&x->x_obj, glist) + gi*EXCITER_PIXEL_GRAIN + polyray, |
| 254 |
text_ypix(&x->x_obj, glist) + x->x_height - (ei+1)*x->x_height/x->x_nbevents + polyray, |
| 255 |
text_xpix(&x->x_obj, glist) + gi*EXCITER_PIXEL_GRAIN + 2*polyray, |
| 256 |
text_ypix(&x->x_obj, glist) + x->x_height - (ei+1)*x->x_height/x->x_nbevents, |
| 257 |
text_xpix(&x->x_obj, glist) + gi*EXCITER_PIXEL_GRAIN + polyray, |
| 258 |
text_ypix(&x->x_obj, glist) + x->x_height - (ei+1)*x->x_height/x->x_nbevents - polyray |
| 259 |
); |
| 260 |
} |
| 261 |
} |
| 262 |
canvas_fixlinesfor( canvas, (t_text*)x ); |
| 263 |
} |
| 264 |
|
| 265 |
static void exciter_draw_erase(t_exciter* x,t_glist* glist) |
| 266 |
{ |
| 267 |
t_canvas *canvas=glist_getcanvas(glist); |
| 268 |
int i, ei, gi; |
| 269 |
|
| 270 |
SYS_VGUI3(".x%lx.c delete %xLINE\n", canvas, x ); |
| 271 |
SYS_VGUI3(".x%lx.c delete %xLOWERCAPTION\n", canvas, x ); |
| 272 |
SYS_VGUI3(".x%lx.c delete %xHIGHERCAPTION\n", canvas, x ); |
| 273 |
SYS_VGUI3(".x%lx.c delete %xIN\n", canvas, x ); |
| 274 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 275 |
{ |
| 276 |
for ( gi=0; gi<(x->x_width/EXCITER_PIXEL_GRAIN); gi++ ) |
| 277 |
{ |
| 278 |
if( *(x->x_sbangs+ei*(x->x_width/EXCITER_PIXEL_GRAIN)+gi) != 0 ) |
| 279 |
{ |
| 280 |
exciter_delete_gem( x, glist, gi, ei ); |
| 281 |
} |
| 282 |
} |
| 283 |
SYS_VGUI4(".x%lx.c delete %xOUT%d\n", canvas, x, ei ); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
static void exciter_draw_select(t_exciter* x,t_glist* glist) |
| 288 |
{ |
| 289 |
t_canvas *canvas=glist_getcanvas(glist); |
| 290 |
|
| 291 |
if(x->x_selected) |
| 292 |
{ |
| 293 |
/* sets the main item in blue */ |
| 294 |
SYS_VGUI3(".x%lx.c itemconfigure %xLINE -outline #0000FF\n", canvas, x); |
| 295 |
|
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
/* sets the main item in black */ |
| 300 |
SYS_VGUI3(".x%lx.c itemconfigure %xLINE -outline #000000\n", canvas, x); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
/* ------------------------ exciter widgetbehaviour----------------------------- */ |
| 305 |
|
| 306 |
|
| 307 |
static void exciter_getrect(t_gobj *z, t_glist *owner, |
| 308 |
int *xp1, int *yp1, int *xp2, int *yp2) |
| 309 |
{ |
| 310 |
t_exciter* x = (t_exciter*)z; |
| 311 |
|
| 312 |
*xp1 = text_xpix(&x->x_obj, owner); |
| 313 |
*yp1 = text_ypix(&x->x_obj, owner); |
| 314 |
*xp2 = text_xpix(&x->x_obj, owner)+x->x_width; |
| 315 |
*yp2 = text_ypix(&x->x_obj, owner)+x->x_height; |
| 316 |
} |
| 317 |
|
| 318 |
static void exciter_save(t_gobj *z, t_binbuf *b) |
| 319 |
{ |
| 320 |
t_exciter *x = (t_exciter *)z; |
| 321 |
int ei,gi; |
| 322 |
|
| 323 |
binbuf_addv(b, "ssiisiiifii", gensym("#X"),gensym("obj"), |
| 324 |
(t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix, |
| 325 |
atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)), |
| 326 |
x->x_width, x->x_height, |
| 327 |
x->x_nbevents, x->x_timegrain, |
| 328 |
x->x_loop, x->x_save |
| 329 |
); |
| 330 |
if ( x->x_save ) |
| 331 |
{ |
| 332 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 333 |
{ |
| 334 |
for ( gi=0; gi<(x->x_width/EXCITER_PIXEL_GRAIN); gi++ ) |
| 335 |
{ |
| 336 |
if( *(x->x_sbangs+ei*(x->x_width/EXCITER_PIXEL_GRAIN)+gi) == 1 ) |
| 337 |
{ |
| 338 |
// post( "exciter : saving ( %d, %d )", ei, gi ); |
| 339 |
binbuf_addv(b, "ii", ei, gi ); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
binbuf_addv(b, ";"); |
| 345 |
} |
| 346 |
|
| 347 |
static void exciter_properties(t_gobj *z, t_glist *owner) |
| 348 |
{ |
| 349 |
char buf[800]; |
| 350 |
t_exciter *x=(t_exciter *)z; |
| 351 |
|
| 352 |
sprintf(buf, "pdtk_exciter_dialog %%s %d %d %d %.2f %d %d\n", |
| 353 |
x->x_width, x->x_height, x->x_nbevents, x->x_timegrain, x->x_loop, x->x_save ); |
| 354 |
// post("exciter_properties : %s", buf ); |
| 355 |
gfxstub_new(&x->x_obj.ob_pd, x, buf); |
| 356 |
} |
| 357 |
|
| 358 |
static void exciter_select(t_gobj *z, t_glist *glist, int selected) |
| 359 |
{ |
| 360 |
t_exciter *x = (t_exciter *)z; |
| 361 |
|
| 362 |
x->x_selected = selected; |
| 363 |
exciter_draw_select( x, glist ); |
| 364 |
} |
| 365 |
|
| 366 |
static void exciter_vis(t_gobj *z, t_glist *glist, int vis) |
| 367 |
{ |
| 368 |
t_exciter *x = (t_exciter *)z; |
| 369 |
|
| 370 |
post("exciter_vis : %d", vis ); |
| 371 |
if (vis) |
| 372 |
{ |
| 373 |
exciter_draw_new( x, glist ); |
| 374 |
exciter_draw_update( x, glist ); |
| 375 |
} |
| 376 |
else |
| 377 |
{ |
| 378 |
exciter_draw_erase( x, glist ); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/* resuming the triggering of events */ |
| 383 |
static void exciter_resume(t_exciter *x) |
| 384 |
{ |
| 385 |
x->x_started = 1; |
| 386 |
} |
| 387 |
|
| 388 |
/* pausing the triggering of events */ |
| 389 |
static void exciter_pause(t_exciter *x) |
| 390 |
{ |
| 391 |
x->x_started = 0; |
| 392 |
} |
| 393 |
|
| 394 |
/* trigger events -- no dsp processing */ |
| 395 |
static void exciter_dialog(t_exciter *x, t_symbol *s, int argc, t_atom *argv) |
| 396 |
{ |
| 397 |
t_int onbevents, owidth, ei, gi, bi; |
| 398 |
t_canvas *canvas=glist_getcanvas(x->x_glist); |
| 399 |
|
| 400 |
if ( !x ) |
| 401 |
{ |
| 402 |
post( "exciter : error :tried to set properties on an unexisting object" ); |
| 403 |
} |
| 404 |
onbevents = x->x_nbevents; |
| 405 |
owidth = x->x_width; |
| 406 |
exciter_draw_erase(x, x->x_glist); |
| 407 |
if ( argc < 6 ) |
| 408 |
{ |
| 409 |
post( "exciter : error in the number of arguments ( %d )", argc ); |
| 410 |
return; |
| 411 |
} |
| 412 |
if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT || |
| 413 |
argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT || |
| 414 |
argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ) |
| 415 |
{ |
| 416 |
post( "exciter : wrong arguments" ); |
| 417 |
return; |
| 418 |
} |
| 419 |
|
| 420 |
exciter_pause( x ); |
| 421 |
x->x_width = argv[0].a_w.w_float; |
| 422 |
if ( x->x_width <= 0 ) x->x_width = 100; |
| 423 |
x->x_height = argv[1].a_w.w_float; |
| 424 |
if ( x->x_height <= 0 ) x->x_height = 100; |
| 425 |
x->x_nbevents = argv[2].a_w.w_float; |
| 426 |
if ( x->x_nbevents < 1 ) x->x_nbevents = 1; |
| 427 |
x->x_timegrain = argv[3].a_w.w_float; |
| 428 |
if ( x->x_timegrain < 0.01 ) |
| 429 |
{ |
| 430 |
post ("exciter : incorrect time grain : forced to 1 tick ( 10 ms )" ); |
| 431 |
x->x_timegrain = 0.01; |
| 432 |
} |
| 433 |
x->x_loop = argv[4].a_w.w_float; |
| 434 |
x->x_save = argv[5].a_w.w_float; |
| 435 |
x->x_started = 0; |
| 436 |
x->x_reltime = 0L; |
| 437 |
x->x_plooptime = 0L; |
| 438 |
x->x_gindex = -1; |
| 439 |
x->x_looplength = x->x_timegrain * x->x_width * 1000 / EXCITER_PIXEL_GRAIN; |
| 440 |
|
| 441 |
// re-allocate arrays and keep old events |
| 442 |
post( "exciter : re-allocate events" ); |
| 443 |
if ( onbevents != x->x_nbevents || owidth != x->x_width ) |
| 444 |
{ |
| 445 |
t_int mevents = ( onbevents > x->x_nbevents ) ? x->x_nbevents : onbevents; |
| 446 |
t_int mwidth = ( owidth > x->x_width ) ? x->x_width : owidth; |
| 447 |
t_int *newbangs; |
| 448 |
|
| 449 |
newbangs = (t_int*) getbytes( x->x_nbevents*x->x_width/EXCITER_PIXEL_GRAIN*sizeof(t_int) ); |
| 450 |
memset( newbangs, 0x0, x->x_nbevents*x->x_width/EXCITER_PIXEL_GRAIN*sizeof(t_int) ); |
| 451 |
for ( ei=0; ei<mevents; ei++ ) |
| 452 |
{ |
| 453 |
for ( gi=0; gi<(mwidth/EXCITER_PIXEL_GRAIN); gi++ ) |
| 454 |
{ |
| 455 |
*(newbangs+ei*(x->x_width/EXCITER_PIXEL_GRAIN)+gi ) = |
| 456 |
*(x->x_sbangs+ei*(owidth/EXCITER_PIXEL_GRAIN)+gi ); |
| 457 |
} |
| 458 |
} |
| 459 |
if ( x->x_sbangs ) |
| 460 |
freebytes( x->x_sbangs, onbevents*owidth/EXCITER_PIXEL_GRAIN*sizeof(t_int) ); |
| 461 |
x->x_sbangs = newbangs; |
| 462 |
} |
| 463 |
|
| 464 |
// re-allocate outlets |
| 465 |
post( "exciter : re-allocate outlets" ); |
| 466 |
if ( onbevents != x->x_nbevents ) |
| 467 |
{ |
| 468 |
post( "exciter : cleaning up old outlets" ); |
| 469 |
if ( x->x_bangs ) |
| 470 |
{ |
| 471 |
for ( ei=0; ei<onbevents; ei++ ) |
| 472 |
{ |
| 473 |
outlet_free( x->x_bangs[ei] ); |
| 474 |
} |
| 475 |
freebytes( x->x_bangs, onbevents*sizeof(t_outlet*) ); |
| 476 |
} |
| 477 |
post( "exciter : creating new ones" ); |
| 478 |
x->x_bangs = (t_outlet **) getbytes( x->x_nbevents*sizeof(t_outlet **) ); |
| 479 |
for ( bi=0; bi<x->x_nbevents; bi++ ) |
| 480 |
{ |
| 481 |
x->x_bangs[bi] = outlet_new( &x->x_obj, &s_bang ); |
| 482 |
} |
| 483 |
} |
| 484 |
canvas_fixlinesfor( canvas, (t_text*)x ); |
| 485 |
exciter_draw_new(x, x->x_glist); |
| 486 |
exciter_draw_update(x, x->x_glist); |
| 487 |
exciter_resume( x ); |
| 488 |
} |
| 489 |
|
| 490 |
static void exciter_delete(t_gobj *z, t_glist *glist) |
| 491 |
{ |
| 492 |
canvas_deletelinesfor(glist, (t_text *)z); |
| 493 |
} |
| 494 |
|
| 495 |
static void exciter_displace(t_gobj *z, t_glist *glist, int dx, int dy) |
| 496 |
{ |
| 497 |
t_exciter *x = (t_exciter *)z; |
| 498 |
t_int xold = text_xpix(&x->x_obj, glist); |
| 499 |
t_int yold = text_ypix(&x->x_obj, glist); |
| 500 |
|
| 501 |
// post( "exciter_displace dx=%d dy=%d", dx, dy ); |
| 502 |
|
| 503 |
x->x_obj.te_xpix += dx; |
| 504 |
x->x_obj.te_ypix += dy; |
| 505 |
if(xold != text_xpix(&x->x_obj, glist) || yold != text_ypix(&x->x_obj, glist)) |
| 506 |
{ |
| 507 |
exciter_draw_move(x, glist); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
static int exciter_click(t_gobj *z, struct _glist *glist, |
| 512 |
int xpix, int ypix, int shift, int alt, int dbl, int doit) |
| 513 |
{ |
| 514 |
t_exciter* x = (t_exciter *)z; |
| 515 |
t_int nevent, npix; |
| 516 |
|
| 517 |
if ( doit) |
| 518 |
{ |
| 519 |
nevent = ( 1 - ( ( ypix - ( (float)x->x_height / (float) x->x_nbevents / 2 ) |
| 520 |
- text_ypix(&x->x_obj, glist)) / (float)x->x_height ) )*(x->x_nbevents-1); |
| 521 |
npix = ( xpix - 1 - text_xpix(&x->x_obj, glist)) / EXCITER_PIXEL_GRAIN; |
| 522 |
// post( "exciter : selected event (%d,%d)", nevent, npix ); |
| 523 |
// set or unset event |
| 524 |
{ |
| 525 |
if ( *(x->x_sbangs+nevent*(x->x_width/EXCITER_PIXEL_GRAIN)+npix ) == 1 ) |
| 526 |
{ |
| 527 |
*(x->x_sbangs+nevent*(x->x_width/EXCITER_PIXEL_GRAIN)+npix ) = 0; |
| 528 |
exciter_delete_gem( x, glist, npix, nevent ); |
| 529 |
} |
| 530 |
else |
| 531 |
{ |
| 532 |
*(x->x_sbangs+nevent*(x->x_width/EXCITER_PIXEL_GRAIN)+npix ) = 1; |
| 533 |
exciter_draw_gem( x, glist, npix, nevent ); |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
return (1); |
| 538 |
} |
| 539 |
|
| 540 |
static t_exciter *exciter_new(t_symbol *s, int argc, t_atom *argv) |
| 541 |
{ |
| 542 |
int bi, i, ei, gi; |
| 543 |
t_exciter *x; |
| 544 |
t_pd *x2; |
| 545 |
|
| 546 |
// post( "exciter_new : create : %s argc =%d", s->s_name, argc ); |
| 547 |
|
| 548 |
x = (t_exciter *)pd_new(exciter_class); |
| 549 |
x->x_glist = (t_glist *) canvas_getcurrent(); |
| 550 |
// new exciter created from the gui |
| 551 |
if ( argc != 0 ) |
| 552 |
{ |
| 553 |
if ( argc < 6 ) |
| 554 |
{ |
| 555 |
post( "exciter : error in the number of arguments ( %d )", argc ); |
| 556 |
return NULL; |
| 557 |
} |
| 558 |
if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT || |
| 559 |
argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT || |
| 560 |
argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ) |
| 561 |
{ |
| 562 |
post( "exciter : wrong arguments" ); |
| 563 |
return NULL; |
| 564 |
} |
| 565 |
|
| 566 |
x->x_width = argv[0].a_w.w_float; |
| 567 |
x->x_height = argv[1].a_w.w_float; |
| 568 |
x->x_nbevents = argv[2].a_w.w_float; |
| 569 |
if ( x->x_nbevents < 1 ) x->x_nbevents = 1; |
| 570 |
x->x_timegrain = argv[3].a_w.w_float; |
| 571 |
x->x_loop = argv[4].a_w.w_float; |
| 572 |
x->x_save = argv[5].a_w.w_float; |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
x->x_width = DEFAULT_EXCITER_WIDTH; |
| 577 |
x->x_height = DEFAULT_EXCITER_HEIGHT; |
| 578 |
x->x_nbevents = DEFAULT_EXCITER_NBEVENTS; |
| 579 |
x->x_timegrain = DEFAULT_EXCITER_GRAIN; |
| 580 |
x->x_loop = 1; |
| 581 |
x->x_save = 1; |
| 582 |
|
| 583 |
} |
| 584 |
|
| 585 |
// common fields for new and restored exciters |
| 586 |
x->x_sbangs = (t_int*) getbytes( x->x_nbevents*x->x_width/EXCITER_PIXEL_GRAIN*sizeof(t_int) ); |
| 587 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 588 |
{ |
| 589 |
for ( gi=0; gi<(x->x_width/EXCITER_PIXEL_GRAIN); gi++ ) |
| 590 |
{ |
| 591 |
*(x->x_sbangs+ei*(x->x_width/EXCITER_PIXEL_GRAIN)+gi) = 0; |
| 592 |
} |
| 593 |
} |
| 594 |
memset( x->x_sbangs, 0x0, x->x_nbevents*x->x_width/EXCITER_PIXEL_GRAIN*sizeof(t_int) ); |
| 595 |
x->x_selected = 0; |
| 596 |
x->x_started = 0; |
| 597 |
x->x_reltime = 0L; |
| 598 |
x->x_plooptime = 0L; |
| 599 |
x->x_gindex = -1; |
| 600 |
x->x_looplength = x->x_timegrain * x->x_width * 1000 / EXCITER_PIXEL_GRAIN; |
| 601 |
|
| 602 |
x->x_bangs = (t_outlet **) getbytes( x->x_nbevents*sizeof(t_outlet **) ); |
| 603 |
for ( bi=0; bi<x->x_nbevents; bi++ ) |
| 604 |
{ |
| 605 |
x->x_bangs[bi] = outlet_new( &x->x_obj, &s_bang ); |
| 606 |
} |
| 607 |
|
| 608 |
// post( "exciter : argc : %d", argc ); |
| 609 |
if ( ( argc != 0 ) && ( x->x_save ) ) |
| 610 |
{ |
| 611 |
int ai = 6; |
| 612 |
int si = 0; |
| 613 |
|
| 614 |
while ( ai < argc - 1 ) |
| 615 |
{ |
| 616 |
*(x->x_sbangs |
| 617 |
+((int)argv[ai].a_w.w_float)*(x->x_width/EXCITER_PIXEL_GRAIN) |
| 618 |
+(int)argv[ai+1].a_w.w_float) = 1; |
| 619 |
ai += 2; |
| 620 |
} |
| 621 |
} |
| 622 |
post( "exciter_new width: %d height : %d", x->x_width, x->x_height ); |
| 623 |
|
| 624 |
return (x); |
| 625 |
} |
| 626 |
|
| 627 |
static void exciter_dump(t_exciter *x) |
| 628 |
{ |
| 629 |
t_int ei, gi; |
| 630 |
|
| 631 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 632 |
{ |
| 633 |
for ( gi=0; gi<(x->x_width/EXCITER_PIXEL_GRAIN); gi++ ) |
| 634 |
{ |
| 635 |
if ( *(x->x_sbangs+ei*(x->x_width/EXCITER_PIXEL_GRAIN)+gi ) != 0 ) |
| 636 |
{ |
| 637 |
post( "exciter : value ( %d, %d ) : %d", ei, gi, |
| 638 |
*(x->x_sbangs+ei*(x->x_width/EXCITER_PIXEL_GRAIN)+gi ) ); |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
/* clearing all events */ |
| 645 |
static void exciter_clear(t_exciter *x) |
| 646 |
{ |
| 647 |
t_int ei, gi; |
| 648 |
|
| 649 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 650 |
{ |
| 651 |
for ( gi=0; gi<(x->x_width/EXCITER_PIXEL_GRAIN); gi++ ) |
| 652 |
{ |
| 653 |
*(x->x_sbangs+ei*(x->x_width/EXCITER_PIXEL_GRAIN)+gi ) = 0; |
| 654 |
} |
| 655 |
} |
| 656 |
exciter_draw_update( x, x->x_glist ); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
/* starting the triggering of events */ |
| 661 |
static void exciter_start(t_exciter *x) |
| 662 |
{ |
| 663 |
x->x_started = 1; |
| 664 |
x->x_reltime = 0L; |
| 665 |
x->x_plooptime = 0L; |
| 666 |
x->x_gindex = -1; |
| 667 |
} |
| 668 |
|
| 669 |
/* stop the triggering of events */ |
| 670 |
static void exciter_stop(t_exciter *x) |
| 671 |
{ |
| 672 |
x->x_started = 0; |
| 673 |
} |
| 674 |
|
| 675 |
/* reset the triggering of events */ |
| 676 |
static void exciter_reset(t_exciter *x) |
| 677 |
{ |
| 678 |
x->x_reltime = 0L; |
| 679 |
x->x_plooptime = 0L; |
| 680 |
x->x_gindex = -1; |
| 681 |
} |
| 682 |
|
| 683 |
static t_int *exciter_perform(t_int *w) |
| 684 |
{ |
| 685 |
t_int ei, gi; |
| 686 |
t_int gstart, gend; |
| 687 |
t_exciter* x = (t_exciter*)(w[1]); |
| 688 |
struct timeval tv; |
| 689 |
struct timezone tz; |
| 690 |
long long looptime = 0L; |
| 691 |
double preltime = x->x_reltime; |
| 692 |
t_canvas *canvas=glist_getcanvas(x->x_glist); |
| 693 |
|
| 694 |
if ( x->x_started ) |
| 695 |
{ |
| 696 |
// get current time in ms |
| 697 |
gettimeofday( &tv, &tz ); |
| 698 |
looptime = tv.tv_sec*1000 + tv.tv_usec/1000; |
| 699 |
if ( x->x_plooptime == 0L ) |
| 700 |
{ |
| 701 |
x->x_plooptime = looptime; |
| 702 |
} |
| 703 |
x->x_reltime += ( looptime - x->x_plooptime ); |
| 704 |
if ( x->x_reltime > x->x_looplength ) |
| 705 |
{ |
| 706 |
if ( x->x_loop ) |
| 707 |
{ |
| 708 |
// post( "exciter : restarting loop" ); |
| 709 |
x->x_reltime = 0; |
| 710 |
preltime = 0; |
| 711 |
x->x_gindex = -1; |
| 712 |
} |
| 713 |
else |
| 714 |
{ |
| 715 |
// post( "exciter : end of the loop" ); |
| 716 |
x->x_reltime = 0; |
| 717 |
x->x_started = 0; |
| 718 |
} |
| 719 |
} |
| 720 |
gstart = preltime/(x->x_timegrain*1000); |
| 721 |
gend = x->x_reltime/(x->x_timegrain*1000); |
| 722 |
|
| 723 |
// prevent overflow due to long long precision |
| 724 |
if ( gstart > x->x_width/EXCITER_PIXEL_GRAIN-1 ) gstart = x->x_width/EXCITER_PIXEL_GRAIN-1; |
| 725 |
if ( gstart < 0 ) gstart = 0; |
| 726 |
if ( gend > x->x_width/EXCITER_PIXEL_GRAIN-1 ) gend = x->x_width/EXCITER_PIXEL_GRAIN-1 ; |
| 727 |
if ( gend < 0 ) gend = 0; |
| 728 |
if ( gstart > x->x_gindex ) |
| 729 |
{ |
| 730 |
// post( "exciter : focus slice : (%d,%d)", gstart, gend ); |
| 731 |
for ( gi=x->x_gindex+1; gi<=gend; gi++ ) |
| 732 |
{ |
| 733 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 734 |
{ |
| 735 |
if ( *(x->x_sbangs+ei*(x->x_width/EXCITER_PIXEL_GRAIN)+gi ) == 1 ) |
| 736 |
{ |
| 737 |
outlet_bang( x->x_bangs[ ei ] ); |
| 738 |
SYS_VGUI5(".x%lx.c itemconfigure %xEVENT%.4d%.4d -fill #00FF00\n", |
| 739 |
canvas, x, gi, ei); |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
// unfocus previous events |
| 744 |
if ( gstart >= x->x_width/EXCITER_PIXEL_GRAIN - 1) |
| 745 |
gstart = x->x_width/EXCITER_PIXEL_GRAIN; // not too proud of this one |
| 746 |
// post( "exciter : unfocus slice : (%d,%d)", x->x_gindex, gstart-1 ); |
| 747 |
for ( gi=x->x_gindex; gi<gstart; gi++ ) |
| 748 |
{ |
| 749 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 750 |
{ |
| 751 |
if ( *(x->x_sbangs+ei*(x->x_width/EXCITER_PIXEL_GRAIN)+gi ) == 1 ) |
| 752 |
{ |
| 753 |
SYS_VGUI5(".x%lx.c itemconfigure %xEVENT%.4d%.4d -fill #FFFFFF\n", |
| 754 |
canvas, x, gi, ei); |
| 755 |
} |
| 756 |
} |
| 757 |
} |
| 758 |
x->x_gindex = gend; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
x->x_plooptime = looptime; |
| 763 |
return (w+2); |
| 764 |
} |
| 765 |
|
| 766 |
static void exciter_dsp(t_exciter *x, t_signal **sp) |
| 767 |
{ |
| 768 |
dsp_add(exciter_perform, 1, x); |
| 769 |
} |
| 770 |
|
| 771 |
static void exciter_free(t_exciter *x) |
| 772 |
{ |
| 773 |
t_int ei; |
| 774 |
|
| 775 |
// post( "exciter~: exciter_free" ); |
| 776 |
if ( x->x_bangs ) |
| 777 |
{ |
| 778 |
for ( ei=0; ei<x->x_nbevents; ei++ ) |
| 779 |
{ |
| 780 |
outlet_free( x->x_bangs[ei] ); |
| 781 |
} |
| 782 |
freebytes( x->x_bangs, x->x_nbevents*sizeof(t_outlet*) ); |
| 783 |
} |
| 784 |
if ( x->x_sbangs ) |
| 785 |
{ |
| 786 |
freebytes( x->x_sbangs, x->x_nbevents*x->x_width/EXCITER_PIXEL_GRAIN*sizeof(t_int) ); |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
void exciter_setup(void) |
| 791 |
{ |
| 792 |
logpost(NULL, 4, exciter_version ); |
| 793 |
exciter_class = class_new(gensym("exciter"), (t_newmethod)exciter_new, |
| 794 |
(t_method)exciter_free, sizeof(t_exciter), 0, A_GIMME, 0); |
| 795 |
class_addmethod(exciter_class, (t_method)exciter_dialog, gensym("dialog"), A_GIMME, 0); |
| 796 |
class_addmethod(exciter_class, (t_method)exciter_dump, gensym("dump"), 0); |
| 797 |
class_addmethod(exciter_class, (t_method)exciter_clear, gensym("clear"), 0); |
| 798 |
class_addmethod(exciter_class, (t_method)exciter_start, gensym("start"), 0); |
| 799 |
class_addmethod(exciter_class, (t_method)exciter_stop, gensym("stop"), 0); |
| 800 |
class_addmethod(exciter_class, (t_method)exciter_reset, gensym("reset"), 0); |
| 801 |
class_addmethod(exciter_class, (t_method)exciter_pause, gensym("pause"), 0); |
| 802 |
class_addmethod(exciter_class, (t_method)exciter_resume, gensym("resume"), 0); |
| 803 |
class_addmethod(exciter_class, (t_method)exciter_dsp, gensym("dsp"), A_NULL); |
| 804 |
exciter_widgetbehavior.w_getrectfn = exciter_getrect; |
| 805 |
exciter_widgetbehavior.w_displacefn = exciter_displace; |
| 806 |
exciter_widgetbehavior.w_selectfn = exciter_select; |
| 807 |
exciter_widgetbehavior.w_activatefn = NULL; |
| 808 |
exciter_widgetbehavior.w_deletefn = exciter_delete; |
| 809 |
exciter_widgetbehavior.w_visfn = exciter_vis; |
| 810 |
exciter_widgetbehavior.w_clickfn = exciter_click; |
| 811 |
|
| 812 |
#if PD_MINOR_VERSION >= 37 |
| 813 |
class_setpropertiesfn(exciter_class, exciter_properties); |
| 814 |
class_setsavefn(exciter_class, exciter_save); |
| 815 |
#else |
| 816 |
exciter_widgetbehavior.w_propertiesfn = exciter_properties; |
| 817 |
exciter_widgetbehavior.w_savefn = exciter_save; |
| 818 |
#endif |
| 819 |
|
| 820 |
class_setwidget(exciter_class, &exciter_widgetbehavior); |
| 821 |
|
| 822 |
sys_vgui("eval [read [open {%s/%s.tcl}]]\n", |
| 823 |
exciter_class->c_externdir->s_name, exciter_class->c_name->s_name); |
| 824 |
} |