Debian Codegraph @

cooled~.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /*------------------------ cooled~ -------------------------------------------- */
2 /* */
3 /* cooled~ : display sound, play parts and modify it with cut and paste */
4 /* constructor : cooled~ | cooled~ <size> <width> <height> */
5 /* */
6 /* Copyleft Yves Degoyon ( [email protected] ) */
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 /* "I am the fly in the ointment" -- Wire + 154 */
27 /* Komet -- Instrumentals */
28 /* ---------------------------------------------------------------------------- */
29
30
31
32 #include <sys/types.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <fcntl.h>
38 #include <sys/stat.h>
39 #include <ctype.h>
40 #include <pthread.h>
41 #include <math.h>
42
43 #ifdef __APPLE__
44 # include <sys/malloc.h>
45 #else
46 # include <malloc.h>
47 #endif
48
49 #ifdef _WIN32
50 # define M_PI 3.14159265358979323846
51 # include <windows.h>
52 #endif
53
54 #ifndef _MSC_VER
55 # include <unistd.h>
56 #endif
57
58 #include "m_pd.h"
59 #include "m_imp.h"
60 #include "g_canvas.h"
61
62
63 static int guidebug=0;
64
65 #define SYS_VGUI2(a,b) if (guidebug) \
66 post(a,b);\
67 sys_vgui(a,b)
68
69 #define SYS_VGUI3(a,b,c) if (guidebug) \
70 post(a,b,c);\
71 sys_vgui(a,b,c)
72
73 #define SYS_VGUI4(a,b,c,d) if (guidebug) \
74 post(a,b,c,d);\
75 sys_vgui(a,b,c,d)
76
77 #define SYS_VGUI5(a,b,c,d,e) if (guidebug) \
78 post(a,b,c,d,e);\
79 sys_vgui(a,b,c,d,e)
80
81 #define SYS_VGUI6(a,b,c,d,e,f) if (guidebug) \
82 post(a,b,c,d,e,f);\
83 sys_vgui(a,b,c,d,e,f)
84
85 #define SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \
86 post(a,b,c,d,e,f,g );\
87 sys_vgui(a,b,c,d,e,f,g)
88
89 #define SYS_VGUI8(a,b,c,d,e,f,g,h) if (guidebug) \
90 post(a,b,c,d,e,f,g,h );\
91 sys_vgui(a,b,c,d,e,f,g,h)
92
93 #define SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \
94 post(a,b,c,d,e,f,g,h,i );\
95 sys_vgui(a,b,c,d,e,f,g,h,i)
96
97 #define SYS_VGUI10(a,b,c,d,e,f,g,h,i,j) if (guidebug) \
98 post(a,b,c,d,e,f,g,h,i,j );\
99 sys_vgui(a,b,c,d,e,f,g,h,i,j)
100
101 #define SYS_VGUI11(a,b,c,d,e,f,g,h,i,j,k) if (guidebug) \
102 post(a,b,c,d,e,f,g,h,i,j,k );\
103 sys_vgui(a,b,c,d,e,f,g,h,i,j,k)
104
105 #define THREAD_SLEEP_TIME 10000 // 10000 us = 10 ms
106 #define COOLED_BGCOLOR "#000000"
107 #define COOLED_FGCOLOR "#34E112"
108 #define COOLED_FRCOLOR "#FFFFFF"
109
110 #define COOLED_DEFAULT_SIZE 1024
111 #define COOLED_DEFAULT_WIDTH 400
112 #define COOLED_DEFAULT_HEIGHT 200
113
114 static char *cooled_version = "cooled~: version 0.13, written by Yves Degoyon ([email protected])";
115
116 static t_class *cooled_class;
117 t_widgetbehavior cooled_widgetbehavior;
118
119 typedef struct _cooled
120 {
121 t_object x_obj;
122
123 int x_size; /* size of the stored sound */
124 t_float x_readpos; /* data's playing position */
125 int x_writepos; /* data's recording position */
126 t_float x_readstart; /* data's starting position for reading */
127 t_float x_readend; /* data's ending position for reading */
128 int x_play; /* playing on/off flag */
129 t_float x_readspeed; /* speed increment */
130 t_float x_record; /* flag to start recording process */
131 t_float x_allocate; /* flag to indicate pending allocation */
132 t_float x_empty; /* flag to indicate it's a brand new sound */
133 t_float *x_rdata; /* table containing right channel samples */
134 t_float *x_ldata; /* table containing leftt channel samples */
135 t_outlet *x_end; /* outlet for end of restitution */
136 t_outlet *x_recend; /* outlet for end of recording */
137 t_outlet *x_sampstart; /* outlet for sample number [ start ] when selecting */
138 t_outlet *x_sampend; /* outlet for sample number [ end ] when selecting */
139 t_float *x_rsemp; /* temporary sample buffer ( right ) */
140 t_float *x_lsemp; /* temporary sample buffer ( left ) */
141 char* x_gifdata; /* buffer to store graphic data */
142 char* x_guicommand; /* buffer to store gui command */
143 int x_draw; /* drawing option */
144
145 /* graphical data block */
146 int x_width; /* graphical width */
147 int x_height; /* graphical height */
148 int x_selected; /* flag to remember if we are seleted or not */
149 int x_erase; /* flag used when an erase is needed */
150 int x_redraw; /* flag used when drawing is needed */
151 t_glist *x_glist; /* keep graphic context for various operations */
152 int x_zoom; /* zoom factor */
153 pthread_t x_updatechild; /* thread id for the update child */
154 int x_updatestart; /* starting position for update */
155 int x_updateend; /* ending position for update */
156 int x_xpos; /* stuck x position */
157 int x_ypos; /* stuck y position */
158 int x_shifted; /* remember shift state from last click */
159 int x_alted; /* remember alt state from last click */
160 int x_xdraw; /* x drawing position */
161 int x_edraw; /* end of drawing */
162
163 t_float x_f; /* float needed for signal input */
164
165 } t_cooled;
166
167 /* ------------------------ drawing functions ---------------------------- */
168
169 static void cooled_update_block(t_cooled *x, t_glist *glist, int bnumber)
170 {
171 int hi, i=0;
172 t_float fspectrum=0.0;
173 int phase=0;
174 char color[8];
175
176 memset( x->x_gifdata, 0x0, x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
177
178 // update cooled~
179 for ( hi=x->x_height-1; hi>=0; hi-- )
180 {
181 if ( ( hi == x->x_height/4) || ( hi == 3*x->x_height/4) )
182 {
183 sprintf( color, "%s ", COOLED_FGCOLOR );
184 }
185 else if ( hi == x->x_height/2)
186 {
187 sprintf( color, "%s ", COOLED_FRCOLOR );
188 }
189 else
190 {
191 sprintf( color, "%s ", COOLED_BGCOLOR );
192 }
193 for ( i=0; i<x->x_zoom; i++ )
194 {
195 strncpy( x->x_gifdata+(hi*x->x_zoom+i)*8, color, 8 );
196 }
197 }
198
199 // set all points
200 {
201 int fsamp = ( bnumber * x->x_size ) / x->x_width;
202 int lsamp = ( ( bnumber+1) * x->x_size ) / x->x_width;
203 int si;
204
205 // post ( "cooled~ : updating samples [%d,%d]", fsamp, lsamp );
206
207 for ( si=fsamp; si<lsamp; si++ )
208 {
209 // calculate right channel index
210 {
211 int rind = 3*x->x_height/4 + ( *(x->x_rdata+si) * (x->x_height/4) ) - 1;
212
213 if ( rind > x->x_height - 1 ) rind = x->x_height - 1;
214 if ( rind < x->x_height/2 ) rind = x->x_height/2;
215
216 sprintf( color, "%s ", COOLED_FGCOLOR );
217 for ( i=0; i<x->x_zoom; i++ )
218 {
219 strncpy( x->x_gifdata+(rind*x->x_zoom+i)*8, color, 8 );
220 }
221 }
222
223 // calculate left channel index
224 {
225 int lind = x->x_height/4 + ( *(x->x_ldata+si) * (x->x_height/4) ) - 1;
226
227 if ( lind > x->x_height/2 - 1 ) lind = x->x_height/2 - 1;
228 if ( lind < 0 ) lind = 0;
229
230 sprintf( color, "%s ", COOLED_FGCOLOR );
231 for ( i=0; i<x->x_zoom; i++ )
232 {
233 strncpy( x->x_gifdata+(lind*x->x_zoom+i)*8, color, 8 );
234 }
235 }
236 }
237 }
238
239 for ( i=0; i<x->x_zoom; i++ )
240 {
241 sprintf( x->x_guicommand, "COOLEDIMAGE%x put {%s} -to %d 0\n", x, x->x_gifdata, (bnumber*x->x_zoom)+i );
242 if ( glist_isvisible( x->x_glist ) )
243 sys_gui( x->x_guicommand );
244 }
245
246 }
247
248 static void cooled_erase_block(t_cooled *x, t_glist *glist, int sample )
249 {
250 t_canvas *canvas=glist_getcanvas(glist);
251 int hi;
252 t_float fspectrum=0.0;
253 char fillColor[ 16 ];
254
255 for ( hi=0; hi<x->x_height; hi++)
256 {
257 {
258 int i;
259
260 for ( i=0; i<x->x_zoom; i++ )
261 {
262 strcpy( x->x_gifdata+i*sizeof("#FFFFFF "), strcat( COOLED_BGCOLOR, " ") );
263 }
264 if ( glist_isvisible( x->x_glist ) )
265 SYS_VGUI5("COOLEDIMAGE%x put {%s} -to %d %d\n", x, x->x_gifdata,
266 sample*x->x_zoom, (x->x_height-hi)*x->x_zoom );
267 }
268 }
269 }
270
271 static void *cooled_do_update_part(void *tdata)
272 {
273 t_cooled *x = (t_cooled*) tdata;
274 t_canvas *canvas=glist_getcanvas(x->x_glist);
275 int si;
276 int nbpoints = 0;
277 t_float percentage = 0, opercentage = 0;
278
279 // loose synchro
280 usleep( THREAD_SLEEP_TIME );
281
282 // check bounds
283 if ( x->x_updateend > x->x_size-1 ) x->x_updateend = x->x_size-1;
284 if ( x->x_updatestart < 0 ) x->x_updatestart = 0;
285
286 post("cooled~ : ok, let's go [updating %d, %d]", x->x_updatestart, x->x_updateend );
287
288 if ( x->x_erase )
289 {
290 for ( si=x->x_updatestart; si<=x->x_updateend; si++ )
291 {
292 cooled_erase_block(x, x->x_glist, si);
293 nbpoints++;
294 percentage = (nbpoints*100/(x->x_updateend-x->x_updatestart+1));
295 if ( (percentage == (int) percentage) && ((int)percentage%5 == 0) && ( percentage != opercentage ) )
296 {
297 // post( "cooled~ : erase part : %d %% completed", (int)percentage );
298 opercentage = percentage;
299 }
300 }
301 }
302
303 percentage = opercentage = nbpoints = 0;
304
305 if ( x->x_redraw )
306 {
307 for ( si=x->x_updatestart; si<=x->x_updateend; si++ )
308 {
309 cooled_update_block(x, x->x_glist, si);
310 nbpoints++;
311 percentage = (nbpoints*100/(x->x_updateend-x->x_updatestart+1));
312 if ( (percentage == (int) percentage) && ((int)percentage%5 == 0) && ( percentage != opercentage ) )
313 {
314 // post( "cooled~ : update part : %d %% completed", (int)percentage );
315 opercentage = percentage;
316 }
317 }
318 }
319
320 if ( glist_isvisible( x->x_glist ) )
321 {
322 SYS_VGUI3( ".x%lx.c delete rectangle %xCLIPZONE\n",
323 canvas, x);
324 if ( ( x->x_readstart != 0 ) || ( x->x_readend != 100 ) )
325 {
326 SYS_VGUI7( ".x%lx.c create rectangle %d %d %d %d -outline #FF0000 -tags %xCLIPZONE -width 2\n",
327 canvas, x->x_xpos+(int)(x->x_readstart*(x->x_width)/100 ),
328 x->x_ypos, x->x_xpos+(int)(x->x_readend*(x->x_width)/100 ),
329 x->x_ypos+x->x_height*x->x_zoom, x );
330 }
331 // set borders in black
332 SYS_VGUI3(".x%lx.c itemconfigure %xCOOLED -outline #000000\n", canvas, x);
333 }
334
335 #ifndef _WIN32
336 post("cooled~ : child thread %d ended", (int)x->x_updatechild );
•337 x->x_updatechild = 0;
338 #endif
339 return NULL;
340 }
341
342 static void cooled_update_part(t_cooled *x, t_glist *glist, int bstart, int bend,
343 int erase, int redraw, int keepframe)
344 {
345 pthread_attr_t update_child_attr;
346 t_canvas *canvas=glist_getcanvas(x->x_glist);
347
348 #ifndef _WIN32
349 if ( x->x_updatechild != 0 )
350 {
351 // post( "cooled~ : error : no update is possible for now" );
352 return;
353 }
354 #endif
355 x->x_updatestart = bstart;
356 x->x_updateend = bend;
357 if ( !keepframe )
358 {
359 x->x_erase = 0;
360 }
361 else
362 {
363 x->x_erase = erase;
364 }
365 x->x_redraw = redraw;
366 // recreate the square if needed
367 if ( glist_isvisible( x->x_glist ) )
368 {
369 if ( ( bstart == 0 ) && ( bend == x->x_width-1 ) && !keepframe )
370 {
371 SYS_VGUI3(".x%lx.c delete %xCOOLEDL\n", canvas, x );
372 SYS_VGUI3(".x%lx.c delete %xCOOLEDR\n", canvas, x );
373 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xCOOLEDR\n",
374 canvas, x->x_xpos, x->x_ypos,
375 x->x_xpos + x->x_width*x->x_zoom,
376 x->x_ypos + x->x_height/2*x->x_zoom,
377 x);
378 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xCOOLEDL\n",
379 canvas, x->x_xpos,
380 x->x_ypos + x->x_height/2*x->x_zoom,
381 x->x_xpos + x->x_width*x->x_zoom,
382 x->x_ypos + x->x_height*x->x_zoom,
383 x);
384 SYS_VGUI2("image delete COOLEDIMAGE%x\n", x );
385 SYS_VGUI3(".x%lx.c delete ICOOLEDIMAGE%x\n", canvas, x );
386 SYS_VGUI4("image create photo COOLEDIMAGE%x -format gif -width %d -height %d\n",
387 x, x->x_width*x->x_zoom, x->x_height*x->x_zoom );
388 SYS_VGUI2("COOLEDIMAGE%x blank\n", x );
389 SYS_VGUI6(".x%lx.c create image %d %d -image COOLEDIMAGE%x -tags ICOOLEDIMAGE%x\n",
390 canvas,
391 x->x_xpos+(x->x_width*x->x_zoom)/2,
392 x->x_ypos+(x->x_height*x->x_zoom)/2, x, x );
393 canvas_fixlinesfor( canvas, (t_text*)x );
394 }
395 // set borders in red
396 SYS_VGUI3(".x%lx.c itemconfigure %xCOOLED -outline #FF0000\n", canvas, x);
397 }
398
399 // launch update thread
400 if ( pthread_attr_init( &update_child_attr ) < 0 )
401 {
402 post( "cooled~ : could not launch update thread" );
403 perror( "pthread_attr_init" );
404 return;
405 }
406 if ( pthread_attr_setdetachstate( &update_child_attr, PTHREAD_CREATE_DETACHED ) < 0 )
407 {
408 post( "cooled~ : could not launch update thread" );
409 perror( "pthread_attr_setdetachstate" );
410 return;
411 }
412 if ( pthread_create( &x->x_updatechild, &update_child_attr, cooled_do_update_part, x ) < 0 )
413 {
414 post( "cooled~ : could not launch update thread" );
415 perror( "pthread_create" );
416 return;
417 }
418 else
419 {
420 // post( "cooled~ : drawing thread %d launched", (int)x->x_updatechild );
421 }
422 }
423
424 static void cooled_draw_new(t_cooled *x, t_glist *glist)
425 {
426 t_canvas *canvas=glist_getcanvas(glist);
427
428 x->x_xpos=text_xpix(&x->x_obj, glist);
429 x->x_ypos=text_ypix(&x->x_obj, glist);
430 x->x_xdraw=text_xpix(&x->x_obj, glist);
431 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xCOOLEDR\n",
432 canvas, x->x_xpos, x->x_ypos,
433 x->x_xpos + x->x_width*x->x_zoom,
434 x->x_ypos + x->x_height/2*x->x_zoom,
435 x);
436 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xCOOLEDL\n",
437 canvas, x->x_xpos,
438 x->x_ypos + x->x_height/2*x->x_zoom,
439 x->x_xpos + x->x_width*x->x_zoom,
440 x->x_ypos + x->x_height*x->x_zoom,
441 x);
442 SYS_VGUI4("image create photo COOLEDIMAGE%x -format gif -width %d -height %d\n",
443 x, x->x_width*x->x_zoom, x->x_height*x->x_zoom );
444 SYS_VGUI2("COOLEDIMAGE%x blank\n", x );
445 SYS_VGUI6(".x%lx.c create image %d %d -image COOLEDIMAGE%x -tags ICOOLEDIMAGE%x\n",
446 canvas,
447 x->x_xpos+(x->x_width*x->x_zoom)/2,
448 x->x_ypos+(x->x_height*x->x_zoom)/2, x, x );
449 if ( x->x_draw) cooled_update_part(x, x->x_glist, 0, x->x_width-1, 0, 1, 0);
450 canvas_fixlinesfor( canvas, (t_text*)x );
451 }
452
453 static void cooled_draw_delete(t_cooled *x, t_glist *glist)
454 {
455 t_canvas *canvas=glist_getcanvas(glist);
456
457 if ( glist_isvisible( glist ) )
458 {
459 SYS_VGUI3( ".x%lx.c delete %xCAPTURE\n", canvas, x );
460 if ( ( x->x_readstart != 0 ) || ( x->x_readend != 100 ) )
461 {
462 SYS_VGUI3( ".x%lx.c delete rectangle %xCLIPZONE\n", canvas, x);
463 }
464 SYS_VGUI3( ".x%lx.c delete line %xINSERTHERE\n", canvas, x);
465 SYS_VGUI3(".x%lx.c delete %xCOOLEDR\n", canvas, x );
466 SYS_VGUI3(".x%lx.c delete %xCOOLEDL\n", canvas, x );
467 SYS_VGUI3(".x%lx.c delete ICOOLEDIMAGE%x\n", canvas, x );
468 SYS_VGUI2("image delete COOLEDIMAGE%x\n", x );
469 }
470 }
471
472 static void cooled_draw_move(t_cooled *x, t_glist *glist)
473 {
474 t_canvas *canvas=glist_getcanvas(glist);
475
476 if ( glist_isvisible( x->x_glist ) )
477 {
478 SYS_VGUI7(".x%lx.c coords %xCOOLEDR %d %d %d %d\n",
479 canvas, x,
480 x->x_xpos, x->x_ypos,
481 x->x_xpos + x->x_width*x->x_zoom,
482 x->x_ypos + x->x_height/2*x->x_zoom);
483 SYS_VGUI7(".x%lx.c coords %xCOOLEDL %d %d %d %d\n",
484 canvas, x,
485 x->x_xpos,
486 x->x_ypos + x->x_height/2*x->x_zoom,
487 x->x_xpos + x->x_width*x->x_zoom,
488 x->x_ypos + x->x_height*x->x_zoom );
489 if ( ( x->x_readstart != 0 ) || ( x->x_readend != 100 ) )
490 {
491 SYS_VGUI7(".x%lx.c coords %xCLIPZONE %d %d %d %d\n",
492 canvas, x,
493 x->x_xpos+(int)(x->x_readstart*(x->x_width)/100*x->x_zoom ),
494 x->x_ypos,
495 x->x_xpos+(int)(x->x_readend*(x->x_width)/100*x->x_zoom ),
496 x->x_ypos+x->x_height*x->x_zoom );
497 }
498 SYS_VGUI7(".x%lx.c coords %xINSERTHERE %d %d %d %d\n",
499 canvas, x,
500 x->x_xdraw,
501 x->x_ypos,
502 x->x_xdraw,
503 x->x_ypos+x->x_height*x->x_zoom );
504 SYS_VGUI5(".x%lx.c coords ICOOLEDIMAGE%x %d %d\n",
505 canvas, x,
506 x->x_xpos+((x->x_width*x->x_zoom)/2),
507 (x->x_ypos+((x->x_height*x->x_zoom)/2)) );
508 canvas_fixlinesfor( canvas, (t_text*)x );
509 }
510 }
511
512 static void cooled_draw_select(t_cooled* x,t_glist* glist)
513 {
514 t_canvas *canvas=glist_getcanvas(glist);
515
516 if ( glist_isvisible( x->x_glist ) )
517 {
518 if(x->x_selected)
519 {
520 /* sets the item in blue */
521 SYS_VGUI3(".x%lx.c itemconfigure %xCOOLEDR -outline #0000FF\n", canvas, x);
522 SYS_VGUI3(".x%lx.c itemconfigure %xCOOLEDL -outline #0000FF\n", canvas, x);
523 }
524 else
525 {
526 SYS_VGUI3(".x%lx.c itemconfigure %xCOOLEDR -outline #000000\n", canvas, x);
527 SYS_VGUI3(".x%lx.c itemconfigure %xCOOLEDL -outline #000000\n", canvas, x);
528 }
529 }
530 }
531
532 /* ------------------------ widget callbacks ----------------------------- */
533
534
535 /* setting the starting point for reading ( in percent ) */
536 static void cooled_readstart(t_cooled *x, t_floatarg fstart)
537 {
538 t_float startpoint = fstart;
539 t_canvas *canvas=glist_getcanvas(x->x_glist);
540
541 if (startpoint < 0) startpoint = 0;
542 if (startpoint > 100) startpoint = 100;
543 x->x_readstart = startpoint;
544 // set readspeed sign
545 if ( ( x->x_readstart > x->x_readend ) && ( x->x_readspeed > 0 ) ) x->x_readspeed = -x->x_readspeed;
546 if ( ( x->x_readstart < x->x_readend ) && ( x->x_readspeed < 0 ) ) x->x_readspeed = -x->x_readspeed;
547 if ( glist_isvisible( x->x_glist ) )
548 {
549 SYS_VGUI3( ".x%lx.c delete rectangle %xCLIPZONE\n",
550 canvas, x);
551 if ( ( x->x_readstart != 0 ) || ( x->x_readend != 100 ) )
552 {
553 SYS_VGUI7( ".x%lx.c create rectangle %d %d %d %d -outline #FF0000 -tags %xCLIPZONE -width 2\n",
554 canvas, x->x_xpos+(int)(x->x_readstart*x->x_width*x->x_zoom/100 ),
555 x->x_ypos, x->x_xpos+(int)(x->x_readend*x->x_width*x->x_zoom/100 ),
556 x->x_ypos+x->x_height*x->x_zoom, x );
557 }
558 }
559 outlet_float( x->x_sampstart, (x->x_readstart*x->x_size)/100 );
560 }
561
562 /* setting the ending point for reading ( in percent ) */
563 static void cooled_readend(t_cooled *x, t_floatarg fend)
564 {
565 t_float endpoint = fend;
566 t_canvas *canvas=glist_getcanvas(x->x_glist);
567
568 if (endpoint < 0) endpoint = 0;
569 if (endpoint > 100) endpoint = 100;
570 x->x_readend = endpoint;
571 // set readspeed sign
572 if ( ( x->x_readstart > x->x_readend ) && ( x->x_readspeed > 0 ) ) x->x_readspeed = -x->x_readspeed;
573 if ( ( x->x_readstart < x->x_readend ) && ( x->x_readspeed < 0 ) ) x->x_readspeed = -x->x_readspeed;
574 if ( glist_isvisible( x->x_glist ) )
575 {
576 SYS_VGUI3( ".x%lx.c delete rectangle %xCLIPZONE\n",
577 canvas, x);
578 if ( ( x->x_readstart != 0 ) || ( x->x_readend != 100 ) )
579 {
580 SYS_VGUI7( ".x%lx.c create rectangle %d %d %d %d -outline #FF0000 -tags %xCLIPZONE -width 2\n",
581 canvas,
582 x->x_xpos+(int)(x->x_readstart*x->x_width*x->x_zoom/100 ),
583 x->x_ypos, x->x_xpos+(int)(x->x_readend*x->x_width*x->x_zoom/100 ),
584 x->x_ypos+x->x_height*x->x_zoom, x );
585 }
586 }
587 outlet_float( x->x_sampend, (x->x_readend*x->x_size)/100 );
588 }
589
590 static void cooled_getrect(t_gobj *z, t_glist *owner,
591 int *xp1, int *yp1, int *xp2, int *yp2)
592 {
593 t_cooled* x = (t_cooled*)z;
594
595 *xp1 = x->x_xpos;
596 *yp1 = x->x_ypos;
597 *xp2 = x->x_xpos+x->x_width*x->x_zoom;
598 *yp2 = x->x_ypos+x->x_height*x->x_zoom+1;
599 }
600
601 static void cooled_save(t_gobj *z, t_binbuf *b)
602 {
603 t_cooled *x = (t_cooled *)z;
604
605 binbuf_addv(b, "ssiisiiii", gensym("#X"),gensym("obj"),
606 (int)x->x_obj.te_xpix, (int)x->x_obj.te_ypix,
607 atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
608 x->x_size, x->x_width, x->x_height, x->x_draw );
609 binbuf_addv(b, ";");
610 }
611
612 static void cooled_select(t_gobj *z, t_glist *glist, int selected)
613 {
614 t_cooled *x = (t_cooled *)z;
615
616 x->x_selected = selected;
617 cooled_draw_select( x, glist );
618 }
619
620 static void cooled_vis(t_gobj *z, t_glist *glist, int vis)
621 {
622 t_cooled *x = (t_cooled *)z;
623 t_rtext *y;
624
625 if (vis)
626 {
627 cooled_draw_new( x, glist );
628 }
629 else
630 {
631 // erase all points
632 cooled_draw_delete( x, glist );
633 }
634 }
635
636 static void cooled_delete(t_gobj *z, t_glist *glist)
637 {
638 canvas_deletelinesfor(glist, (t_text *)z);
639 }
640
641 static void cooled_displace(t_gobj *z, t_glist *glist, int dx, int dy)
642 {
643 t_cooled *x = (t_cooled *)z;
644 int xold = x->x_xpos;
645 int yold = x->x_ypos;
646
647 x->x_xpos += dx;
648 x->x_ypos += dy;
649
650 if ( ( x->x_xpos != xold ) || ( x->x_ypos != yold ) )
651 {
652 cooled_draw_move( x, glist );
653 }
654
655 }
656
657 static void cooled_motion(t_cooled *x, t_floatarg dx, t_floatarg dy)
658 {
659 // post( "cooled_motion @ [%d,%d] dx=%f dy=%f alt=%d", x->x_xdraw, x->x_ydraw, dx, dy, x->x_alted );
660 if ( dx != 0 )
661 {
662 cooled_readstart( x, ((t_float)( x->x_xdraw - x->x_xpos ) * 100 )/ (t_float)( x->x_width * x->x_zoom ) );
663 x->x_edraw += dx;
664 cooled_readend( x, ((t_float)( x->x_edraw - x->x_xpos ) * 100 )/ (t_float)( x->x_width * x->x_zoom ) );
665 }
666 }
667
668 /* erase data form readstart to readend */
669 static void cooled_erase( t_cooled *x )
670 {
671 int startsamp, endsamp, si;
672 int lreadstart = x->x_readstart, lreadend = x->x_readend;
673
674 if (x->x_allocate)
675 {
676 post( "cooled~ : error : cannot erase while re-allocation" );
677 return;
678 }
679 // post( "cooled~ : erase" );
680 if ( lreadstart <= lreadend )
681 {
682 startsamp = (lreadstart*x->x_size)/100;
683 endsamp = (lreadend*x->x_size)/100;
684 }
685 else
686 {
687 startsamp = (lreadend*x->x_size)/100;
688 endsamp = (lreadstart*x->x_size)/100;
689 }
690
691 for ( si=startsamp; si<=endsamp; si++ )
692 {
693 *(x->x_rdata+si) = 0.;
694 *(x->x_ldata+si) = 0.;
695 }
696 if ( x->x_draw ) cooled_update_part(x, x->x_glist, startsamp*x->x_width/x->x_size, endsamp*(x->x_width-1)/x->x_size, 0, 1, 1);
697 }
698 /* paste data form readstart to readend */
699 static void cooled_paste( t_cooled *x )
700 {
701 int startsamp, endsamp, si, inssamp, hlimit, csize;
702 int lreadstart = x->x_readstart, lreadend = x->x_readend;
703
704 if (x->x_allocate)
705 {
706 post( "cooled~ : error : cannot paste while re-allocation" );
707 return;
708 }
709 if ( lreadstart <= lreadend )
710 {
711 startsamp = (lreadstart*x->x_size)/100;
712 endsamp = (lreadend*x->x_size)/100;
713 }
714 else
715 {
716 startsamp = (lreadend*x->x_size)/100;
717 endsamp = (lreadstart*x->x_size)/100;
718 }
719
720 // insert data at insertion point
721 inssamp = ( x->x_xdraw - x->x_xpos) * x->x_size / ( x->x_width * x->x_zoom );
722 // post( "cooled~ : replace [%d,%d] to %d", startsamp, endsamp, inssamp );
723 csize=endsamp-startsamp;
724 for ( si=0; si<=csize; si++ )
725 {
726 if ( ( si + inssamp >= x->x_size ) || ( startsamp + si >= x->x_size ) ) break;
727 *(x->x_rsemp+si) = *(x->x_rdata+startsamp+si);
728 *(x->x_lsemp+si) = *(x->x_ldata+startsamp+si);
729 }
730 hlimit = si;
731 for ( si=0 ; si<=hlimit; si++ )
732 {
733 *(x->x_rdata+inssamp+si) += *(x->x_rsemp+si);
734 *(x->x_ldata+inssamp+si) += *(x->x_lsemp+si);
735 }
736
737 // post( "cooled~ : updating [%d,%d]", inssamp*x->x_width/x->x_size, (inssamp+hlimit)*x->x_width/x->x_size );
738 if ( x->x_draw ) cooled_update_part(x, x->x_glist, inssamp*x->x_width/x->x_size, (inssamp+hlimit)*(x->x_width-1)/x->x_size, 0, 1, 1);
739 }
740 /* replace data form readstart to readend */
741 static void cooled_replace( t_cooled *x )
742 {
743 int startsamp, endsamp, si, inssamp, hlimit, csize;
744 int lreadstart = x->x_readstart, lreadend = x->x_readend;
745
746 if (x->x_allocate)
747 {
748 post( "cooled~ : error : cannot replace while re-allocation" );
749 return;
750 }
751 if ( lreadstart <= lreadend )
752 {
753 startsamp = (lreadstart*x->x_size)/100;
754 endsamp = (lreadend*x->x_size)/100;
755 }
756 else
757 {
758 startsamp = (lreadend*x->x_size)/100;
759 endsamp = (lreadstart*x->x_size)/100;
760 }
761
762 // insert data at insertion point
763 inssamp = ( x->x_xdraw - x->x_xpos) * x->x_size / ( x->x_width * x->x_zoom );
764 // post( "cooled~ : replace [%d,%d] to %d", startsamp, endsamp, inssamp );
765 csize=endsamp-startsamp;
766 for ( si=0; si<=csize; si++ )
767 {
768 if ( ( si + inssamp >= x->x_size ) || ( startsamp + si >= x->x_size ) ) break;
769 *(x->x_rsemp+si) = *(x->x_rdata+startsamp+si);
770 *(x->x_lsemp+si) = *(x->x_ldata+startsamp+si);
771 }
772 hlimit = si;
773 for ( si=0 ; si<=hlimit; si++ )
774 {
775 *(x->x_rdata+inssamp+si) = *(x->x_rsemp+si);
776 *(x->x_ldata+inssamp+si) = *(x->x_lsemp+si);
777 }
778
779 // post( "cooled~ : updating [%d,%d]", inssamp*x->x_width/x->x_size, (inssamp+hlimit)*x->x_width/x->x_size );
780 if ( x->x_draw ) cooled_update_part(x, x->x_glist, inssamp*x->x_width/x->x_size, (inssamp+hlimit)*(x->x_width-1)/x->x_size, 0, 1, 1);
781
782 }
783
784 /* call editor's property dialog */
785 static void cooled_properties(t_gobj *z, t_glist *owner)
786 {
787 char buf[800];
788 t_cooled *x=(t_cooled *)z;
789
790 sprintf(buf, "pdtk_cooled_dialog %%s %d %d %d\n",
791 x->x_width, x->x_height, x->x_draw);
792 // post("cooled_properties : %s", buf );
793 gfxstub_new(&x->x_obj.ob_pd, x, buf);
794 }
795
796 /* handle properties change */
797 static void cooled_dialog(t_cooled *x, t_symbol *s, int argc, t_atom *argv)
798 {
799 if ( !x )
800 {
801 post( "cooled~ : error :tried to set properties on an unexisting object" );
802 }
803 if ( argc != 3 )
804 {
805 post( "cooled~ : error in the number of arguments ( %d instead of 3 )", argc );
806 return;
807 }
808 if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT || argv[2].a_type != A_FLOAT )
809 {
810 post( "cooled~ : wrong arguments" );
811 return;
812 }
813 cooled_draw_delete(x, x->x_glist);
814 if ( x->x_gifdata != NULL )
815 {
816 freebytes(x->x_gifdata, x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
817 post( "Freed %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
818 x->x_gifdata = NULL;
819 }
820 if ( x->x_guicommand != NULL )
821 {
822 freebytes(x->x_guicommand, 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
823 post( "Freed %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
824 x->x_guicommand = NULL;
825 }
826 x->x_width = (int)argv[0].a_w.w_float;
827 if ( x->x_width < 0 ) x->x_width = 100;
828 x->x_height = (int)argv[1].a_w.w_float;
829 if ( x->x_height < 0 ) x->x_height = 100;
830 x->x_draw = (int)argv[2].a_w.w_float;
831 if ( !(x->x_gifdata = ( char* ) getbytes( x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) )
832 {
833 post( "cooled~ : error : could not allocate buffers" );
834 return;
835 }
836 else
837 {
838 post( "cooled~ : allocated %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
839 }
840 if ( !(x->x_guicommand = ( char* ) getbytes( 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) )
841 {
842 post( "cooled~ : error : could not allocate buffers" );
843 return;
844 }
845 else
846 {
847 post( "cooled~ : allocated %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
848 }
849 cooled_draw_new(x, x->x_glist);
850 }
851
852 /* handle clicks */
853 static int cooled_click(t_gobj *z, struct _glist *glist,
854 int xpix, int ypix, int shift, int alt, int dbl, int doit)
855 {
856 t_cooled* x = (t_cooled *)z;
857 int pipos;
858 t_canvas *canvas=glist_getcanvas(x->x_glist);
859
860 // post( "cooled_click : x=%d y=%d doit=%d alt=%d, shift=%d", xpix, ypix, doit, alt, shift );
861 if ( doit )
862 {
863 if ( shift && alt )
864 {
865 cooled_paste(x);
866 }
867 else if ( shift )
868 {
869 cooled_replace(x);
870 }
871 else if ( alt )
872 {
873 cooled_erase(x);
874 }
875 else
876 {
877 x->x_xdraw = xpix;
878 x->x_edraw = xpix;
879 x->x_shifted = shift;
880 x->x_alted = alt;
881 // activate motion callback
882 glist_grab( glist, &x->x_obj.te_g, (t_glistmotionfn)cooled_motion,
883 0, xpix, ypix );
884
885 // draw insertion line
886 if ( glist_isvisible( x->x_glist ) )
887 {
888 SYS_VGUI3( ".x%lx.c delete line %xINSERTHERE\n",
889 canvas, x);
890 SYS_VGUI7( ".x%lx.c create line %d %d %d %d -fill #00FFFF -tags %xINSERTHERE -width 2\n",
891 canvas, x->x_xdraw,
892 x->x_ypos, x->x_xdraw,
893 x->x_ypos+x->x_height*x->x_zoom, x );
894 }
895
896 }
897
898 }
899 else
900 {
901 // nothing
902 }
903 return (1);
904
905 }
906
907 /* clean up */
908 static void cooled_free(t_cooled *x)
909 {
910 if ( x->x_rdata != NULL )
911 {
912 freebytes(x->x_rdata, x->x_size*sizeof(float) );
913 post( "Freed %d bytes", x->x_size*sizeof(float) );
914 x->x_rdata = NULL;
915 }
916 if ( x->x_ldata != NULL )
917 {
918 freebytes(x->x_ldata, x->x_size*sizeof(float) );
919 post( "Freed %d bytes", x->x_size*sizeof(float) );
920 x->x_ldata = NULL;
921 }
922 if ( x->x_rsemp != NULL )
923 {
924 freebytes(x->x_rsemp, x->x_size*sizeof(float) );
925 post( "Freed %d bytes", x->x_size*sizeof(float) );
926 x->x_rsemp = NULL;
927 }
928 if ( x->x_lsemp != NULL )
929 {
930 freebytes(x->x_lsemp, x->x_size*sizeof(float) );
931 post( "Freed %d bytes", x->x_size*sizeof(float) );
932 x->x_lsemp = NULL;
933 }
934 if ( x->x_gifdata != NULL )
935 {
936 freebytes(x->x_gifdata, x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
937 post( "Freed %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
938 x->x_gifdata = NULL;
939 }
940 if ( x->x_guicommand != NULL )
941 {
942 freebytes(x->x_guicommand, 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
943 post( "Freed %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
944 x->x_guicommand = NULL;
945 }
946 }
947
948 /* allocate tables for storing sound and temporary copy */
949 static int cooled_allocate(t_cooled *x)
950 {
951 int fi;
952
953 if ( !(x->x_rdata = getbytes( x->x_size*sizeof(float) ) ) )
954 {
955 post( "cooled~ : error : could not allocate buffers" );
956 return -1;
957 }
958 else
959 {
960 post( "cooled~ : allocated %d bytes", x->x_size*sizeof(float) );
961 }
962 if ( !(x->x_ldata = getbytes( x->x_size*sizeof(float) ) ) )
963 {
964 post( "cooled~ : error : could not allocate buffers" );
965 return -1;
966 }
967 else
968 {
969 post( "cooled~ : allocated %d bytes", x->x_size*sizeof(float) );
970 }
971 if ( !(x->x_rsemp = getbytes( x->x_size*sizeof(float) ) ) )
972 {
973 post( "cooled~ : error : could not allocate buffers" );
974 return -1;
975 }
976 else
977 {
978 post( "cooled~ : allocated %d bytes", x->x_size*sizeof(float) );
979 }
980 if ( !(x->x_lsemp = getbytes( x->x_size*sizeof(float) ) ) )
981 {
982 post( "cooled~ : error : could not allocate buffers" );
983 return -1;
984 }
985 else
986 {
987 post( "cooled~ : allocated %d bytes", x->x_size*sizeof(float) );
988 }
989 if ( !(x->x_gifdata = ( char* ) getbytes( x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) )
990 {
991 post( "cooled~ : error : could not allocate buffers" );
992 return -1;
993 }
994 else
995 {
996 post( "cooled~ : allocated %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
997 }
998 if ( !(x->x_guicommand = ( char* ) getbytes( 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) )
999 {
1000 post( "cooled~ : error : could not allocate buffers" );
1001 return -1;
1002 }
1003 else
1004 {
1005 post( "cooled~ : allocated %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
1006 }
1007
1008 return 0;
1009 }
1010
1011 /* allocate tables for storing sound and temporary copy */
1012 static int cooled_reallocate(t_cooled *x, int ioldsize, int inewsize)
1013 {
1014 int fi, csize;
1015 t_float *prdata=x->x_rdata, *pldata=x->x_ldata, *prsemp=x->x_rsemp, *plsemp=x->x_lsemp;
1016
1017 if ( !(x->x_rdata = getbytes( inewsize*sizeof(float) ) ) )
1018 {
1019 post( "cooled~ : error : could not allocate buffers" );
1020 return -1;
1021 }
1022 else
1023 {
1024 post( "cooled~ : allocated %d bytes", inewsize*sizeof(float) );
1025 }
1026 if ( !(x->x_ldata = getbytes( inewsize*sizeof(float) ) ) )
1027 {
1028 post( "cooled~ : error : could not allocate buffers" );
1029 return -1;
1030 }
1031 else
1032 {
1033 post( "cooled~ : allocated %d bytes", inewsize*sizeof(float) );
1034 }
1035 if ( !(x->x_rsemp = getbytes( inewsize*sizeof(float) ) ) )
1036 {
1037 post( "cooled~ : error : could not allocate buffers" );
1038 return -1;
1039 }
1040 else
1041 {
1042 post( "cooled~ : allocated %d bytes", inewsize*sizeof(float) );
1043 }
1044 if ( !(x->x_lsemp = getbytes( inewsize*sizeof(float) ) ) )
1045 {
1046 post( "cooled~ : error : could not allocate buffers" );
1047 return -1;
1048 }
1049 else
1050 {
1051 post( "cooled~ : allocated %d bytes", inewsize*sizeof(float) );
1052 }
1053
1054 if ( ioldsize <= inewsize )
1055 {
1056 csize = ioldsize;
1057 }
1058 else
1059 {
1060 csize = inewsize;
1061 }
1062 memcpy( x->x_rdata, prdata, csize*sizeof(float) );
1063 memcpy( x->x_ldata, pldata, csize*sizeof(float) );
1064 memcpy( x->x_rsemp, prsemp, csize*sizeof(float) );
1065 memcpy( x->x_lsemp, plsemp, csize*sizeof(float) );
1066
1067 if ( prdata != NULL )
1068 {
1069 freebytes(prdata, ioldsize*sizeof(float) );
1070 post( "Freed %d bytes", ioldsize*sizeof(float) );
1071 }
1072 if ( pldata != NULL )
1073 {
1074 freebytes(pldata, ioldsize*sizeof(float) );
1075 post( "Freed %d bytes", ioldsize*sizeof(float) );
1076 }
1077 if ( prsemp != NULL )
1078 {
1079 freebytes(prsemp, ioldsize*sizeof(float) );
1080 post( "Freed %d bytes", ioldsize*sizeof(float) );
1081 }
1082 if ( plsemp != NULL )
1083 {
1084 freebytes(plsemp, ioldsize*sizeof(float) );
1085 post( "Freed %d bytes", ioldsize*sizeof(float) );
1086 }
1087
1088 return 0;
1089 }
1090
1091 /* records and playback the sound */
1092 static t_int *cooled_perform(t_int *w)
1093 {
1094 t_float *lin = (t_float *)(w[1]);
1095 t_float *rin = (t_float *)(w[2]);
1096 t_float *rout = (t_float *)(w[3]);
1097 t_float *lout = (t_float *)(w[4]);
1098 int is;
1099 int n = (int)(w[5]); /* number of samples */
1100 int startsamp, endsamp;
1101 t_cooled *x = (t_cooled *)(w[6]);
1102 int lreadstart = x->x_readstart, lreadend = x->x_readend;
1103
1104 if ( lreadstart <= lreadend )
1105 {
1106 startsamp = (lreadstart*x->x_size)/100;
1107 endsamp = (lreadend*x->x_size)/100;
1108 }
1109 else
1110 {
1111 startsamp = (lreadend*x->x_size)/100;
1112 endsamp = (lreadstart*x->x_size)/100;
1113 }
1114
1115 while (n--)
1116 {
1117 // eventually records input
1118 if ( x->x_record )
1119 {
1120 *(x->x_ldata+x->x_writepos)=*(lin);
1121 *(x->x_rdata+x->x_writepos)=*(rin);
1122 x->x_writepos++;
1123 if ( x->x_writepos >= x->x_size )
1124 {
1125 x->x_record=0;
1126 x->x_writepos=0;
1127 if ( x->x_draw ) cooled_update_part(x, x->x_glist, 0, x->x_width-1, 0, 1, 0);
1128 outlet_bang(x->x_recend);
1129 if ( x->x_empty ) x->x_empty = 0;
1130 // post( "cooled~ : stopped recording" );
1131 }
1132 }
1133 // set outputs
1134 *rout = 0.0;
1135 *lout = 0.0;
1136 if ( x->x_play)
1137 {
1138 is=0;
1139 *lout = *(x->x_ldata+(int)x->x_readpos);
1140 *rout = *(x->x_rdata+(int)x->x_readpos);
1141 x->x_readpos+=x->x_readspeed;
1142 // post( "xreadpos : %f (added %f) %d", x->x_readpos, x->x_readspeed, x->x_readend );
1143 if ( ( x->x_readspeed >= 0 ) && ( x->x_readpos >= endsamp ) )
1144 {
1145 x->x_play=0;
1146 x->x_readpos=(float)(startsamp);
1147 // post( "cooled~ : stopped playing (readpos=%d)", x->x_readpos );
1148 outlet_bang(x->x_end);
1149 }
1150 if ( ( x->x_readspeed < 0 ) && ( x->x_readpos <= startsamp ) )
1151 {
1152 x->x_play=0;
1153 x->x_readpos = (float)(endsamp);
1154 // post( "cooled~ : stopped playing (readpos=%d)", x->x_readpos );
1155 outlet_bang(x->x_end);
1156 }
1157 }
1158 rout++;
1159 lout++;
1160 rin++;
1161 lin++;
1162 }
1163 // post( "cooled~ : read : %f:%d : write: %d:%d", x->x_readpos, x->x_play, x->x_writepos, x->x_record );
1164 return (w+7);
1165 }
1166
1167 static void cooled_dsp(t_cooled *x, t_signal **sp)
1168 {
1169 dsp_add(cooled_perform, 6, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, sp[0]->s_n, x);
1170 }
1171
1172 /* record the cooled */
1173 static void cooled_record(t_cooled *x)
1174 {
1175 if (x->x_allocate)
1176 {
1177 post( "cooled~ : error : cannot record while re-allocation" );
1178 return;
1179 }
1180 x->x_record=1;
1181 x->x_writepos=0;
1182 post( "cooled~ : recording on" );
1183 }
1184
1185 /* map to stereo */
1186 static void cooled_stereo(t_cooled *x)
1187 {
1188 int si;
1189
1190 if (x->x_allocate)
1191 {
1192 post( "cooled~ : error : cannot map to stereo while re-allocation" );
1193 return;
1194 }
1195 for ( si=0; si<x->x_size; si++ )
1196 {
1197 *(x->x_rdata+si) = *(x->x_ldata+si);
1198 }
1199 if ( x->x_draw ) cooled_update_part(x, x->x_glist, 0, x->x_width-1, !x->x_empty, !x->x_empty, 0);
1200 }
1201
1202 /* play the cooled */
1203 static void cooled_play(t_cooled *x)
1204 {
1205 x->x_play=1;
1206 // post( "cooled~ : playing on" );
1207 }
1208
1209 /* sets the reading speed */
1210 static void cooled_readspeed(t_cooled *x, t_floatarg freadspeed)
1211 {
1212 x->x_readspeed=freadspeed;
1213 }
1214
1215 /* resize cooled */
1216 static void cooled_resize(t_cooled *x, t_floatarg fnewsize )
1217 {
1218 if (fnewsize <= 0)
1219 {
1220 post( "cooled~ : error : wrong size" );
1221 return;
1222 }
1223 if (x->x_allocate)
1224 {
1225 post( "cooled~ : error : cannot resize while re-allocation" );
1226 return;
1227 }
1228 #ifndef _WIN32
1229 if (x->x_updatechild > 0)
1230 {
1231 post( "cooled~ : can't resize now, an update is pending." );
1232 return;
1233 }
1234 #endif
1235 post( "cooled~ : reallocating tables" );
1236 x->x_allocate = 1;
1237 x->x_play = 0;
1238 x->x_record = 0;
1239 cooled_readstart( x, 0);
1240 cooled_readend( x, 100);
1241 cooled_reallocate(x, x->x_size, fnewsize);
1242 x->x_size = fnewsize;
1243 x->x_empty = 1;
1244 if ( x->x_readstart <= x->x_readend )
1245 {
1246 x->x_readpos = (x->x_readstart*x->x_size)/100;
1247 }
1248 else
1249 {
1250 x->x_readpos = (x->x_readend*x->x_size)/100;
1251 }
1252 // erase all points, as data is zero no drawing is needed
1253 if ( x->x_draw) cooled_update_part(x, x->x_glist, 0, x->x_width-1, 0, !x->x_empty, 1);
1254 x->x_allocate = 0;
1255 }
1256
1257 /* set zoom factor */
1258 static void cooled_zoom(t_cooled *x, t_floatarg fzoom )
1259 {
1260 if (fzoom < 1)
1261 {
1262 post( "cooled~ : error : wrong zoom factor" );
1263 return;
1264 }
1265 if (x->x_allocate)
1266 {
1267 post( "cooled~ : error : cannot zoom while re-allocation" );
1268 return;
1269 }
1270 if ( x->x_gifdata != NULL )
1271 {
1272 freebytes(x->x_gifdata, x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
1273 post( "Freed %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
1274 x->x_gifdata = NULL;
1275 }
1276 if ( x->x_guicommand != NULL )
1277 {
1278 freebytes(x->x_guicommand, 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
1279 post( "Freed %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
1280 x->x_guicommand = NULL;
1281 }
1282 x->x_zoom = (int)fzoom;
1283 if ( !(x->x_gifdata = ( char* ) getbytes( x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) )
1284 {
1285 post( "cooled~ : error : could not allocate buffers" );
1286 return;
1287 }
1288 else
1289 {
1290 post( "cooled~ : allocated %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
1291 }
1292 if ( !(x->x_guicommand = ( char* ) getbytes( 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) )
1293 {
1294 post( "cooled~ : error : could not allocate buffers" );
1295 return;
1296 }
1297 else
1298 {
1299 post( "cooled~ : allocated %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
1300 }
1301 if ( x->x_draw) cooled_update_part(x, x->x_glist, 0, x->x_width-1, !x->x_empty, !x->x_empty, 0);
1302 canvas_fixlinesfor(x->x_glist, (t_text*)x );
1303 }
1304
1305 /* modify the loop positions */
1306 static void cooled_loop(t_cooled *x, t_symbol *soperator )
1307 {
1308 char *operator = soperator->s_name;
1309 int ci;
1310 t_float fvalue, freadstart = x->x_readstart, freadend = x->x_readend;
1311
1312 if ( (soperator->s_name[0] != '*') &&
1313 (soperator->s_name[0] != '/') &&
1314 (soperator->s_name[0] != '>') &&
1315 (soperator->s_name[0] != '<') )
1316 {
1317 post( "cooled~ : error : wrong operator argument : should be *|/|>|<" );
1318 return;
1319 }
1320 for ( ci=1; ci<(int)strlen( soperator->s_name ); ci++ )
1321 {
1322 if ( !(isdigit(soperator->s_name[ci])||(soperator->s_name[ci]=='.')) )
1323 {
1324 post( "cooled~ : error : wrong operation value : %s : should be in a float format", soperator->s_name+1 );
1325 return;
1326 }
1327 }
1328 if ( sscanf( soperator->s_name+1, "%f", &fvalue ) != 1 )
1329 {
1330 post( "cooled~ : error : can't get operation value" );
1331 return;
1332 }
1333 switch( soperator->s_name[0] )
1334 {
1335 case '*' :
1336 freadend = x->x_readstart + fvalue*(x->x_readend-x->x_readstart);
1337 break;
1338
1339 case '/' :
1340 if ( fvalue != 0 )
1341 {
1342 freadend = x->x_readstart + (x->x_readend-x->x_readstart)/fvalue;
1343 };
1344 break;
1345
1346 case '>' :
1347 freadstart = x->x_readstart + fvalue*(x->x_readend-x->x_readstart);
1348 freadend = x->x_readend + fvalue*(x->x_readend-x->x_readstart);
1349 break;
1350
1351 case '<' :
1352 freadstart = x->x_readstart - fvalue*(x->x_readend-x->x_readstart);
1353 freadend = x->x_readend - fvalue*(x->x_readend-x->x_readstart);
1354 break;
1355 }
1356 if ( freadstart < 0.0 ) freadstart = 0.0;
1357 if ( freadend < 0.0 ) freadend = 0.0;
1358 if ( freadstart > 100.0 ) freadstart = 100.0;
1359 if ( freadend > 100.0 ) freadend = 100.0;
1360 cooled_readstart( x, freadstart);
1361 cooled_readend( x, freadend);
1362 }
1363
1364 /* refresh data */
1365 static void cooled_refresh(t_cooled *x)
1366 {
1367 if (x->x_allocate)
1368 {
1369 post( "cooled~ : error : cannot refresh while re-allocation" );
1370 return;
1371 }
1372 if ( x->x_draw ) cooled_update_part(x, x->x_glist, 0, x->x_width-1, 0, 1, 1);
1373 }
1374
1375 static void *cooled_new(t_symbol *s, int argc, t_atom *argv)
1376 {
1377 t_cooled *x = (t_cooled *)pd_new(cooled_class);
1378 outlet_new(&x->x_obj, &s_signal);
1379 outlet_new(&x->x_obj, &s_signal);
1380 x->x_recend = outlet_new(&x->x_obj, &s_bang );
1381 x->x_end = outlet_new(&x->x_obj, &s_bang );
1382 x->x_sampstart = outlet_new(&x->x_obj, &s_float );
1383 x->x_sampend = outlet_new(&x->x_obj, &s_float );
1384 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
1385 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("readstart"));
1386 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("readend"));
1387
1388 if ( argc != 0 )
1389 {
1390 if ( argc < 3 )
1391 {
1392 post( "audience~ : error in the number of arguments ( %d )", argc );
1393 return NULL;
1394 }
1395 if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT ||
1396 argv[2].a_type != A_FLOAT )
1397 {
1398 post( "audience~ : wrong arguments" );
1399 return NULL;
1400 }
1401 x->x_size = (int)argv[0].a_w.w_float;
1402 if ( x->x_size < 1 ) x->x_size = 1;
1403 x->x_width = (int)argv[1].a_w.w_float;
1404 if ( x->x_width < 10 ) x->x_width = 10;
1405 x->x_height = (int)argv[2].a_w.w_float;
1406 if ( x->x_height < 10 ) x->x_height = 10;
1407 if ( argc == 3 )
1408 x->x_draw = 1;
1409 else
1410 x->x_draw = (int)argv[3].a_w.w_float;
1411 }
1412 else
1413 {
1414 x->x_size = COOLED_DEFAULT_SIZE;
1415 x->x_width = COOLED_DEFAULT_WIDTH;
1416 x->x_height = COOLED_DEFAULT_HEIGHT;
1417 x->x_draw = 1;
1418 }
1419
1420 // activate graphical callbacks
1421 class_setwidget(cooled_class, &cooled_widgetbehavior);
1422
1423 x->x_play = 0;
1424 x->x_readspeed = 1.;
1425 x->x_record = 0;
1426 x->x_allocate = 0;
1427 x->x_readpos = 0.;
1428 x->x_writepos = 0;
1429 x->x_rdata = NULL;
1430 x->x_ldata = NULL;
1431 x->x_empty = 1;
1432 x->x_xpos = -1;
1433 x->x_ypos = -1;
1434 /* graphic data */
1435 x->x_selected = 0;
1436 x->x_zoom = 1;
1437 #ifndef _WIN32
1438 x->x_updatechild = 0;
1439 #endif
1440 x->x_glist = (t_glist*)canvas_getcurrent();
1441 // post( "cooled~ : new : readend=%d", x->x_readend );
1442 cooled_readstart( x, 0);
1443 cooled_readend( x, 100);
1444
1445 if ( cooled_allocate(x) <0 )
1446 {
1447 return NULL;
1448 }
1449 else
1450 {
1451 return(x);
1452 }
1453
1454 }
1455
1456 void cooled_tilde_setup(void)
1457 {
•1458 logpost(NULL, 4, "%s", cooled_version);
1459 cooled_class = class_new(gensym("cooled~"), (t_newmethod)cooled_new, (t_method)cooled_free,
1460 sizeof(t_cooled), 0, A_GIMME, 0);
1461
1462
1463 // set callbacks
1464 cooled_widgetbehavior.w_getrectfn = cooled_getrect;
1465 cooled_widgetbehavior.w_displacefn = cooled_displace;
1466 cooled_widgetbehavior.w_selectfn = cooled_select;
1467 cooled_widgetbehavior.w_activatefn = NULL;
1468 cooled_widgetbehavior.w_deletefn = cooled_delete;
1469 cooled_widgetbehavior.w_visfn = cooled_vis;
1470 cooled_widgetbehavior.w_clickfn = cooled_click;
1471
1472 class_setpropertiesfn(cooled_class, cooled_properties);
1473 class_setsavefn(cooled_class, cooled_save);
1474
1475 CLASS_MAINSIGNALIN( cooled_class, t_cooled, x_f );
1476 class_addmethod(cooled_class, (t_method)cooled_dsp, gensym("dsp"), A_NULL);
1477 class_addmethod(cooled_class, (t_method)cooled_record, gensym("record"), A_NULL);
1478 class_addmethod(cooled_class, (t_method)cooled_resize, gensym("resize"), A_FLOAT, A_NULL);
1479 class_addmethod(cooled_class, (t_method)cooled_zoom, gensym("zoom"), A_FLOAT, A_NULL);
1480 class_addmethod(cooled_class, (t_method)cooled_play, gensym("play"), A_NULL);
1481 class_addmethod(cooled_class, (t_method)cooled_refresh, gensym("refresh"), A_NULL);
1482 class_addmethod(cooled_class, (t_method)cooled_readstart, gensym("readstart"), A_FLOAT, A_NULL);
1483 class_addmethod(cooled_class, (t_method)cooled_stereo, gensym("stereo"), A_NULL);
1484 class_addmethod(cooled_class, (t_method)cooled_readend, gensym("readend"), A_FLOAT, A_NULL);
1485 class_addmethod(cooled_class, (t_method)cooled_readspeed, gensym("readspeed"), A_FLOAT, A_NULL);
1486 class_addmethod(cooled_class, (t_method)cooled_loop, gensym("loop"), A_SYMBOL, A_NULL);
1487 class_addmethod(cooled_class, (t_method)cooled_dialog, gensym("dialog"), A_GIMME, 0);
1488
1489 sys_vgui("eval [read [open {%s/%s.tcl}]]\n",
1490 cooled_class->c_externdir->s_name, cooled_class->c_name->s_name);
1491 }