Debian Codegraph @

scratcher~.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*------------------------ scratcher~ ------------------------------------------ */
2 /* */
3 /* scratcher~ : lets you record, and then, scratch a sound. */
4 /* constructor : scratcher~ */
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've lost my kids and wife" */
27 /* "Because I got high da da dap da da dap" */
28 /* Afro Man -- Because I Got High */
29 /* ---------------------------------------------------------------------------- */
30
31
32
33 #include <sys/types.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <fcntl.h>
39 #include <sys/stat.h>
40 #ifdef __APPLE__
41 #include <sys/malloc.h>
42 #else
43 #include <malloc.h>
44 #endif
45 #include <ctype.h>
46 #include <pthread.h>
47 #include <time.h>
48 #include <sys/time.h>
49 #include <math.h>
50
51 #ifdef _WIN32
52 #define M_PI 3.14159265358979323846
53 #else
54 #include <unistd.h>
55 #endif
56
57 #include "m_pd.h"
58 #include "m_imp.h"
59 #include "g_canvas.h"
60
61
62 static int guidebug=0;
63 static int ignorevisible=1; // ignore visible test
64 // because this seems to lead to bad refresh
65 // wait for a fix
66
67 #define SYS_VGUI2(a,b) if (guidebug) \
68 post(a,b);\
69 sys_vgui(a,b)
70
71 #define SYS_VGUI3(a,b,c) if (guidebug) \
72 post(a,b,c);\
73 sys_vgui(a,b,c)
74
75 #define SYS_VGUI4(a,b,c,d) if (guidebug) \
76 post(a,b,c,d);\
77 sys_vgui(a,b,c,d)
78
79 #define SYS_VGUI5(a,b,c,d,e) if (guidebug) \
80 post(a,b,c,d,e);\
81 sys_vgui(a,b,c,d,e)
82
83 #define SYS_VGUI6(a,b,c,d,e,f) if (guidebug) \
84 post(a,b,c,d,e,f);\
85 sys_vgui(a,b,c,d,e,f)
86
87 #define SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \
88 post(a,b,c,d,e,f,g );\
89 sys_vgui(a,b,c,d,e,f,g)
90
91 #define SYS_VGUI8(a,b,c,d,e,f,g,h) if (guidebug) \
92 post(a,b,c,d,e,f,g,h);\
93 sys_vgui(a,b,c,d,e,f,g,h)
94
95 #define SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \
96 post(a,b,c,d,e,f,g,h,i );\
97 sys_vgui(a,b,c,d,e,f,g,h,i)
98
99 #define SYS_VGUI10(a,b,c,d,e,f,g,h,i,j) if (guidebug) \
100 post(a,b,c,d,e,f,g,h,i,j );\
101 sys_vgui(a,b,c,d,e,f,g,h,i,j)
102
103 #define SYS_VGUI11(a,b,c,d,e,f,g,h,i,j,k) if (guidebug) \
104 post(a,b,c,d,e,f,g,h,i,j,k );\
105 sys_vgui(a,b,c,d,e,f,g,h,i,j,k)
106
107 #define DEFAULT_SCRATCHER_SIZE 88200 // 2 seconds at 44100 hertz
108 #define DEFAULT_SCRATCHER_WIDTH 200
109 #define DEFAULT_SCRATCHER_HEIGHT 200
110 #define DEFAULT_SCRATCHER_SENSIBILITY 25
111 #define DEFAULT_SCRATCHER_MAX_SPEED 2.0
112 #define DEFAULT_TURNTABLE_INERTIA 0.01
113
114 #define SCRATCHER_NB_GROOVES 20
115 #define SCRATCHER_MOVE_TIMEOUT 20
116
117 static char *scratcher_version = "scratcher~: version 0.10, written by Yves Degoyon ([email protected])";
118
119 static t_class *scratcher_class;
120 t_widgetbehavior scratcher_widgetbehavior;
121
122 typedef struct _scratcher
123 {
124 t_object x_obj;
125
126 t_int x_size; /* size of the recorded sound ( in samples ) */
127 t_float x_samplerate; /* sample rate */
128 t_int x_blocksize; /* current block size ( might be modified by block~ object ) */
129 t_float x_readpos; /* data's playing position */
130 t_int x_writepos; /* data's recording position */
131 t_int x_play; /* playing on/off flag */
132 t_float x_readspeed; /* number of grouped blocks for reading */
133 t_float x_maxspeed; /* maximum speed limit */
134 t_float x_record; /* flag to start recording process */
135 t_int x_empty; /* flag to indicate it's a brand new scratcher */
136 t_float x_speedinc; /* speed increment */
137 long long x_lastmovetime; /* instant ( in ms ) of the last movement */
138 t_float *x_sdata; /* sound data */
139 t_int x_sensibility; /* sensibility ( amount of change for a dy ) */
140 t_int x_motioned; /* flag to indicate the mouse motion */
141 t_int x_mousemoved; /* flag to indicate the mouse movement */
142 t_float x_inertia; /* turntable inertia */
143
144 /* graphical data block */
145 t_int x_selected; /* flag to remember if we are seleted or not */
146 t_int x_width; /* width of the graphical object */
147 t_int x_height; /* height of the graphical object */
148 t_int x_showspeed; /* toggle on/off speed line */
149 t_glist *x_glist; /* keep graphic context for various operations */
150
151 t_float x_f; /* float needed for signal input */
152
153 } t_scratcher;
154
155 /* ------------------------ drawing functions ---------------------------- */
156
157 static void scratcher_draw_new(t_scratcher *x, t_glist *glist)
158 {
159 t_canvas *canvas=glist_getcanvas(glist);
160 t_int ci;
161
162 SYS_VGUI7(".x%lx.c create oval %d %d %d %d -fill #000000 -tags %xSCRATCHER\n",
163 canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
164 text_xpix(&x->x_obj, glist) + x->x_width,
165 text_ypix(&x->x_obj, glist) + x->x_height,
166 x);
167 for ( ci=0; ci<SCRATCHER_NB_GROOVES; ci ++)
168 {
169 SYS_VGUI8(".x%lx.c create oval %d %d %d %d -outline #FFFFFF -tags %xGROOVE%d\n",
170 canvas, text_xpix(&x->x_obj, glist) + ci*x->x_width/(2*SCRATCHER_NB_GROOVES),
171 text_ypix(&x->x_obj, glist) + ci*x->x_height/(2*SCRATCHER_NB_GROOVES),
172 text_xpix(&x->x_obj, glist) + x->x_width - ci*x->x_width/(2*SCRATCHER_NB_GROOVES),
173 text_ypix(&x->x_obj, glist) + x->x_height - ci*x->x_height/(2*SCRATCHER_NB_GROOVES),
174 x, ci);
175 }
176 if ( x->x_showspeed )
177 {
178 SYS_VGUI7( ".x%lx.c create line %d %d %d %d -fill #FF0000 -tags %xSPEEDBAR -width 3\n",
179 canvas, text_xpix(&x->x_obj, glist)+x->x_width/2,
180 text_ypix(&x->x_obj, glist)+x->x_height/2,
181 text_xpix(&x->x_obj, glist)+x->x_width/2 + (int)(x->x_width/2*cos( x->x_readspeed - 1. )),
182 text_ypix(&x->x_obj, glist)+x->x_height/2 - (int)(x->x_width/2*sin( x->x_readspeed - 1. )),
183 x );
184 }
185 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %xFSCRATCHER\n",
186 canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
187 text_xpix(&x->x_obj, glist) + x->x_width,
188 text_ypix(&x->x_obj, glist) + x->x_height,
189 x);
190 canvas_fixlinesfor( canvas, (t_text*)x );
191 }
192
193 static void scratcher_draw_delete(t_scratcher *x, t_glist *glist)
194 {
195 t_int ci;
196 t_canvas *canvas=glist_getcanvas(glist);
197
198 if ( glist_isvisible( glist ) )
199 {
200 SYS_VGUI3( ".x%lx.c delete %xSCRATCHER\n", canvas, x );
201 SYS_VGUI3( ".x%lx.c delete %xFSCRATCHER\n", canvas, x );
202 SYS_VGUI3( ".x%lx.c delete %xSPEEDBAR\n", canvas, x );
203 for ( ci=0; ci<SCRATCHER_NB_GROOVES; ci ++)
204 {
205 SYS_VGUI4( ".x%lx.c delete %xGROOVE%d\n", canvas, x, ci );
206 }
207 }
208 }
209
210 static void scratcher_draw_move(t_scratcher *x, t_glist *glist)
211 {
212 t_canvas *canvas=glist_getcanvas(glist);
213 t_int ci;
214
215 if ( glist_isvisible( x->x_glist ) )
216 {
217 SYS_VGUI7(".x%lx.c coords %xSCRATCHER %d %d %d %d\n",
218 canvas, x,
219 text_xpix(&x->x_obj, glist)-1, text_ypix(&x->x_obj, glist)-1,
220 text_xpix(&x->x_obj, glist)+x->x_width+1,
221 text_ypix(&x->x_obj, glist)+x->x_height+1);
222 SYS_VGUI7(".x%lx.c coords %xFSCRATCHER %d %d %d %d\n",
223 canvas, x,
224 text_xpix(&x->x_obj, glist)-1, text_ypix(&x->x_obj, glist)-1,
225 text_xpix(&x->x_obj, glist)+x->x_width+1,
226 text_ypix(&x->x_obj, glist)+x->x_height+1);
227 if ( x->x_showspeed )
228 {
229 SYS_VGUI7( ".x%lx.c coords %xSPEEDBAR %d %d %d %d\n",
230 canvas, x,
231 text_xpix(&x->x_obj, glist)+x->x_width/2,
232 text_ypix(&x->x_obj, glist)+x->x_height/2,
233 text_xpix(&x->x_obj, glist)+x->x_width/2 + (int)(x->x_width/2*cos( x->x_readspeed - 1 )),
234 text_ypix(&x->x_obj, glist)+x->x_height/2 - (int)(x->x_width/2*sin( x->x_readspeed - 1 ))
235 );
236 }
237 for ( ci=0; ci<SCRATCHER_NB_GROOVES; ci ++)
238 {
239 SYS_VGUI8(".x%lx.c coords %xGROOVE%d %d %d %d %d\n",
240 canvas, x, ci,
241 text_xpix(&x->x_obj, glist) + ci*x->x_width/(2*SCRATCHER_NB_GROOVES),
242 text_ypix(&x->x_obj, glist) + ci*x->x_height/(2*SCRATCHER_NB_GROOVES),
243 text_xpix(&x->x_obj, glist) + x->x_width - ci*x->x_width/(2*SCRATCHER_NB_GROOVES),
244 text_ypix(&x->x_obj, glist) + x->x_height - ci*x->x_height/(2*SCRATCHER_NB_GROOVES)
245 );
246 }
247 canvas_fixlinesfor( canvas, (t_text*)x );
248 }
249 }
250
251 static void scratcher_draw_select(t_scratcher* x,t_glist* glist)
252 {
253 t_canvas *canvas=glist_getcanvas(glist);
254
255 if ( glist_isvisible( x->x_glist ) )
256 {
257 if(x->x_selected)
258 {
259 }
260 else
261 {
262 }
263 }
264 }
265
266 /* ------------------------ widget callbacks ----------------------------- */
267
268
269 static void scratcher_getrect(t_gobj *z, t_glist *owner,
270 int *xp1, int *yp1, int *xp2, int *yp2)
271 {
272 t_scratcher* x = (t_scratcher*)z;
273
274 *xp1 = text_xpix(&x->x_obj, owner);
275 *yp1 = text_ypix(&x->x_obj, owner);
276 *xp2 = text_xpix(&x->x_obj, owner)+x->x_width;
277 *yp2 = text_ypix(&x->x_obj, owner)+x->x_height;
278 }
279
280 static void scratcher_save(t_gobj *z, t_binbuf *b)
281 {
282 t_scratcher *x = (t_scratcher *)z;
283
284 binbuf_addv(b, "ssiisiiiiff", gensym("#X"),gensym("obj"),
285 (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix,
286 atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
287 x->x_size, x->x_width, x->x_height,
288 x->x_sensibility, x->x_maxspeed, x->x_inertia );
289 binbuf_addv(b, ";");
290 }
291
292 static void scratcher_select(t_gobj *z, t_glist *glist, int selected)
293 {
294 t_scratcher *x = (t_scratcher *)z;
295
296 x->x_selected = selected;
297 scratcher_draw_select( x, glist );
298 }
299
300 static void scratcher_vis(t_gobj *z, t_glist *glist, int vis)
301 {
302 t_scratcher *x = (t_scratcher *)z;
303 t_rtext *y;
304
305 if (vis)
306 {
307 scratcher_draw_new( x, glist );
308 }
309 else
310 {
311 scratcher_draw_delete( x, glist );
312 }
313 }
314
315 static void scratcher_delete(t_gobj *z, t_glist *glist)
316 {
317 canvas_deletelinesfor(glist, (t_text *)z);
318 }
319
320 static void scratcher_displace(t_gobj *z, t_glist *glist, int dx, int dy)
321 {
322 t_scratcher *x = (t_scratcher *)z;
323
324 x->x_obj.te_xpix += dx;
325 x->x_obj.te_ypix += dy;
326
327 scratcher_draw_move( x, glist );
328 }
329
330 static void scratcher_motion(t_scratcher *x, t_floatarg dx, t_floatarg dy)
331 {
332 struct timeval tv;
333 struct timezone tz;
334 t_canvas *canvas=glist_getcanvas(x->x_glist);
335
336 // post( "scratcher_motion dx=%f dy=%f", dx, dy );
337
338 x->x_speedinc += dy / x->x_sensibility;
339
340 x->x_mousemoved = 1;
341 // get current time in ms
342 gettimeofday( &tv, &tz );
343 x->x_lastmovetime = tv.tv_sec*1000 + tv.tv_usec/1000;
344 // post( "scratcher~ : move time : %ld", x->x_lastmovetime );
345
346 if ( x->x_showspeed )
347 {
348 SYS_VGUI7( ".x%lx.c coords %xSPEEDBAR %d %d %d %d\n",
349 canvas, x,
350 text_xpix(&x->x_obj, x->x_glist)+x->x_width/2,
351 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2,
352 text_xpix(&x->x_obj, x->x_glist)+x->x_width/2 + (int)(x->x_width/2*cos( x->x_readspeed - 1 )),
353 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2 - (int)(x->x_width/2*sin( x->x_readspeed - 1 ))
354 );
355 }
356 }
357
358 static void scratcher_properties(t_gobj *z, t_glist *owner)
359 {
360 char buf[800];
361 t_scratcher *x=(t_scratcher *)z;
362
363 sprintf(buf, "pdtk_scratcher_dialog %%s %d %d\n",
364 x->x_width, x->x_height );
365 // post("scratcher_properties : %s", buf );
366 gfxstub_new(&x->x_obj.ob_pd, x, buf);
367 }
368
369 static void scratcher_dialog(t_scratcher *x, t_symbol *s, int argc, t_atom *argv)
370 {
371 if ( !x )
372 {
373 post( "scratcher : error :tried to set properties on an unexisting object" );
374 }
375 if ( argc != 2 )
376 {
377 post( "scratcher : error in the number of arguments ( %d instead of 2 )", argc );
378 return;
379 }
380 if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT )
381 {
382 post( "scratcher : wrong arguments" );
383 return;
384 }
385 x->x_width = (int)argv[0].a_w.w_float;
386 x->x_height = (int)argv[1].a_w.w_float;
387 scratcher_draw_delete(x, x->x_glist);
388 scratcher_draw_new(x, x->x_glist);
389 }
390
391 /* reset reading speed */
392 static void scratcher_reset(t_scratcher *x)
393 {
394 t_canvas *canvas=glist_getcanvas(x->x_glist);
395
396 x->x_readspeed=1.;
397 if ( x->x_showspeed )
398 {
399 SYS_VGUI7( ".x%lx.c coords %xSPEEDBAR %d %d %d %d\n",
400 canvas, x,
401 text_xpix(&x->x_obj, x->x_glist)+x->x_width/2,
402 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2,
403 text_xpix(&x->x_obj, x->x_glist)+x->x_width/2 + (int)(x->x_width/2*cos( x->x_readspeed - 1 )),
404 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2 - (int)(x->x_width/2*sin( x->x_readspeed - 1 ))
405 );
406 }
407 }
408
409 static int scratcher_click(t_gobj *z, struct _glist *glist,
410 int xpix, int ypix, int shift, int alt, int dbl, int doit)
411 {
412 t_scratcher* x = (t_scratcher *)z;
413 t_canvas *canvas=glist_getcanvas(x->x_glist);
414
415 // post( "scratcher_click : x=%d y=%d doit=%d alt=%d, shift=%d", xpix, ypix, doit, alt, shift );
416 if ( doit )
417 {
418 // activate motion callback
419 glist_grab( glist, &x->x_obj.te_g, (t_glistmotionfn)scratcher_motion,
420 0, xpix, ypix );
421 x->x_readspeed=0.;
422 x->x_motioned = 1;
423 if ( x->x_showspeed )
424 {
425 SYS_VGUI7( ".x%lx.c coords %xSPEEDBAR %d %d %d %d\n",
426 canvas, x,
427 text_xpix(&x->x_obj, glist)+x->x_width/2,
428 text_ypix(&x->x_obj, glist)+x->x_height/2,
429 text_xpix(&x->x_obj, glist)+x->x_width/2 + (int)(x->x_width/2*cos( x->x_readspeed - 1 )),
430 text_ypix(&x->x_obj, glist)+x->x_height/2 - (int)(x->x_width/2*sin( x->x_readspeed - 1 ))
431 );
432 }
433 }
434 else
435 {
436 if ( x->x_play ) scratcher_reset(x);
437 x->x_motioned = 0;
438 }
439 return (1);
440 }
441
442 /* clean up */
443 static void scratcher_free(t_scratcher *x)
444 {
445 if ( x->x_sdata != NULL )
446 {
447 freebytes(x->x_sdata, x->x_size*sizeof(t_float) );
448 post( "scratcher~ : freed %d bytes", x->x_size*sizeof(t_float) );
449 x->x_sdata = NULL;
450 }
451 }
452
453 /* allocate tables for storing sample data */
454 static t_int scratcher_allocate(t_scratcher *x)
455 {
456 if ( !(x->x_sdata = getbytes( x->x_size*sizeof(float) ) ) )
457 {
458 post( "scratcher~ : could not allocate %d bytes", x->x_size*sizeof(t_float) );
459 return -1;
460 }
461 else
462 {
463 post( "scratcher~ : allocated %d bytes", x->x_size*sizeof(t_float) );
464 }
465 return 0;
466 }
467
468 /* reallocate tables for storing sample data */
469 static t_int scratcher_reallocate(t_scratcher *x, t_int ioldsize, t_int inewsize)
470 {
471 t_float *pdata;
472
473 pdata = x->x_sdata;
474 if ( !(x->x_sdata = getbytes( inewsize*sizeof(float) ) ) )
475 {
476 post( "scratcher~ : could not allocate %d bytes", inewsize*sizeof(t_float) );
477 return -1;
478 }
479 else
480 {
481 post( "scratcher~ : allocated %d bytes", inewsize*sizeof(t_float) );
482 }
483 if ( pdata != NULL )
484 {
485 freebytes(pdata, ioldsize*sizeof(t_float) );
486 post( "scratcher~ : freed %d bytes", ioldsize*sizeof(t_float) );
487 }
488 return 0;
489 }
490
491 /* records or playback the scratcher */
492 static t_int *scratcher_perform(t_int *w)
493 {
494 t_float *in = (t_float *)(w[1]);
495 t_float *out = (t_float *)(w[2]);
496 t_int n = (int)(w[3]); /* number of samples */
497 t_scratcher *x = (t_scratcher *)(w[4]);
498 struct timeval tv;
499 struct timezone tz;
500 long long perftime = 0L;
501 t_canvas *canvas=glist_getcanvas(x->x_glist);
502
503 x->x_readspeed += x->x_speedinc;
504 if ( x->x_readspeed > x->x_maxspeed )
505 {
506 x->x_readspeed = x->x_maxspeed;
507 }
508 if ( x->x_readspeed < -x->x_maxspeed )
509 {
510 x->x_readspeed = -x->x_maxspeed;
511 }
512 x->x_speedinc = 0;
513
514 // if the mouse hasn't moved for a certain time, reset speed
515
516 if ( x->x_mousemoved )
517 {
518 // get current time in ms
519 gettimeofday( &tv, &tz );
520 perftime = tv.tv_sec*1000 + tv.tv_usec/1000;
521 if ( perftime - x->x_lastmovetime > SCRATCHER_MOVE_TIMEOUT )
522 {
523 // post( "scratcher~ : mouse timeout (m=%ld)", perftime );
524 // post( "scratcher~ : (m=%ld)", x->x_lastmovetime );
525 if ( x->x_readspeed > 0. )
526 {
527 x->x_readspeed -= x->x_inertia;
528 // post( "scratcher~ : dec speed" );
529 }
530 if ( x->x_readspeed < 0. )
531 {
532 x->x_readspeed += x->x_inertia;
533 // post( "scratcher~ : inc speed" );
534 }
535 if ( ( x->x_readspeed <= x->x_inertia && x->x_readspeed > 0 ) ||
536 ( x->x_readspeed >= -x->x_inertia && x->x_readspeed < 0 ) )
537 {
538 x->x_mousemoved = 0;
539 x->x_readspeed = 0.;
540 }
541 if ( x->x_showspeed )
542 {
543 SYS_VGUI7( ".x%lx.c coords %xSPEEDBAR %d %d %d %d\n",
544 canvas, x,
545 text_xpix(&x->x_obj, x->x_glist)+x->x_width/2,
546 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2,
547 text_xpix(&x->x_obj, x->x_glist)+x->x_width/2 + (int)(x->x_width/2*cos( x->x_readspeed - 1 )),
548 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2 - (int)(x->x_width/2*sin( x->x_readspeed - 1 ))
549 );
550 }
551 }
552 }
553
554 while (n--)
555 {
556 // eventually records input
557 if ( x->x_record)
558 {
559 *(x->x_sdata+x->x_writepos)=*in;
560 x->x_writepos++;
561 if ( x->x_writepos >= x->x_size )
562 {
563 x->x_record=0;
564 x->x_writepos=0;
565 if ( x->x_empty ) x->x_empty = 0;
566 // post( "scratcher~ : stopped recording" );
567 }
568 }
569 // set outputs
570 if ( x->x_play)
571 {
572 *out = *(x->x_sdata+(int)x->x_readpos);
573 x->x_readpos+=x->x_readspeed;
574 if ( x->x_readpos < 0 ) x->x_readpos = x->x_size-1;
575 if ( x->x_readpos >= x->x_size ) x->x_readpos = 0;
576 }
577 else
578 {
579 *out=0.0;
580 }
581
582 in++;
583 out++;
584 }
585
586 return (w+5);
587 }
588
589 static void scratcher_dsp(t_scratcher *x, t_signal **sp)
590 {
591 dsp_add(scratcher_perform, 4, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n, x);
592 }
593
594 /* play the sound */
595 static void scratcher_play(t_scratcher *x)
596 {
597 t_canvas *canvas=glist_getcanvas(x->x_glist);
598
599 x->x_play=1;
600 // reset read position
601 x->x_readpos=0;
602 x->x_readspeed=1.;
603 if ( x->x_showspeed )
604 {
605 SYS_VGUI3( ".x%lx.c delete %xSPEEDBAR\n", canvas, x );
606 SYS_VGUI7( ".x%lx.c create line %d %d %d %d -fill #FF0000 -tags %xSPEEDBAR -width 3\n",
607 canvas, text_xpix(&x->x_obj, x->x_glist)+x->x_width/2,
608 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2,
609 text_xpix(&x->x_obj, x->x_glist)+x->x_width/2 + (int)(x->x_width/2*cos( x->x_readspeed - 1. )),
610 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2 - (int)(x->x_width/2*sin( x->x_readspeed - 1. )),
611 x );
612 }
613 }
614
615 /* stop playing */
616 static void scratcher_stop(t_scratcher *x)
617 {
618 t_canvas *canvas=glist_getcanvas(x->x_glist);
619
620 x->x_play=0;
621 // reset read position
622 x->x_readpos=0;
623 x->x_readspeed=0.;
624 if ( x->x_showspeed )
625 {
626 SYS_VGUI3( ".x%lx.c delete %xSPEEDBAR\n", canvas, x );
627 SYS_VGUI7( ".x%lx.c create line %d %d %d %d -fill #FF0000 -tags %xSPEEDBAR -width 3\n",
628 canvas, text_xpix(&x->x_obj, x->x_glist)+x->x_width/2,
629 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2,
630 text_xpix(&x->x_obj, x->x_glist)+x->x_width/2 + (int)(x->x_width/2*cos( x->x_readspeed - 1. )),
631 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2 - (int)(x->x_width/2*sin( x->x_readspeed - 1. )),
632 x );
633 }
634 }
635
636 /* record the sound */
637 static void scratcher_record(t_scratcher *x)
638 {
639 scratcher_stop(x);
640 x->x_record=1;
641 x->x_writepos=0;
642 }
643
644 /* resize sound */
645 static void scratcher_resize(t_scratcher *x, t_floatarg fnewsize )
646 {
647 t_int dspstate;
648 struct timespec ts;
649
650 if (fnewsize <= 0)
651 {
652 post( "scratcher~ : error : wrong size" );
653 return;
654 }
655 post( "scratcher~ : reallocating tables" );
656 x->x_record = 0;
657 x->x_play = 0;
658
659 scratcher_reallocate(x, x->x_size, fnewsize);
660 x->x_size = fnewsize;
661 }
662
663 /* set sensibility */
664 static void scratcher_sensibility(t_scratcher *x, t_floatarg fsensibility )
665 {
666 if (fsensibility <= 0)
667 {
668 post( "scratcher~ : error : wrong sensibility" );
669 return;
670 }
671 x->x_sensibility = fsensibility;
672 }
673
674 /* set maximum speed */
675 static void scratcher_maxspeed(t_scratcher *x, t_floatarg fmaxspeed )
676 {
677 if (fmaxspeed < 0)
678 {
679 post( "scratcher~ : error : wrong maximum speed" );
680 return;
681 }
682 x->x_maxspeed = fmaxspeed;
683 }
684
685 /* set turntable inertia */
686 static void scratcher_inertia(t_scratcher *x, t_floatarg finertia )
687 {
688 if (finertia < 0)
689 {
690 post( "scratcher~ : error : wrong inertia" );
691 return;
692 }
693 x->x_inertia = finertia;
694 }
695
696 /* toggle speed line */
697 static void scratcher_showspeed(t_scratcher *x, t_floatarg fshowspeed )
698 {
699 t_canvas *canvas=glist_getcanvas(x->x_glist);
700
701 if (fshowspeed == 0)
702 {
703 x->x_showspeed = 0;
704 SYS_VGUI3( ".x%lx.c delete %xSPEEDBAR\n", canvas, x );
705 }
706 else
707 {
708 x->x_showspeed = 1;
709 SYS_VGUI3( ".x%lx.c delete %xSPEEDBAR\n", canvas, x );
710 SYS_VGUI7( ".x%lx.c create line %d %d %d %d -fill #FF0000 -tags %xSPEEDBAR -width 3\n",
711 canvas, text_xpix(&x->x_obj, x->x_glist)+x->x_width/2,
712 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2,
713 text_xpix(&x->x_obj, x->x_glist)+x->x_width/2 + (int)(x->x_width/2*cos( x->x_readspeed - 1. )),
714 text_ypix(&x->x_obj, x->x_glist)+x->x_height/2 - (int)(x->x_width/2*sin( x->x_readspeed - 1. )),
715 x );
716 }
717 }
718
719 static void *scratcher_new(t_symbol *s, int argc, t_atom *argv)
720 {
721 t_scratcher *x = (t_scratcher *)pd_new(scratcher_class);
722 outlet_new(&x->x_obj, &s_signal);
723
724 // new scratcher created from the gui
725 if ( argc != 0 )
726 {
727 if ( argc < 3 )
728 {
729 post( "scratcher~ : error in the number of arguments ( < 3 )", argc );
730 return NULL;
731 }
732 if ( argv[0].a_type != A_FLOAT ||
733 argv[1].a_type != A_FLOAT ||
734 argv[2].a_type != A_FLOAT )
735 {
736 post( "scratcher~ : wrong arguments" );
737 return NULL;
738 }
739 x->x_size = argv[0].a_w.w_float;
740 x->x_width = argv[1].a_w.w_float;
741 x->x_height = argv[2].a_w.w_float;
742 if ( argc >= 4 )
743 {
744 x->x_sensibility = argv[3].a_w.w_float;
745 }
746 else
747 {
748 x->x_sensibility = DEFAULT_SCRATCHER_SENSIBILITY;
749 }
750 if ( argc >= 5 )
751 {
752 x->x_maxspeed = argv[4].a_w.w_float;
753 }
754 else
755 {
756 x->x_maxspeed = DEFAULT_SCRATCHER_MAX_SPEED;
757 }
758 if ( argc >= 6 )
759 {
760 x->x_inertia = argv[5].a_w.w_float;
761 }
762 else
763 {
764 x->x_inertia = DEFAULT_TURNTABLE_INERTIA;
765 }
766 }
767 else
768 {
769 x->x_size = DEFAULT_SCRATCHER_SIZE;
770 x->x_width = DEFAULT_SCRATCHER_WIDTH;
771 x->x_height = DEFAULT_SCRATCHER_HEIGHT;
772 x->x_sensibility = DEFAULT_SCRATCHER_SENSIBILITY;
773 x->x_maxspeed = DEFAULT_SCRATCHER_MAX_SPEED;
774 x->x_inertia = DEFAULT_TURNTABLE_INERTIA;
775 }
776
777 x->x_play = 0;
778 x->x_record = 0;
779 x->x_readspeed = 1.;
780 x->x_readpos = 0.;
781 x->x_speedinc = 0.;
782 x->x_writepos = 0;
783 x->x_sdata = NULL;
784 x->x_empty = 1;
785 x->x_showspeed = 1;
786 x->x_mousemoved = 0;
787 x->x_lastmovetime = 0L;
788 x->x_selected = 0;
789 x->x_glist = (t_glist*)canvas_getcurrent();
790
791 // activate graphical callbacks
792 class_setwidget(scratcher_class, &scratcher_widgetbehavior);
793
794 // post( "scratcher~ : new scratcher : size = %d", x->x_size );
795 if ( scratcher_allocate(x) <0 )
796 {
797 return NULL;
798 }
799 else
800 {
801 return(x);
802 }
803
804 }
805
806 void scratcher_tilde_setup(void)
807 {
•808 logpost(NULL, 4, "%s", scratcher_version);
809 scratcher_class = class_new(gensym("scratcher~"), (t_newmethod)scratcher_new, (t_method)scratcher_free,
810 sizeof(t_scratcher), 0, A_GIMME, 0);
811
812
813 // set callbacks
814 scratcher_widgetbehavior.w_getrectfn = scratcher_getrect;
815 scratcher_widgetbehavior.w_displacefn = scratcher_displace;
816 scratcher_widgetbehavior.w_selectfn = scratcher_select;
817 scratcher_widgetbehavior.w_activatefn = NULL;
818 scratcher_widgetbehavior.w_deletefn = scratcher_delete;
819 scratcher_widgetbehavior.w_visfn = scratcher_vis;
820 scratcher_widgetbehavior.w_clickfn = scratcher_click;
821
822 #if PD_MINOR_VERSION >= 37
823 class_setpropertiesfn(scratcher_class, scratcher_properties);
824 class_setsavefn(scratcher_class, scratcher_save);
825 #else
826 scratcher_widgetbehavior.w_propertiesfn = scratcher_properties;
827 scratcher_widgetbehavior.w_savefn = scratcher_save;
828 #endif
829
830 CLASS_MAINSIGNALIN( scratcher_class, t_scratcher, x_f );
831 class_addmethod(scratcher_class, (t_method)scratcher_dsp, gensym("dsp"), A_NULL);
832 class_addmethod(scratcher_class, (t_method)scratcher_record, gensym("record"), A_NULL);
833 class_addmethod(scratcher_class, (t_method)scratcher_resize, gensym("resize"), A_FLOAT, A_NULL);
834 class_addmethod(scratcher_class, (t_method)scratcher_sensibility, gensym("sensibility"), A_FLOAT, A_NULL);
835 class_addmethod(scratcher_class, (t_method)scratcher_maxspeed, gensym("maxspeed"), A_FLOAT, A_NULL);
836 class_addmethod(scratcher_class, (t_method)scratcher_inertia, gensym("inertia"), A_FLOAT, A_NULL);
837 class_addmethod(scratcher_class, (t_method)scratcher_showspeed, gensym("showspeed"), A_FLOAT, A_NULL);
838 class_addmethod(scratcher_class, (t_method)scratcher_play, gensym("play"), A_NULL);
839 class_addmethod(scratcher_class, (t_method)scratcher_stop, gensym("stop"), A_NULL);
840 class_addmethod(scratcher_class, (t_method)scratcher_reset, gensym("reset"), A_NULL);
841 class_addmethod(scratcher_class, (t_method)scratcher_dialog, gensym("dialog"), A_GIMME, A_NULL);
842
843 sys_vgui("eval [read [open {%s/%s.tcl}]]\n",
844 scratcher_class->c_externdir->s_name,
845 scratcher_class->c_name->s_name);
846 }