aboutsummaryrefslogtreecommitdiff
path: root/cooled~/cooled~.c
blob: c79ee1c4ab0fd5d6c54569ed7da18e364de0db9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
/*------------------------ cooled~ -------------------------------------------- */
/*                                                                              */
/* cooled~ : display sound, play parts and modify it with cut and paste         */
/* constructor : cooled~ | cooled~ <size> <width> <height>                      */
/*                                                                              */
/* Copyleft Yves Degoyon ( ydegoyon@free.fr )                                   */
/*                                                                              */
/* This program is free software; you can redistribute it and/or                */
/* modify it under the terms of the GNU General Public License                  */
/* as published by the Free Software Foundation; either version 2               */
/* of the License, or (at your option) any later version.                       */
/*                                                                              */
/* See file LICENSE for further informations on licensing terms.                */
/*                                                                              */
/* This program is distributed in the hope that it will be useful,              */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of               */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                */
/* GNU General Public License for more details.                                 */
/*                                                                              */
/* You should have received a copy of the GNU General Public License            */
/* along with this program; if not, write to the Free Software                  */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  */
/*                                                                              */
/* Based on PureData by Miller Puckette and others.                             */
/*                                                                              */
/* "I am the fly in the ointment" -- Wire + 154                                 */
/* Komet -- Instrumentals                                                       */
/* ---------------------------------------------------------------------------- */



#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <malloc.h>
#include <ctype.h>
#include <pthread.h>
#ifdef UNIX
#include <unistd.h>
#endif
#ifdef NT
#define M_PI 3.14159265358979323846
#endif
#include <math.h>

#include "m_pd.h"
#include "m_imp.h"
#include "g_canvas.h"
#include "t_tk.h"

static int guidebug=0;
static int ignorevisible=1; // ignore visible test
                         // because this seems to lead to bad refresh
                         // wait for a fix                            

#define SYS_VGUI2(a,b) if (guidebug) \
                         post(a,b);\
                         sys_vgui(a,b)

#define SYS_VGUI3(a,b,c) if (guidebug) \
                         post(a,b,c);\
                         sys_vgui(a,b,c)

#define SYS_VGUI4(a,b,c,d) if (guidebug) \
                         post(a,b,c,d);\
                         sys_vgui(a,b,c,d)

#define SYS_VGUI5(a,b,c,d,e) if (guidebug) \
                         post(a,b,c,d,e);\
                         sys_vgui(a,b,c,d,e)

#define SYS_VGUI6(a,b,c,d,e,f) if (guidebug) \
                         post(a,b,c,d,e,f);\
                         sys_vgui(a,b,c,d,e,f)

#define SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \
                         post(a,b,c,d,e,f,g );\
                         sys_vgui(a,b,c,d,e,f,g)

#define SYS_VGUI8(a,b,c,d,e,f,g,h) if (guidebug) \
                         post(a,b,c,d,e,f,g,h );\
                         sys_vgui(a,b,c,d,e,f,g,h)

#define SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \
                         post(a,b,c,d,e,f,g,h,i );\
                         sys_vgui(a,b,c,d,e,f,g,h,i)

#define SYS_VGUI10(a,b,c,d,e,f,g,h,i,j) if (guidebug) \
                         post(a,b,c,d,e,f,g,h,i,j );\
                         sys_vgui(a,b,c,d,e,f,g,h,i,j)

#define SYS_VGUI11(a,b,c,d,e,f,g,h,i,j,k) if (guidebug) \
                         post(a,b,c,d,e,f,g,h,i,j,k );\
                         sys_vgui(a,b,c,d,e,f,g,h,i,j,k)

#define THREAD_SLEEP_TIME 10000   // 10000 us = 10 ms
#define COOLED_BGCOLOR "#000000"
#define COOLED_FGCOLOR "#34E112"
#define COOLED_FRCOLOR "#FFFFFF"

#define COOLED_DEFAULT_SIZE 1024
#define COOLED_DEFAULT_WIDTH 400
#define COOLED_DEFAULT_HEIGHT 200

static char   *cooled_version = "cooled~: version 0.13, written by Yves Degoyon (ydegoyon@free.fr)";

static t_class *cooled_class;
t_widgetbehavior cooled_widgetbehavior;

typedef struct _cooled
{
    t_object x_obj;

    int x_size;                  /* size of the stored sound                                  */
    t_float x_readpos;             /* data's playing position                                   */
    int x_writepos;              /* data's recording position                                 */
    t_float x_readstart;           /* data's starting position for reading                      */
    t_float x_readend;             /* data's ending position for reading                        */
    int x_play;                  /* playing on/off flag                                       */
    t_float x_readspeed;           /* speed increment                                           */
    t_float x_record;              /* flag to start recording process                           */
    t_float x_allocate;            /* flag to indicate pending allocation                       */
    t_float x_empty;               /* flag to indicate it's a brand new sound                   */
    t_float *x_rdata;              /* table containing right channel samples                    */
    t_float *x_ldata;              /* table containing leftt channel samples                    */
    t_outlet *x_end;               /* outlet for end of restitution                             */
    t_outlet *x_recend;            /* outlet for end of recording                               */
    t_outlet *x_sampstart;         /* outlet for sample number [ start ] when selecting         */
    t_outlet *x_sampend;           /* outlet for sample number [ end ] when selecting           */
    t_float *x_rsemp;              /* temporary sample buffer ( right )                         */
    t_float *x_lsemp;              /* temporary sample buffer ( left )                          */
    char*   x_gifdata;             /* buffer to store graphic data                              */
    char*   x_guicommand;          /* buffer to store gui command                               */
    int   x_draw;                /* drawing option                                            */

           /* graphical data block */
    int x_width;                 /* graphical width                             */
    int x_height;                /* graphical height                            */
    int x_selected;              /* flag to remember if we are seleted or not   */
    int x_erase;                 /* flag used when an erase is needed           */
    int x_redraw;                /* flag used when drawing  is needed           */
    t_glist *x_glist;              /* keep graphic context for various operations */
    int x_zoom;                    /* zoom factor                                 */
    pthread_t x_updatechild;       /* thread id for the update child              */
    int x_updatestart;             /* starting position for update                */
    int x_updateend;               /* ending position for update                  */
    int x_xpos;                    /* stuck x position                            */
    int x_ypos;                    /* stuck y position                            */
    int x_shifted;                 /* remember shift state from last click        */
    int x_alted;                   /* remember alt state from last click          */
    int x_xdraw;                   /* x drawing position                          */
    int x_edraw;                   /* end of drawing                              */

    t_float x_f;                   /* float needed for signal input */

} t_cooled;

/* ------------------------ drawing functions ---------------------------- */

static void cooled_update_block(t_cooled *x, t_glist *glist, int bnumber)
{
    int hi, i=0;
    t_float fspectrum=0.0;
    int phase=0;
    char color[8];

    memset( x->x_gifdata, 0x0, x->x_height*x->x_zoom*sizeof("#FFFFFF ") );

    // update cooled~
    for ( hi=x->x_height-1; hi>=0; hi-- )
    {
      if ( ( hi == x->x_height/4) || ( hi == 3*x->x_height/4) )
      {
         sprintf( color, "%s ", COOLED_FGCOLOR );
      }
      else if ( hi == x->x_height/2) 
      {
         sprintf( color, "%s ", COOLED_FRCOLOR );
      }
      else
      {
         sprintf( color, "%s ", COOLED_BGCOLOR );
      }
      for ( i=0; i<x->x_zoom; i++ )
      {
         strncpy( x->x_gifdata+(hi*x->x_zoom+i)*8, color, 8 );
      }
    }

    // set all points
    {
       int fsamp = ( bnumber * x->x_size ) / x->x_width;
       int lsamp = ( ( bnumber+1) * x->x_size ) / x->x_width;
       int si;

       // post ( "cooled~ : updating samples [%d,%d]", fsamp, lsamp );

       for ( si=fsamp;si<lsamp; si++ )
       {
         // calculate right channel index
         {
            int rind =  3*x->x_height/4 + ( *(x->x_rdata+si) * (x->x_height/4) ) - 1;

            if ( rind > x->x_height - 1 ) rind = x->x_height - 1;
            if ( rind < x->x_height/2 ) rind = x->x_height/2;

            sprintf( color, "%s ", COOLED_FGCOLOR );
            for ( i=0; i<x->x_zoom; i++ )
            {
                strncpy( x->x_gifdata+(rind*x->x_zoom+i)*8, color, 8 );
            }
         }

         // calculate left channel index
         {
            int lind =  x->x_height/4 + ( *(x->x_ldata+si) * (x->x_height/4) ) - 1;

            if ( lind > x->x_height/2 - 1 ) lind = x->x_height/2 - 1;
            if ( lind < 0 ) lind = 0;

            sprintf( color, "%s ", COOLED_FGCOLOR );
            for ( i=0; i<x->x_zoom; i++ )
            {
                strncpy( x->x_gifdata+(lind*x->x_zoom+i)*8, color, 8 );
            }
         }
       }
    }

    for ( i=0; i<x->x_zoom; i++ )
    {
        sprintf( x->x_guicommand, "COOLEDIMAGE%x put {%s} -to %d 0\n", x, x->x_gifdata, (bnumber*x->x_zoom)+i );
        if ( glist_isvisible( x->x_glist ) )
           sys_gui( x->x_guicommand );
    }

}

static void cooled_erase_block(t_cooled *x, t_glist *glist, int sample )
{
  t_canvas *canvas=glist_getcanvas(glist);
  int hi;
  t_float fspectrum=0.0;
  char fillColor[ 16 ];

     for ( hi=0; hi<x->x_height; hi++)
     {
        {
           int i;

           for ( i=0; i<x->x_zoom; i++ )
           {
                strcpy( x->x_gifdata+i*sizeof("#FFFFFF "), strcat( COOLED_BGCOLOR, " ") );
           }
           if ( glist_isvisible( x->x_glist ) )
              SYS_VGUI5("COOLEDIMAGE%x put {%s} -to %d %d\n", x, x->x_gifdata, 
                               sample*x->x_zoom, (x->x_height-hi)*x->x_zoom );
        }
     }
}

static void *cooled_do_update_part(void *tdata)
{
 t_cooled *x = (t_cooled*) tdata;
 t_canvas *canvas=glist_getcanvas(x->x_glist);
 int si;
 int nbpoints = 0;
 t_float percentage = 0, opercentage = 0;

   // loose synchro
   usleep( THREAD_SLEEP_TIME );

   // check bounds
   if ( x->x_updateend > x->x_size-1 ) x->x_updateend = x->x_size-1;
   if ( x->x_updatestart < 0 ) x->x_updatestart = 0;

   post("cooled~ : ok, let's go [updating %d, %d]", x->x_updatestart, x->x_updateend );

   if ( x->x_erase )
   {
     for ( si=x->x_updatestart; si<=x->x_updateend; si++ )
     {
       cooled_erase_block(x, x->x_glist, si);
       nbpoints++;
       percentage = (nbpoints*100/(x->x_updateend-x->x_updatestart+1));
       if ( (percentage == (int) percentage) && ((int)percentage%5 == 0) && ( percentage != opercentage ) )
       {
         // post( "cooled~ : erase part : %d %% completed", (int)percentage );
         opercentage = percentage;
       }
     }
   }
   
   percentage = opercentage = nbpoints = 0;

   if ( x->x_redraw )
   {
     for ( si=x->x_updatestart; si<=x->x_updateend; si++ )
     {
       cooled_update_block(x, x->x_glist, si);
       nbpoints++;
       percentage = (nbpoints*100/(x->x_updateend-x->x_updatestart+1));
       if ( (percentage == (int) percentage) && ((int)percentage%5 == 0) && ( percentage != opercentage ) )
       {
         // post( "cooled~ : update part : %d %% completed", (int)percentage );
         opercentage = percentage;
       }
     }
   }

   if ( glist_isvisible( x->x_glist ) )
   {
      SYS_VGUI3( ".x%x.c delete rectangle %xCLIPZONE\n",
               canvas, x);
      if ( ( x->x_readstart != 0 ) || ( x->x_readend != 100 ) )
      {
         SYS_VGUI7( ".x%x.c create rectangle %d %d %d %d -outline #FF0000 -tags %xCLIPZONE -width 2\n",
                     canvas, x->x_xpos+(int)(x->x_readstart*(x->x_width)/100 ),
                          x->x_ypos, x->x_xpos+(int)(x->x_readend*(x->x_width)/100 ), 
                          x->x_ypos+x->x_height*x->x_zoom, x );
      }
      // set borders in black
      SYS_VGUI3(".x%x.c itemconfigure %xCOOLED -outline #000000\n", canvas, x);
   }

   post("cooled~ : child thread %d ended", (int)x->x_updatechild );
   x->x_updatechild = 0;
   return NULL;
}

static void cooled_update_part(t_cooled *x, t_glist *glist, int bstart, int bend, 
                                 int erase, int redraw, int keepframe)
{
  pthread_attr_t update_child_attr;
  t_canvas *canvas=glist_getcanvas(x->x_glist);

    if ( x->x_updatechild != 0 )
    {
       // post( "cooled~ : error : no update is possible for now" );
       return;
    }
    x->x_updatestart = bstart;
    x->x_updateend = bend;
    if ( !keepframe ) 
    {
       x->x_erase = 0;
    }
    else
    {
       x->x_erase = erase;
    }
    x->x_redraw = redraw;
    // recreate the square if needed
    if ( glist_isvisible( x->x_glist ) )
    {
      if ( ( bstart == 0 ) && ( bend == x->x_width-1 ) && !keepframe )
      {
        SYS_VGUI3(".x%x.c delete %xCOOLEDL\n", canvas, x );
        SYS_VGUI3(".x%x.c delete %xCOOLEDR\n", canvas, x );
        SYS_VGUI7(".x%x.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xCOOLEDR\n",
	     canvas, x->x_xpos, x->x_ypos,
	     x->x_xpos + x->x_width*x->x_zoom, 
             x->x_ypos + x->x_height/2*x->x_zoom,
	     x);
        SYS_VGUI7(".x%x.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xCOOLEDL\n",
	     canvas, x->x_xpos,
             x->x_ypos + x->x_height/2*x->x_zoom,
	     x->x_xpos + x->x_width*x->x_zoom, 
             x->x_ypos + x->x_height*x->x_zoom,
	     x);
        SYS_VGUI2("image delete COOLEDIMAGE%x\n", x );
        SYS_VGUI3(".x%x.c delete ICOOLEDIMAGE%x\n", canvas, x );
        SYS_VGUI4("image create photo COOLEDIMAGE%x -format gif -width %d -height %d\n", 
                          x, x->x_width*x->x_zoom, x->x_height*x->x_zoom );
        SYS_VGUI2("COOLEDIMAGE%x blank\n", x );
        SYS_VGUI6(".x%x.c create image %d %d -image COOLEDIMAGE%x -tags ICOOLEDIMAGE%x\n", 
                          canvas, 
                          x->x_xpos+(x->x_width*x->x_zoom)/2, 
                          x->x_ypos+(x->x_height*x->x_zoom)/2, x, x );
        canvas_fixlinesfor( canvas, (t_text*)x );
      }
      // set borders in red
      SYS_VGUI3(".x%x.c itemconfigure %xCOOLED -outline #FF0000\n", canvas, x);
    }

    // launch update thread
    if ( pthread_attr_init( &update_child_attr ) < 0 ) {
       post( "cooled~ : could not launch update thread" );
       perror( "pthread_attr_init" );
       return;
    }
    if ( pthread_attr_setdetachstate( &update_child_attr, PTHREAD_CREATE_DETACHED ) < 0 ) {
       post( "cooled~ : could not launch update thread" );
       perror( "pthread_attr_setdetachstate" );
       return;
    }
    if ( pthread_create( &x->x_updatechild, &update_child_attr, cooled_do_update_part, x ) < 0 ) {
       post( "cooled~ : could not launch update thread" );
       perror( "pthread_create" );
       return;
    }
    else 
    {
       // post( "cooled~ : drawing thread %d launched", (int)x->x_updatechild );
    }
}
 
static void cooled_draw_new(t_cooled *x, t_glist *glist)
{
 t_canvas *canvas=glist_getcanvas(glist);

   x->x_xpos=text_xpix(&x->x_obj, glist);
   x->x_ypos=text_ypix(&x->x_obj, glist);
   x->x_xdraw=text_xpix(&x->x_obj, glist);
   SYS_VGUI7(".x%x.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xCOOLEDR\n",
	     canvas, x->x_xpos, x->x_ypos,
	     x->x_xpos + x->x_width*x->x_zoom, 
             x->x_ypos + x->x_height/2*x->x_zoom,
	     x);
   SYS_VGUI7(".x%x.c create rectangle %d %d %d %d -fill #FFFFFF -tags %xCOOLEDL\n",
	     canvas, x->x_xpos,
             x->x_ypos + x->x_height/2*x->x_zoom,
	     x->x_xpos + x->x_width*x->x_zoom, 
             x->x_ypos + x->x_height*x->x_zoom,
	     x);
    SYS_VGUI4("image create photo COOLEDIMAGE%x -format gif -width %d -height %d\n", 
                          x, x->x_width*x->x_zoom, x->x_height*x->x_zoom );
    SYS_VGUI2("COOLEDIMAGE%x blank\n", x );
    SYS_VGUI6(".x%x.c create image %d %d -image COOLEDIMAGE%x -tags ICOOLEDIMAGE%x\n", 
                          canvas, 
                          x->x_xpos+(x->x_width*x->x_zoom)/2, 
                          x->x_ypos+(x->x_height*x->x_zoom)/2, x, x );
    if ( x->x_draw) cooled_update_part(x, x->x_glist, 0, x->x_width-1, 0, 1, 0);
    canvas_fixlinesfor( canvas, (t_text*)x );
}

static void cooled_draw_delete(t_cooled *x, t_glist *glist)
{
 t_canvas *canvas=glist_getcanvas(glist);

    if ( glist_isvisible( glist ) )
    {
       SYS_VGUI3( ".x%x.c delete %xCAPTURE\n", canvas, x );
       if ( ( x->x_readstart != 0 ) || ( x->x_readend != 100 ) )
       {
          SYS_VGUI3( ".x%x.c delete rectangle %xCLIPZONE\n", canvas, x);
       }
       SYS_VGUI3( ".x%x.c delete line %xINSERTHERE\n", canvas, x);
       SYS_VGUI3(".x%x.c delete %xCOOLEDR\n", canvas, x );
       SYS_VGUI3(".x%x.c delete %xCOOLEDL\n", canvas, x );
       SYS_VGUI3(".x%x.c delete ICOOLEDIMAGE%x\n", canvas, x );
       SYS_VGUI2("image delete COOLEDIMAGE%x\n", x );
    }
}

static void cooled_draw_move(t_cooled *x, t_glist *glist)
{
 t_canvas *canvas=glist_getcanvas(glist);

   if ( glist_isvisible( x->x_glist ) )
   {
    SYS_VGUI7(".x%x.c coords %xCOOLEDR %d %d %d %d\n",
	     canvas, x,
	     x->x_xpos, x->x_ypos,
	     x->x_xpos + x->x_width*x->x_zoom, 
             x->x_ypos + x->x_height/2*x->x_zoom);
    SYS_VGUI7(".x%x.c coords %xCOOLEDL %d %d %d %d\n",
	     canvas, x,
	     x->x_xpos,
             x->x_ypos + x->x_height/2*x->x_zoom,
	     x->x_xpos + x->x_width*x->x_zoom, 
             x->x_ypos + x->x_height*x->x_zoom );
    if ( ( x->x_readstart != 0 ) || ( x->x_readend != 100 ) )
    {
       SYS_VGUI7(".x%x.c coords %xCLIPZONE %d %d %d %d\n",
	     canvas, x,
             x->x_xpos+(int)(x->x_readstart*(x->x_width)/100*x->x_zoom ),
             x->x_ypos, 
             x->x_xpos+(int)(x->x_readend*(x->x_width)/100*x->x_zoom ), 
             x->x_ypos+x->x_height*x->x_zoom );
     }
     SYS_VGUI7(".x%x.c coords %xINSERTHERE %d %d %d %d\n",
	     canvas, x,
             x->x_xdraw,
             x->x_ypos, 
             x->x_xdraw,
             x->x_ypos+x->x_height*x->x_zoom );
     SYS_VGUI5(".x%x.c coords ICOOLEDIMAGE%x %d %d\n",
  	     canvas, x,
             x->x_xpos+((x->x_width*x->x_zoom)/2), 
             (x->x_ypos+((x->x_height*x->x_zoom)/2)) );
    canvas_fixlinesfor( canvas, (t_text*)x );
   }
}

static void cooled_draw_select(t_cooled* x,t_glist* glist)
{
 t_canvas *canvas=glist_getcanvas(glist);

   if ( glist_isvisible( x->x_glist ) )
   {
    if(x->x_selected)
    {
        /* sets the item in blue */
	SYS_VGUI3(".x%x.c itemconfigure %xCOOLEDR -outline #0000FF\n", canvas, x);
	SYS_VGUI3(".x%x.c itemconfigure %xCOOLEDL -outline #0000FF\n", canvas, x);
    }
    else
    {
	SYS_VGUI3(".x%x.c itemconfigure %xCOOLEDR -outline #000000\n", canvas, x);
	SYS_VGUI3(".x%x.c itemconfigure %xCOOLEDL -outline #000000\n", canvas, x);
    }
   }
}

/* ------------------------ widget callbacks ----------------------------- */


    /* setting the starting point for reading ( in percent ) */
static void cooled_readstart(t_cooled *x, t_floatarg fstart)
{
  t_float startpoint = fstart;
  t_canvas *canvas=glist_getcanvas(x->x_glist);

    if (startpoint < 0) startpoint = 0;
    if (startpoint > 100) startpoint = 100;
    x->x_readstart = startpoint;
    // set readspeed sign
    if ( ( x->x_readstart > x->x_readend ) && ( x->x_readspeed > 0 ) ) x->x_readspeed = -x->x_readspeed;
    if ( ( x->x_readstart < x->x_readend ) && ( x->x_readspeed < 0 ) ) x->x_readspeed = -x->x_readspeed;
    if ( glist_isvisible( x->x_glist ) )
    {
       SYS_VGUI3( ".x%x.c delete rectangle %xCLIPZONE\n",
                   canvas, x);
       if ( ( x->x_readstart != 0 ) || ( x->x_readend != 100 ) )
       {
          SYS_VGUI7( ".x%x.c create rectangle %d %d %d %d -outline #FF0000 -tags %xCLIPZONE -width 2\n",
                      canvas, x->x_xpos+(int)(x->x_readstart*x->x_width*x->x_zoom/100 ),
                          x->x_ypos, x->x_xpos+(int)(x->x_readend*x->x_width*x->x_zoom/100 ), 
                          x->x_ypos+x->x_height*x->x_zoom, x );
       }
    }
    outlet_float( x->x_sampstart, (x->x_readstart*x->x_size)/100 );
}

    /* setting the ending point for reading ( in percent ) */
static void cooled_readend(t_cooled *x, t_floatarg fend)
{
  t_float endpoint = fend;
  t_canvas *canvas=glist_getcanvas(x->x_glist);

    if (endpoint < 0) endpoint = 0;
    if (endpoint > 100) endpoint = 100;
    x->x_readend = endpoint;
    // set readspeed sign
    if ( ( x->x_readstart > x->x_readend ) && ( x->x_readspeed > 0 ) ) x->x_readspeed = -x->x_readspeed;
    if ( ( x->x_readstart < x->x_readend ) && ( x->x_readspeed < 0 ) ) x->x_readspeed = -x->x_readspeed;
    if ( glist_isvisible( x->x_glist ) )
    {
       SYS_VGUI3( ".x%x.c delete rectangle %xCLIPZONE\n",
                   canvas, x);
       if ( ( x->x_readstart != 0 ) || ( x->x_readend != 100 ) )
       {
          SYS_VGUI7( ".x%x.c create rectangle %d %d %d %d -outline #FF0000 -tags %xCLIPZONE -width 2\n",
               canvas, 
               x->x_xpos+(int)(x->x_readstart*x->x_width*x->x_zoom/100 ),
               x->x_ypos, x->x_xpos+(int)(x->x_readend*x->x_width*x->x_zoom/100 ), 
               x->x_ypos+x->x_height*x->x_zoom, x );
       }
    }
    outlet_float( x->x_sampend, (x->x_readend*x->x_size)/100 );
}

static void cooled_getrect(t_gobj *z, t_glist *owner,
			    int *xp1, int *yp1, int *xp2, int *yp2)
{
 t_cooled* x = (t_cooled*)z;

   *xp1 = x->x_xpos;
   *yp1 = x->x_ypos;
   *xp2 = x->x_xpos+x->x_width*x->x_zoom;
   *yp2 = x->x_ypos+x->x_height*x->x_zoom+1;
}

static void cooled_save(t_gobj *z, t_binbuf *b)
{
 t_cooled *x = (t_cooled *)z;

   binbuf_addv(b, "ssiisiiii", gensym("#X"),gensym("obj"),
		(int)x->x_obj.te_xpix, (int)x->x_obj.te_ypix,
		gensym("cooled~"), x->x_size, x->x_width, x->x_height, x->x_draw );
   binbuf_addv(b, ";");
}

static void cooled_select(t_gobj *z, t_glist *glist, int selected)
{
 t_cooled *x = (t_cooled *)z;

   x->x_selected = selected;
   cooled_draw_select( x, glist );
}

static void cooled_vis(t_gobj *z, t_glist *glist, int vis)
{
 t_cooled *x = (t_cooled *)z;
 t_rtext *y;

   if (vis)
   {
      cooled_draw_new( x, glist );
   }
   else
   {
      // erase all points
      cooled_draw_delete( x, glist );
   }
}

static void cooled_delete(t_gobj *z, t_glist *glist)
{
    canvas_deletelinesfor( glist_getcanvas(glist), (t_text *)z);
}

static void cooled_displace(t_gobj *z, t_glist *glist, int dx, int dy)
{
  t_cooled *x = (t_cooled *)z;
  int xold = x->x_xpos;
  int yold = x->x_ypos;

    x->x_xpos += dx;
    x->x_ypos += dy;

    if ( ( x->x_xpos != xold ) || ( x->x_ypos != yold ) )
    {
       cooled_draw_move( x, glist );
    }

}

static void cooled_motion(t_cooled *x, t_floatarg dx, t_floatarg dy)
{
   // post( "cooled_motion @ [%d,%d] dx=%f dy=%f alt=%d", x->x_xdraw, x->x_ydraw, dx, dy, x->x_alted );
   if ( dx != 0 )
   {
      cooled_readstart( x, ((t_float)( x->x_xdraw - x->x_xpos ) * 100 )/ (t_float)( x->x_width * x->x_zoom ) ); 
      x->x_edraw += dx;
      cooled_readend( x, ((t_float)( x->x_edraw - x->x_xpos ) * 100 )/ (t_float)( x->x_width * x->x_zoom )  ); 
   }
}

     /* erase data form readstart to readend */
static void cooled_erase( t_cooled *x )
{
  int startsamp, endsamp, si;
  int lreadstart = x->x_readstart, lreadend = x->x_readend;

    if (x->x_allocate) {
        post( "cooled~ : error : cannot erase while re-allocation" );
        return;
    }
    // post( "cooled~ : erase" );
    if ( lreadstart <= lreadend )
    {
       startsamp = (lreadstart*x->x_size)/100;
       endsamp = (lreadend*x->x_size)/100;
    }
    else
    {
       startsamp = (lreadend*x->x_size)/100;
       endsamp = (lreadstart*x->x_size)/100;
    }

    for ( si=startsamp; si<=endsamp; si++ )
    {
        *(x->x_rdata+si) = 0.;
        *(x->x_ldata+si) = 0.;
    }
    if ( x->x_draw ) cooled_update_part(x, x->x_glist, startsamp*x->x_width/x->x_size, endsamp*(x->x_width-1)/x->x_size, 0, 1, 1);
}
     /* paste data form readstart to readend */
static void cooled_paste( t_cooled *x )
{
  int startsamp, endsamp, si, inssamp, hlimit, csize;
  int lreadstart = x->x_readstart, lreadend = x->x_readend;

    if (x->x_allocate) {
        post( "cooled~ : error : cannot paste while re-allocation" );
        return;
    }
    if ( lreadstart <= lreadend )
    {
       startsamp = (lreadstart*x->x_size)/100;
       endsamp = (lreadend*x->x_size)/100;
    }
    else
    {
       startsamp = (lreadend*x->x_size)/100;
       endsamp = (lreadstart*x->x_size)/100;
    }
    
    // insert data at insertion point
    inssamp = ( x->x_xdraw - x->x_xpos) * x->x_size / ( x->x_width * x->x_zoom );
    // post( "cooled~ : replace [%d,%d] to %d", startsamp, endsamp, inssamp );
    csize=endsamp-startsamp;
    for ( si=0; si<=csize; si++ )
    {
        if ( ( si + inssamp >= x->x_size ) || ( startsamp + si >= x->x_size ) ) break;
        *(x->x_rsemp+si) = *(x->x_rdata+startsamp+si);
        *(x->x_lsemp+si) = *(x->x_ldata+startsamp+si);
    }
    hlimit = si;
    for ( si=0 ; si<=hlimit; si++ )
    { 
        *(x->x_rdata+inssamp+si) += *(x->x_rsemp+si);
        *(x->x_ldata+inssamp+si) += *(x->x_lsemp+si);
    }

    // post( "cooled~ : updating [%d,%d]", inssamp*x->x_width/x->x_size, (inssamp+hlimit)*x->x_width/x->x_size );
    if ( x->x_draw ) cooled_update_part(x, x->x_glist, inssamp*x->x_width/x->x_size, (inssamp+hlimit)*(x->x_width-1)/x->x_size, 0, 1, 1);
}
     /* replace data form readstart to readend */
static void cooled_replace( t_cooled *x )
{
  int startsamp, endsamp, si, inssamp, hlimit, csize;
  int lreadstart = x->x_readstart, lreadend = x->x_readend;

    if (x->x_allocate) {
        post( "cooled~ : error : cannot replace while re-allocation" );
        return;
    }
    if ( lreadstart <= lreadend )
    {
       startsamp = (lreadstart*x->x_size)/100;
       endsamp = (lreadend*x->x_size)/100;
    }
    else
    {
       startsamp = (lreadend*x->x_size)/100;
       endsamp = (lreadstart*x->x_size)/100;
    }
    
    // insert data at insertion point
    inssamp = ( x->x_xdraw - x->x_xpos) * x->x_size / ( x->x_width * x->x_zoom );
    // post( "cooled~ : replace [%d,%d] to %d", startsamp, endsamp, inssamp );
    csize=endsamp-startsamp;
    for ( si=0; si<=csize; si++ )
    {
        if ( ( si + inssamp >= x->x_size ) || ( startsamp + si >= x->x_size ) ) break;
        *(x->x_rsemp+si) = *(x->x_rdata+startsamp+si);
        *(x->x_lsemp+si) = *(x->x_ldata+startsamp+si);
    }
    hlimit = si;
    for ( si=0 ; si<=hlimit; si++ )
    { 
        *(x->x_rdata+inssamp+si) = *(x->x_rsemp+si);
        *(x->x_ldata+inssamp+si) = *(x->x_lsemp+si);
    }

    // post( "cooled~ : updating [%d,%d]", inssamp*x->x_width/x->x_size, (inssamp+hlimit)*x->x_width/x->x_size );
    if ( x->x_draw ) cooled_update_part(x, x->x_glist, inssamp*x->x_width/x->x_size, (inssamp+hlimit)*(x->x_width-1)/x->x_size, 0, 1, 1);

}

	/* call editor's property dialog */
static void cooled_properties(t_gobj *z, t_glist *owner)
{
   char buf[800];
   t_cooled *x=(t_cooled *)z;

   sprintf(buf, "pdtk_cooled_dialog %%s %d %d %d\n",
            x->x_width, x->x_height, x->x_draw);
   // post("cooled_properties : %s", buf );
   gfxstub_new(&x->x_obj.ob_pd, x, buf);
}

	/* handle properties change */
static void cooled_dialog(t_cooled *x, t_symbol *s, int argc, t_atom *argv)
{
   if ( !x ) {
     post( "cooled~ : error :tried to set properties on an unexisting object" );
   }
   if ( argc != 3 )
   {
      post( "cooled~ : error in the number of arguments ( %d instead of 3 )", argc );
      return;
   }
   if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT || argv[2].a_type != A_FLOAT ) {
      post( "cooled~ : wrong arguments" );
      return;
   }
   cooled_draw_delete(x, x->x_glist);
   if ( x->x_gifdata != NULL ) {
       freebytes(x->x_gifdata, x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       post( "Freed %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       x->x_gifdata = NULL;
   }
   if ( x->x_guicommand != NULL ) {
       freebytes(x->x_guicommand, 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       post( "Freed %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       x->x_guicommand = NULL;
   }
   x->x_width = (int)argv[0].a_w.w_float;
   if ( x->x_width < 0 ) x->x_width = 100;
   x->x_height = (int)argv[1].a_w.w_float;
   if ( x->x_height < 0 ) x->x_height = 100;
   x->x_draw = (int)argv[2].a_w.w_float;
   if ( !(x->x_gifdata = ( char* ) getbytes( x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return;
   } else {
       post( "cooled~ : allocated %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
   }
   if ( !(x->x_guicommand = ( char* ) getbytes( 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return;
   } else {
       post( "cooled~ : allocated %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
   }
   cooled_draw_new(x, x->x_glist);
}

	/* handle clicks */
static int cooled_click(t_gobj *z, struct _glist *glist,
			    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
 t_cooled* x = (t_cooled *)z;
 int pipos;
 t_canvas *canvas=glist_getcanvas(x->x_glist);

    // post( "cooled_click : x=%d y=%d doit=%d alt=%d, shift=%d", xpix, ypix, doit, alt, shift );
    if ( doit )
    {
      if ( shift && alt )
      {
         cooled_paste(x);
      }
      else if ( shift )
      {
         cooled_replace(x);
      }
      else if ( alt )
      {
         cooled_erase(x);
      }
      else
      {
         x->x_xdraw = xpix; 
         x->x_edraw = xpix; 
         x->x_shifted = shift;
         x->x_alted = alt;
         // activate motion callback
         glist_grab( glist, &x->x_obj.te_g, (t_glistmotionfn)cooled_motion,
                  0, xpix, ypix );

         // draw insertion line
         if ( glist_isvisible( x->x_glist ) )
         {
            SYS_VGUI3( ".x%x.c delete line %xINSERTHERE\n",
                        canvas, x);
            SYS_VGUI7( ".x%x.c create line %d %d %d %d -fill #00FFFF -tags %xINSERTHERE -width 2\n",
                        canvas, x->x_xdraw,
               x->x_ypos, x->x_xdraw,
               x->x_ypos+x->x_height*x->x_zoom, x );
          }

      }

    }
    else
    {
         // nothing
    }
    return (1);

}

	/* clean up */
static void cooled_free(t_cooled *x)    
{
    if ( x->x_rdata != NULL ) {
       freebytes(x->x_rdata, x->x_size*sizeof(float) );
       post( "Freed %d bytes", x->x_size*sizeof(float) );
       x->x_rdata = NULL;
    }
    if ( x->x_ldata != NULL ) {
       freebytes(x->x_ldata, x->x_size*sizeof(float) );
       post( "Freed %d bytes", x->x_size*sizeof(float) );
       x->x_ldata = NULL;
    }
    if ( x->x_rsemp != NULL ) {
       freebytes(x->x_rsemp, x->x_size*sizeof(float) );
       post( "Freed %d bytes", x->x_size*sizeof(float) );
       x->x_rsemp = NULL;
    }
    if ( x->x_lsemp != NULL ) {
       freebytes(x->x_lsemp, x->x_size*sizeof(float) );
       post( "Freed %d bytes", x->x_size*sizeof(float) );
       x->x_lsemp = NULL;
    }
    if ( x->x_gifdata != NULL ) {
       freebytes(x->x_gifdata, x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       post( "Freed %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       x->x_gifdata = NULL;
    }
    if ( x->x_guicommand != NULL ) {
       freebytes(x->x_guicommand, 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       post( "Freed %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       x->x_guicommand = NULL;
    }
}

    /* allocate tables for storing sound and temporary copy */
static int cooled_allocate(t_cooled *x)
{
  int fi;

    if ( !(x->x_rdata = getbytes( x->x_size*sizeof(float) ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return -1;
    } else {
       post( "cooled~ : allocated %d bytes", x->x_size*sizeof(float) );
    }
    if ( !(x->x_ldata = getbytes( x->x_size*sizeof(float) ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return -1;
    } else {
       post( "cooled~ : allocated %d bytes", x->x_size*sizeof(float) );
    }
    if ( !(x->x_rsemp = getbytes( x->x_size*sizeof(float) ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return -1;
    } else {
       post( "cooled~ : allocated %d bytes", x->x_size*sizeof(float) );
    }
    if ( !(x->x_lsemp = getbytes( x->x_size*sizeof(float) ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return -1;
    } else {
       post( "cooled~ : allocated %d bytes", x->x_size*sizeof(float) );
    }
    if ( !(x->x_gifdata = ( char* ) getbytes( x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return -1;
    } else {
       post( "cooled~ : allocated %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
    }
    if ( !(x->x_guicommand = ( char* ) getbytes( 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return -1;
    } else {
       post( "cooled~ : allocated %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
    }
    
    return 0;
}

    /* allocate tables for storing sound and temporary copy */
static int cooled_reallocate(t_cooled *x, int ioldsize, int inewsize)
{
  int fi, csize;
  t_float *prdata=x->x_rdata, *pldata=x->x_ldata, *prsemp=x->x_rsemp, *plsemp=x->x_lsemp;

    if ( !(x->x_rdata = getbytes( inewsize*sizeof(float) ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return -1;
    } else {
       post( "cooled~ : allocated %d bytes", inewsize*sizeof(float) );
    }
    if ( !(x->x_ldata = getbytes( inewsize*sizeof(float) ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return -1;
    } else {
       post( "cooled~ : allocated %d bytes", inewsize*sizeof(float) );
    }
    if ( !(x->x_rsemp = getbytes( inewsize*sizeof(float) ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return -1;
    } else {
       post( "cooled~ : allocated %d bytes", inewsize*sizeof(float) );
    }
    if ( !(x->x_lsemp = getbytes( inewsize*sizeof(float) ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return -1;
    } else {
       post( "cooled~ : allocated %d bytes", inewsize*sizeof(float) );
    }

    if ( ioldsize <= inewsize )
    {
      csize = ioldsize;
    }
    else
    {
      csize = inewsize;
    }
    memcpy( x->x_rdata, prdata, csize*sizeof(float) );
    memcpy( x->x_ldata, pldata, csize*sizeof(float) );
    memcpy( x->x_rsemp, prsemp, csize*sizeof(float) );
    memcpy( x->x_lsemp, plsemp, csize*sizeof(float) );

    if ( prdata != NULL ) {
       freebytes(prdata, ioldsize*sizeof(float) );
       post( "Freed %d bytes", ioldsize*sizeof(float) );
    }
    if ( pldata != NULL ) {
       freebytes(pldata, ioldsize*sizeof(float) );
       post( "Freed %d bytes", ioldsize*sizeof(float) );
    }
    if ( prsemp != NULL ) {
       freebytes(prsemp, ioldsize*sizeof(float) );
       post( "Freed %d bytes", ioldsize*sizeof(float) );
    }
    if ( plsemp != NULL ) {
       freebytes(plsemp, ioldsize*sizeof(float) );
       post( "Freed %d bytes", ioldsize*sizeof(float) );
    }
    
    return 0;
}

    /* records and playback the sound  */
static t_int *cooled_perform(t_int *w)
{
    t_float *lin = (t_float *)(w[1]);
    t_float *rin = (t_float *)(w[2]);
    t_float *rout = (t_float *)(w[3]);
    t_float *lout = (t_float *)(w[4]);
    int   is;
    int n = (int)(w[5]);                      /* number of samples */
    int startsamp, endsamp;
    t_cooled *x = (t_cooled *)(w[6]);
    int lreadstart = x->x_readstart, lreadend = x->x_readend;

    if ( lreadstart <= lreadend )
    {
       startsamp = (lreadstart*x->x_size)/100;
       endsamp = (lreadend*x->x_size)/100;
    }
    else
    {
       startsamp = (lreadend*x->x_size)/100;
       endsamp = (lreadstart*x->x_size)/100;
    }

    while (n--) 
    {
        // eventually records input
        if ( x->x_record ) {
           *(x->x_ldata+x->x_writepos)=*(lin);
           *(x->x_rdata+x->x_writepos)=*(rin);
           x->x_writepos++;
           if ( x->x_writepos >= x->x_size ) {
             x->x_record=0;
             x->x_writepos=0;
             if ( x->x_draw ) cooled_update_part(x, x->x_glist, 0, x->x_width-1, 0, 1, 0);
             outlet_bang(x->x_recend);
             if ( x->x_empty ) x->x_empty = 0;
             // post( "cooled~ : stopped recording" );
           }
        }
        // set outputs
        *rout = 0.0;
        *lout = 0.0;
        if ( x->x_play) {
            is=0;
            *lout = *(x->x_ldata+(int)x->x_readpos);
            *rout = *(x->x_rdata+(int)x->x_readpos);
            x->x_readpos+=x->x_readspeed;
            // post( "xreadpos : %f (added %f) %d", x->x_readpos, x->x_readspeed, x->x_readend );
            if ( ( x->x_readspeed >= 0 ) && ( x->x_readpos >= endsamp ) ) {
              x->x_play=0;
              x->x_readpos=(float)(startsamp);
              // post( "cooled~ : stopped playing (readpos=%d)", x->x_readpos );
              outlet_bang(x->x_end);
            }
            if ( ( x->x_readspeed < 0 ) && ( x->x_readpos <= startsamp ) ) {
              x->x_play=0;
              x->x_readpos = (float)(endsamp);
              // post( "cooled~ : stopped playing (readpos=%d)", x->x_readpos );
              outlet_bang(x->x_end);
            }
        }
        rout++;lout++;
        rin++;lin++;
    }
    // post( "cooled~ : read : %f:%d : write: %d:%d", x->x_readpos, x->x_play, x->x_writepos, x->x_record );
    return (w+7);
}

static void cooled_dsp(t_cooled *x, t_signal **sp)
{
    dsp_add(cooled_perform, 6, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, sp[0]->s_n, x);
}

    /* record the cooled */
static void cooled_record(t_cooled *x)
{
    if (x->x_allocate) {
        post( "cooled~ : error : cannot record while re-allocation" );
        return;
    }
    x->x_record=1;
    x->x_writepos=0;
    post( "cooled~ : recording on" );
}

    /* map to stereo */
static void cooled_stereo(t_cooled *x)
{
  int si;

    if (x->x_allocate) {
        post( "cooled~ : error : cannot map to stereo while re-allocation" );
        return;
    }
    for ( si=0; si<x->x_size; si++ )
    {
        *(x->x_rdata+si) = *(x->x_ldata+si);
    }
    if ( x->x_draw ) cooled_update_part(x, x->x_glist, 0, x->x_width-1, !x->x_empty, !x->x_empty, 0);
}

    /* play the cooled */
static void cooled_play(t_cooled *x)
{
    x->x_play=1;
    // post( "cooled~ : playing on" );
}

    /* sets the reading speed */
static void cooled_readspeed(t_cooled *x, t_floatarg freadspeed)
{
    x->x_readspeed=freadspeed;
}

    /* resize cooled */
static void cooled_resize(t_cooled *x, t_floatarg fnewsize )
{
    if (fnewsize <= 0) {
        post( "cooled~ : error : wrong size" );
        return;
    }
    if (x->x_allocate) {
        post( "cooled~ : error : cannot resize while re-allocation" );
        return;
    }
    if (x->x_updatechild > 0) {
        post( "cooled~ : can't resize now, an update is pending." );
        return;
    }
    post( "cooled~ : reallocating tables" );
    x->x_allocate = 1;
    x->x_play = 0;
    x->x_record = 0;
    cooled_readstart( x, 0);
    cooled_readend( x, 100);
    cooled_reallocate(x, x->x_size, fnewsize);
    x->x_size = fnewsize;
    x->x_empty = 1;
    if ( x->x_readstart <= x->x_readend )
    {
       x->x_readpos = (x->x_readstart*x->x_size)/100;
    }
    else
    {
       x->x_readpos = (x->x_readend*x->x_size)/100;
    }
    // erase all points, as data is zero no drawing is needed
    if ( x->x_draw) cooled_update_part(x, x->x_glist, 0, x->x_width-1, 0, !x->x_empty, 1);
    x->x_allocate = 0;
}

    /* set zoom factor */
static void cooled_zoom(t_cooled *x, t_floatarg fzoom )
{
    if (fzoom < 1) {
        post( "cooled~ : error : wrong zoom factor" );
        return;
    }
    if (x->x_allocate) {
        post( "cooled~ : error : cannot zoom while re-allocation" );
        return;
    }
    if ( x->x_gifdata != NULL ) {
       freebytes(x->x_gifdata, x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       post( "Freed %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       x->x_gifdata = NULL;
    }
    if ( x->x_guicommand != NULL ) {
       freebytes(x->x_guicommand, 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       post( "Freed %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
       x->x_guicommand = NULL;
    }
    x->x_zoom = (int)fzoom;
    if ( !(x->x_gifdata = ( char* ) getbytes( x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return;
    } else {
       post( "cooled~ : allocated %d bytes", x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
    }
    if ( !(x->x_guicommand = ( char* ) getbytes( 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") ) ) ) {
       post( "cooled~ : error : could not allocate buffers" );
       return;
    } else {
       post( "cooled~ : allocated %d bytes", 128+x->x_height*x->x_zoom*sizeof("#FFFFFF ") );
    }
    if ( x->x_draw) cooled_update_part(x, x->x_glist, 0, x->x_width-1, !x->x_empty, !x->x_empty, 0);
    canvas_fixlinesfor( glist_getcanvas( x->x_glist ), (t_text*)x );
}

    /* modify the loop positions */
static void cooled_loop(t_cooled *x, t_symbol *soperator )
{
  char *operator = soperator->s_name;
  int ci;
  t_float fvalue, freadstart = x->x_readstart, freadend = x->x_readend;

    if ( (soperator->s_name[0] != '*')  &&    
         (soperator->s_name[0] != '/')  &&    
         (soperator->s_name[0] != '>')  &&    
         (soperator->s_name[0] != '<')  ) {
        post( "cooled~ : error : wrong operator argument : should be *|/|>|<" );
        return;
    }
    for ( ci=1; ci<(int)strlen( soperator->s_name ); ci++ )
    {
      if ( !(isdigit(soperator->s_name[ci])||(soperator->s_name[ci]=='.')) ) {
        post( "cooled~ : error : wrong operation value : %s : should be in a float format", soperator->s_name+1 );
        return;
      }
    }
    if ( sscanf( soperator->s_name+1, "%f", &fvalue ) != 1 ) {
        post( "cooled~ : error : can't get operation value" );
        return;
    }
    switch( soperator->s_name[0] )
    {
       case '*' :
	 freadend = x->x_readstart + fvalue*(x->x_readend-x->x_readstart);
         break;

       case '/' :
         if ( fvalue != 0 ) { freadend = x->x_readstart + (x->x_readend-x->x_readstart)/fvalue; };
         break;

       case '>' :
	 freadstart = x->x_readstart + fvalue*(x->x_readend-x->x_readstart);
	 freadend = x->x_readend + fvalue*(x->x_readend-x->x_readstart);
         break;

       case '<' :
	 freadstart = x->x_readstart - fvalue*(x->x_readend-x->x_readstart);
	 freadend = x->x_readend - fvalue*(x->x_readend-x->x_readstart);
         break;
    }
    if ( freadstart < 0.0 ) freadstart = 0.0;
    if ( freadend < 0.0 ) freadend = 0.0;
    if ( freadstart > 100.0 ) freadstart = 100.0;
    if ( freadend > 100.0 ) freadend = 100.0;
    cooled_readstart( x, freadstart);
    cooled_readend( x, freadend);
}

    /* refresh data    */
static void cooled_refresh(t_cooled *x)
{
    if (x->x_allocate) {
        post( "cooled~ : error : cannot refresh while re-allocation" );
        return;
    }
    if ( x->x_draw ) cooled_update_part(x, x->x_glist, 0, x->x_width-1, 0, 1, 1);
}

static void *cooled_new(t_symbol *s, int argc, t_atom *argv)
{
    t_cooled *x = (t_cooled *)pd_new(cooled_class);
    outlet_new(&x->x_obj, &s_signal);
    outlet_new(&x->x_obj, &s_signal);
    x->x_recend = outlet_new(&x->x_obj, &s_bang );
    x->x_end = outlet_new(&x->x_obj, &s_bang );
    x->x_sampstart = outlet_new(&x->x_obj, &s_float );
    x->x_sampend = outlet_new(&x->x_obj, &s_float );
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("readstart"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("readend"));
    
    if ( argc != 0 )
    {
      if ( argc < 3 )
      {
        post( "audience~ : error in the number of arguments ( %d )", argc );
        return NULL;
      }
      if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT ||
           argv[2].a_type != A_FLOAT ) {
        post( "audience~ : wrong arguments" );
        return NULL;
      }
      x->x_size = (int)argv[0].a_w.w_float;
      if ( x->x_size < 1 ) x->x_size = 1;
      x->x_width = (int)argv[1].a_w.w_float;
      if ( x->x_width < 10 ) x->x_width = 10;
      x->x_height = (int)argv[2].a_w.w_float;
      if ( x->x_height < 10 ) x->x_height = 10;
      if ( argc == 3 ) 
         x->x_draw = 1;
      else
         x->x_draw = (int)argv[3].a_w.w_float;
    }
    else
    {
      x->x_size = COOLED_DEFAULT_SIZE;
      x->x_width = COOLED_DEFAULT_WIDTH;
      x->x_height = COOLED_DEFAULT_HEIGHT;
      x->x_draw = 1;
    }

    // activate graphical callbacks
    class_setwidget(cooled_class, &cooled_widgetbehavior);
    
    x->x_play = 0;
    x->x_readspeed = 1.;
    x->x_record = 0;
    x->x_allocate = 0;
    x->x_readpos = 0.;
    x->x_writepos = 0;
    x->x_rdata = NULL;
    x->x_ldata = NULL;
    x->x_empty = 1;
    x->x_xpos = -1;
    x->x_ypos = -1;
    /* graphic data */
    x->x_selected = 0;
    x->x_zoom = 1;
    x->x_updatechild = 0;
    x->x_glist = (t_glist*)canvas_getcurrent();
    // post( "cooled~ : new : readend=%d", x->x_readend );
    cooled_readstart( x, 0);
    cooled_readend( x, 100);

    if ( cooled_allocate(x) <0 ) {
      return NULL;
    } else {
      return(x);
    }

}

void cooled_tilde_setup(void)
{
#include "cooled~.tk2c"
    post(cooled_version);
    cooled_class = class_new(gensym("cooled~"), (t_newmethod)cooled_new, (t_method)cooled_free,
                    sizeof(t_cooled), 0, A_GIMME, 0);
    class_sethelpsymbol( cooled_class, gensym("cooled~.pd") );

    // set callbacks
    cooled_widgetbehavior.w_getrectfn =    cooled_getrect;
    cooled_widgetbehavior.w_displacefn =   cooled_displace;
    cooled_widgetbehavior.w_selectfn =     cooled_select;
    cooled_widgetbehavior.w_activatefn =   NULL;
    cooled_widgetbehavior.w_deletefn =     cooled_delete;
    cooled_widgetbehavior.w_visfn =        cooled_vis;
    cooled_widgetbehavior.w_clickfn =      cooled_click;

    class_setpropertiesfn(cooled_class, cooled_properties);
    class_setsavefn(cooled_class, cooled_save);

    CLASS_MAINSIGNALIN( cooled_class, t_cooled, x_f );
    class_addmethod(cooled_class, (t_method)cooled_dsp, gensym("dsp"), A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_record, gensym("record"), A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_resize, gensym("resize"), A_FLOAT, A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_zoom, gensym("zoom"), A_FLOAT, A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_play, gensym("play"), A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_refresh, gensym("refresh"), A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_readstart, gensym("readstart"), A_FLOAT, A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_stereo, gensym("stereo"), A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_readend, gensym("readend"), A_FLOAT, A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_readspeed, gensym("readspeed"), A_FLOAT, A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_loop, gensym("loop"), A_SYMBOL, A_NULL);
    class_addmethod(cooled_class, (t_method)cooled_dialog, gensym("dialog"), A_GIMME, 0);
}