aboutsummaryrefslogtreecommitdiff
path: root/disarrain~.c
blob: ce680a5ae2c9900edda188feba18beff33a23bbf (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
#include "MSPd.h"
#include "fftease.h"

#if MSP
void *disarrain_class;
#endif 

#if PD
static t_class *disarrain_class;
#endif

#define OBJECT_NAME "disarrain~"


typedef struct _disarrain
{
#if MSP
  t_pxobject x_obj;
#endif
#if PD
  t_object x_obj;
  float x_f;
#endif  
  int R;
  int	N;
  int	N2;
  int	Nw;
  int	Nw2; 
  int	D; 
  int	i;
  int	inCount;
  float *Wanal;	
  float *Wsyn;	
  float *input;	
  float *Hwin;
  float *buffer;
  float *channel;
  float *last_channel;
  float *composite_channel;
  float *output;
  int overlap;
  int winfac;
  //

  int *shuffle_mapping;
  int *last_shuffle_mapping;
  int *shuffle_tmp; // work space for making a new distribution
  int shuffle_count;// number of bins to swap
  int last_shuffle_count;// ditto from last shuffle mapping
  int max_bin;
  //
  float mult; 
  float *trigland;
  int *bitshuffle;
  //
  void *list_outlet;
  t_atom *list_data;
  short mute;
  short bypass;
  float frame_duration; // duration in seconds of a single frame
  float interpolation_duration; // duration in seconds of interpolation
  int interpolation_frames; // number of frames to interpolate
  int frame_countdown; // keep track of position in interpolation
  int overlap_factor;// determines window size, etc.
  float top_frequency;// for remapping spectrum
  int perform_method;// 0 for lean, 1 for full conversion
  // for convert
//  float *c_lastphase_in;
//  float *c_lastphase_out;
  float c_fundamental;
  float c_factor_in;
  float c_factor_out;
  // check switching algorithm
  short lock;// lock for switching mapping arrays, but not used now
  short force_fade; // new fadetime set regardless of situation
  short force_switch;// binds new distribution to change of bin count
} t_disarrain;

void *disarrain_new(t_symbol *msg, short argc, t_atom *argv);

t_int *disarrain_perform_lean(t_int *w);
t_int *disarrain_perform_full(t_int *w);
void disarrain_dsp(t_disarrain *x, t_signal **sp, short *count);
void disarrain_assist(t_disarrain *x, void *b, long m, long a, char *s);
void disarrain_switch_count (t_disarrain *x, t_floatarg i);
void disarrain_topfreq (t_disarrain *x, t_floatarg freq);
void disarrain_fadetime (t_disarrain *x, t_floatarg f);
void reset_shuffle( t_disarrain *x );
void disarrain_showstate( t_disarrain *x );
void disarrain_list (t_disarrain *x, t_symbol *msg, short argc, t_atom *argv);
void disarrain_setstate (t_disarrain *x, t_symbol *msg, short argc, t_atom *argv);
void disarrain_isetstate (t_disarrain *x, t_symbol *msg, short argc, t_atom *argv);
int rand_index(int max);
void disarrain_mute(t_disarrain *x, t_floatarg toggle);
void disarrain_bypass(t_disarrain *x, t_floatarg toggle);
void copy_shuffle_array(t_disarrain *x);
void interpolate_frames_to_channel(t_disarrain *x);
void disarrain_killfade(t_disarrain *x);
void disarrain_forcefade(t_disarrain *x, t_floatarg toggle);
void disarrain_init(t_disarrain *x, short initialized);
void disarrain_free(t_disarrain *x);
void disarrain_overlap(t_disarrain *x, t_floatarg o);
void disarrain_winfac(t_disarrain *x, t_floatarg o);
void disarrain_fftinfo(t_disarrain *x);
void disarrain_force_switch(t_disarrain *x, t_floatarg toggle);


#if MSP
void main(void)
{
  setup((t_messlist **)&disarrain_class, (method)disarrain_new, (method)disarrain_free, 
  (short)sizeof(t_disarrain), 0, A_GIMME, 0);
  addmess((method)disarrain_dsp, "dsp", A_CANT, 0);
  addint((method)disarrain_switch_count);
  addbang((method)reset_shuffle);
  addmess((method)disarrain_showstate,"showstate",0);
  addmess ((method)disarrain_list, "list", A_GIMME, 0);
  addmess ((method)disarrain_setstate, "setstate", A_GIMME, 0);
  addmess ((method)disarrain_isetstate, "isetstate", A_GIMME, 0);
  addmess((method)disarrain_assist,"assist",A_CANT,0);
  addmess ((method)disarrain_mute, "mute", A_FLOAT, 0);
  addmess ((method)disarrain_topfreq, "topfreq", A_FLOAT, 0);
  addmess ((method)disarrain_fadetime, "fadetime", A_FLOAT, 0);
  addmess ((method)disarrain_bypass, "bypass", A_FLOAT, 0);
  addmess ((method)disarrain_forcefade, "forcefade", A_FLOAT, 0);
  addmess ((method)disarrain_force_switch, "force_switch", A_FLOAT, 0);
  addmess ((method)disarrain_switch_count, "switch_count", A_FLOAT, 0);
  addmess ((method)disarrain_killfade, "killfade", 0);
  addmess ((method)reset_shuffle, "reset_shuffle", 0);
  
  addmess((method)disarrain_overlap, "overlap",  A_DEFFLOAT, 0);
  addmess((method)disarrain_winfac, "winfac",  A_DEFFLOAT, 0);
  addmess((method)disarrain_fftinfo, "fftinfo", 0);
  dsp_initclass();
  post("%s %s",OBJECT_NAME,FFTEASE_ANNOUNCEMENT);
}
#endif

#if PD
void disarrain_tilde_setup(void)
{
  disarrain_class = class_new(gensym("disarrain~"), (t_newmethod)disarrain_new, 
			 (t_method)disarrain_free ,sizeof(t_disarrain), 0,A_GIMME,0);
  CLASS_MAINSIGNALIN(disarrain_class, t_disarrain, x_f);
  class_addmethod(disarrain_class, (t_method)disarrain_dsp, gensym("dsp"), 0);
  class_addmethod(disarrain_class, (t_method)disarrain_showstate, gensym("showstate"), 0);
  class_addmethod(disarrain_class, (t_method)disarrain_list, gensym("list"), A_GIMME, 0);
  class_addmethod(disarrain_class, (t_method)disarrain_setstate, gensym("setstate"), A_GIMME, 0);
  class_addmethod(disarrain_class, (t_method)disarrain_isetstate, gensym("isetstate"), A_GIMME, 0);

  class_addmethod(disarrain_class, (t_method)disarrain_mute, gensym("mute"), A_FLOAT, 0);
  class_addmethod(disarrain_class, (t_method)disarrain_topfreq, gensym("topfreq"), A_FLOAT, 0);
  class_addmethod(disarrain_class, (t_method)disarrain_fadetime, gensym("fadetime"), A_FLOAT, 0);
  class_addmethod(disarrain_class, (t_method)disarrain_bypass, gensym("bypass"), A_FLOAT, 0);
  class_addmethod(disarrain_class, (t_method)disarrain_forcefade, gensym("forcefade"), A_FLOAT, 0);
class_addmethod(disarrain_class, (t_method)disarrain_force_switch, gensym("force_switch"), A_FLOAT, 0);


//  class_addmethod(disarrain_class, (t_method)disarrain_killfade, gensym("reset"), A_FLOAT, 0);
  class_addmethod(disarrain_class, (t_method)reset_shuffle, gensym("bang"),  0);
  class_addmethod(disarrain_class, (t_method)reset_shuffle, gensym("reset_shuffle"),  0);
  class_addmethod(disarrain_class, (t_method)disarrain_switch_count, gensym("switch_count"), A_FLOAT, 0);
  
  class_addmethod(disarrain_class, (t_method)disarrain_overlap, gensym("overlap"), A_DEFFLOAT,0);
  class_addmethod(disarrain_class, (t_method)disarrain_winfac, gensym("winfac"), A_DEFFLOAT,0);
  class_addmethod(disarrain_class, (t_method)disarrain_fftinfo, gensym("fftinfo"), 0);
	post("%s %s",OBJECT_NAME,FFTEASE_ANNOUNCEMENT);
}
#endif

void disarrain_free(t_disarrain *x)
{
#if MSP
    dsp_free((t_pxobject *) x);
#endif
     freebytes(x->Wanal, x->Nw * sizeof(float));	
     freebytes(x->Wsyn, x->Nw * sizeof(float));	
     freebytes(x->input, x->Nw * sizeof(float));	
     freebytes(x->Hwin, x->Nw * sizeof(float));
     freebytes(x->buffer, x->N * sizeof(float));
     freebytes(x->channel, (x->N+2) * sizeof(float));
     freebytes(x->last_channel, (x->N+2) * sizeof(float));
     freebytes(x->composite_channel, x->N+2 * sizeof(float));
     freebytes(x->output, x->Nw * sizeof(float));
     freebytes(x->bitshuffle, (x->N * 2) * sizeof(int));
     freebytes(x->trigland, x->N * 2 * sizeof(float));
     freebytes(x->shuffle_mapping, x->N2 * sizeof(int)) ;
     freebytes(x->last_shuffle_mapping, x->N2 * sizeof(int)) ;
     freebytes(x->shuffle_tmp, x->N2 * sizeof(int)) ;
     freebytes(x->list_data,(x->N+2) * sizeof(t_atom)) ;
//     freebytes(x->c_lastphase_in, (x->N2+1)*sizeof(float));
//     freebytes(x->c_lastphase_out,(x->N2+1)* sizeof(float));
}



void disarrain_init(t_disarrain *x, short initialized)
{
  int i;
  float curfreq;

  if(!fftease_power_of_two(x->winfac)){
    x->winfac = 1;
  }
  if(!fftease_power_of_two(x->overlap)){
    x->overlap = 4;
  }	
  x->N = x->D * x->overlap;
  x->Nw = x->N * x->winfac;	
  limit_fftsize(&x->N,&x->Nw,OBJECT_NAME);
  x->c_fundamental =  (float) x->R/(float)( (x->N2)<<1 );
  x->c_factor_in =  (float) x->R/((float)x->D * TWOPI);
  x->c_factor_out = TWOPI * (float)  x->D / (float) x->R;

  x->N2 = (x->N)>>1;
  x->Nw2 = (x->Nw)>>1;
  x->inCount = -(x->Nw);
  x->mult = 1. / (float) x->N;
  
  x->lock = 1; // not good enough
  
  if(initialized == 0){
    x->Wanal = (float *) calloc(MAX_Nw , sizeof(float));	
    x->Wsyn = (float *) calloc(MAX_Nw , sizeof(float));	
    x->input = (float *) calloc(MAX_Nw , sizeof(float));	
    x->Hwin = (float *) calloc(MAX_Nw , sizeof(float));
    x->buffer = (float *) calloc(MAX_N , sizeof(float));
    x->channel = (float *) calloc(MAX_N+2 , sizeof(float));
    x->last_channel = (float *) calloc(MAX_N+2 , sizeof(float));
    x->composite_channel = (float *) calloc(MAX_N+2 , sizeof(float));
    x->output = (float *) calloc(MAX_Nw , sizeof(float));
    x->bitshuffle = (int *) calloc((MAX_N * 2) , sizeof(int));
    x->trigland = (float *) calloc(MAX_N * 2 , sizeof(float));
    x->shuffle_mapping = (int *) calloc( MAX_N2 , sizeof(int) ) ;
    x->last_shuffle_mapping = (int *) calloc( MAX_N2 , sizeof(int) ) ;
    x->shuffle_tmp = (int *) calloc( MAX_N2 , sizeof(int) ) ;
    x->list_data = (t_atom *) calloc((MAX_N+2) , sizeof(t_atom) ) ;
    x->mute = 0;
    x->bypass = 0;
    x->force_fade = 0;
    x->interpolation_duration = 0.1; //seconds
  } 
		memset((char *)x->input,0,x->Nw * sizeof(float));
		memset((char *)x->output,0,x->Nw * sizeof(float));
		memset((char *)x->buffer,0,x->N * sizeof(float));
		memset((char *)x->channel,0,(x->N+2) * sizeof(float));
		memset((char *)x->last_channel,0,(x->N+2) * sizeof(float));


  init_rdft( x->N, x->bitshuffle, x->trigland);
  makewindows( x->Hwin, x->Wanal, x->Wsyn, x->Nw, x->N, x->D);
 
  
  if(initialized != 2){
	  if( x->top_frequency < x->c_fundamental || x->top_frequency > 20000) {
	    x->top_frequency = 1000.0 ;
	  }
	  x->max_bin = 1;  
	  curfreq = 0;
	  while( curfreq < x->top_frequency ) {
	    ++(x->max_bin);
	    curfreq += x->c_fundamental ;
	  }
	  for( i = 0; i < x->N2; i++ ) {
	    x->shuffle_mapping[i] = x->last_shuffle_mapping[i] = i*2;
	  }
	  reset_shuffle(x); // set shuffle lookup
	  copy_shuffle_array(x);// copy it to the last lookup (for interpolation)
	  x->frame_duration = (float) x->D / (float) x->R;
	  x->interpolation_frames = x->interpolation_duration / x->frame_duration;
	  x->frame_countdown = 0;
	  x->shuffle_count = 0;
	  x->last_shuffle_count = 0;
  }
  x->lock = 0;
 }


void disarrain_force_switch(t_disarrain *x, t_floatarg f)
{
	x->force_switch = (short)f;
}

void disarrain_fadetime (t_disarrain *x, t_floatarg f)
{
  int frames;
  float duration;
  
  // forcefade allows forcing new fadetime at any time
  if(! x->force_fade) {
#if MSP
    if(!sys_getdspstate()){
      return; // DSP is inactive
    }
#endif
    if(x->frame_countdown) {
      error("disarrain: fade in progress, fadetime reset blocked");
      return;
    }
  }
  
  duration = f * .001;
  frames = duration / x->frame_duration;
  if( frames <= 1){
    error("%s: too short fade",OBJECT_NAME);
    return;
  }
  x->interpolation_duration = f * .001;
  x->interpolation_frames = frames;

}

void disarrain_killfade(t_disarrain *x)
{
  x->frame_countdown = 0;

}

void disarrain_topfreq (t_disarrain *x, t_floatarg freq)
{
  float funda = (float) x->R / (2. * (float) x->N) ;
  float curfreq;
 
  if( freq < funda || freq > 20000) {
    freq = 1000.0 ;
  }
  x->max_bin = 1;  
  curfreq = 0;
  while( curfreq < freq ) {
    ++(x->max_bin);
    curfreq += funda ;
  }
}

void disarrain_assist (t_disarrain *x, void *b, long msg, long arg, char *dst)
{
  if (msg==1) {
    switch (arg) {
    case 0: sprintf(dst,"(signal) Input"); break;
    }
  } else if (msg==2) {
    switch (arg) {
    case 0:	sprintf(dst,"(signal) Output"); break;
    case 1: sprintf(dst,"(signal) Interpolation Sync"); break;
    case 2: sprintf(dst,"(list) Current State"); break;
    }
  }
}

void *disarrain_new(t_symbol *msg, short argc, t_atom *argv)
{

#if MSP
  t_disarrain *x = (t_disarrain *)newobject(disarrain_class);
  x->list_outlet = listout((t_pxobject *)x);
  dsp_setup((t_pxobject *)x,1);
  outlet_new((t_pxobject *)x, "signal");
  outlet_new((t_pxobject *)x, "signal");
#endif

#if PD
  t_disarrain *x = (t_disarrain *)pd_new(disarrain_class);
  outlet_new(&x->x_obj, gensym("signal"));
  outlet_new(&x->x_obj, gensym("signal"));
  x->list_outlet = outlet_new(&x->x_obj,gensym("list"));
#endif

  srand(time(0));

  x->D = sys_getblksize();
  x->R = sys_getsr(); 
  x->top_frequency = atom_getfloatarg(0,argc,argv);
  x->overlap_factor = atom_getintarg(1,argc,argv);
  x->winfac = atom_getintarg(2,argc,argv);

  disarrain_init(x,0);
  return (x);
}

void disarrain_forcefade(t_disarrain *x, t_floatarg toggle)
{
  x->force_fade = (short)toggle;	
}

void disarrain_mute(t_disarrain *x, t_floatarg toggle)
{
  x->mute = (short)toggle;	
}

void disarrain_bypass(t_disarrain *x, t_floatarg toggle)
{
  x->bypass = (short)toggle;	
}

void disarrain_overlap(t_disarrain *x, t_floatarg df)
{
int o = (int)df;
  if(!fftease_power_of_two(o)){
    error("%d is not a power of two",o);
    return;
  }
  x->overlap = (int)o;
  disarrain_init(x,1);
}

void disarrain_winfac(t_disarrain *x, t_floatarg f)
{
int wf = (int)f;
  if(!fftease_power_of_two(wf)){
    error("%f is not a power of two",wf);
    return;
  }
  x->winfac = wf;
  disarrain_init(x,2); /* calling lighter reinit routine */
}

void disarrain_fftinfo( t_disarrain *x )
{
  if( ! x->overlap ){
    post("zero overlap!");
    return;
  }
  post("%s: FFT size %d, hopsize %d, windowsize %d", OBJECT_NAME, x->N, x->N/x->overlap, x->Nw);
}


// lean convert perform method
t_int *disarrain_perform_lean(t_int *w)
{
  t_disarrain *x = (t_disarrain *) (w[1]);
  t_float *in = (t_float *)(w[2]);
  t_float *out = (t_float *)(w[3]);
  t_float *vec_sync = (t_float *)(w[4]);
  int n = w[5];
  int R = x->R;
  int Nw = x->Nw;
  int N = x->N ;
  int N2 = x-> N2;
  int Nw2 = x->Nw2;
  float *Wanal = x->Wanal;
  float *Wsyn = x->Wsyn;
  float *Hwin = x->Hwin;
  	
  float *input = x->input; 
  float *output = x->output;
  float *buffer = x->buffer;
  float *channel = x->channel;
  float *last_channel = x->last_channel;
  int		i,j;
  int inCount = x->inCount;
				
  int	D = x->D;
  float tmp;
  float ival = 0.0;
  int *shuffle_mapping = x->shuffle_mapping;
  int shuffle_count = x->shuffle_count;
  int *last_shuffle_mapping = x->last_shuffle_mapping;
  int last_shuffle_count = x->last_shuffle_count;	
  float mult = x->mult ;
  int *bitshuffle = x->bitshuffle;
  float *trigland = x->trigland ;
  int frame_countdown = x->frame_countdown; // will read from variable
  int interpolation_frames = x->interpolation_frames;
  if( x->mute || x->lock ){
    while( n-- ){
      *out++ = 0.0;
    }
    return (w+6); 
  }
  if( x->bypass ){
    while( n-- ){
      *out++ = *in++ * 0.5; // gain compensation
    }
    return (w+6);
  }

  inCount += D;

  for ( j = 0 ; j < Nw - D ; j++ ){
    input[j] = input[j+D];
  }
  for ( j = Nw - D; j < Nw; j++ ) {
    input[j] = *in++;
  }

  fold(input, Wanal, Nw, buffer, N, inCount);	
  rdft(N, 1, buffer, bitshuffle, trigland);
    
  leanconvert(buffer, channel, N2);

  // first time for interpolation, just do last frame 

  if(frame_countdown == interpolation_frames){

    for( i = 0, j = 0; i < last_shuffle_count ; i++, j+=2){
      tmp = channel[j];
      channel[j] = channel[last_shuffle_mapping[i]];
      channel[last_shuffle_mapping[i]] = tmp;
    }
    --frame_countdown;
  } 
  else if( frame_countdown > 0 ){
    ival = (float)frame_countdown/(float)interpolation_frames;
    // copy current frame to lastframe
    for(j = 0; j < N; j+=2){
      last_channel[j] = channel[j];
    }	
    // make last frame swap
    for(i = 0, j = 0; i < last_shuffle_count ; i++, j+=2){
      tmp = last_channel[j];
      last_channel[j] = last_channel[last_shuffle_mapping[i]];
      last_channel[last_shuffle_mapping[i]] = tmp;

    }	
    // make current frame swap
    for( i = 0, j = 0; i < shuffle_count ; i++, j+=2){
      tmp = channel[j];
      channel[j]  = channel[shuffle_mapping[i]];
      channel[shuffle_mapping[i]]  = tmp;

    }
    // now interpolate between the two

    for(j = 0; j < N; j+=2){
      channel[j] = channel[j] + ival * (last_channel[j] - channel[j]);
    }
		
    --frame_countdown;
    if(frame_countdown <= 0){
      copy_shuffle_array(x);
    }
  } else {
    // otherwise straight swapping
    for( i = 0, j = 0; i < shuffle_count ; i++, j+=2){
      tmp = channel[j];
      channel[j]  = channel[ shuffle_mapping[i]];
      channel[shuffle_mapping[i]] = tmp;     
    }
  }
  leanunconvert( channel, buffer,  N2 );

  rdft( N, -1, buffer, bitshuffle, trigland );
  overlapadd( buffer, N, Wsyn, output, Nw, inCount);

  for ( j = 0; j < D; j++ )
    *out++ = output[j] * mult;

  for ( j = 0; j < Nw - D; j++ )
    output[j] = output[j+D];
			
  for ( j = Nw - D; j < Nw; j++ )
    output[j] = 0.;
/* send out sync signal */
  for(j = 0; j < n; j++){
  	vec_sync[j] = ival;
  }
  /* restore state variables */
  x->inCount = inCount % Nw;
  x->frame_countdown = frame_countdown;
  return (w+6);
}		


void interpolate_frames_to_channel(t_disarrain *x)
{
  float ival;
  float tmp;
  int i,j;
  int frame_countdown = x->frame_countdown;
  int interpolation_frames = x->interpolation_frames;
  float *channel = x->channel;
  float *last_channel = x->last_channel;
  int *shuffle_mapping = x->shuffle_mapping;
  int shuffle_count = x->shuffle_count;
  int *last_shuffle_mapping = x->shuffle_mapping;
  int last_shuffle_count = x->shuffle_count;	
  int local_max_bins;
  int N = x->N;
	
  ival = (float)frame_countdown/(float)interpolation_frames;

//  post("interpolation:%f",ival);
  local_max_bins = (shuffle_count > last_shuffle_count)? shuffle_count : last_shuffle_count;
  // copy channel (only amplitudes)
  for(j = 0; j < N; j+=2){
    last_channel[j] = channel[j];
  }
  // make last frame
  for( i = 0, j = 0; i < last_shuffle_count ; i++, j+=2){
    tmp = last_channel[j];
    last_channel[j] = last_channel[last_shuffle_mapping[i]];
    last_channel[last_shuffle_mapping[i]] = tmp;
  }
  // make current frame
  for( i = 0, j = 0; i < shuffle_count ; i++, j+=2){
    tmp = channel[j];
    channel[j]  = channel[shuffle_mapping[i]];
    channel[shuffle_mapping[i]]  = tmp;
  }
  // now interpolate between the two

  for(j = 0; j < N; j+=2){
    // channel[j] = channel[j] + ival * (last_channel[j] - channel[j]);
		
    // or better?
    channel[j] += ival * (last_channel[j] - channel[j]);
  }
}


void disarrain_switch_count (t_disarrain *x, t_floatarg f)
{
int i = f;
/*
#if MSP
  if(! sys_getdspstate()){
    return; // DSP is inactive
  }
#endif
*/
  if( x->frame_countdown && !x->force_fade){
    error("%s: fade in progress, no action taken",OBJECT_NAME);
    return;
  }
  if( i < 0 ){
    i = 0;
  }
  if( i > x->N2 ) {
    i = x->N2;
  }
  copy_shuffle_array(x);
  x->last_shuffle_count = x->shuffle_count;
  x->shuffle_count = i;
  x->frame_countdown = x->interpolation_frames; // force interpolation
}


void reset_shuffle (t_disarrain *x)
{
  int i;
  int temp, p1, p2;
  int max = x->max_bin;
  int  N2 = x->N2;
  
  int *shuffle_tmp = x->shuffle_tmp;
  int *shuffle_mapping = x->shuffle_mapping;

  
  copy_shuffle_array(x);

  for( i = 0; i < N2; i++ ) {
    shuffle_tmp[i] = i;
  }
  // improve this algorithm
		
  for( i = 0; i < max; i++ ) {
    p1 = shuffle_tmp[ rand_index( max ) ];
    p2 = shuffle_tmp[ rand_index( max ) ];
    temp = shuffle_tmp[p1];
    shuffle_tmp[ p1 ] = shuffle_tmp[ p2 ];
    shuffle_tmp[ p2 ] = temp;
  }
	
	
	
  // now map to amplitude channels
  for( i = 0; i < N2; i++ ) {
    shuffle_tmp[i] *= 2;
  }
	
  // force interpolation
  x->frame_countdown = x->interpolation_frames;	
  
  x->lock = 1;
  for( i = 0; i < N2; i++ ) {
    shuffle_mapping[i] = shuffle_tmp[i];
  }

  x->lock = 0;
}

void copy_shuffle_array(t_disarrain *x)
{
  int i;
  int N2 = x->N2;
  int *shuffle_mapping = x->shuffle_mapping;
  int *last_shuffle_mapping = x->last_shuffle_mapping;	


  for(i = 0; i<N2; i++){
    last_shuffle_mapping[i] = shuffle_mapping[i];
  }
  x->last_shuffle_count = x->shuffle_count;

}

int rand_index(int max) {

  return (rand() % max);
}

void disarrain_dsp(t_disarrain *x, t_signal **sp, short *count)
{
  long i;

  if(x->D != sp[0]->s_n ||x->D != sp[0]->s_n ) {
    x->R = sp[0]->s_sr;
    x->D = sp[0]->s_n;
    disarrain_init(x,1);
  }

  dsp_add(disarrain_perform_lean, 5, x, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);

}

// ENTER STORED SHUFFLE
void disarrain_list (t_disarrain *x, t_symbol *msg, short argc, t_atom *argv) {
  short i;
  int ival;
//  post("list message called");
  x->shuffle_count = argc;
//  post("list: count now %d",x->shuffle_count );
  for (i=0; i < argc; i++) {
  #if MSP
    ival = argv[i].a_w.w_long;
  #endif
  
  #if PD
    ival = atom_getfloatarg(i,argc,argv);
  #endif
    if (ival < x->N2) {
      x->shuffle_mapping[i] = ival;
//      post("set %d to %d",i, x->shuffle_mapping[ i ]);
    } else {
//      post ("%d out of range",ival);
    }
		
  }
//  post("last val is %d", x->shuffle_mapping[argc - 1]);
  return;
}


void disarrain_isetstate (t_disarrain *x, t_symbol *msg, short argc, t_atom *argv) {
  short i;
  int ival;
  
//  x->last_shuffle_count = x->shuffle_count;
  
  copy_shuffle_array(x);
  x->shuffle_count = argc;
  
  
//  x->lock = 1;
    
  for (i=0; i < argc; i++) {
    ival = 2 * atom_getfloatarg(i,argc,argv);

    if ( ival < x->N2 && ival >= 0) {
      x->shuffle_mapping[ i ] = ival;
    }else {
    	error("%s: %d is out of range",OBJECT_NAME, ival);
    }
  }

//  x->lock = 0;
  x->frame_countdown = x->interpolation_frames;
  
  return;
}

void disarrain_setstate (t_disarrain *x, t_symbol *msg, short argc, t_atom *argv) {
  short i;
  int ival;
  
  x->shuffle_count = argc;
  for (i=0; i < argc; i++) {
	  ival = 2 *atom_getfloatarg(i,argc,argv);

    if ( ival < x->N2 && ival >= 0) {
      x->shuffle_mapping[ i ] = ival;
    } else {
    	error("%s: %d is out of range",OBJECT_NAME, ival);
    }
  }
  return;
}

// REPORT CURRENT SHUFFLE STATUS
void disarrain_showstate (t_disarrain *x ) {

  t_atom *list_data = x->list_data;

  short i;
#if MSP
  for( i = 0; i < x->shuffle_count; i++ ) {
    SETLONG(list_data+i,x->shuffle_mapping[i]/2);
  }
#endif

#if PD
  for( i = 0; i < x->shuffle_count; i++ ) {
    SETFLOAT(list_data+i,(float)x->shuffle_mapping[i]/2);
  }
#endif	
  outlet_list(x->list_outlet,0,x->shuffle_count,list_data);

  return;
}