Debian Codegraph @

sonogram~.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /*------------------------ sonogram~ ------------------------------------------ */
2 /* */
3 /* sonogram~ : lets you record, play back and modify a sonogram */
4 /* constructor : sonogram <size> <graphical=0|1> <phasogram=0|1> */
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 /* "Living at night" */
27 /* "Doesn't help for my complexion" */
28 /* David Thomas - Final Solution */
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 <math.h>
48
49 #ifdef _WIN32
50 # include <io.h>
51 # define random rand
52 #endif
53
54 #ifndef _MSC_VER /* only MSVC doesn't have unistd.h */
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 static int ignorevisible=1; // ignore visible test
65 // because this seems to lead to bad refresh
66 // wait for a fix
67
68 #define SYS_VGUI2(a,b) if (guidebug) \
69 post(a,b);\
70 sys_vgui(a,b)
71
72 #define SYS_VGUI3(a,b,c) if (guidebug) \
73 post(a,b,c);\
74 sys_vgui(a,b,c)
75
76 #define SYS_VGUI4(a,b,c,d) if (guidebug) \
77 post(a,b,c,d);\
78 sys_vgui(a,b,c,d)
79
80 #define SYS_VGUI5(a,b,c,d,e) if (guidebug) \
81 post(a,b,c,d,e);\
82 sys_vgui(a,b,c,d,e)
83
84 #define SYS_VGUI6(a,b,c,d,e,f) if (guidebug) \
85 post(a,b,c,d,e,f);\
86 sys_vgui(a,b,c,d,e,f)
87
88 #define SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \
89 post(a,b,c,d,e,f,g );\
90 sys_vgui(a,b,c,d,e,f,g)
91
92 #define SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \
93 post(a,b,c,d,e,f,g,h,i );\
94 sys_vgui(a,b,c,d,e,f,g,h,i)
95
96 #define SYS_VGUI10(a,b,c,d,e,f,g,h,i,j) if (guidebug) \
97 post(a,b,c,d,e,f,g,h,i,j );\
98 sys_vgui(a,b,c,d,e,f,g,h,i,j)
99
100 #define SYS_VGUI11(a,b,c,d,e,f,g,h,i,j,k) if (guidebug) \
101 post(a,b,c,d,e,f,g,h,i,j,k );\
102 sys_vgui(a,b,c,d,e,f,g,h,i,j,k)
103
104 #define THREAD_SLEEP_TIME 100000 // 100000 us = 100 ms
105
106 static char *sonogram_version = "sonogram~: version 0.14, written by Yves Degoyon ([email protected])";
107
108 static t_class *sonogram_class;
109 t_widgetbehavior sonogram_widgetbehavior;
110
111
112 typedef struct _sonogram
113 {
114 t_object x_obj;
115
116 t_int x_size; /* size of the stored fft ( in blocks~ ) */
117 t_float x_samplerate; /* sample rate */
118 t_int x_blocksize; /* current block size ( might be modified by block~ object ) */
119 t_float x_readpos; /* data's playing position */
120 t_int x_writepos; /* data's recording position */
121 t_int x_readstart; /* data's starting position for reading */
122 t_int x_readend; /* data's ending position for reading */
123 t_int x_modstart; /* data's starting position for modifications */
124 t_int x_modend; /* data's ending position for modifications */
125 t_int x_play; /* playing on/off flag */
126 t_float x_readspeed; /* number of grouped blocks for reading */
127 t_float x_record; /* flag to start recording process */
128 t_float x_empty; /* flag to indicate it's a brand new sonogram */
129 t_float *x_rdata; /* table containing real part of the fft */
130 t_float *x_rudata; /* undo real data */
131 t_float *x_idata; /* table containing imaginery part of the fft */
132 t_float *x_iudata; /* undo imaginery data */
133 t_float x_phase; /* phase to apply on output */
134 t_outlet *x_end; /* outlet for end of restitution */
135 t_outlet *x_recend; /* outlet for end of recording */
136 t_int *x_multfreq; /* array of multiplicative factor */
137 char *x_gifdata; /* buffer for graphical data */
138 char *x_guicommand; /* buffer for graphical command */
139 t_int x_uxs; /* starting x position for undo */
140 t_int x_uxe; /* ending x position for undo */
141 t_int x_uys; /* starting y position for undo */
142 t_int x_uye; /* ending y position for undo */
143
144 /* graphical data block */
145 t_int x_enhancemode; /* flag to set enhance mode */
146 t_int x_graphic; /* flag to set graphic mode */
147 t_int x_phaso; /* flag to indicate if phasogram is shown */
148 t_int x_selected; /* flag to remember if we are seleted or not */
149 t_int x_erase; /* flag used when an erase is needed */
150 t_int x_redraw; /* flag used when drawing is needed */
151 t_int x_nbupdated; /* number of points updated */
152 t_glist *x_glist; /* keep graphic context for various operations */
153 t_int x_zoom; /* zoom factor */
154 #ifndef _WIN32
155 pthread_t x_updatechild; /* thread id for the update child */
156 #else
157 int x_updatechild;
158 #endif
159 t_int x_updatestart; /* starting position for update */
160 t_int x_updateend; /* ending position for update */
161 t_int x_xpos; /* stuck x position */
162 t_int x_ypos; /* stuck y position */
163 t_int x_shifted; /* remember shift state from last click */
164 t_int x_alted; /* remember alt state from last click */
165 t_int x_aftermousedown; /* indicates the mousedown event */
166 t_int x_xstartcapture; /* x of the start of the capture */
167 t_int x_ystartcapture; /* y of the start of the capture */
168 t_int x_xendcapture; /* x of the start of the capture */
169 t_int x_yendcapture; /* y of the start of the capture */
170 t_int x_xdraw; /* x drawing position */
171 t_int x_ydraw; /* y drawing position */
172 t_float x_modstep; /* step for graphical modifications */
173
174 t_float x_f; /* float needed for signal input */
175
176 } t_sonogram;
177
178 /* ------------------------ drawing functions ---------------------------- */
179 static char* sonogram_get_fill_color( t_float fspectrum )
180 {
181 if ( fspectrum < 0.01 )
182 {
183 return "#EEEEEE ";
184 }
185 else if ( fspectrum < 0.1 )
186 {
187 return "#DDDDDD ";
188 }
189 else if ( fspectrum < 0.5 )
190 {
191 return "#CCCCCC ";
192 }
193 else if ( fspectrum < 1 )
194 {
195 return "#BBBBBB ";
196 }
197 else if ( fspectrum < 2 )
198 {
199 return "#AAAAAA ";
200 }
201 else if ( fspectrum < 5 )
202 {
203 return "#999999 ";
204 }
205 else if ( fspectrum < 10 )
206 {
207 return "#888888 ";
208 }
209 else if ( fspectrum < 20 )
210 {
211 return "#777777 ";
212 }
213 else if ( fspectrum < 30 )
214 {
215 return "#666666 ";
216 }
217 else if ( fspectrum < 40 )
218 {
219 return "#555555 ";
220 }
221 else if ( fspectrum < 50 )
222 {
223 return "#444444 ";
224 }
225 else if ( fspectrum < 60 )
226 {
227 return "#333333 ";
228 }
229 else if ( fspectrum < 80 )
230 {
231 return "#222222 ";
232 }
233 else if ( fspectrum < 100 )
234 {
235 return "#111111 ";
236 }
237 else
238 {
239 return "#000000 ";
240 }
241 }
242
243 static char* phasogram_get_fill_color( t_int phase )
244 {
245 if ( phase < 0 )
246 {
247 if ( phase > -10 )
248 {
249 return "#111111 ";
250 }
251 else if ( phase > -20 )
252 {
253 return "#222222 ";
254 }
255 else if ( phase > -30 )
256 {
257 return "#333333 ";
258 }
259 else if ( phase > -40 )
260 {
261 return "#444444 ";
262 }
263 else if ( phase > -50 )
264 {
265 return "#555555 ";
266 }
267 else if ( phase > -60 )
268 {
269 return "#666666 ";
270 }
271 else if ( phase > -70 )
272 {
273 return "#777777 ";
274 }
275 else if ( phase > -80 )
276 {
277 return "#888888 ";
278 }
279 else
280 {
281 return "#999999 ";
282 }
283 }
284 else
285 {
286 if ( phase == 0 )
287 {
288 return "#FFFFFF ";
289 }
290 else if ( phase < 10 )
291 {
292 return "#111111 ";
293 }
294 else if ( phase < 20 )
295 {
296 return "#222222 ";
297 }
298 else if ( phase < 30 )
299 {
300 return "#333333 ";
301 }
302 else if ( phase < 40 )
303 {
304 return "#444444 ";
305 }
306 else if ( phase < 50 )
307 {
308 return "#555555 ";
309 }
310 else if ( phase < 60 )
311 {
312 return "#666666 ";
313 }
314 else if ( phase < 70 )
315 {
316 return "#777777 ";
317 }
318 else if ( phase < 80 )
319 {
320 return "#888888 ";
321 }
322 else
323 {
324 return "#999999 ";
325 }
326 }
327 // normally never reached
328 return "";
329 }
330
331 static void sonogram_update_point(t_sonogram *x, t_glist *glist, t_int sample, t_int frequency)
332 {
333 t_canvas *canvas=glist_getcanvas(glist);
334 t_float fspectrum=0.0;
335 t_int phase=0.0;
336 char newColor[ 8 ], olColor[8];
337 int i;
338
339 fspectrum =
340 sqrt( pow( *(x->x_rdata+sample*x->x_blocksize+frequency), 2) +
341 pow( *(x->x_idata+sample*x->x_blocksize+frequency), 2) );
342 phase = (int) ( atan2( *(x->x_idata+(sample*x->x_blocksize)+frequency),
343 *(x->x_rdata+(sample*x->x_blocksize)+frequency) )*180/M_PI );
344 if ( x->x_empty && ( fspectrum != 0 ))
345 {
346 x->x_empty = 0;
347 }
348 strncpy( newColor, sonogram_get_fill_color( fspectrum ), 8 );
349
350 for ( i=0; i<x->x_zoom; i++ )
351 {
352 sprintf( x->x_gifdata, "%s", newColor );
353 }
354 for ( i=0; i<x->x_zoom; i++ )
355 {
356 SYS_VGUI5("SONIMAGE%x put {%s} -to %d %d\n", x, x->x_gifdata,
357 sample*x->x_zoom+i, (x->x_blocksize/2-frequency)*x->x_zoom );
358 }
359
360 if ( x->x_phaso )
361 {
362 strncpy( newColor, phasogram_get_fill_color( phase ), 8 );
363 strcpy( x->x_gifdata, "" );
364 for ( i=0; i<x->x_zoom; i++ )
365 {
366 sprintf( x->x_gifdata, "%s", newColor );
367 }
368 for ( i=0; i<x->x_zoom; i++ )
369 {
370 SYS_VGUI5("FAZIMAGE%x put {%s} -to %d %d\n", x, x->x_gifdata,
371 sample*x->x_zoom+i, (x->x_blocksize/2-frequency)*x->x_zoom );
372 }
373 }
374
375 x->x_nbupdated++;
376 }
377
378 static void sonogram_update_block(t_sonogram *x, t_glist *glist, t_int bnumber)
379 {
380 t_int fi, i=0;
381 t_float fspectrum=0.0;
382 t_int phase=0;
383 char color[8];
384
385 // update sonogram
386 for ( fi=x->x_blocksize/2-1; fi>=0; fi-- )
387 {
388 fspectrum =
389 sqrt( pow( *(x->x_rdata+bnumber*x->x_blocksize+fi), 2) +
390 pow( *(x->x_idata+bnumber*x->x_blocksize+fi), 2) );
391 strncpy( color, sonogram_get_fill_color( fspectrum ), 8 );
392 for ( i=0; i<x->x_zoom; i++ )
393 {
394 strncpy( x->x_gifdata+((x->x_blocksize/2-fi-1)*x->x_zoom+i)*8, color, 8 );
395 }
396 }
397 for ( i=0; i<x->x_zoom; i++ )
398 {
399 sprintf( x->x_guicommand, "SONIMAGE%x put {%s} -to %d 0\n", (unsigned int)x, x->x_gifdata, (bnumber*x->x_zoom)+i );
400 sys_gui( x->x_guicommand );
401 }
402
403 // update phasogram
404 if ( x->x_phaso )
405 {
406 strcpy( x->x_gifdata, "" );
407 for ( fi=x->x_blocksize/2-1; fi>=0; fi-- )
408 {
409 phase = (int) ( atan2( *(x->x_idata+bnumber*x->x_blocksize+fi),
410 *(x->x_rdata+bnumber*x->x_blocksize+fi) )*180/M_PI );
411 strncpy( color, phasogram_get_fill_color( phase ), 8 );
412 for ( i=0; i<x->x_zoom; i++ )
413 {
414 strncpy( x->x_gifdata+((x->x_blocksize/2-fi-1)*x->x_zoom+i)*8, color, 8 );
415 }
416 }
417 for ( i=0; i<x->x_zoom; i++ )
418 {
419 sprintf( x->x_guicommand, "FAZIMAGE%x put {%s} -to %d 0\n", (unsigned int)x, x->x_gifdata, (bnumber*x->x_zoom)+i );
420 sys_gui( x->x_guicommand );
421 }
422 }
423
424 }
425
426 static void sonogram_erase_block(t_sonogram *x, t_glist *glist, t_int bnumber )
427 {
428 t_canvas *canvas=glist_getcanvas(glist);
429 t_int fi;
430 t_float fspectrum=0.0;
431 char fillColor[ 16 ];
432
433 for ( fi=0; fi<x->x_blocksize/2; fi++)
434 {
435 {
436 int i;
437
438 for ( i=0; i<x->x_zoom; i++ )
439 {
440 strncpy( x->x_gifdata+i*sizeof("#FFFFFF "), "#FFFFFF ", 8 );
441 }
442 SYS_VGUI5("SONIMAGE%x put {%s} -to %d %d\n", x, x->x_gifdata,
443 bnumber*x->x_zoom, (x->x_blocksize/2-fi)*x->x_zoom );
444 }
445 }
446 }
447
448 static void *sonogram_do_update_part(void *tdata)
449 {
450 t_sonogram *x = (t_sonogram*) tdata;
451 t_int si;
452 t_int nbpoints = 0;
453 t_float percentage = 0, opercentage = 0;
454 t_canvas *canvas=glist_getcanvas(x->x_glist);
455
456
457 // loose synchro
458 usleep( THREAD_SLEEP_TIME );
459
460 // check boundaries
461 if ( x->x_updateend > x->x_size-1 ) x->x_updateend = x->x_size-1;
462 if ( x->x_updatestart < 0 ) x->x_updatestart = 0;
463
464 // post("sonogram~ : ok, let's go [updating %d, %d]", x->x_updatestart, x->x_updateend );
465
466 if ( x->x_erase )
467 {
468 for ( si=x->x_updatestart; si<=x->x_updateend; si++ )
469 {
470 sonogram_erase_block(x, x->x_glist, si);
471 nbpoints++;
472 percentage = (nbpoints*100/(x->x_updateend-x->x_updatestart+1));
473 if ( (percentage == (int) percentage) && ((int)percentage%5 == 0) && ( percentage != opercentage ) )
474 {
475 // post( "sonogram~ : erase part : %d %% completed", (int)percentage );
476 opercentage = percentage;
477 }
478 }
479 }
480
481 percentage = opercentage = nbpoints = 0;
482
483 if ( x->x_redraw )
484 {
485 for ( si=x->x_updatestart; si<=x->x_updateend; si++ )
486 {
487 sonogram_update_block(x, x->x_glist, si);
488 nbpoints++;
489 percentage = (nbpoints*100/(x->x_updateend-x->x_updatestart+1));
490 if ( (percentage == (int) percentage) && ((int)percentage%5 == 0) && ( percentage != opercentage ) )
491 {
492 // post( "sonogram~ : update part : %d %% completed", (int)percentage );
493 opercentage = percentage;
494 }
495 }
496 }
497
498 // set borders in black
499 SYS_VGUI3(".x%lx.c itemconfigure %xSONOGRAM -outline #000000\n", canvas, x);
500 if ( x->x_phaso )
501 {
502 SYS_VGUI3(".x%lx.c itemconfigure %xPHASOGRAM -outline #000000\n", canvas, x);
503 }
504
505 // post("sonogram~ : child thread %d ended (nb_updated=%d)", (int)x->x_updatechild, x->x_nbupdated );
506 x->x_updatechild = 0;
507 return NULL;
508 }
509
510 static void sonogram_update_part(t_sonogram *x, t_glist *glist, t_int bstart, t_int bend,
511 t_int erase, t_int redraw, t_int keepframe)
512 {
513 pthread_attr_t update_child_attr;
514 pthread_t update_child;
515 t_canvas *canvas=glist_getcanvas(glist);
516
517 if ( x->x_graphic )
518 {
519 if ( x->x_updatechild != 0 )
520 {
521 // post( "sonogram~ : error : no update is possible for now" );
522 return;
523 }
524 x->x_updatestart = bstart;
525 x->x_updateend = bend;
526 if ( !keepframe )
527 {
528 x->x_erase = 0;
529 }
530 else
531 {
532 x->x_erase = erase;
533 }
534 x->x_redraw = redraw;
535 x->x_nbupdated = 0;
536 // recreate the square if needed
537 if ( ( bstart == 0 ) && ( bend == x->x_size-1 ) && !keepframe )
538 {
539 SYS_VGUI3(".x%lx.c delete %xSONOGRAM\n", canvas, x );
540 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xSONOGRAM\n",
541 glist_getcanvas(glist), x->x_xpos-1, x->x_ypos-1,
542 x->x_xpos + x->x_size*x->x_zoom+1,
543 x->x_ypos + x->x_blocksize/2*x->x_zoom+1,
544 x);
545 SYS_VGUI2("image delete SONIMAGE%x\n", x );
546 SYS_VGUI3(".x%lx.c delete ISONIMAGE%x\n", canvas, x );
547 SYS_VGUI4("image create photo SONIMAGE%x -format gif -width %d -height %d\n",
548 x, x->x_size*x->x_zoom, x->x_blocksize/2*x->x_zoom );
549 SYS_VGUI2("SONIMAGE%x blank\n", x);
550 SYS_VGUI6(".x%lx.c create image %d %d -image SONIMAGE%x -tags ISONIMAGE%x\n",
551 canvas, x->x_xpos+((x->x_size*x->x_zoom)/2),
552 (x->x_ypos+((x->x_blocksize/2*x->x_zoom)/2)), x, x );
553 if ( x->x_phaso )
554 {
555 SYS_VGUI3(".x%lx.c delete %xPHASOGRAM\n", canvas, x );
556 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xPHASOGRAM\n",
557 canvas, x->x_xpos-1, x->x_ypos+x->x_blocksize/2*x->x_zoom+2,
558 x->x_xpos + x->x_size*x->x_zoom +1,
559 x->x_ypos + x->x_blocksize*x->x_zoom + 3,
560 x);
561 SYS_VGUI2("image delete FAZIMAGE%x\n", x );
562 SYS_VGUI3(".x%lx.c delete IFAZIMAGE%x\n", canvas, x );
563 SYS_VGUI4("image create photo FAZIMAGE%x -format gif -width %d -height %d\n",
564 x, x->x_size*x->x_zoom, x->x_blocksize/2*x->x_zoom );
565 SYS_VGUI2("FAZIMAGE%x blank\n", x);
566 SYS_VGUI6(".x%lx.c create image %d %d -image FAZIMAGE%x -tags IFAZIMAGE%x\n",
567 canvas, x->x_xpos+((x->x_size*x->x_zoom)/2),
568 x->x_ypos+3*((x->x_blocksize/2*x->x_zoom)/2)+2, x, x );
569 }
570 canvas_fixlinesfor( canvas, (t_text*)x );
571 }
572 // set borders in red
573 SYS_VGUI3(".x%lx.c itemconfigure %xSONOGRAM -outline #FF0000\n", canvas, x);
574 if ( x->x_phaso )
575 {
576 SYS_VGUI3(".x%lx.c itemconfigure %xPHASOGRAM -outline #FF0000\n", canvas, x);
577 }
578
579 // launch update thread
580 if ( pthread_attr_init( &update_child_attr ) < 0 )
581 {
582 post( "sonogram~ : could not launch update thread" );
583 perror( "pthread_attr_init" );
584 return;
585 }
586 if ( pthread_attr_setdetachstate( &update_child_attr, PTHREAD_CREATE_DETACHED ) < 0 )
587 {
588 post( "sonogram~ : could not launch update thread" );
589 perror( "pthread_attr_setdetachstate" );
590 return;
591 }
592 if ( pthread_create( &x->x_updatechild, &update_child_attr, sonogram_do_update_part, x ) < 0 )
593 {
594 post( "sonogram~ : could not launch update thread" );
595 perror( "pthread_create" );
596 return;
597 }
598 else
599 {
600 // post( "sonogram~ : drawing thread %d launched", (int)x->x_updatechild );
601 }
602 }
603 }
604
605 /* paste selection at the drawing point */
606 static void sonogram_paste( t_sonogram* x)
607 {
608 t_int pxstart = (x->x_xdraw-x->x_xpos)/x->x_zoom;
609 t_int pystart = (x->x_ypos-x->x_ydraw)/x->x_zoom+x->x_blocksize/2;
610 t_int cxs,cxe,cys,cye,si=0,fi=0;
611 t_float *icopy;
612 t_float *rcopy;
613 t_int copynd;
614
615 if ( x->x_xstartcapture > x->x_xendcapture )
616 {
617 fi = x->x_xstartcapture;
618 x->x_xstartcapture = x->x_xendcapture;
619 x->x_xendcapture = fi;
620 }
621 if ( x->x_ystartcapture > x->x_yendcapture )
622 {
623 fi = x->x_ystartcapture;
624 x->x_ystartcapture = x->x_yendcapture;
625 x->x_yendcapture = fi;
626 }
627 cxs=(x->x_xstartcapture-x->x_xpos)/x->x_zoom;
628 cxe=(x->x_xendcapture-x->x_xpos)/x->x_zoom;
629 cys=(x->x_ypos-x->x_yendcapture)/x->x_zoom+x->x_blocksize/2;
630 cye=(x->x_ypos-x->x_ystartcapture)/x->x_zoom+x->x_blocksize/2;
631 if ( cye < 0 ) cye=0;
632 if ( cys < 0 ) cys=0;
633 if ( cye >= x->x_blocksize/2 ) cye=x->x_blocksize/2-1;
634 if ( cys >= x->x_blocksize/2 ) cys=x->x_blocksize/2-1;
635 if ( cxe >= x->x_size ) cxe=x->x_size-1;
636 if ( cxs >= x->x_size ) cxs=x->x_size-1;
637
638 // make a copy first
639 icopy = ( t_float* ) getbytes( ( cxe-cxs+1 )*( cye-cys+1 )*sizeof( t_float ) );
640 rcopy = ( t_float* ) getbytes( ( cxe-cxs+1 )*( cye-cys+1 )*sizeof( t_float ) );
641 if ( !icopy || !rcopy )
642 {
643 post( "sonogram~ : cannot allocate buffers for pasting" );
644 return;
645 }
646 // copy initial data
647 copynd = 0;
648 for ( si=cxs; si<=cxe; si++)
649 {
650 for ( fi=cys; fi<=cye; fi++)
651 {
652 *(rcopy+copynd) = *(x->x_rdata+(si)*x->x_blocksize+fi);
653 *(icopy+copynd) = *(x->x_idata+(si)*x->x_blocksize+fi);
654 copynd++;
655 }
656 }
657
658 post( "sonogram~ : paste from [%d,%d,%d,%d] to [%d,%d]", cxs, cys, cxe, cye, pxstart, pystart );
659
660 for ( si=cxs; si<=cxe; si++)
661 {
662 if ( pxstart+si-cxs >= x->x_size ) break;
663 copynd = (si-cxs)*(cye-cys+1);
664 for ( fi=cys; fi<=cye; fi++)
665 {
666 // post ( "sonogram~ : si : %d : fi : %d : copynd : %d", si, fi, copynd );
667 if ( pystart+fi-cys >= x->x_blocksize/2 ) break;
668 *(x->x_rudata+((si-cxs)*x->x_blocksize)+(fi-cys)) = *(x->x_rdata+((pxstart+si-cxs)*x->x_blocksize)+(pystart+fi-cys));
669 *(x->x_iudata+((si-cxs)*x->x_blocksize)+(fi-cys)) = *(x->x_idata+((pxstart+si-cxs)*x->x_blocksize)+(pystart+fi-cys));
670 if ( x->x_enhancemode )
671 {
672 // save data for undo
673 *(x->x_rdata+((pxstart+si-cxs)*x->x_blocksize)+(pystart+fi-cys)) += *(rcopy+copynd);
674 *(x->x_idata+((pxstart+si-cxs)*x->x_blocksize)+(pystart+fi-cys)) += *(icopy+copynd);
675 }
676 else
677 {
678 *(x->x_rdata+((pxstart+si-cxs)*x->x_blocksize)+(pystart+fi-cys)) = *(rcopy+copynd);
679 *(x->x_idata+((pxstart+si-cxs)*x->x_blocksize)+(pystart+fi-cys)) = *(icopy+copynd);
680 }
681 copynd++;
682 }
683 }
684
685 x->x_uxs = pxstart;
686 x->x_uxe = pxstart+(si-1)-cxs;
687 x->x_uys = pystart;
688 x->x_uye = pystart+(fi-1)-cys;;
689
690 freebytes( rcopy, ( cxe-cxs+1 )*( cye-cys+1 )*sizeof( t_float ) );
691 freebytes( icopy, ( cxe-cxs+1 )*( cye-cys+1 )*sizeof( t_float ) );
692 }
693
694 /* paste phase at the drawing point */
695 static void sonogram_paste_phase( t_sonogram* x)
696 {
697 t_int pxstart = (x->x_xdraw-x->x_xpos)/x->x_zoom;
698 t_int pystart = (x->x_ypos+x->x_blocksize/2*x->x_zoom+1-x->x_ydraw)/x->x_zoom+x->x_blocksize/2;
699 t_int cxs,cxe,cys,cye,si,fi;
700 t_float fspectrum, fdestspectrum;
701 t_float fphase, fdestphase;
702 t_float *icopy;
703 t_float *rcopy;
704 t_int copynd;
705 t_canvas *canvas=glist_getcanvas(x->x_glist);
706
707
708 if ( x->x_xstartcapture > x->x_xendcapture )
709 {
710 fi = x->x_xstartcapture;
711 x->x_xstartcapture = x->x_xendcapture;
712 x->x_xendcapture = fi;
713 }
714 if ( x->x_ystartcapture > x->x_yendcapture )
715 {
716 fi = x->x_ystartcapture;
717 x->x_ystartcapture = x->x_yendcapture;
718 x->x_yendcapture = fi;
719 }
720 cxs=(x->x_xstartcapture-x->x_xpos)/x->x_zoom;
721 cxe=(x->x_xendcapture-x->x_xpos)/x->x_zoom;
722 cys=(x->x_ypos+x->x_blocksize/2*x->x_zoom+1-x->x_yendcapture)/x->x_zoom+x->x_blocksize/2;
723 cye=(x->x_ypos+x->x_blocksize/2*x->x_zoom+1-x->x_ystartcapture)/x->x_zoom+x->x_blocksize/2;
724 if ( cye < 0 ) cye=0;
725 if ( cys < 0 ) cys=0;
726 if ( cye >= x->x_blocksize/2 ) cye=x->x_blocksize/2-1;
727 if ( cys >= x->x_blocksize/2 ) cys=x->x_blocksize/2-1;
728 if ( cxe >= x->x_size ) cxe=x->x_size-1;
729 if ( cxs >= x->x_size ) cxs=x->x_size-1;
730
731 // make a copy first
732 icopy = ( t_float* ) getbytes( ( cxe-cxs+1 )*( cye-cys+1 )*sizeof( t_float ) );
733 rcopy = ( t_float* ) getbytes( ( cxe-cxs+1 )*( cye-cys+1 )*sizeof( t_float ) );
734 if ( !icopy || !rcopy )
735 {
736 post( "sonogram~ : cannot allocate buffers for pasting" );
737 return;
738 }
739 // copy initial data
740 copynd = 0;
741 for ( si=cxs; si<=cxe; si++)
742 {
743 for ( fi=cys; fi<=cye; fi++)
744 {
745 *(rcopy+copynd) = *(x->x_rdata+(si)*x->x_blocksize+fi);
746 *(icopy+copynd) = *(x->x_idata+(si)*x->x_blocksize+fi);
747 copynd++;
748 }
749 }
750
751 post( "sonogram~ : paste phase from [%d,%d,%d,%d] to [%d,%d]", cxs, cys, cxe, cye, pxstart, pystart );
752
753 for ( si=cxs; si<=cxe; si++)
754 {
755 if ( pxstart+si-cxs >= x->x_size ) break;
756 copynd = (si-cxs)*(cye-cys+1);
757 for ( fi=cys; fi<=cye; fi++)
758 {
759 if ( pystart+fi-cys > x->x_blocksize+1 ) break;
760 fphase = atan2( *(icopy+copynd), *(rcopy+copynd) );
761 fdestspectrum =
762 sqrt( pow( *(x->x_rdata+(pxstart+si-cxs)*x->x_blocksize+(pystart+fi-cys)), 2) +
763 pow( *(x->x_idata+(pxstart+si-cxs)*x->x_blocksize+(pystart+fi-cys)), 2) );
764 fdestphase = atan2( *(x->x_idata+(pxstart+si-cxs)*x->x_blocksize+(pystart+fi-cys)),
765 *(x->x_rdata+(pxstart+si-cxs)*x->x_blocksize+(pystart+fi-cys)) );
766 if ( x->x_enhancemode )
767 {
768 *(x->x_rdata+((pxstart+si-cxs)*x->x_blocksize)+(pystart+fi-cys)) +=
769 fdestspectrum*cos( fdestphase + fphase );
770 *(x->x_idata+((pxstart+si-cxs)*x->x_blocksize)+(pystart+fi-cys)) +=
771 fdestspectrum*sin( fdestphase + fphase );
772 }
773 else
774 {
775 *(x->x_rdata+((pxstart+si-cxs)*x->x_blocksize)+(pystart+fi-cys)) =
776 fdestspectrum*cos( fphase );
777 *(x->x_idata+((pxstart+si-cxs)*x->x_blocksize)+(pystart+fi-cys)) =
778 fdestspectrum*sin( fphase );
779 }
780 copynd++;
781 }
782 }
783
784 freebytes( rcopy, ( cxe-cxs+1 )*( cye-cys+1 )*sizeof( t_float ) );
785 freebytes( icopy, ( cxe-cxs+1 )*( cye-cys+1 )*sizeof( t_float ) );
786
787 sonogram_update_part(x, x->x_glist, pxstart, pxstart+(si-1)-cxs, 0, 1, 1);
788 // start a new capture
789 SYS_VGUI3( ".x%lx.c delete %xCAPTURE\n", canvas, x );
790 x->x_xstartcapture = x->x_xdraw;
791 x->x_ystartcapture = x->x_ydraw;
792 x->x_xendcapture = x->x_xdraw;
793 x->x_yendcapture = x->x_ydraw;
794
795 }
796
797 static void sonogram_draw_new(t_sonogram *x, t_glist *glist)
798 {
799 t_canvas *canvas=glist_getcanvas(glist);
800
801 x->x_xpos=text_xpix(&x->x_obj, glist);
802 x->x_ypos=text_ypix(&x->x_obj, glist);
803 if ( x->x_graphic )
804 {
805 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xSONOGRAM\n",
806 canvas, x->x_xpos-1, x->x_ypos-1,
807 x->x_xpos + x->x_size*x->x_zoom+1,
808 x->x_ypos + x->x_blocksize/2*x->x_zoom+1,
809 x);
810 SYS_VGUI4("image create photo SONIMAGE%x -format gif -width %d -height %d\n",
811 x, x->x_size*x->x_zoom, x->x_blocksize/2*x->x_zoom );
812 SYS_VGUI2("SONIMAGE%x blank\n", x);
813 SYS_VGUI6(".x%lx.c create image %d %d -image SONIMAGE%x -tags ISONIMAGE%x\n",
814 canvas, x->x_xpos+((x->x_size*x->x_zoom)/2),
815 (x->x_ypos+((x->x_blocksize/2*x->x_zoom)/2)), x, x );
816 if ( x->x_phaso )
817 {
818 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xPHASOGRAM\n",
819 canvas, x->x_xpos-1, x->x_ypos+x->x_blocksize/2*x->x_zoom+2,
820 x->x_xpos + x->x_size*x->x_zoom +1,
821 x->x_ypos + x->x_blocksize*x->x_zoom + 3,
822 x);
823 SYS_VGUI4("image create photo FAZIMAGE%x -format gif -width %d -height %d\n",
824 x, x->x_size*x->x_zoom, x->x_blocksize/2*x->x_zoom );
825 SYS_VGUI2("FAZIMAGE%x blank\n", x);
826 SYS_VGUI6(".x%lx.c create image %d %d -image FAZIMAGE%x -tags IFAZIMAGE%x\n",
827 canvas, x->x_xpos+((x->x_size*x->x_zoom)/2),
828 x->x_ypos+3*((x->x_blocksize/2*x->x_zoom)/2)+2, x, x );
829 }
830 canvas_fixlinesfor( canvas, (t_text*)x );
831 }
832 }
833
834 static void sonogram_draw_delete(t_sonogram *x, t_glist *glist)
835 {
836 t_canvas *canvas=glist_getcanvas(glist);
837
838 if ( x->x_graphic && glist_isvisible( glist ) )
839 {
840 SYS_VGUI3( ".x%lx.c delete %xCAPTURE\n", canvas, x );
841 SYS_VGUI3( ".x%lx.c delete line %xREADSTART\n", canvas, x);
842 SYS_VGUI3( ".x%lx.c delete line %xREADEND\n", canvas, x);
843 SYS_VGUI3( ".x%lx.c delete line %xMODSTART\n", canvas, x);
844 SYS_VGUI3( ".x%lx.c delete line %xMODEND\n", canvas, x);
845 SYS_VGUI3(".x%lx.c delete %xSONOGRAM\n", canvas, x );
846 SYS_VGUI3(".x%lx.c delete %xPHASOGRAM\n", canvas, x );
847 SYS_VGUI3(".x%lx.c delete %xISONIMAGE\n", canvas, x );
848 SYS_VGUI2("image delete SONIMAGE%x\n", x );
849 if ( x->x_phaso )
850 {
851 SYS_VGUI3(".x%lx.c delete %xIFAZIMAGE\n", canvas, x );
852 SYS_VGUI2("image delete FAZIMAGE%x\n", x );
853 }
854 }
855 }
856
857 static void sonogram_draw_move(t_sonogram *x, t_glist *glist)
858 {
859 t_canvas *canvas=glist_getcanvas(glist);
860
861 if ( x->x_graphic && glist_isvisible( x->x_glist ) )
862 {
863 SYS_VGUI7(".x%lx.c coords %xSONOGRAM %d %d %d %d\n",
864 canvas, x,
865 x->x_xpos-1, x->x_ypos-1,
866 x->x_xpos+x->x_size*x->x_zoom+1,
867 x->x_ypos+x->x_blocksize/2*x->x_zoom+1);
868 SYS_VGUI5(".x%lx.c coords ISONIMAGE%x %d %d\n",
869 canvas, x,
870 x->x_xpos+((x->x_size*x->x_zoom)/2),
871 (x->x_ypos+((x->x_blocksize/2*x->x_zoom)/2)) );
872 if ( x->x_phaso )
873 {
874 SYS_VGUI7(".x%lx.c coords %xPHASOGRAM %d %d %d %d\n",
875 canvas, x,
876 x->x_xpos-1, x->x_ypos+(x->x_blocksize/2*x->x_zoom)+1,
877 x->x_xpos+x->x_size*x->x_zoom+1,
878 x->x_ypos+x->x_blocksize*x->x_zoom+3);
879 SYS_VGUI5(".x%lx.c coords IFAZIMAGE%x %d %d\n",
880 canvas, x,
881 x->x_xpos+((x->x_size*x->x_zoom)/2),
882 x->x_ypos+3*((x->x_blocksize/2*x->x_zoom)/2)+2 );
883 }
884 canvas_fixlinesfor( canvas, (t_text*)x );
885 }
886 }
887
888 static void sonogram_draw_select(t_sonogram* x,t_glist* glist)
889 {
890 t_canvas *canvas=glist_getcanvas(glist);
891
892 if ( x->x_graphic && glist_isvisible( x->x_glist ) )
893 {
894 if(x->x_selected)
895 {
896 /* sets the item in blue */
897 SYS_VGUI3(".x%lx.c itemconfigure %xSONOGRAM -outline #0000FF\n", canvas, x);
898 if ( x->x_phaso )
899 {
900 SYS_VGUI3(".x%lx.c itemconfigure %xPHASOGRAM -outline #0000FF\n", canvas, x);
901 }
902 }
903 else
904 {
905 SYS_VGUI3(".x%lx.c itemconfigure %xSONOGRAM -outline #000000\n", canvas, x);
906 if ( x->x_phaso )
907 {
908 SYS_VGUI3(".x%lx.c itemconfigure %xPHASOGRAM -outline #000000\n", canvas, x);
909 }
910 }
911 }
912 }
913
914 /* ------------------------ widget callbacks ----------------------------- */
915
916
917 static void sonogram_getrect(t_gobj *z, t_glist *owner,
918 int *xp1, int *yp1, int *xp2, int *yp2)
919 {
920 t_sonogram* x = (t_sonogram*)z;
921
922 *xp1 = x->x_xpos;
923 *yp1 = x->x_ypos;
924 if ( !x->x_phaso )
925 {
926 *xp2 = x->x_xpos+x->x_size*x->x_zoom;
927 *yp2 = x->x_ypos+x->x_blocksize/2*x->x_zoom+1;
928 }
929 else
930 {
931 *xp2 = x->x_xpos+x->x_size*x->x_zoom;
932 *yp2 = x->x_ypos+x->x_blocksize*x->x_zoom+3;
933 }
934 }
935
936 static void sonogram_save(t_gobj *z, t_binbuf *b)
937 {
938 t_sonogram *x = (t_sonogram *)z;
939
940 binbuf_addv(b, "ssiisiii", gensym("#X"),gensym("obj"),
941 (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix,
942 atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
943 x->x_size, x->x_graphic, x->x_phaso );
944 binbuf_addv(b, ";");
945 }
946
947 static void sonogram_select(t_gobj *z, t_glist *glist, int selected)
948 {
949 t_sonogram *x = (t_sonogram *)z;
950
951 x->x_selected = selected;
952 sonogram_draw_select( x, glist );
953 }
954
955 static void sonogram_vis(t_gobj *z, t_glist *glist, int vis)
956 {
957 t_sonogram *x = (t_sonogram *)z;
958 t_rtext *y;
959
960 if (vis)
961 {
962 sonogram_draw_new( x, glist );
963 }
964 else
965 {
966 // erase all points
967 sonogram_draw_delete( x, glist );
968 }
969 }
970
971 static void sonogram_delete(t_gobj *z, t_glist *glist)
972 {
973 canvas_deletelinesfor(glist, (t_text *)z);
974 }
975
976 static void sonogram_displace(t_gobj *z, t_glist *glist, int dx, int dy)
977 {
978 t_sonogram *x = (t_sonogram *)z;
979 int xold = x->x_xpos;
980 int yold = x->x_ypos;
981
982 x->x_xpos += dx;
983 x->x_ypos += dy;
984
985 if ( ( x->x_xpos != xold ) || ( x->x_ypos != yold ) )
986 {
987 sonogram_draw_move( x, glist );
988 }
989
990 }
991
992 static void sonogram_modify_point( t_sonogram* x, t_int sample, t_int frequency, t_int alted )
993 {
994 if ( alted )
995 {
996 *(x->x_rdata+(sample*x->x_blocksize)+frequency) = 0;
997 *(x->x_idata+(sample*x->x_blocksize)+frequency) = 0;
998 }
999 else
1000 {
1001 if ( x->x_enhancemode )
1002 {
1003 *(x->x_rdata+(sample*x->x_blocksize)+frequency) *= x->x_modstep;
1004 *(x->x_idata+(sample*x->x_blocksize)+frequency) *= x->x_modstep;
1005 }
1006 else
1007 {
1008 *(x->x_rdata+(sample*x->x_blocksize)+frequency) += x->x_modstep;
1009 *(x->x_idata+(sample*x->x_blocksize)+frequency) += x->x_modstep;
1010 }
1011 }
1012 }
1013
1014 static void sonogram_modify_point_phase( t_sonogram* x, t_int sample, t_int frequency, t_int alted )
1015 {
1016 t_float fspectrum;
1017 t_float fphase;
1018
1019 fspectrum =
1020 sqrt( pow( *(x->x_rdata+sample*x->x_blocksize+frequency), 2) +
1021 pow( *(x->x_idata+sample*x->x_blocksize+frequency), 2) );
1022 fphase = atan2( *(x->x_idata+sample*x->x_blocksize+frequency),
1023 *(x->x_rdata+sample*x->x_blocksize+frequency) );
1024 if ( alted==4 )
1025 {
1026 // setting phase to 0
1027 *(x->x_rdata+(sample*x->x_blocksize)+frequency) = fspectrum;
1028 *(x->x_idata+(sample*x->x_blocksize)+frequency) = 0;
1029 }
1030 else
1031 {
1032 if ( x->x_enhancemode )
1033 {
1034 *(x->x_rdata+(sample*x->x_blocksize)+frequency) = fspectrum*cos( fphase*x->x_modstep );
1035 *(x->x_idata+(sample*x->x_blocksize)+frequency) = fspectrum*sin( fphase*x->x_modstep );
1036 }
1037 else
1038 {
1039 *(x->x_rdata+(sample*x->x_blocksize)+frequency) = fspectrum*cos( fphase+x->x_modstep );
1040 *(x->x_idata+(sample*x->x_blocksize)+frequency) = fspectrum*sin( fphase+x->x_modstep );
1041 }
1042 }
1043 }
1044
1045 static void sonogram_motion(t_sonogram *x, t_floatarg dx, t_floatarg dy)
1046 {
1047 t_int fdraw=0, sdraw=0;
1048 t_canvas *canvas=glist_getcanvas(x->x_glist);
1049
1050 // post( "sonogram_motion @ [%d,%d] dx=%f dy=%f alt=%d", x->x_xdraw, x->x_ydraw, dx, dy, x->x_alted );
1051 if ( ( x->x_shifted || (x->x_alted==4) ) )
1052 {
1053 if ( (x->x_xdraw+dx) >= x->x_xpos &&
1054 (x->x_xdraw+dx) <= x->x_xpos+x->x_size*x->x_zoom )
1055 {
1056 x->x_xdraw += dx;
1057 }
1058 if ( (x->x_ydraw+dy) >= x->x_ypos &&
1059 (x->x_ydraw+dy) <= x->x_ypos+x->x_blocksize*x->x_zoom )
1060 {
1061 x->x_ydraw += dy;
1062 }
1063 sdraw=(x->x_xdraw-x->x_xpos)/x->x_zoom;
1064 if ( x->x_ydraw <= x->x_ypos+x->x_blocksize/2*x->x_zoom )
1065 {
1066 fdraw=(x->x_ypos-x->x_ydraw)/x->x_zoom+x->x_blocksize/2;
1067 // post( "modify point @ [%d, %d] alted=%d", sdraw, fdraw, x->x_alted );
1068 sonogram_modify_point( x, sdraw, fdraw, x->x_alted );
1069 }
1070 if ( x->x_ydraw >= x->x_ypos+x->x_blocksize/2*x->x_zoom+1 )
1071 {
1072 fdraw=(x->x_ypos+x->x_blocksize*x->x_zoom/2+1-x->x_ydraw)/x->x_zoom+x->x_blocksize/2;
1073 // post( "modify phase @ [%d, %d]", sdraw, fdraw );
1074 sonogram_modify_point_phase( x, sdraw, fdraw, x->x_alted );
1075 }
1076 sonogram_update_point( x, x->x_glist, sdraw, fdraw );
1077 }
1078 else
1079 {
1080 if ( (x->x_xendcapture+dx) >= x->x_xpos &&
1081 (x->x_xendcapture+dx) <= x->x_xpos+x->x_size*x->x_zoom )
1082 {
1083 x->x_xendcapture += dx;
1084 }
1085 if ( (x->x_yendcapture+dy) >= x->x_ypos &&
1086 (x->x_yendcapture+dy) <= x->x_ypos+x->x_blocksize*x->x_zoom )
1087 {
1088 x->x_yendcapture += dy;
1089 }
1090 SYS_VGUI3( ".x%lx.c delete %xCAPTURE\n", canvas, x );
1091 SYS_VGUI7( ".x%lx.c create rectangle %d %d %d %d -outline #0000FF -tags %xCAPTURE\n",
1092 canvas, x->x_xstartcapture, x->x_ystartcapture, x->x_xendcapture, x->x_yendcapture, x );
1093 }
1094 }
1095
1096 static int sonogram_click(t_gobj *z, struct _glist *glist,
1097 int xpix, int ypix, int shift, int alt, int dbl, int doit)
1098 {
1099 t_sonogram* x = (t_sonogram *)z;
1100 t_int si,fi;
1101 t_canvas *canvas=glist_getcanvas(x->x_glist);
1102
1103
1104 // post( "sonogram_click : x=%d y=%d doit=%d alt=%d, shift=%d", xpix, ypix, doit, alt, shift );
1105 if ( x->x_aftermousedown == 1 && doit == 0)
1106 {
1107 x->x_aftermousedown = 1;
1108 }
1109 else
1110 {
1111 x->x_aftermousedown = 0;
1112 }
1113 if ( doit )
1114 {
1115 x->x_xdraw = xpix;
1116 x->x_ydraw = ypix;
1117 x->x_shifted = shift;
1118 x->x_alted = alt;
1119 // activate motion callback
1120 glist_grab( glist, &x->x_obj.te_g, (t_glistmotionfn)sonogram_motion,
1121 0, xpix, ypix );
1122
1123 if ( shift && alt && (x->x_xstartcapture != x->x_xendcapture ) )
1124 {
1125 sonogram_paste(x);
1126 sonogram_paste_phase(x);
1127 }
1128 else if ( shift && (x->x_xstartcapture != x->x_xendcapture ) )
1129 {
1130 // add or multiply modstep
1131 if ( x->x_xstartcapture > x->x_xendcapture )
1132 {
1133 fi = x->x_xstartcapture;
1134 x->x_xstartcapture = x->x_xendcapture;
1135 x->x_xendcapture = fi;
1136 }
1137 if ( x->x_ystartcapture > x->x_yendcapture )
1138 {
1139 fi = x->x_ystartcapture;
1140 x->x_ystartcapture = x->x_yendcapture;
1141 x->x_yendcapture = fi;
1142 }
1143 for ( si=(x->x_xstartcapture-x->x_xpos)/x->x_zoom;
1144 si<=(x->x_xendcapture-x->x_xpos)/x->x_zoom; si++)
1145 {
1146 for ( fi=(x->x_ypos-x->x_yendcapture)/x->x_zoom+x->x_blocksize/2;
1147 fi<=(x->x_ypos-x->x_ystartcapture)/x->x_zoom+x->x_blocksize/2; fi++)
1148 {
1149 sonogram_modify_point( x, si, fi, alt );
1150 }
1151 for ( fi=(x->x_ypos+x->x_blocksize/2*x->x_zoom+1-x->x_yendcapture)/x->x_zoom+x->x_blocksize/2;
1152 fi<=(x->x_ypos+x->x_blocksize/2*x->x_zoom+1-x->x_ystartcapture)/x->x_zoom+x->x_blocksize/2; fi++)
1153 {
1154 sonogram_modify_point_phase( x, si, fi, alt );
1155 }
1156 }
1157 // post( "modified y from %d to %d", (x->x_ypos-x->x_yendcapture)/x->x_zoom+x->x_blocksize/2,
1158 // (x->x_ypos-x->x_ystartcapture)/x->x_zoom+x->x_blocksize/2 );
1159 sonogram_update_part(x, x->x_glist, (x->x_xstartcapture-x->x_xpos)/x->x_zoom,
1160 (x->x_xendcapture-x->x_xpos)/x->x_zoom, 0, 1, 1);
1161 }
1162 else if ( (alt==4) && (x->x_xstartcapture != x->x_xendcapture ) )
1163 {
1164 // clean up area
1165 if ( x->x_xstartcapture > x->x_xendcapture )
1166 {
1167 fi = x->x_xstartcapture;
1168 x->x_xstartcapture = x->x_xendcapture;
1169 x->x_xendcapture = fi;
1170 }
1171 if ( x->x_ystartcapture > x->x_yendcapture )
1172 {
1173 fi = x->x_ystartcapture;
1174 x->x_ystartcapture = x->x_yendcapture;
1175 x->x_yendcapture = fi;
1176 }
1177 for ( si=(x->x_xstartcapture-x->x_xpos)/x->x_zoom;
1178 si<=(x->x_xendcapture-x->x_xpos)/x->x_zoom; si++)
1179 {
1180 for ( fi=(x->x_ypos-x->x_yendcapture)/x->x_zoom+x->x_blocksize/2;
1181 fi<=(x->x_ypos-x->x_ystartcapture)/x->x_zoom+x->x_blocksize/2; fi++)
1182 {
1183 sonogram_modify_point( x, si, fi, alt );
1184 }
1185 for ( fi=(x->x_ypos+x->x_blocksize/2*x->x_zoom+1-x->x_yendcapture)/x->x_zoom+x->x_blocksize/2;
1186 fi<=(x->x_ypos+x->x_blocksize/2*x->x_zoom+1-x->x_ystartcapture)/x->x_zoom+x->x_blocksize/2; fi++)
1187 {
1188 sonogram_modify_point_phase( x, si, fi, alt );
1189 }
1190 }
1191 sonogram_update_part(x, x->x_glist, (x->x_xstartcapture-x->x_xpos)/x->x_zoom,
1192 (x->x_xendcapture-x->x_xpos)/x->x_zoom, 0, 1, 1);
1193 }
1194 // start a new capture
1195 SYS_VGUI3( ".x%lx.c delete %xCAPTURE\n", canvas, x );
1196 x->x_xstartcapture = xpix;
1197 x->x_ystartcapture = ypix;
1198 x->x_xendcapture = xpix;
1199 x->x_yendcapture = ypix;
1200 }
1201 else
1202 {
1203 // nothing
1204 }
1205 x->x_aftermousedown = doit;
1206 return (1);
1207 }
1208
1209 /* clean up */
1210 static void sonogram_free(t_sonogram *x)
1211 {
1212 if ( x->x_rdata != NULL )
1213 {
1214 freebytes(x->x_rdata, x->x_size*x->x_blocksize*sizeof(float) );
1215 post( "Freed %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
1216 x->x_rdata = NULL;
1217 }
1218 if ( x->x_idata != NULL )
1219 {
1220 freebytes(x->x_idata, x->x_size*x->x_blocksize*sizeof(float) );
1221 post( "Freed %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
1222 x->x_idata = NULL;
1223 }
1224 if ( x->x_rudata != NULL )
1225 {
1226 freebytes(x->x_rudata, x->x_size*x->x_blocksize*sizeof(float) );
1227 post( "Freed %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
1228 x->x_rdata = NULL;
1229 }
1230 if ( x->x_iudata != NULL )
1231 {
1232 freebytes(x->x_iudata, x->x_size*x->x_blocksize*sizeof(float) );
1233 post( "Freed %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
1234 x->x_idata = NULL;
1235 }
1236 if ( x->x_gifdata != NULL )
1237 {
1238 freebytes(x->x_gifdata, (x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1239 post( "Freed %d bytes", (x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1240 x->x_gifdata = NULL;
1241 }
1242 if ( x->x_guicommand != NULL )
1243 {
1244 freebytes(x->x_guicommand, 128+(x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1245 post( "Freed %d bytes", 128+(x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1246 x->x_guicommand = NULL;
1247 }
1248 }
1249
1250 /* allocate tables for storing ffts */
1251 static t_int sonogram_allocate(t_sonogram *x)
1252 {
1253 t_int fi;
1254
1255 if ( !(x->x_rdata = getbytes( x->x_size*x->x_blocksize*sizeof(float) ) ) )
1256 {
1257 post( "sonogram~ : error : could not allocate buffers" );
1258 return -1;
1259 }
1260 else
1261 {
1262 post( "sonogram~ : allocated %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
1263 }
1264 if ( !(x->x_idata = getbytes( x->x_size*x->x_blocksize*sizeof(float) ) ) )
1265 {
1266 post( "sonogram~ : error : could not allocate buffers" );
1267 return -1;
1268 }
1269 else
1270 {
1271 post( "sonogram~ : allocated %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
1272 }
1273 if ( !(x->x_multfreq = getbytes( x->x_blocksize*sizeof(t_int) ) ) )
1274 {
1275 post( "sonogram~ : error : could not allocate buffers" );
1276 return -1;
1277 }
1278 else
1279 {
1280 post( "sonogram~ : allocated %d bytes", x->x_blocksize*sizeof(t_int) );
1281 }
1282 for ( fi=0; fi<x->x_blocksize; fi++ )
1283 {
1284 *(x->x_multfreq+fi)=1;
1285 }
1286 // no undo is available
1287 x->x_uxs = x->x_uxe = x->x_uys = x->x_uye = -1;
1288 if ( !(x->x_rudata = getbytes( x->x_size*x->x_blocksize*sizeof(float) ) ) )
1289 {
1290 post( "sonogram~ : error : could not allocate buffers" );
1291 return -1;
1292 }
1293 else
1294 {
1295 post( "sonogram~ : allocated %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
1296 }
1297 if ( !(x->x_iudata = getbytes( x->x_size*x->x_blocksize*sizeof(float) ) ) )
1298 {
1299 post( "sonogram~ : error : could not allocate buffers" );
1300 return -1;
1301 }
1302 else
1303 {
1304 post( "sonogram~ : allocated %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
1305 }
1306 if ( !( x->x_gifdata = ( char* ) getbytes( (x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") ) ) )
1307 {
1308 post( "sonogram~ : error : could not allocate buffers" );
1309 return -1;
1310 }
1311 else
1312 {
1313 post( "sonogram~ : allocated %d bytes", (x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1314 }
1315 if ( !( x->x_guicommand = ( char* ) getbytes( 128+(x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") ) ) )
1316 {
1317 post( "sonogram~ : error : could not allocate buffers" );
1318 return -1;
1319 }
1320 else
1321 {
1322 post( "sonogram~ : allocated %d bytes", 128+(x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1323 }
1324
1325 return 0;
1326 }
1327
1328 /* reallocate tables for storing ffts */
1329 static t_int sonogram_reallocate(t_sonogram *x, t_int ioldsize, t_int inewsize)
1330 {
1331 t_int fi;
1332 t_float *prdata=x->x_rdata, *pidata=x->x_idata;
1333 t_float *prudata=x->x_rudata, *piudata=x->x_iudata;
1334
1335 if ( !(x->x_rdata = getbytes( inewsize*x->x_blocksize*sizeof(float) ) ) )
1336 {
1337 post( "sonogram~ : error : could not allocate buffers" );
1338 return -1;
1339 }
1340 else
1341 {
1342 post( "sonogram~ : allocated %d bytes", inewsize*x->x_blocksize*sizeof(float) );
1343 }
1344 if ( !(x->x_idata = getbytes( inewsize*x->x_blocksize*sizeof(float) ) ) )
1345 {
1346 post( "sonogram~ : error : could not allocate buffers" );
1347 return -1;
1348 }
1349 else
1350 {
1351 post( "sonogram~ : allocated %d bytes", inewsize*x->x_blocksize*sizeof(float) );
1352 }
1353 // no undo is available
1354 x->x_uxs = x->x_uxe = x->x_uys = x->x_uye = -1;
1355 if ( !(x->x_rudata = getbytes( inewsize*x->x_blocksize*sizeof(float) ) ) )
1356 {
1357 post( "sonogram~ : error : could not allocate buffers" );
1358 return -1;
1359 }
1360 else
1361 {
1362 post( "sonogram~ : allocated %d bytes", inewsize*x->x_blocksize*sizeof(float) );
1363 }
1364 if ( !(x->x_iudata = getbytes( inewsize*x->x_blocksize*sizeof(float) ) ) )
1365 {
1366 post( "sonogram~ : error : could not allocate buffers" );
1367 return -1;
1368 }
1369 else
1370 {
1371 post( "sonogram~ : allocated %d bytes", inewsize*x->x_blocksize*sizeof(float) );
1372 }
1373 if ( prdata != NULL )
1374 {
1375 freebytes(prdata, ioldsize*x->x_blocksize*sizeof(float) );
1376 post( "Freed %d bytes", ioldsize*x->x_blocksize*sizeof(float) );
1377 }
1378 if ( pidata != NULL )
1379 {
1380 freebytes(pidata, ioldsize*x->x_blocksize*sizeof(float) );
1381 post( "Freed %d bytes", ioldsize*x->x_blocksize*sizeof(float) );
1382 }
1383 if ( prudata != NULL )
1384 {
1385 freebytes(prudata, ioldsize*x->x_blocksize*sizeof(float) );
1386 post( "Freed %d bytes", ioldsize*x->x_blocksize*sizeof(float) );
1387 }
1388 if ( piudata != NULL )
1389 {
1390 freebytes(piudata, ioldsize*x->x_blocksize*sizeof(float) );
1391 post( "Freed %d bytes", ioldsize*x->x_blocksize*sizeof(float) );
1392 }
1393
1394 return 0;
1395 }
1396 /* records or playback the sonogram */
1397 static t_int *sonogram_perform(t_int *w)
1398 {
1399 t_float *rin = (t_float *)(w[1]);
1400 t_float *iin = (t_float *)(w[2]);
1401 t_float *rout = (t_float *)(w[3]);
1402 t_float *iout = (t_float *)(w[4]);
1403 t_float fspectrum = 0.0;
1404 t_float fphase = 0.0;
1405 t_int is;
1406 t_int n = (int)(w[5]); /* number of samples */
1407 t_sonogram *x = (t_sonogram *)(w[6]);
1408 t_int bi;
1409 t_int startsamp, endsamp;
1410
1411 if ( x->x_readstart <= x->x_readend )
1412 {
1413 startsamp = (x->x_readstart*x->x_size)/100;
1414 endsamp = (x->x_readend*x->x_size)/100;
1415 }
1416 else
1417 {
1418 startsamp = (x->x_readend*x->x_size)/100;
1419 endsamp = (x->x_readstart*x->x_size)/100;
1420 }
1421
1422 // reallocate tables if blocksize has been changed
1423 if ( n != x->x_blocksize && x->x_updatechild == 0 )
1424 {
1425 post( "sonogram~ : reallocating tables" );
1426 // erase all points
1427 sonogram_free(x);
1428 x->x_blocksize = n;
1429 sonogram_allocate(x);
1430 sonogram_update_part(x, x->x_glist, 0, x->x_size-1, !x->x_empty, 0, 0);
1431 canvas_fixlinesfor(x->x_glist, (t_text*)x );
1432 }
1433
1434 bi = 0;
1435 while (bi<n)
1436 {
1437 // eventually records input
1438 if ( x->x_record)
1439 {
1440 *(x->x_rdata+(x->x_writepos*x->x_blocksize)+bi)=(*(rin))*(*(x->x_multfreq+bi));
1441 *(x->x_idata+(x->x_writepos*x->x_blocksize)+bi)=(*(iin))*(*(x->x_multfreq+bi));
1442 }
1443 // set outputs
1444 *rout = 0.0;
1445 *iout = 0.0;
1446 if ( x->x_play)
1447 {
1448 is=0;
1449 fspectrum =
1450 sqrt( pow( *(x->x_rdata+(((int)x->x_readpos+is)*x->x_blocksize)+bi), 2) +
1451 pow( *(x->x_idata+(((int)x->x_readpos+is)*x->x_blocksize)+bi), 2) );
1452 fphase = atan2( *(x->x_idata+(((int)x->x_readpos+is)*x->x_blocksize)+bi),
1453 *(x->x_rdata+(((int)x->x_readpos+is)*x->x_blocksize)+bi) );
1454 fphase += (x->x_phase/180.0)*(M_PI);
1455 *rout += fspectrum*cos( fphase );
1456 *iout += fspectrum*sin( fphase );
1457 }
1458 rout++;
1459 iout++;
1460 rin++;
1461 iin++;
1462 bi++;
1463
1464 }
1465 // reset playing position until next play
1466 if ( x->x_play )
1467 {
1468 x->x_readpos+=x->x_readspeed;
1469 // post( "xreadpos : %f (added %f) %d", x->x_readpos, x->x_readspeed, x->x_readend );
1470 if ( ( x->x_readspeed >= 0 ) && ( x->x_readpos >= endsamp ) )
1471 {
1472 x->x_play=0;
1473 x->x_readpos=(float)(startsamp);
1474 // post( "cooled~ : stopped playing (readpos=%d)", x->x_readpos );
1475 outlet_bang(x->x_end);
1476 }
1477 if ( ( x->x_readspeed < 0 ) && ( x->x_readpos <= startsamp ) )
1478 {
1479 x->x_play=0;
1480 x->x_readpos = (float)(endsamp);
1481 // post( "cooled~ : stopped playing (readpos=%d)", x->x_readpos );
1482 outlet_bang(x->x_end);
1483 }
1484 }
1485 // reset recording position until next record
1486 if ( x->x_record )
1487 {
1488 x->x_writepos++;
1489 if ( x->x_writepos >= x->x_size )
1490 {
1491 x->x_record=0;
1492 x->x_writepos=0;
1493 sonogram_update_part(x, x->x_glist, 0, x->x_size-1, 0, 1, 0);
1494 outlet_bang(x->x_recend);
1495 if ( x->x_empty ) x->x_empty = 0;
1496 // post( "sonogram~ : stopped recording" );
1497 }
1498 }
1499 // post( "sonogram~ : read : %f:%d : write: %d:%d", x->x_readpos, x->x_play, x->x_writepos, x->x_record );
1500 return (w+7);
1501 }
1502
1503 static void sonogram_dsp(t_sonogram *x, t_signal **sp)
1504 {
1505 dsp_add(sonogram_perform, 6, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, sp[0]->s_n, x);
1506 }
1507
1508 /* record the sonogram */
1509 static void sonogram_record(t_sonogram *x)
1510 {
1511 x->x_record=1;
1512 x->x_writepos=0;
1513 // post( "sonogram~ : recording on" );
1514 }
1515
1516 /* play the sonogram */
1517 static void sonogram_play(t_sonogram *x)
1518 {
1519 x->x_play=1;
1520 x->x_readpos=(x->x_readstart*x->x_size)/100;
1521 // post( "sonogram~ : playing on" );
1522 }
1523
1524 /* setting the starting point for reading ( in percent ) */
1525 static void sonogram_readstart(t_sonogram *x, t_floatarg fstart)
1526 {
1527 t_float startpoint = fstart;
1528 t_canvas *canvas=glist_getcanvas(x->x_glist);
1529
1530 if (startpoint < 0) startpoint = 0;
1531 if (startpoint > 100) startpoint = 100;
1532 x->x_readstart=startpoint;
1533 // set readspeed sign
1534 if ( ( x->x_readstart > x->x_readend ) && ( x->x_readspeed > 0 ) ) x->x_readspeed = -x->x_readspeed;
1535 if ( ( x->x_readstart < x->x_readend ) && ( x->x_readspeed < 0 ) ) x->x_readspeed = -x->x_readspeed;
1536 if ( x->x_graphic && glist_isvisible( x->x_glist ) )
1537 {
1538 SYS_VGUI3( ".x%lx.c delete line %xREADSTART\n",
1539 canvas, x);
1540 SYS_VGUI7( ".x%lx.c create line %d %d %d %d -fill #FF0000 -tags %xREADSTART -width 3\n",
1541 canvas, x->x_xpos+(x->x_readstart*(x->x_size)/100 ),
1542 x->x_ypos, x->x_xpos+(x->x_readstart*(x->x_size)/100 ),
1543 x->x_ypos+x->x_blocksize*x->x_zoom, x );
1544 }
1545 }
1546
1547 /* setting the starting point for modification ( in percent ) */
1548 static void sonogram_modstart(t_sonogram *x, t_floatarg fstart)
1549 {
1550 t_float startpoint = fstart;
1551 t_canvas *canvas=glist_getcanvas(x->x_glist);
1552
1553 if (startpoint < 0) startpoint = 0;
1554 if (startpoint > 100) startpoint = 100;
1555 if ( startpoint > x->x_modend )
1556 {
1557 x->x_modstart = x->x_modend;
1558 post( "sonogram~ : warning : range for modifications is null" );
1559 }
1560 else
1561 {
1562 x->x_modstart=startpoint;
1563 }
1564 if ( x->x_graphic && glist_isvisible( x->x_glist ) )
1565 {
1566 SYS_VGUI3( ".x%lx.c delete line %xMODSTART\n",
1567 canvas, x);
1568 SYS_VGUI7( ".x%lx.c create line %d %d %d %d -fill #11E834 -tags %xMODSTART -width 3\n",
1569 canvas, x->x_xpos+(x->x_modstart*(x->x_size)/100 ),
1570 x->x_ypos, x->x_xpos+(x->x_modstart*(x->x_size)/100 ),
1571 x->x_ypos+x->x_blocksize*x->x_zoom, x );
1572 }
1573 }
1574
1575 /* setting the modification step for graphical mode */
1576 static void sonogram_modstep(t_sonogram *x, t_floatarg fmodstep)
1577 {
1578 if ( x->x_graphic )
1579 {
1580 x->x_modstep = fmodstep;
1581 }
1582 }
1583
1584 /* setting enhance mode */
1585 static void sonogram_enhancemode(t_sonogram *x, t_floatarg fenhancemode)
1586 {
1587 if ( x->x_graphic )
1588 {
1589 x->x_enhancemode = fenhancemode;
1590 }
1591 }
1592
1593 /* setting the ending point for reading ( in percent ) */
1594 static void sonogram_readend(t_sonogram *x, t_floatarg fend)
1595 {
1596 t_float endpoint = fend;
1597 t_canvas *canvas=glist_getcanvas(x->x_glist);
1598
1599 if (endpoint < 0) endpoint = 0;
1600 if (endpoint > 100) endpoint = 100;
1601 x->x_readend=endpoint;
1602 // set readspeed sign
1603 if ( ( x->x_readstart > x->x_readend ) && ( x->x_readspeed > 0 ) ) x->x_readspeed = -x->x_readspeed;
1604 if ( ( x->x_readstart < x->x_readend ) && ( x->x_readspeed < 0 ) ) x->x_readspeed = -x->x_readspeed;
1605 if ( x->x_graphic && glist_isvisible( x->x_glist ) )
1606 {
1607 SYS_VGUI3( ".x%lx.c delete line %xREADEND\n",
1608 canvas, x);
1609 SYS_VGUI7( ".x%lx.c create line %d %d %d %d -fill #FF0000 -tags %xREADEND -width 3\n",
1610 canvas, x->x_xpos+(x->x_readend*(x->x_size)/100 ),
1611 x->x_ypos, x->x_xpos+(x->x_readend*(x->x_size)/100 ),
1612 x->x_ypos+x->x_blocksize*x->x_zoom, x );
1613 }
1614 }
1615
1616 /* setting the ending point for modification ( in percent ) */
1617 static void sonogram_modend(t_sonogram *x, t_floatarg fend)
1618 {
1619 t_float endpoint = fend;
1620 t_canvas *canvas=glist_getcanvas(x->x_glist);
1621
1622 if (endpoint < 0) endpoint = 0;
1623 if (endpoint > 100) endpoint = 100;
1624 if ( endpoint < x->x_modstart )
1625 {
1626 x->x_modend = x->x_modstart;
1627 post( "sonogram~ : warning : range for modifications is null" );
1628 }
1629 else
1630 {
1631 x->x_modend=endpoint;
1632 }
1633 if ( x->x_graphic && glist_isvisible( x->x_glist ) )
1634 {
1635 SYS_VGUI3( ".x%lx.c delete line %xMODEND\n",
1636 canvas, x);
1637 SYS_VGUI7( ".x%lx.c create line %d %d %d %d -fill #11E834 -tags %xMODEND -width 3\n",
1638 canvas, x->x_xpos+(x->x_modend*(x->x_size)/100 ),
1639 x->x_ypos, x->x_xpos+(x->x_modend*(x->x_size)/100 ),
1640 x->x_ypos+x->x_blocksize*x->x_zoom, x );
1641 }
1642 }
1643
1644 /* sets the reading speed */
1645 static void sonogram_readspeed(t_sonogram *x, t_floatarg freadspeed)
1646 {
1647 if (freadspeed <= 0 )
1648 {
1649 post( "sonogram~ : wrong readspeed argument" );
1650 return;
1651 }
1652 x->x_readspeed=freadspeed;
1653 }
1654
1655 /* enhance frequencies */
1656 static void sonogram_enhance(t_sonogram *x, t_floatarg fstartfreq, t_floatarg fendfreq, t_floatarg fenhance, t_floatarg fnoupdate )
1657 {
1658 t_int samplestart, sampleend, si, fi=0, ffi=0;
1659 t_float oldenergy;
1660
1661 if (fstartfreq < 0 || fendfreq < 0 ||
1662 fstartfreq > x->x_blocksize || fendfreq > x->x_blocksize ||
1663 fstartfreq > fendfreq )
1664 {
1665 post( "sonogram~ : error : wrong frequencies range" );
1666 return;
1667 }
1668 if ( fenhance < 0 )
1669 {
1670 post( "sonogram~ : error : wrong multiplicating factor" );
1671 return;
1672 }
1673 samplestart=(x->x_modstart*(x->x_size-1))/100;
1674 sampleend=(x->x_modend*(x->x_size-1))/100;
1675 // post("enhancing portion [%d,%d]", samplestart, sampleend );
1676 for ( si=samplestart; si<=sampleend; si++ )
1677 {
1678 for ( fi=(int)fstartfreq; fi<=(int)fendfreq; fi++ )
1679 {
1680 *(x->x_multfreq+fi) = fenhance;
1681 if ( (fi != 0) && (fi != x->x_blocksize/2-1) )
1682 {
1683 /* multiply both r*sin(a) and r*cos(a) to mutiply r */
1684 *(x->x_rdata+(si*x->x_blocksize)+fi) *= fenhance;
1685 *(x->x_idata+(si*x->x_blocksize)+fi) *= fenhance;
1686 }
1687 }
1688 }
1689 // post( "sonogram~ : enhanced %d,%d", fi, ffi );
1690 if ( !(int)fnoupdate )
1691 {
1692 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
1693 }
1694 }
1695
1696 /* add a constant to frequencies */
1697 static void sonogram_add(t_sonogram *x, t_floatarg fstartfreq, t_floatarg fendfreq, t_floatarg fadd)
1698 {
1699 t_int samplestart, sampleend, si, fi;
1700 t_float oldenergy;
1701
1702 if (fstartfreq < 0 || fendfreq < 0 ||
1703 fstartfreq > x->x_blocksize || fendfreq > x->x_blocksize ||
1704 fstartfreq > fendfreq )
1705 {
1706 post( "sonogram~ : error : wrong frequencies range" );
1707 return;
1708 }
1709 samplestart=(x->x_modstart*(x->x_size-1))/100;
1710 sampleend=(x->x_modend*(x->x_size-1))/100;
1711 // post("enhancing portion [%d,%d]", samplestart, sampleend );
1712 for ( si=samplestart; si<=sampleend; si++ )
1713 {
1714 for ( fi=(int)fstartfreq; fi<=(int)fendfreq; fi++ )
1715 {
1716 /* multiply both r*sin(a) and r*cos(a) to mutiply r */
1717 *(x->x_rdata+(si*x->x_blocksize)+fi) += fadd;
1718 *(x->x_idata+(si*x->x_blocksize)+fi) += fadd;
1719 }
1720 }
1721 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
1722 }
1723
1724 /* resize sonogram */
1725 static void sonogram_resize(t_sonogram *x, t_floatarg fnewsize )
1726 {
1727 if (fnewsize <= 0)
1728 {
1729 post( "sonogram~ : error : wrong size" );
1730 return;
1731 }
1732 if (x->x_updatechild > 0)
1733 {
1734 post( "sonogram~ : can't resize now, an update is pending." );
1735 return;
1736 }
1737 post( "sonogram~ : reallocating tables" );
1738 x->x_record=0;
1739 x->x_play=0;
1740 sonogram_reallocate(x, x->x_size, fnewsize);
1741 x->x_size = fnewsize;
1742 // erase all points, as data is zero no drawing is needed
1743 sonogram_update_part(x, x->x_glist, 0, x->x_size-1, 0, 0, 0);
1744 }
1745
1746 /* set zoom factor */
1747 static void sonogram_zoom(t_sonogram *x, t_floatarg fzoom )
1748 {
1749 post( "sonogram~: warning : zoom and big block factors might lead to a crash" );
1750 if (fzoom < 1)
1751 {
1752 post( "sonogram~ : error : wrong zoom factor" );
1753 return;
1754 }
1755 if ( x->x_gifdata != NULL )
1756 {
1757 freebytes(x->x_gifdata, (x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1758 post( "Freed %d bytes", (x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1759 x->x_gifdata = NULL;
1760 }
1761 if ( x->x_guicommand != NULL )
1762 {
1763 freebytes(x->x_guicommand, 128+(x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1764 post( "Freed %d bytes", 128+(x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1765 x->x_guicommand = NULL;
1766 }
1767 x->x_zoom = (int)fzoom;
1768 if ( !( x->x_gifdata = ( char* ) getbytes( (x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") ) ) )
1769 {
1770 post( "sonogram~ : error : could not allocate buffers" );
1771 return;
1772 }
1773 else
1774 {
1775 post( "sonogram~ : allocated %d bytes", (x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1776 }
1777 if ( !( x->x_guicommand = ( char* ) getbytes( 128+(x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") ) ) )
1778 {
1779 post( "sonogram~ : error : could not allocate buffers" );
1780 return;
1781 }
1782 else
1783 {
1784 post( "sonogram~ : allocated %d bytes", 128+(x->x_blocksize/2)*x->x_zoom*sizeof("#FFFFFF ") );
1785 }
1786 sonogram_update_part(x, x->x_glist, 0, x->x_size-1, !x->x_empty, !x->x_empty, 0);
1787 canvas_fixlinesfor(x->x_glist, (t_text*)x );
1788 }
1789
1790 /* refresh data */
1791 static void sonogram_refresh(t_sonogram *x)
1792 {
1793 sonogram_update_part(x, x->x_glist, 0, x->x_size-1, 0, 1, 1);
1794 }
1795
1796 /* flip frequencies */
1797 static void sonogram_flipfreqs(t_sonogram *x)
1798 {
1799 t_int samplestart, sampleend, si, fi;
1800 t_float fvalue;
1801 t_int ioperon;
1802
1803 samplestart=(x->x_modstart*(x->x_size-1))/100;
1804 sampleend=(x->x_modend*(x->x_size-1))/100;
1805 ioperon=x->x_blocksize/2;
1806 for ( si=samplestart; si<=sampleend; si++ )
1807 {
1808 for ( fi=0; fi<=ioperon/2; fi++ )
1809 {
1810 fvalue = *(x->x_rdata+(si*x->x_blocksize)+fi);
1811 *(x->x_rdata+(si*x->x_blocksize)+fi) = *(x->x_rdata+(si*x->x_blocksize)+(ioperon-fi-1));
1812 *(x->x_rdata+(si*x->x_blocksize)+(ioperon-fi-1)) = fvalue;
1813 fvalue = *(x->x_idata+(si*x->x_blocksize)+fi);
1814 *(x->x_idata+(si*x->x_blocksize)+fi) = *(x->x_idata+(si*x->x_blocksize)+(ioperon-fi-1));
1815 *(x->x_idata+(si*x->x_blocksize)+(ioperon-fi-1)) = fvalue;
1816 }
1817 }
1818 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
1819 }
1820
1821 /* flip blocks */
1822 static void sonogram_flipblocks(t_sonogram *x)
1823 {
1824 t_int samplestart, sampleend, middlesample, fi, si;
1825 t_float fvalue;
1826
1827 samplestart=(x->x_modstart*(x->x_size-1))/100;
1828 sampleend=(x->x_modend*(x->x_size-1))/100;
1829 middlesample = ( sampleend+samplestart+1 ) / 2;
1830 for ( si=samplestart; si<=middlesample; si++ )
1831 {
1832 for ( fi=0; fi<x->x_blocksize; fi++ )
1833 {
1834 fvalue = *(x->x_rdata+((si)*x->x_blocksize)+fi);
1835 *(x->x_rdata+((si)*x->x_blocksize)+fi) = *(x->x_rdata+((sampleend+samplestart-si)*x->x_blocksize)+fi);
1836 *(x->x_rdata+((sampleend+samplestart-si)*x->x_blocksize)+fi) = fvalue;
1837 fvalue = *(x->x_idata+((si)*x->x_blocksize)+fi);
1838 *(x->x_idata+((si)*x->x_blocksize)+fi) = *(x->x_idata+((sampleend+samplestart-si)*x->x_blocksize)+fi);
1839 *(x->x_idata+((sampleend+samplestart-si)*x->x_blocksize)+fi) = fvalue;
1840 }
1841 }
1842 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
1843 }
1844
1845 /* undo if available */
1846 static void sonogram_undo(t_sonogram *x)
1847 {
1848 t_int si,fi;
1849
1850 if ( x->x_uxs == -1 )
1851 {
1852 post( "sonogram~ : nothing to undo, man" );
1853 return;
1854 }
1855
1856 post( "sonogram~ : restoring region [%d,%d,%d,%d]", x->x_uxs, x->x_uys, x->x_uxe, x->x_uye );
1857 for ( si=x->x_uxs; si<=x->x_uxe; si++ )
1858 {
1859 for ( fi=x->x_uys; fi<=x->x_uye; fi++ )
1860 {
1861 *(x->x_rdata+((si)*x->x_blocksize)+fi) = *(x->x_rudata+(si-x->x_uxs)*x->x_blocksize+(fi-x->x_uys));
1862 *(x->x_idata+((si)*x->x_blocksize)+fi) = *(x->x_iudata+(si-x->x_uxs)*x->x_blocksize+(fi-x->x_uys));
1863 }
1864 }
1865 sonogram_update_part(x, x->x_glist, x->x_uxs, x->x_uxe, 0, 1, 1);
1866 }
1867
1868 /* zswap exchanges real and imaginery part */
1869 static void sonogram_zswap(t_sonogram *x)
1870 {
1871 t_int samplestart, sampleend, fi, si;
1872 t_float fvalue;
1873
1874 samplestart=(x->x_modstart*(x->x_size-1))/100;
1875 sampleend=(x->x_modend*(x->x_size-1))/100;
1876 for ( si=samplestart; si<=sampleend; si++ )
1877 {
1878 for ( fi=0; fi<x->x_blocksize; fi++ )
1879 {
1880 fvalue = *(x->x_rdata+(si*x->x_blocksize)+fi);
1881 *(x->x_rdata+(si*x->x_blocksize)+fi) = *(x->x_idata+(si*x->x_blocksize)+fi);
1882 *(x->x_idata+(si*x->x_blocksize)+fi) = fvalue;
1883 }
1884 }
1885 }
1886
1887 /* swap points */
1888 static void sonogram_swappoints(t_sonogram *x, t_floatarg fnbpoints)
1889 {
1890 t_int samplestart, sampleend, sp;
1891 t_float s1, s2, f1, f2;
1892 t_float fvalue;
1893
1894 if (fnbpoints <= 0)
1895 {
1896 post( "sonogram~ : error : bad number of points" );
1897 return;
1898 }
1899 samplestart=(x->x_modstart*(x->x_size-1))/100;
1900 sampleend=(x->x_modend*(x->x_size-1))/100;
1901
1902 for ( sp=0; sp<fnbpoints; sp++ )
1903 {
1904 s1 = samplestart + (random()%(sampleend-samplestart));
1905 s2 = samplestart + (random()%(sampleend-samplestart));
1906 f1 = random()%( x->x_blocksize/2-1 );
1907 f2 = random()%( x->x_blocksize/2-1 );
1908 fvalue = *(x->x_rdata+((int)s1*x->x_blocksize)+(int)f1);
1909 *(x->x_rdata+((int)s1*x->x_blocksize)+(int)f1) = *(x->x_rdata+((int)s2*x->x_blocksize)+(int)f2);
1910 *(x->x_rdata+((int)s2*x->x_blocksize)+(int)f2) = fvalue;
1911 fvalue = *(x->x_idata+((int)s1*x->x_blocksize)+(int)f1);
1912 *(x->x_idata+((int)s1*x->x_blocksize)+(int)f1) = *(x->x_idata+((int)s2*x->x_blocksize)+(int)f2);
1913 *(x->x_idata+((int)s2*x->x_blocksize)+(int)f2) = fvalue;
1914 }
1915 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
1916 }
1917
1918 /* average blocks according to a factor */
1919 static void sonogram_average(t_sonogram *x, t_floatarg fnbblocks)
1920 {
1921 t_int samplestart, sampleend, fi, si, ssi;
1922 t_float fraverage, fiaverage;
1923
1924 if (fnbblocks < 1)
1925 {
1926 post( "sonogram~ : error : bad average factor" );
1927 return;
1928 }
1929 samplestart=(x->x_modstart*(x->x_size-1))/100;
1930 sampleend=(x->x_modend*(x->x_size-1))/100;
1931
1932 fraverage=fiaverage=0.0;
1933 for ( fi=0; fi<x->x_blocksize; fi++ )
1934 {
1935 for ( si=samplestart; si<=sampleend-fnbblocks; si+=fnbblocks )
1936 {
1937 fraverage=fiaverage=0.0;
1938 for ( ssi=0; ssi<fnbblocks; ssi++ )
1939 {
1940 fraverage += *(x->x_rdata+((int)(si+ssi)*x->x_blocksize)+fi);
1941 fiaverage += *(x->x_idata+((int)(si+ssi)*x->x_blocksize)+fi);
1942 }
1943 fraverage /= fnbblocks;
1944 fiaverage /= fnbblocks;
1945 for ( ssi=0; ssi<fnbblocks; ssi++ )
1946 {
1947 *(x->x_rdata+((int)(si+ssi)*x->x_blocksize)+fi)=fraverage;
1948 *(x->x_idata+((int)(si+ssi)*x->x_blocksize)+fi)=fiaverage;
1949 }
1950 }
1951 }
1952 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
1953 }
1954
1955 /* go up by the given number */
1956 static void sonogram_goup(t_sonogram *x, t_floatarg fgoup)
1957 {
1958 t_int samplestart, sampleend, sp, sf;
1959
1960 if (fgoup <= 0 || fgoup > x->x_blocksize/2)
1961 {
1962 post( "sonogram~ : error : wrong offset in goup function" );
1963 return;
1964 }
1965 samplestart=(x->x_modstart*(x->x_size-1))/100;
1966 sampleend=(x->x_modend*(x->x_size-1))/100;
1967
1968 for ( sp=samplestart; sp<=sampleend; sp++ )
1969 {
1970 for (sf=(x->x_blocksize/2)-fgoup-1; sf>=0; sf-- )
1971 {
1972 *(x->x_rdata+((int)sp*x->x_blocksize)+(sf+(int)fgoup)) =
1973 *(x->x_rdata+((int)sp*x->x_blocksize)+sf);
1974 *(x->x_idata+((int)sp*x->x_blocksize)+(sf+(int)fgoup)) =
1975 *(x->x_idata+((int)sp*x->x_blocksize)+sf);
1976
1977 }
1978 for (sf=0; sf<fgoup; sf++ )
1979 {
1980 *(x->x_rdata+((int)sp*x->x_blocksize)+(int)sf) = 0.0;
1981 *(x->x_idata+((int)sp*x->x_blocksize)+(int)sf) = 0.0;
1982 }
1983 }
1984 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
1985 }
1986
1987 /* roll up by the given number */
1988 static void sonogram_roll(t_sonogram *x, t_floatarg froll)
1989 {
1990 t_int samplestart, sampleend, sp, sf;
1991 t_float *fprvalues;
1992 t_float *fpivalues;
1993
1994 if (froll <= 0 || froll > x->x_blocksize/2)
1995 {
1996 post( "sonogram~ : error : wrong offset in roll function" );
1997 return;
1998 }
1999 fprvalues = (t_float*)getbytes( ((int)froll)*sizeof( float ) );
2000 if ( !fprvalues )
2001 {
2002 post( "sonogram~ : error : could not allocate %d bytes", ((int)froll)*sizeof(float) );
2003 return;
2004 }
2005 fpivalues = (t_float*)getbytes( ((int)froll)*sizeof( float ) );
2006 if ( !fpivalues )
2007 {
2008 post( "sonogram~ : error : could not allocate %d bytes", ((int)froll)*sizeof(float) );
2009 return;
2010 }
2011
2012 samplestart=(x->x_modstart*(x->x_size-1))/100;
2013 sampleend=(x->x_modend*(x->x_size-1))/100;
2014
2015 for ( sp=samplestart; sp<=sampleend; sp++ )
2016 {
2017
2018 // saving values
2019 for (sf=0; sf<froll; sf++ )
2020 {
2021 *(fprvalues+sf) =
2022 *(x->x_rdata+((int)sp*x->x_blocksize)+(x->x_blocksize/2-(int)froll+sf));
2023 *(fpivalues+sf) =
2024 *(x->x_idata+((int)sp*x->x_blocksize)+(x->x_blocksize/2-(int)froll+sf));
2025 }
2026 for (sf=(x->x_blocksize/2)-froll-1; sf>=0; sf-- )
2027 {
2028 *(x->x_rdata+((int)sp*x->x_blocksize)+(sf+(int)froll)) =
2029 *(x->x_rdata+((int)sp*x->x_blocksize)+sf);
2030 *(x->x_idata+((int)sp*x->x_blocksize)+(sf+(int)froll)) =
2031 *(x->x_idata+((int)sp*x->x_blocksize)+sf);
2032 }
2033 for (sf=0; sf<froll; sf++ )
2034 {
2035 *(x->x_rdata+((int)sp*x->x_blocksize)+(int)sf) = *(fprvalues+sf);
2036 *(x->x_idata+((int)sp*x->x_blocksize)+(int)sf) = *(fpivalues+sf);
2037 }
2038 }
2039 freebytes( fprvalues, (int)froll*sizeof(float) );
2040 freebytes( fpivalues, (int)froll*sizeof(float) );
2041 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
2042 }
2043
2044 /* suppress point below the threshold */
2045 static void sonogram_threshold(t_sonogram *x, t_floatarg fthreshold)
2046 {
2047 t_int samplestart, sampleend, sp, sf;
2048 t_float fspectrum;
2049
2050 if (fthreshold <= 0)
2051 {
2052 post( "sonogram~ : error : wrong threshold" );
2053 return;
2054 }
2055 samplestart=(x->x_modstart*(x->x_size-1))/100;
2056 sampleend=(x->x_modend*(x->x_size-1))/100;
2057
2058 for ( sp=samplestart; sp<=sampleend; sp++ )
2059 {
2060 for (sf=0; sf<=(x->x_blocksize/2)-1; sf++ )
2061 {
2062 fspectrum = sqrt( pow( *(x->x_rdata+sp*x->x_blocksize+sf), 2) +
2063 pow( *(x->x_idata+sp*x->x_blocksize+sf), 2) );
2064 if ( fspectrum < fthreshold )
2065 {
2066 *(x->x_rdata+sp*x->x_blocksize+sf) = 0.0;
2067 *(x->x_idata+sp*x->x_blocksize+sf) = 0.0;
2068 }
2069 }
2070 }
2071 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
2072 }
2073
2074 /* change the phase */
2075 static void sonogram_phase(t_sonogram *x, t_floatarg fincphase)
2076 {
2077 if (fincphase < 0 || fincphase > 90)
2078 {
2079 post( "sonogram~ : error : wrong phase in phase function : out of [0,90]" );
2080 return;
2081 }
2082 x->x_phase = fincphase;
2083 }
2084
2085 /* go down by the given number */
2086 static void sonogram_godown(t_sonogram *x, t_floatarg fgodown)
2087 {
2088 t_int samplestart, sampleend, sp, sf;
2089
2090 if (fgodown <= 0 || fgodown > x->x_blocksize/2)
2091 {
2092 post( "sonogram~ : error : wrong offset in godown function" );
2093 return;
2094 }
2095 samplestart=(x->x_modstart*(x->x_size-1))/100;
2096 sampleend=(x->x_modend*(x->x_size-1))/100;
2097
2098 for ( sp=samplestart; sp<=sampleend; sp++ )
2099 {
2100 for (sf=0; sf<=(x->x_blocksize/2)-fgodown-1; sf++ )
2101 {
2102 *(x->x_rdata+((int)sp*x->x_blocksize)+sf) =
2103 *(x->x_rdata+((int)sp*x->x_blocksize)+(sf+(int)fgodown));
2104 *(x->x_idata+((int)sp*x->x_blocksize)+sf) =
2105 *(x->x_idata+((int)sp*x->x_blocksize)+(sf+(int)fgodown));
2106 }
2107 for (sf=(x->x_blocksize/2)-fgodown; sf<(x->x_blocksize/2); sf++ )
2108 {
2109 *(x->x_rdata+((int)sp*x->x_blocksize)+(int)sf) = 0.0;
2110 *(x->x_idata+((int)sp*x->x_blocksize)+(int)sf) = 0.0;
2111 }
2112 }
2113 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
2114 }
2115
2116 /* swap blocks */
2117 static void sonogram_swapblocks(t_sonogram *x, t_floatarg fperstart, t_floatarg fperend, t_floatarg fpersize)
2118 {
2119 t_int samplestart, samplestartb, samplesize, sp, sf;
2120 t_int iperstart, iperend, ipersize;
2121 t_float s1, s2;
2122 t_float fvalue;
2123
2124 iperstart = fperstart;
2125 iperend = fperend;
2126 ipersize = fpersize;
2127
2128 if (iperstart < 0 || iperstart > iperend ||
2129 iperend <= 0 || iperend+ipersize > 100 ||
2130 ipersize < 0 || fpersize > 100 )
2131 {
2132 post( "sonogram~ : error : wrong interval [%d%%, %d%%] <-> [%d%%, %d%%]",
2133 iperstart, iperstart+ipersize, iperend, iperend+ipersize );
2134 return;
2135 }
2136
2137 samplestart=(x->x_modstart*(x->x_size-1))/100;
2138 samplestartb=(x->x_modend*(x->x_size-1))/100;
2139 samplesize=((samplestartb-samplestart)*ipersize)/100;
2140 samplestart=samplestart+((samplestartb-samplestart)*iperstart)/100;
2141 samplestartb=samplestart+((samplestartb-samplestart)*iperend)/100;
2142
2143 post( "swap blocks [%d,%d] and [%d,%d]", samplestart, samplestart+samplesize, samplestartb, samplestartb+samplesize );
2144
2145 for ( sp=samplesize; sp>=0; sp-- )
2146 {
2147 for ( sf=0; sf<x->x_blocksize; sf++)
2148 {
2149 fvalue = *(x->x_rdata+((int)(samplestart+sp)*x->x_blocksize)+sf);
2150 *(x->x_rdata+((int)(samplestart+sp)*x->x_blocksize)+sf) = *(x->x_rdata+((int)(samplestartb+sp)*x->x_blocksize)+sf);
2151 *(x->x_rdata+((int)(samplestartb+sp)*x->x_blocksize)+sf) = fvalue;
2152 fvalue = *(x->x_idata+((int)(samplestart+sp)*x->x_blocksize)+sf);
2153 *(x->x_idata+((int)(samplestart+sp)*x->x_blocksize)+sf) = *(x->x_idata+((int)(samplestartb+sp)*x->x_blocksize)+sf);
2154 *(x->x_idata+((int)(samplestartb+sp)*x->x_blocksize)+sf) = fvalue;
2155 }
2156 }
2157 sonogram_update_part(x, x->x_glist, 0, x->x_size-1, 0, 1, 1);
2158 }
2159
2160 /* swap frequencies */
2161 static void sonogram_swapfreqs(t_sonogram *x, t_floatarg ffirstfreq, t_floatarg fsecondfreq)
2162 {
2163 t_int samplestart, sampleend, sp;
2164 t_float fvalue;
2165
2166 if (ffirstfreq < 0 || fsecondfreq <0)
2167 {
2168 post( "sonogram~ : error : wrong frequencies" );
2169 return;
2170 }
2171 samplestart=(x->x_modstart*(x->x_size-1))/100;
2172 sampleend=(x->x_modend*(x->x_size-1))/100;
2173
2174 for ( sp=samplestart; sp<=sampleend; sp++ )
2175 {
2176 fvalue = *(x->x_rdata+((int)sp*x->x_blocksize)+(int)ffirstfreq);
2177 *(x->x_rdata+((int)sp*x->x_blocksize)+(int)ffirstfreq) =
2178 *(x->x_rdata+((int)sp*x->x_blocksize)+(int)fsecondfreq);
2179 *(x->x_rdata+((int)sp*x->x_blocksize)+(int)fsecondfreq) = fvalue;
2180 fvalue = *(x->x_idata+((int)sp*x->x_blocksize)+(int)ffirstfreq);
2181 *(x->x_idata+((int)sp*x->x_blocksize)+(int)ffirstfreq) =
2182 *(x->x_idata+((int)sp*x->x_blocksize)+(int)fsecondfreq);
2183 *(x->x_idata+((int)sp*x->x_blocksize)+(int)fsecondfreq) = fvalue;
2184 }
2185 sonogram_update_part(x, x->x_glist, samplestart, sampleend, 0, 1, 1);
2186 }
2187
2188 static void *sonogram_new(t_floatarg fsize, t_floatarg fgraphic, t_floatarg fphaso)
2189 {
2190 t_sonogram *x = (t_sonogram *)pd_new(sonogram_class);
2191 outlet_new(&x->x_obj, &s_signal);
2192 outlet_new(&x->x_obj, &s_signal);
2193 x->x_recend = outlet_new(&x->x_obj, &s_bang );
2194 x->x_end = outlet_new(&x->x_obj, &s_bang );
2195 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
2196 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("readstart"));
2197 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("readend"));
2198 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("modstart"));
2199 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("modend"));
2200
2201 if ( fsize <= 0 || ( fgraphic != 0 && fgraphic != 1 ) || ( fphaso != 0 && fphaso != 1 ) )
2202 {
•2203 pd_error(x, "sonogram~ : missing or negative creation arguments" );
2204 return NULL;
2205 }
2206
2207 // activate graphical callbacks
2208 if ( fgraphic != 0 )
2209 {
2210 class_setwidget(sonogram_class, &sonogram_widgetbehavior);
2211 }
2212 x->x_graphic = (int) fgraphic;
2213 x->x_phaso = (int) fphaso;
2214
2215 x->x_size = fsize;
2216 x->x_blocksize = sys_getblksize();
2217 x->x_play = 0;
2218 x->x_readspeed = 1.;
2219 x->x_record = 0;
2220 x->x_readpos = 0.;
2221 x->x_writepos = 0;
2222 x->x_modstart = 0;
2223 x->x_readstart = 0;
2224 x->x_modend = 100;
2225 x->x_readend = 100;
2226 x->x_rdata = NULL;
2227 x->x_idata = NULL;
2228 x->x_phase = 0.0;
2229 x->x_empty = 1;
2230 x->x_xpos = -1;
2231 x->x_ypos = -1;
2232 x->x_samplerate = sys_getsr();
2233 /* graphic data */
2234 x->x_selected = 0;
2235 x->x_zoom = 1;
2236 x->x_updatechild = 0;
2237 x->x_modstep = 1.1;
2238 x->x_enhancemode = 0;
2239 x->x_glist = (t_glist*)canvas_getcurrent();
2240
2241 if ( sonogram_allocate(x) <0 )
2242 {
2243 return NULL;
2244 }
2245 else
2246 {
2247 return(x);
2248 }
2249
2250 }
2251
2252 void sonogram_tilde_setup(void)
2253 {
•2254 logpost(NULL, 4, "%s", sonogram_version);
2255 sonogram_class = class_new(gensym("sonogram~"), (t_newmethod)sonogram_new, (t_method)sonogram_free,
2256 sizeof(t_sonogram), 0, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
2257
2258
2259 // set callbacks
2260 sonogram_widgetbehavior.w_getrectfn = sonogram_getrect;
2261 sonogram_widgetbehavior.w_displacefn = sonogram_displace;
2262 sonogram_widgetbehavior.w_selectfn = sonogram_select;
2263 sonogram_widgetbehavior.w_activatefn = NULL;
2264 sonogram_widgetbehavior.w_deletefn = sonogram_delete;
2265 sonogram_widgetbehavior.w_visfn = sonogram_vis;
2266 sonogram_widgetbehavior.w_clickfn = sonogram_click;
2267
2268
2269 #if PD_MINOR_VERSION >= 37
2270 class_setpropertiesfn(sonogram_class, NULL);
2271 class_setsavefn(sonogram_class, sonogram_save);
2272 #else
2273 sonogram_widgetbehavior.w_propertiesfn = NULL;
2274 sonogram_widgetbehavior.w_savefn = sonogram_save;
2275 #endif
2276
2277 CLASS_MAINSIGNALIN( sonogram_class, t_sonogram, x_f );
2278 class_addmethod(sonogram_class, (t_method)sonogram_dsp, gensym("dsp"), A_NULL);
2279 class_addmethod(sonogram_class, (t_method)sonogram_record, gensym("record"), A_NULL);
2280 class_addmethod(sonogram_class, (t_method)sonogram_enhance, gensym("enhance"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);
2281 class_addmethod(sonogram_class, (t_method)sonogram_add, gensym("add"), A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);
2282 class_addmethod(sonogram_class, (t_method)sonogram_resize, gensym("resize"), A_FLOAT, A_NULL);
2283 class_addmethod(sonogram_class, (t_method)sonogram_zoom, gensym("zoom"), A_FLOAT, A_NULL);
2284 class_addmethod(sonogram_class, (t_method)sonogram_swappoints, gensym("swappoints"), A_FLOAT, A_NULL);
2285 class_addmethod(sonogram_class, (t_method)sonogram_average, gensym("average"), A_FLOAT, A_NULL);
2286 class_addmethod(sonogram_class, (t_method)sonogram_swapblocks, gensym("swapblocks"), A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);
2287 class_addmethod(sonogram_class, (t_method)sonogram_swapfreqs, gensym("swapfreqs"), A_FLOAT, A_FLOAT, A_NULL);
2288 class_addmethod(sonogram_class, (t_method)sonogram_flipfreqs, gensym("flipfreqs"), A_NULL);
2289 class_addmethod(sonogram_class, (t_method)sonogram_flipblocks, gensym("flipblocks"), A_NULL);
2290 class_addmethod(sonogram_class, (t_method)sonogram_play, gensym("play"), A_NULL);
2291 class_addmethod(sonogram_class, (t_method)sonogram_refresh, gensym("refresh"), A_NULL);
2292 class_addmethod(sonogram_class, (t_method)sonogram_enhancemode, gensym("enhancemode"), A_FLOAT, A_NULL);
2293 class_addmethod(sonogram_class, (t_method)sonogram_goup, gensym("goup"), A_FLOAT, A_NULL);
2294 class_addmethod(sonogram_class, (t_method)sonogram_godown, gensym("godown"), A_FLOAT, A_NULL);
2295 class_addmethod(sonogram_class, (t_method)sonogram_roll, gensym("roll"), A_FLOAT, A_NULL);
2296 class_addmethod(sonogram_class, (t_method)sonogram_threshold, gensym("threshold"), A_FLOAT, A_NULL);
2297 class_addmethod(sonogram_class, (t_method)sonogram_phase, gensym("phase"), A_FLOAT, A_NULL);
2298 class_addmethod(sonogram_class, (t_method)sonogram_zswap, gensym("zswap"), A_NULL);
2299 class_addmethod(sonogram_class, (t_method)sonogram_modstep, gensym("modstep"), A_FLOAT, A_NULL);
2300 class_addmethod(sonogram_class, (t_method)sonogram_modstart, gensym("modstart"), A_FLOAT, A_NULL);
2301 class_addmethod(sonogram_class, (t_method)sonogram_modend, gensym("modend"), A_FLOAT, A_NULL);
2302 class_addmethod(sonogram_class, (t_method)sonogram_readstart, gensym("readstart"), A_FLOAT, A_NULL);
2303 class_addmethod(sonogram_class, (t_method)sonogram_readend, gensym("readend"), A_FLOAT, A_NULL);
2304 class_addmethod(sonogram_class, (t_method)sonogram_readspeed, gensym("readspeed"), A_FLOAT, A_NULL);
2305 class_addmethod(sonogram_class, (t_method)sonogram_undo, gensym("undo"), A_NULL);
2306 }