Debian Codegraph @

pianoroll.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* Copyright (c) 1997-1999 Miller Puckette. */
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 /* pianoroll : a graphical object which enables */
6 /* to control a sequencer ( pitch and volume ) */
7 /* */
8 /* Copyleft Yves Degoyon ( [email protected] ) */
9 /* */
10 /* This program is free software; you can redistribute it and/or */
11 /* modify it under the terms of the GNU General Public License */
12 /* as published by the Free Software Foundation; either version 2 */
13 /* of the License, or (at your option) any later version. */
14 /* */
15 /* See file LICENSE for further informations on licensing terms. */
16 /* */
17 /* This program is distributed in the hope that it will be useful, */
18 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
19 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
20 /* GNU General Public License for more details. */
21 /* */
22 /* You should have received a copy of the GNU General Public License */
23 /* along with this program; if not, write to the Free Software */
24 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 /* */
26 /* Based on PureData by Miller Puckette and others. */
27 /* */
28 /* "If you obey society's rules" */
29 /* "You'll be society's fool" */
30 /* Devo - Society's rules */
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 "m_pd.h"
40 #include "m_imp.h"
41 #include "g_canvas.h"
42
43 #include "pianoroll.h"
44
45 #ifdef _WIN32
46 #include <io.h>
47 #else
48 #include <unistd.h>
49 #endif
50
51 /* support older Pd versions without sys_fopen(), sys_fclose(), and, sys_close() */
52 #if PD_MAJOR_VERSION == 0 && PD_MINOR_VERSION < 44
53 #define sys_fopen fopen
54 #define sys_fclose fclose
55 #define sys_close close
56 #endif
57
58 /* needed to create a pianoroll from PD's menu
59 void canvas_objtext(t_glist *gl, int xpos, int ypos, int selected, t_binbuf *b);
60 void canvas_startmotion(t_canvas *x);
61 */
62
63 #define DEFAULT_SEQUENCER_WIDTH 200
64 #define DEFAULT_SEQUENCER_HEIGHT 200
65 #define DEFAULT_SEQUENCER_STEPS 16
66 #define DEFAULT_SEQUENCER_NBGRADES 31
67 #define DEFAULT_SEQUENCER_PITCH_MIN -15
68 #define DEFAULT_SEQUENCER_PITCH_MAX 15
69
70 static char *pianoroll_version = "pianoroll: a graphical sequencer controller, version 0.10 ([email protected])";
71
72 t_widgetbehavior pianoroll_widgetbehavior;
73 static t_class *pianoroll_class;
74 static int pianorollcount=0;
75
76 static int guidebug=0;
77 static int pointsize = 5;
78
79 #define SYS_VGUI2(a,b) if (guidebug) \
80 post(a,b);\
81 sys_vgui(a,b)
82
83 #define SYS_VGUI3(a,b,c) if (guidebug) \
84 post(a,b,c);\
85 sys_vgui(a,b,c)
86
87 #define SYS_VGUI4(a,b,c,d) if (guidebug) \
88 post(a,b,c,d);\
89 sys_vgui(a,b,c,d)
90
91 #define SYS_VGUI5(a,b,c,d,e) if (guidebug) \
92 post(a,b,c,d,e);\
93 sys_vgui(a,b,c,d,e)
94
95 #define SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \
96 post(a,b,c,d,e,f,g );\
97 sys_vgui(a,b,c,d,e,f,g)
98
99 #define SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \
100 post(a,b,c,d,e,f,g,h,i );\
101 sys_vgui(a,b,c,d,e,f,g,h,i)
102
103 /* drawing functions */
104 static void pianoroll_draw_update(t_pianoroll *x, t_glist *glist)
105 {
106 t_int si;
107 t_canvas *canvas=glist_getcanvas(glist);
108
109 for ( si=0; si<x->x_nbsteps; si++ )
110 {
111 int vi = (int)((1.0-x->x_volumes[si])*(x->x_nbgrades-1));
112 int pi = (int)((x->x_pmax-x->x_peaches[si])/(x->x_pmax-x->x_pmin)*(x->x_nbgrades-1));
113
114 x->x_ivolumes[ si ] = vi;
115 x->x_ipeaches[ si ] = pi;
116 SYS_VGUI5(".x%lx.c itemconfigure %xPITCH%.4d%.4d -fill #FFFF00\n", canvas, x, si, pi);
117 SYS_VGUI5(".x%lx.c itemconfigure %xVOLUME%.4d%.4d -fill #FF0000\n", canvas, x, si, vi);
118 }
119 }
120
121 static void pianoroll_draw_new(t_pianoroll *x, t_glist *glist)
122 {
123 t_canvas *canvas=glist_getcanvas(glist);
124
125 // draw the grid
126 {
127 int gi, gj;
128 t_float xgstep = x->x_width/x->x_nbsteps;
129 t_float ygstep = x->x_height/x->x_nbgrades;
130 for ( gi=0; gi<x->x_nbsteps; gi++ )
131 {
132 for ( gj=0; gj<x->x_nbgrades; gj++ )
133 {
134 SYS_VGUI9(".x%lx.c create rectangle %d %d %d %d -fill #771623 -outline #998121 -tags %xPITCH%.4d%.4d\n",
135 canvas,
136 text_xpix(&x->x_obj, glist)+(int)(gi*xgstep),
137 text_ypix(&x->x_obj, glist)+(int)(gj*ygstep),
138 text_xpix(&x->x_obj, glist)+(int)(gi*xgstep)+(int)(2*xgstep/3),
139 text_ypix(&x->x_obj, glist)+(int)((gj+1)*ygstep),
140 x, gi, gj );
141 SYS_VGUI9(".x%lx.c create rectangle %d %d %d %d -fill #562663 -outline #998121 -tags %xVOLUME%.4d%.4d\n",
142 canvas,
143 text_xpix(&x->x_obj, glist)+(int)(gi*xgstep)+(int)(2*xgstep/3),
144 text_ypix(&x->x_obj, glist)+(int)(gj*ygstep),
145 text_xpix(&x->x_obj, glist)+(int)((gi+1)*xgstep),
146 text_ypix(&x->x_obj, glist)+(int)((gj+1)*ygstep),
147 x, gi, gj );
148 }
149 }
150 // adjust height and width
151 x->x_width = (int)((x->x_nbsteps)*xgstep);
152 x->x_height = (int)((x->x_nbgrades)*ygstep);
153 }
154 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %xIN\n",
155 canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist) - 1,
156 text_xpix(&x->x_obj, glist)+7, text_ypix(&x->x_obj, glist),
157 x);
158 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %xOUTL\n",
159 canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist) + x->x_height+1,
160 text_xpix(&x->x_obj, glist)+7, text_ypix(&x->x_obj, glist) + x->x_height+2,
161 x);
162 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %xOUTR\n",
163 canvas, text_xpix(&x->x_obj, glist)+x->x_width-7, text_ypix(&x->x_obj, glist) + x->x_height+1,
164 text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist) + x->x_height+2,
165 x);
166
167 canvas_fixlinesfor( canvas, (t_text*)x );
168 }
169
170 static void pianoroll_draw_move(t_pianoroll *x, t_glist *glist)
171 {
172 t_canvas *canvas=glist_getcanvas(glist);
173
174 // move the grid
175 {
176 int gi, gj;
177 t_float xgstep = x->x_width/x->x_nbsteps;
178 t_float ygstep = x->x_height/x->x_nbgrades;
179 for ( gi=0; gi<x->x_nbsteps; gi++ )
180 {
181 for ( gj=0; gj<x->x_nbgrades; gj++ )
182 {
183 SYS_VGUI9(".x%lx.c coords %xPITCH%.4d%.4d %d %d %d %d\n",
184 canvas, x, gi, gj,
185 text_xpix(&x->x_obj, glist)+(int)(gi*xgstep),
186 text_ypix(&x->x_obj, glist)+(int)(gj*ygstep),
187 text_xpix(&x->x_obj, glist)+(int)(gi*xgstep)+(int)(2*xgstep/3),
188 text_ypix(&x->x_obj, glist)+(int)((gj+1)*ygstep)
189 );
190 SYS_VGUI9(".x%lx.c coords %xVOLUME%.4d%.4d %d %d %d %d\n",
191 canvas, x, gi, gj,
192 text_xpix(&x->x_obj, glist)+(int)(gi*xgstep)+(int)(2*xgstep/3),
193 text_ypix(&x->x_obj, glist)+(int)(gj*ygstep),
194 text_xpix(&x->x_obj, glist)+(int)((gi+1)*xgstep),
195 text_ypix(&x->x_obj, glist)+(int)((gj+1)*ygstep)
196 );
197 }
198 }
199 }
200 SYS_VGUI7(".x%lx.c coords %xIN %d %d %d %d \n",
201 canvas, x, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist) - 1,
202 text_xpix(&x->x_obj, glist)+7, text_ypix(&x->x_obj, glist)
203 );
204 SYS_VGUI7(".x%lx.c coords %xOUTL %d %d %d %d\n",
205 canvas, x, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist) + x->x_height+1,
206 text_xpix(&x->x_obj, glist)+7, text_ypix(&x->x_obj, glist) + x->x_height+2
207 );
208 SYS_VGUI7(".x%lx.c coords %xOUTR %d %d %d %d\n",
209 canvas, x, text_xpix(&x->x_obj, glist)+x->x_width-7, text_ypix(&x->x_obj, glist) + x->x_height+1,
210 text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist) + x->x_height+2
211 );
212 canvas_fixlinesfor( canvas, (t_text*)x );
213 }
214
215 static void pianoroll_draw_erase(t_pianoroll* x,t_glist* glist)
216 {
217 t_canvas *canvas=glist_getcanvas(glist);
218 t_int i;
219
220 SYS_VGUI3(".x%lx.c delete %xIN\n", canvas, x);
221 SYS_VGUI3(".x%lx.c delete %xOUTL\n", canvas, x);
222 SYS_VGUI3(".x%lx.c delete %xOUTR\n", canvas, x);
223 // delete the grid
224 {
225 int gi, gj;
226 for ( gi=0; gi<x->x_nbsteps; gi++ )
227 {
228 for ( gj=0; gj<x->x_nbgrades; gj++ )
229 {
230 SYS_VGUI5(".x%lx.c delete %xPITCH%.4d%.4d\n", canvas, x, gi, gj);
231 SYS_VGUI5(".x%lx.c delete %xVOLUME%.4d%.4d\n", canvas, x, gi, gj);
232 }
233 }
234 }
235 }
236
237 static void pianoroll_draw_select(t_pianoroll* x,t_glist* glist)
238 {
239 t_canvas *canvas=glist_getcanvas(glist);
240
241 if(x->x_selected)
242 {
243 pd_bind(&x->x_obj.ob_pd, x->x_name);
244 /* sets the item in blue */
245 }
246 else
247 {
248 pd_unbind(&x->x_obj.ob_pd, x->x_name);
249 }
250 }
251
252 /* ------------------------ pianoroll widgetbehaviour----------------------------- */
253
254
255 static void pianoroll_getrect(t_gobj *z, t_glist *owner,
256 int *xp1, int *yp1, int *xp2, int *yp2)
257 {
258 t_pianoroll* x = (t_pianoroll*)z;
259
260 *xp1 = text_xpix(&x->x_obj, owner);
261 *yp1 = text_ypix(&x->x_obj, owner);
262 *xp2 = text_xpix(&x->x_obj, owner)+x->x_width;
263 *yp2 = text_ypix(&x->x_obj, owner)+x->x_height;
264 }
265
266 static void pianoroll_save(t_gobj *z, t_binbuf *b)
267 {
268 t_pianoroll *x = (t_pianoroll *)z;
269 t_int i;
270
271 // post( "saving pianoroll : %s", x->x_name->s_name );
272 binbuf_addv(b, "ssiissiiffiiifi", gensym("#X"),gensym("obj"),
273 (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix,
274 atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
275 x->x_name, x->x_width, x->x_height,
276 x->x_pmin, x->x_pmax,
277 x->x_nbgrades, x->x_nbsteps,
278 x->x_defvalue, x->x_transpose, x->x_save
279 );
280 if ( x->x_save )
281 {
282 for ( i=0; i<x->x_nbsteps; i++ )
283 {
284 binbuf_addv(b, "ff", x->x_peaches[i], x->x_volumes[i] );
285 }
286 }
287 binbuf_addv(b, ";");
288 }
289
290 static void pianoroll_properties(t_gobj *z, t_glist *owner)
291 {
292 char buf[800];
293 t_pianoroll *x=(t_pianoroll *)z;
294
295 sprintf(buf, "pdtk_pianoroll_dialog %%s %s %d %d %.2f %.2f %d %d %d %d\n",
296 x->x_name->s_name, x->x_width, x->x_height, x->x_pmin, x->x_pmax,
297 x->x_nbgrades, x->x_nbsteps, x->x_defvalue, x->x_save );
298 // post("pianoroll_properties : %s", buf );
299 gfxstub_new(&x->x_obj.ob_pd, x, buf);
300 }
301
302 static void pianoroll_select(t_gobj *z, t_glist *glist, int selected)
303 {
304 t_pianoroll *x = (t_pianoroll *)z;
305
306 x->x_selected = selected;
307 pianoroll_draw_select( x, glist );
308 }
309
310 static void pianoroll_vis(t_gobj *z, t_glist *glist, int vis)
311 {
312 t_pianoroll *x = (t_pianoroll *)z;
313 t_rtext *y;
314
315 // post("pianoroll_vis : %d", vis );
316 if (vis)
317 {
318 pianoroll_draw_new( x, glist );
319 pianoroll_draw_update( x, glist );
320 }
321 else
322 {
323 pianoroll_draw_erase( x, glist );
324 }
325 }
326
327 static void pianoroll_dialog(t_pianoroll *x, t_symbol *s, int argc, t_atom *argv)
328 {
329 int si, onbsteps;
330 t_float *newpeaches, *newvolumes;
331
332 if ( !x )
333 {
334 post( "pianoroll : error :tried to set properties on an unexisting object" );
335 }
336 if ( ( argv[5].a_w.w_float <= 0 ) || ( argv[5].a_w.w_float <= 0 ) )
337 {
338 post( "pianoroll : error : wrong number of steps or grades" );
339 return;
340 }
341 if ( ( argv[3].a_w.w_float >= argv[4].a_w.w_float ) )
342 {
343 post( "pianoroll : error : min pitch is >= to max pitch" );
344 return;
345 }
346 if ( argc != 9 )
347 {
348 post( "pianoroll : error in the number of arguments ( %d )", argc );
349 return;
350 }
351 if ( argv[0].a_type != A_SYMBOL || argv[1].a_type != A_FLOAT ||
352 argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
353 argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ||
354 argv[6].a_type != A_FLOAT || argv[7].a_type != A_FLOAT ||
355 argv[8].a_type != A_FLOAT
356 )
357 {
358 post( "pianoroll : wrong arguments" );
359 return;
360 }
361 pianoroll_draw_erase(x, x->x_glist);
362 x->x_name = argv[0].a_w.w_symbol;
363 x->x_width = (int)argv[1].a_w.w_float;
364 x->x_height = (int)argv[2].a_w.w_float;
365 x->x_pmin = argv[3].a_w.w_float;
366 x->x_pmax = argv[4].a_w.w_float;
367 x->x_nbgrades = argv[5].a_w.w_float;
368 onbsteps = x->x_nbsteps;
369 x->x_nbsteps = argv[6].a_w.w_float;
370 x->x_defvalue = argv[7].a_w.w_float;
371 x->x_save = argv[8].a_w.w_float;
372
373 if ( onbsteps != x->x_nbsteps )
374 {
375 int cmindex = ( onbsteps > x->x_nbsteps ) ? x->x_nbsteps : onbsteps;
376
377 newpeaches = ( t_float* ) getbytes( x->x_nbsteps*sizeof(t_float) );
378 newvolumes = ( t_float* ) getbytes( x->x_nbsteps*sizeof(t_float) );
379 for ( si=0; si<cmindex ; si++ )
380 {
381 newpeaches[si] = x->x_peaches[si];
382 newvolumes[si] = x->x_volumes[si];
383 }
384 freebytes( x->x_peaches, onbsteps*sizeof( t_float ) );
385 freebytes( x->x_volumes, onbsteps*sizeof( t_float ) );
386 x->x_peaches = newpeaches;
387 x->x_volumes = newvolumes;
388 for ( si=onbsteps; si<x->x_nbsteps; si++ )
389 {
390 x->x_peaches[si] = x->x_defvalue;
391 x->x_volumes[si] = 1.0;
392 }
393 freebytes( x->x_ipeaches, onbsteps*sizeof( t_int ) );
394 freebytes( x->x_ivolumes, onbsteps*sizeof( t_int ) );
395 x->x_ipeaches = ( t_int* ) getbytes( x->x_nbsteps*sizeof(t_int) );
396 x->x_ivolumes = ( t_int* ) getbytes( x->x_nbsteps*sizeof(t_int) );
397 }
398 pianoroll_draw_new(x, x->x_glist);
399 pianoroll_draw_update(x, x->x_glist);
400 }
401
402 static void pianoroll_delete(t_gobj *z, t_glist *glist)
403 {
404 canvas_deletelinesfor(glist, (t_text *)z);
405 }
406
407 static void pianoroll_displace(t_gobj *z, t_glist *glist, int dx, int dy)
408 {
409 t_pianoroll *x = (t_pianoroll *)z;
410 int xold = text_xpix(&x->x_obj, glist);
411 int yold = text_ypix(&x->x_obj, glist);
412
413 // post( "pianoroll_displace dx=%d dy=%d", dx, dy );
414
415 x->x_obj.te_xpix += dx;
416 x->x_obj.te_ypix += dy;
417 if(xold != x->x_obj.te_xpix || yold != x->x_obj.te_ypix)
418 {
419 pianoroll_draw_move(x, x->x_glist);
420 }
421 }
422
423 static int pianoroll_click(t_gobj *z, struct _glist *glist,
424 int xpix, int ypix, int shift, int alt, int dbl, int doit)
425 {
426 t_pianoroll* x = (t_pianoroll *)z;
427 t_canvas *canvas=glist_getcanvas(glist);
428
429 if ( doit)
430 {
431 // calculate position to update
432 {
433 int si, gi;
434 t_float xgstep = x->x_width/x->x_nbsteps;
435 t_float ygstep = x->x_height/x->x_nbgrades;
436
437 si = ( xpix - text_xpix(&x->x_obj, glist) ) / xgstep;
438 gi = ( ypix - text_ypix(&x->x_obj, glist) ) / ygstep;
439
440 // post( "pianoroll : step : %d : grade : %d", si, gi );
441
442 if ( ( xpix - text_xpix(&x->x_obj, glist) ) > ( si*xgstep+2*xgstep/3 ) )
443 {
444 {
445 SYS_VGUI5(".x%lx.c itemconfigure %xVOLUME%.4d%.4d -fill #562663\n", canvas, x, si, x->x_ivolumes[ si ] );
446 }
447
448 x->x_volumes[ si ] = (((float)x->x_nbgrades-1-(float)gi))/(float)(x->x_nbgrades-1);
449 SYS_VGUI5(".x%lx.c itemconfigure %xVOLUME%.4d%.4d -fill #FF0000\n", canvas, x, si, gi);
450 x->x_ivolumes[ si ] = gi;
451 }
452 else
453 {
454 {
455 SYS_VGUI5(".x%lx.c itemconfigure %xPITCH%.4d%.4d -fill #761623\n", canvas, x, si, x->x_ipeaches[ si ]);
456 }
457
458 x->x_peaches[ si ] = x->x_pmin+(float)(x->x_nbgrades-1-gi)/(float)(x->x_nbgrades-1)*(float)(x->x_pmax-x->x_pmin);
459 SYS_VGUI5(".x%lx.c itemconfigure %xPITCH%.4d%.4d -fill #FFFF00\n", canvas, x, si, gi);
460 x->x_ipeaches[ si ] = gi;
461 }
462 }
463
464 }
465 return (1);
466 }
467
468 static void pianoroll_transpose(t_pianoroll *x, t_floatarg ftranspose)
469 {
470 x->x_transpose = ftranspose;
471 }
472
473 static void pianoroll_save_file(t_pianoroll *x, t_symbol *ffile)
474 {
475 FILE *tmph;
476 t_int si;
477
478 if ( ( tmph = sys_fopen( ffile->s_name, "w" ) ) == NULL )
479 {
480 post( "pianoroll : could not open file : %s for writing", ffile->s_name );
481 return;
482 }
483
484 // post( "saving pianoroll : %s", x->x_name->s_name );
485 fprintf(tmph, "%d %d %f %f %d %d %d %f %d ",
486 x->x_width, x->x_height,
487 x->x_pmin, x->x_pmax,
488 x->x_nbgrades, x->x_nbsteps,
489 x->x_defvalue, x->x_transpose, x->x_save );
490 for ( si=0; si<x->x_nbsteps; si++ )
491 {
492 fprintf(tmph, "%f %f ", x->x_peaches[si], x->x_volumes[si] );
493 }
494
495 if ( fclose( tmph ) == -1 )
496 {
497 post( "pianoroll : could not close file : %s ", ffile->s_name );
498 return;
499 }
500 }
501
502 static void pianoroll_load(t_pianoroll *x, t_symbol *ffile)
503 {
504 FILE *tmph;
505 t_int si;
506
507 if ( ( tmph = sys_fopen( ffile->s_name, "r" ) ) == NULL )
508 {
509 post( "pianoroll : could not open file : %s for reading", ffile->s_name );
510 return;
511 }
512
513 pianoroll_draw_erase(x, x->x_glist);
514 freebytes( x->x_peaches, x->x_nbsteps*sizeof( t_float ) );
515 freebytes( x->x_volumes, x->x_nbsteps*sizeof( t_float ) );
516 freebytes( x->x_ipeaches, x->x_nbsteps*sizeof( t_int ) );
517 freebytes( x->x_ivolumes, x->x_nbsteps*sizeof( t_int ) );
518
519 if ( fscanf(tmph, "%d %d %f %f %d %d %d %f %d",
520 &x->x_width, &x->x_height,
521 &x->x_pmin, &x->x_pmax,
522 &x->x_nbgrades, &x->x_nbsteps,
523 &x->x_defvalue, &x->x_transpose, &x->x_save ) != 9 )
524 {
525 post( "pianoroll : could not restore data from file : %s", ffile->s_name );
526 return;
527 }
528
529 x->x_peaches = ( t_float* ) getbytes( x->x_nbsteps*sizeof(t_float) );
530 x->x_volumes = ( t_float* ) getbytes( x->x_nbsteps*sizeof(t_float) );
531 x->x_ipeaches = ( t_int* ) getbytes( x->x_nbsteps*sizeof(t_int) );
532 x->x_ivolumes = ( t_int* ) getbytes( x->x_nbsteps*sizeof(t_int) );
533 for ( si=0; si<x->x_nbsteps ; si++ )
534 {
535 fscanf( tmph, "%f", &x->x_peaches[si] );
536 fscanf( tmph, "%f", &x->x_volumes[si] );
537 }
538
539 pianoroll_draw_new(x, x->x_glist);
540 pianoroll_draw_update(x, x->x_glist);
541
542 if ( fclose( tmph ) == -1 )
543 {
544 post( "pianoroll : could not close file : %s ", ffile->s_name );
545 return;
546 }
547 }
548
549 static void pianoroll_init(t_pianoroll *x)
550 {
551 t_int si;
552
553 for ( si=0; si<x->x_nbsteps; si++ )
554 {
555 x->x_peaches[si] = x->x_defvalue;
556 x->x_volumes[si] = 1.0;
557 }
558 pianoroll_draw_erase(x, x->x_glist );
559 pianoroll_draw_new(x, x->x_glist );
560 pianoroll_draw_update(x, x->x_glist );
561 }
562
563 static void pianoroll_pitch(t_pianoroll *x, t_floatarg fpos, t_floatarg fpitch)
564 {
565 t_canvas *canvas=glist_getcanvas(x->x_glist);
566 t_int ipos;
567
568 if ( ( ( (t_int) fpos ) < 0 ) || ( ( (t_int) fpos ) >= x->x_nbsteps ) )
569 {
570 post( "pianoroll : wrong pitch position : %d", fpos );
571 return;
572 }
573 ipos = (t_int) fpos;
574 if ( ( ( (t_int) fpitch ) < x->x_pmin ) || ( ( (t_int) fpitch ) > x->x_pmax ) )
575 {
576 post( "pianoroll : wrong pitch value : %d", fpitch );
577 return;
578 }
579
580 {
581 SYS_VGUI5(".x%lx.c itemconfigure %xPITCH%.4d%.4d -fill #761623\n", canvas, x, ipos, x->x_ipeaches[ ipos ]);
582
583 x->x_ipeaches[ ipos ] = (t_int) ( ( ( x->x_pmax - fpitch ) / ( x->x_pmax - x->x_pmin ) ) * ( x->x_nbgrades - 1 ) );
584 x->x_peaches[ ipos ] = x->x_pmin+(float)(x->x_nbgrades-1-x->x_ipeaches[ ipos ])/(float)(x->x_nbgrades-1)*(float)(x->x_pmax-x->x_pmin);
585 SYS_VGUI5(".x%lx.c itemconfigure %xPITCH%.4d%.4d -fill #FFFF00\n", canvas, x, ipos, x->x_ipeaches[ ipos ]);
586 }
587 }
588
589 static void pianoroll_volume(t_pianoroll *x, t_floatarg fpos, t_floatarg fvol)
590 {
591 t_canvas *canvas=glist_getcanvas(x->x_glist);
592 t_int ipos;
593
594 if ( ( ( (t_int) fpos ) < 0 ) || ( ( (t_int) fpos ) >= x->x_nbsteps ) )
595 {
596 post( "pianoroll : wrong volume position : %d", fpos );
597 return;
598 }
599 ipos = (t_int) fpos;
600 if ( ( ( (t_int) fvol ) < 0.0 ) || ( ( (t_int) fvol ) > 1.0 ) )
601 {
602 post( "pianoroll : wrong volume value : %d", fvol );
603 return;
604 }
605
606 {
607 SYS_VGUI5(".x%lx.c itemconfigure %xVOLUME%.4d%.4d -fill #562663\n", canvas, x, ipos, x->x_ivolumes[ ipos ] );
608
609 x->x_ivolumes[ ipos ] = (t_int) ( ( 1 - fvol ) * (x->x_nbgrades-1) );
610 x->x_volumes[ ipos ] = (((float)x->x_nbgrades-1-(float)x->x_ivolumes[ ipos ]))/(float)(x->x_nbgrades-1);
611 SYS_VGUI5(".x%lx.c itemconfigure %xVOLUME%.4d%.4d -fill #FF0000\n", canvas, x, ipos, x->x_ivolumes[ ipos ] );
612 }
613 }
614
615 static void pianoroll_float(t_pianoroll *x, t_floatarg fposition)
616 {
617 t_int pposition, rposition, rrposition;
618 t_float fpart;
619 t_int pi;
620 t_canvas *canvas;
621
622 pposition = ( (int)fposition - 1 ) % x->x_nbsteps;
623 if ( pposition < 0 ) pposition += x->x_nbsteps;
624 rposition = ( (int)fposition ) % x->x_nbsteps;
625 if ( rposition < 0 ) rposition += x->x_nbsteps;
626 rrposition = ( (int)fposition +1 ) % x->x_nbsteps;
627 if ( rrposition < 0 ) rrposition += x->x_nbsteps;
628
629 // post ( "pposition=%d rposition=%d rrposition=%d", pposition, rposition, rrposition );
630 fpart = fposition - (int)fposition;
631 pi=-1;
632 canvas=glist_getcanvas(x->x_glist);
633
634 outlet_float( x->x_pitch, x->x_peaches[ rposition ] +
635 fpart*(x->x_peaches[ rrposition ] - x->x_peaches[ rposition ] ) +
636 x->x_transpose );
637 outlet_float( x->x_volume, x->x_volumes[ rposition ] +
638 fpart*(x->x_volumes[ rrposition ] - x->x_volumes[ rposition ] ) );
639
640 // graphical update
641 {
642 if ( x->x_scurrent != -1 )
643 {
644 SYS_VGUI5(".x%lx.c itemconfigure %xPITCH%.4d%.4d -fill #FFFF00\n", canvas, x,
645 x->x_scurrent, x->x_ipeaches[ x->x_scurrent ]);
646 }
647 x->x_scurrent = rposition;
648 SYS_VGUI5(".x%lx.c itemconfigure %xPITCH%.4d%.4d -fill #00FF00\n", canvas, x,
649 x->x_scurrent, x->x_ipeaches[ x->x_scurrent ]);
650 }
651 }
652
653 static t_pianoroll *pianoroll_new(t_symbol *s, int argc, t_atom *argv)
654 {
655 int si, i, zz;
656 t_pianoroll *x;
657 t_pd *x2;
658 char *str;
659
660 // post( "pianoroll_new : create : %s argc =%d", s->s_name, argc );
661
662 x = (t_pianoroll *)pd_new(pianoroll_class);
663 // new pianoroll created from the gui
664 if ( argc != 0 )
665 {
666 if ( argc < 10 )
667 {
668 post( "pianoroll : error in the number of arguments ( %d )", argc );
669 return NULL;
670 }
671 if ( ( argv[5].a_w.w_float <= 0 ) || ( argv[5].a_w.w_float <= 0 ) )
672 {
673 post( "pianoroll : error : wrong number of steps or grades" );
674 return NULL;
675 }
676 if ( ( argv[3].a_w.w_float >= argv[4].a_w.w_float ) )
677 {
678 post( "pianoroll : error : min pitch is > to max pitch" );
679 return NULL;
680 }
681 if ( argv[0].a_type != A_SYMBOL || argv[1].a_type != A_FLOAT ||
682 argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
683 argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ||
684 argv[6].a_type != A_FLOAT || argv[7].a_type != A_FLOAT ||
685 argv[8].a_type != A_FLOAT || argv[9].a_type != A_FLOAT )
686 {
687 post( "pianoroll : wrong arguments" );
688 return NULL;
689 }
690
691 // update pianoroll count
692 if (!strncmp((str = argv[0].a_w.w_symbol->s_name), "pianoroll", 9)
693 && (zz = atoi(str + 9)) > pianorollcount)
694 {
695 // post( "pianoroll : already %d objects", pianorollcount );
696 pianorollcount = zz;
697 }
698 x->x_name = argv[0].a_w.w_symbol;
699 pd_bind(&x->x_obj.ob_pd, x->x_name);
700 x->x_width = argv[1].a_w.w_float;
701 x->x_height = argv[2].a_w.w_float;
702 x->x_pmin = argv[3].a_w.w_float;
703 x->x_pmax = argv[4].a_w.w_float;
704 x->x_nbgrades = argv[5].a_w.w_float;
705 x->x_nbsteps = argv[6].a_w.w_float;
706 x->x_peaches = (t_float*) getbytes( x->x_nbsteps*sizeof(t_float) );
707 x->x_ipeaches = (t_int*) getbytes( x->x_nbsteps*sizeof(t_int) );
708 x->x_volumes = (t_float*) getbytes( x->x_nbsteps*sizeof(t_float) );
709 x->x_ivolumes = (t_int*) getbytes( x->x_nbsteps*sizeof(t_int) );
710 x->x_defvalue = argv[7].a_w.w_float;
711 x->x_transpose = argv[8].a_w.w_float;
712 x->x_save = argv[9].a_w.w_float;
713 }
714 else
715 {
716 char buf[40];
717
718 sprintf(buf, "pianoroll%d", ++pianorollcount);
719 s = gensym(buf);
720
721 x->x_name = s;
722 pd_bind(&x->x_obj.ob_pd, x->x_name);
723
724 x->x_width = DEFAULT_SEQUENCER_WIDTH;
725 x->x_height = DEFAULT_SEQUENCER_HEIGHT;
726 x->x_pmin = DEFAULT_SEQUENCER_PITCH_MIN;
727 x->x_pmax = DEFAULT_SEQUENCER_PITCH_MAX;
728 x->x_nbgrades = DEFAULT_SEQUENCER_NBGRADES;
729 x->x_nbsteps = DEFAULT_SEQUENCER_STEPS;
730 x->x_peaches = (t_float*) getbytes( x->x_nbsteps*sizeof(t_float) );
731 x->x_ipeaches = (t_int*) getbytes( x->x_nbsteps*sizeof(t_int) );
732 x->x_volumes = (t_float*) getbytes( x->x_nbsteps*sizeof(t_float) );
733 x->x_ivolumes = (t_int*) getbytes( x->x_nbsteps*sizeof(t_int) );
734 x->x_defvalue = 0;
735 x->x_transpose = 0;
736 x->x_save = 1;
737
738 }
739
740 // common fields for new and restored pianorolls
741 x->x_selected = 0;
742 x->x_scurrent = -1;
743 x->x_glist = (t_glist *) canvas_getcurrent();
744 x->x_pitch = outlet_new(&x->x_obj, &s_float );
745 x->x_volume = outlet_new(&x->x_obj, &s_float );
746 // post( "pianoroll : argc : %d", argc );
747 if ( ( argc != 0 ) && ( x->x_save ) )
748 {
749 int ai = 10;
750 int si = 0;
751
752 while ( ai < argc )
753 {
754 x->x_peaches[si] = argv[ai++].a_w.w_float;
755 if ( ai >= argc ) break;
756 x->x_volumes[si++] = argv[ai++].a_w.w_float;
757 }
758 }
759 else // following arguments are the values of pitch, volumes
760 {
761 for ( si=0; si<x->x_nbsteps; si++ )
762 {
763 x->x_peaches[si] = x->x_defvalue;
764 x->x_volumes[si] = 1.0;
765 }
766 }
767
768 // post( "pianoroll_new name : %s width: %d height : %d", x->x_name->s_name, x->x_width, x->x_height );
769
770 return (x);
771 }
772
773 static void pianoroll_free(t_pianoroll *x)
774 {
775 // post( "pianoroll~: pianoroll_free" );
776 if ( x->x_peaches )
777 {
778 freebytes( x->x_peaches, x->x_nbsteps*sizeof(t_float) );
779 }
780 if ( x->x_ipeaches )
781 {
782 freebytes( x->x_ipeaches, x->x_nbsteps*sizeof(t_int) );
783 }
784 if ( x->x_volumes )
785 {
786 freebytes( x->x_volumes, x->x_nbsteps*sizeof(t_float) );
787 }
788 if ( x->x_ivolumes )
789 {
790 freebytes( x->x_ivolumes, x->x_nbsteps*sizeof(t_int) );
791 }
792 }
793
794 void pianoroll_setup(void)
795 {
•796 logpost(NULL, 4, "%s", pianoroll_version );
797 pianoroll_class = class_new(gensym("pianoroll"), (t_newmethod)pianoroll_new,
798 (t_method)pianoroll_free, sizeof(t_pianoroll), 0, A_GIMME, 0);
799 class_addmethod(pianoroll_class, (t_method)pianoroll_float, &s_float, A_FLOAT, 0);
800 class_addmethod(pianoroll_class, (t_method)pianoroll_transpose, gensym("transpose"),
801 A_FLOAT, 0);
802 class_addmethod(pianoroll_class, (t_method)pianoroll_init, gensym("init"), 0);
803 class_addmethod(pianoroll_class, (t_method)pianoroll_dialog, gensym("dialog"), A_GIMME, 0);
804 class_addmethod(pianoroll_class, (t_method)pianoroll_save_file, gensym("save"), A_SYMBOL, 0);
805 class_addmethod(pianoroll_class, (t_method)pianoroll_load, gensym("load"), A_SYMBOL, 0);
806 class_addmethod(pianoroll_class, (t_method)pianoroll_pitch, gensym("pitch"), A_FLOAT, A_FLOAT, 0);
807 class_addmethod(pianoroll_class, (t_method)pianoroll_volume, gensym("volume"), A_FLOAT, A_FLOAT, 0);
808
809
810 pianoroll_widgetbehavior.w_getrectfn = pianoroll_getrect;
811 pianoroll_widgetbehavior.w_displacefn = pianoroll_displace;
812 pianoroll_widgetbehavior.w_selectfn = pianoroll_select;
813 pianoroll_widgetbehavior.w_activatefn = NULL;
814 pianoroll_widgetbehavior.w_deletefn = pianoroll_delete;
815 pianoroll_widgetbehavior.w_visfn = pianoroll_vis;
816 pianoroll_widgetbehavior.w_clickfn = pianoroll_click;
817
818 #if PD_MINOR_VERSION >= 37
819 class_setpropertiesfn(pianoroll_class, pianoroll_properties);
820 class_setsavefn(pianoroll_class, pianoroll_save);
821 #else
822 pianoroll_widgetbehavior.w_propertiesfn = pianoroll_properties;
823 pianoroll_widgetbehavior.w_savefn = pianoroll_save;
824 #endif
825
826 class_setwidget(pianoroll_class, &pianoroll_widgetbehavior);
827
828 sys_vgui("eval [read [open {%s/%s.tcl}]]\n",
829 pianoroll_class->c_externdir->s_name,
830 pianoroll_class->c_name->s_name);
831 }