-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffet~.c
2147 lines (1835 loc) · 57.4 KB
/
buffet~.c
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
#include "MSPd.h"
#include "fftease.h"
// adapted for Pd
#define CUSHION_FRAMES (128) // pad for grabbing
#define MAX_CHANNELS (2)
#define MAX_RMS_BUFFER (0.250)
#define MIN_RMS_BUFFER (.001)
#define MAX_EVENTS (1024)
#define MAX_RMS_FRAMES (32768)
#define OBJECT_NAME "buffet~"
static t_class *buffet_class;
typedef struct {
float *b_samples;
long b_valid;
long b_nchans;
long b_frames;
} t_guffer; // stuff we care about from garrays and buffers
typedef struct _buffet
{
t_object x_obj;
float x_f;
t_symbol *wavename; // name of waveform buffer
t_guffer *wavebuf; // holds waveform samples
t_guffer *destbuf; // for copying to another buffer
float sr; // sampling rate
short hosed; // buffers are bad
float minframes; // minimum replacement block in sample frames
float maxframes; // maximum replacement block in sample frames
long storage_maxframes; // maxframe limit that current memory can handle
float *storage; //temporary memory to store replacement block (set to maxframes * channels)
long storage_bytes; // amount of currently allocated memory
float fade; // fadein/fadeout time in sample frames
float sync; // input from groove sync signal
long swapframes; // number of frames in swap block
long r1startframe; //start frame for block 1
long r2startframe; // start frame for block 2
float dc_coef; // filter coefficient
float dc_gain; // normalization factor
short initialized; // first time or not
float *rmsbuf;// for onset analysis
float rmschunk; // store lowest rms value in buffer
void *list; // for start/end list
void *bang; // completion bang
void *floater; // outputs noise floor
t_atom *listdata;// to report est. start/stop times of events in buffer
float *analbuf; // contain overall envelope
float *onset; // contain attack times for percussive evaluations
short autoredraw; // to kill redraw if it impacts performance
} t_buffet;
void buffet_setbuf(t_buffet *x, t_symbol *wavename);
void *buffet_new(t_symbol *msg, short argc, t_atom *argv);
t_int *buffet_perform(t_int *w);
void buffet_dsp(t_buffet *x, t_signal **sp);
float buffet_boundrand(float min, float max);
void buffet_assist (t_buffet *x, void *b, long msg, long arg, char *dst);
void buffet_dsp_free(t_buffet *x);
void buffet_swap(t_buffet *x);
void buffet_specswap(t_buffet *x, t_symbol *msg, short argc, t_atom *argv);
void buffet_retroblock(t_buffet *x);
void buffet_nakedswap(t_buffet *x);
void buffet_overlap(t_buffet *x, t_floatarg f);
void buffet_minswap(t_buffet *x, t_floatarg f);
void buffet_maxswap(t_buffet *x, t_floatarg f);
void buffet_nosync_setswap(t_buffet *x);
void buffet_info(t_buffet *x);
void buffet_killdc(t_buffet *x);
void buffet_rmschunk(t_buffet *x, t_symbol *msg, short argc, t_atom *argv);
void buffet_fadein(t_buffet *x, t_floatarg f);
void buffet_fadeout(t_buffet *x, t_floatarg f);
void buffet_internal_fadein(t_buffet *x, t_symbol *msg, short argc, t_atom *argv);
void buffet_internal_fadeout(t_buffet *x, t_symbol *msg, short argc, t_atom *argv);
void buffet_dc_gain(t_buffet *x, t_floatarg f);
void buffet_dc_coef(t_buffet *x, t_floatarg f);
void buffet_normalize(t_buffet *x, t_floatarg f);
void buffet_rotatetozero(t_buffet *x, t_floatarg f);
void buffet_autoredraw(t_buffet *x, t_floatarg f);
void buffet_erase(t_buffet *x, t_symbol *msg, short argc, t_atom *argv);
void buffet_events(t_buffet *x, t_symbol *msg, short argc, t_atom *argv);
void buffet_pevents(t_buffet *x, t_symbol *msg, short argc, t_atom *argv);
void buffet_detect_onsets(t_buffet *x, t_symbol *msg, short argc, t_atom *argv);
void buffet_detect_subband_onsets(t_buffet *x, t_symbol *msg, short argc, t_atom *argv);
void buffet_copy_to_buffer(t_buffet *x, t_symbol *msg, short argc, t_atom *argv);
int buffet_setdestbuf(t_buffet *x, t_symbol *wavename);
void buffet_init(t_buffet *x, short initialized);
void buffet_reverse(t_buffet *x);
void buffet_redraw(t_buffet *x);
void buffet_redraw_named(t_buffet *x, t_symbol *arrayname);
void buffet_update(t_buffet *x);
void buffet_tilde_setup(void)
{
buffet_class = class_new(gensym("buffet~"),(t_newmethod)buffet_new,(t_method)buffet_dsp_free, sizeof(t_buffet),
0, A_GIMME,0);
CLASS_MAINSIGNALIN(buffet_class,t_buffet, x_f );
class_addmethod(buffet_class,(t_method)buffet_dsp,gensym("dsp"),A_CANT,0);
class_addmethod(buffet_class,(t_method)buffet_swap,gensym("swap"),0);
class_addmethod(buffet_class,(t_method)buffet_specswap,gensym("specswap"),A_GIMME,0);
class_addmethod(buffet_class,(t_method)buffet_events,gensym("events"),A_GIMME,0);
class_addmethod(buffet_class,(t_method)buffet_pevents,gensym("pevents"),A_GIMME,0);
class_addmethod(buffet_class,(t_method)buffet_detect_onsets,gensym("detect_onsets"),A_GIMME,0);
class_addmethod(buffet_class,(t_method)buffet_detect_subband_onsets,gensym("detect_subband_onsets"),A_GIMME,0);
class_addmethod(buffet_class,(t_method)buffet_retroblock,gensym("retroblock"),0);
class_addmethod(buffet_class,(t_method)buffet_minswap,gensym("minswap"),A_FLOAT,0);
class_addmethod(buffet_class,(t_method)buffet_maxswap,gensym("maxswap"),A_FLOAT,0);
class_addmethod(buffet_class,(t_method)buffet_fadein,gensym("fadein"),A_FLOAT,0);
class_addmethod(buffet_class,(t_method)buffet_fadeout,gensym("fadeout"),A_FLOAT,0);
class_addmethod(buffet_class,(t_method)buffet_overlap,gensym("overlap"),A_FLOAT,0);
class_addmethod(buffet_class,(t_method)buffet_dc_gain,gensym("dc_gain"),A_FLOAT,0);
class_addmethod(buffet_class,(t_method)buffet_dc_coef,gensym("dc_coef"),A_FLOAT,0);
class_addmethod(buffet_class,(t_method)buffet_normalize,gensym("normalize"),A_FLOAT,0);
class_addmethod(buffet_class,(t_method)buffet_rotatetozero,gensym("rotatetozero"),A_FLOAT,0);
class_addmethod(buffet_class,(t_method)buffet_autoredraw,gensym("autoredraw"),A_FLOAT,0);
class_addmethod(buffet_class,(t_method)buffet_killdc,gensym("killdc"),0);
class_addmethod(buffet_class,(t_method)buffet_nakedswap,gensym("nakedswap"),0);
class_addmethod(buffet_class,(t_method)buffet_rmschunk,gensym("rmschunk"),A_GIMME,0);
class_addmethod(buffet_class,(t_method)buffet_erase,gensym("erase"),A_GIMME,0);
class_addmethod(buffet_class,(t_method)buffet_copy_to_buffer,gensym("copy_to_buffer"),A_GIMME,0);
class_addmethod(buffet_class,(t_method)buffet_internal_fadein,gensym("internal_fadein"),A_GIMME,0);
class_addmethod(buffet_class,(t_method)buffet_internal_fadeout,gensym("internal_fadeout"),A_GIMME,0);
class_addmethod(buffet_class,(t_method)buffet_setbuf,gensym("setbuf"),A_SYMBOL,0);
class_addmethod(buffet_class,(t_method)buffet_reverse,gensym("reverse"),0);
class_addmethod(buffet_class,(t_method)buffet_info,gensym("info"),0);
potpourri_announce(OBJECT_NAME);
}
void buffet_info(t_buffet *x)
{
long totalframes;
buffet_setbuf(x, x->wavename);
if( x->hosed ){
error("buffet~ needs a valid buffer");
return;
}
if( ! x->sr){
error("zero sample rate!");
return;
}
totalframes = x->wavebuf->b_frames;
post("minswap: %f, maxswap: %f", 1000. * x->minframes / x->sr, 1000. * x->maxframes / x->sr);
post("buffer size: %f", 1000. * totalframes / x->sr);
}
void buffet_overlap(t_buffet *x, t_floatarg f)
{
if( f < 0.01 ){
error("minimum fade time is 0.01 milliseconds");
return;
}
x->fade = f * .001 * x->sr;
}
void buffet_events(t_buffet *x, t_symbol *msg, short argc, t_atom *argv)
{
float *b_samples;
long b_nchans;
long b_frames;
t_atom *listdata = x->listdata;
float bufsize;
float onthresh;
float offthresh;
long bufsamps;
long aframes; // frames to analyze
float tadv;
long i,j;
float meansq;
float rmsval;
long bindex;
float ipos;
short activated = 0;
float realtime = 0.0;
int event_count = 0;
float buffer_duration;
buffet_setbuf(x, x->wavename);
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
b_frames = x->wavebuf->b_frames;
// duration in ms.
buffer_duration = 1000.0 * (float)b_frames / x->sr;
bufsize = .001 * atom_getfloatarg(0,argc,argv);
if(bufsize > MAX_RMS_BUFFER){
bufsize = MAX_RMS_BUFFER;
post("%s: setting analysis buffer to maximum: %f",OBJECT_NAME, MAX_RMS_BUFFER * 1000.0);
} else if(bufsize < MIN_RMS_BUFFER){
bufsize = MIN_RMS_BUFFER;
post("%s: setting analysis buffer to maximum: %f",OBJECT_NAME, MIN_RMS_BUFFER * 1000.0);
}
onthresh = atom_getfloatarg(1,argc,argv);
offthresh = atom_getfloatarg(2,argc,argv);
bufsamps = x->sr * bufsize;
bufsamps = bufsize * x->sr;
tadv = (float)bufsamps / x->sr;
// post("actual window size: %f",tadv);
aframes = (long) ( (float) b_frames / (float)bufsamps ) - 1;
if(aframes < 2){
error("%s: this buffer is too short to analyze",OBJECT_NAME);
return;
}
// post("analyzing %d frames",aframes);
for(i = 0; i < aframes; i++){
meansq = 0.0;
ipos = b_nchans * i * bufsamps;
/* only analyze first channel */
for(j = 0; j < bufsamps; j+= b_nchans){
bindex = ipos + j;
meansq += b_samples[bindex] * b_samples[bindex];
}
meansq /= (float) bufsamps;
rmsval = sqrt(meansq);
realtime += tadv;
if(rmsval > onthresh && ! activated) {
activated = 1;
// post("event %d starts at %f",event_count+1, realtime);
if(event_count >= MAX_EVENTS){
error("%s: exceeded maximum of %d events",OBJECT_NAME, MAX_EVENTS);
break;
}
SETFLOAT(listdata+(event_count*2), realtime * 1000.0);
}
else if( rmsval < offthresh && activated ){
activated = 0;
// post("event %d ends at %f",event_count, realtime);
SETFLOAT(listdata+((event_count*2) + 1), realtime * 1000.0);
++event_count;
}
}
if(activated){
post("%s: missed the end of the last event; setting to end of buffer",OBJECT_NAME);
SETFLOAT(listdata+((event_count*2) + 1), buffer_duration);
++event_count;
}
outlet_list(x->list, 0, event_count * 2, listdata);
}
void buffet_pevents(t_buffet *x, t_symbol *msg, short argc, t_atom *argv)
{
float *b_samples;
long b_nchans;
long b_frames;
t_atom *listdata = x->listdata;
float bufsize;
//float onthresh;
//float offthresh;
float diffthresh;
long bufsamps;
long aframes; // frames to analyze
float tadv;
//long current_frame = 0;
long i,j;
float meansq;
float rmsval;
long bindex;
float ipos;
//short activated = 0;
float realtime = 0.0;
int event_count = 0;
float buffer_duration;
float mindiff, maxdiff, absdiff,rmsdiff;
float *analbuf = x->analbuf;
float *onset = x->onset;
//float endtime;
buffet_setbuf(x, x->wavename);
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
b_frames = x->wavebuf->b_frames;
// duration in ms.
buffer_duration = 1000.0 * (float)b_frames / x->sr;
bufsize = .001 * atom_getfloatarg(0,argc,argv);
if(bufsize > MAX_RMS_BUFFER){
bufsize = MAX_RMS_BUFFER;
post("%s: setting analysis buffer to maximum: %f",OBJECT_NAME, MAX_RMS_BUFFER * 1000.0);
} else if(bufsize < MIN_RMS_BUFFER){
bufsize = MIN_RMS_BUFFER;
post("%s: setting analysis buffer to maximum: %f",OBJECT_NAME, MIN_RMS_BUFFER * 1000.0);
}
diffthresh = atom_getfloatarg(1,argc,argv);
bufsamps = x->sr * bufsize;
bufsamps = bufsize * x->sr;
tadv = (float)bufsamps / x->sr;
aframes = (long) ( (float) b_frames / (float)bufsamps );
if(aframes < 2){
error("%s: this buffer is too short to analyze",OBJECT_NAME);
return;
}
if(aframes > MAX_RMS_FRAMES){
post("too many frames: try a larger buffer size");
return;
}
analbuf[0] = 0; // for first comparison
for(i = 1; i < aframes; i++){
meansq = 0.0;
ipos = b_nchans * i * bufsamps;
/* only analyze first channel */
for(j = 0; j < bufsamps; j+= b_nchans){
bindex = ipos + j;
meansq += b_samples[bindex] * b_samples[bindex];
}
meansq /= (float) bufsamps;
analbuf[i] = rmsval = sqrt(meansq);
realtime += tadv;
}
realtime = 0;
mindiff = 9999.;
maxdiff = 0.0;
/* LPF - use elsewhere?
for(i = 1; i < aframes; i++ ){
analbuf[i] = (analbuf[i] + analbuf[i-1]) * 0.5;
}
*/
// look for big changes in direction
for(i = 1; i < aframes; i++ ){
rmsdiff = analbuf[i] - analbuf[i-1];
absdiff = fabs(rmsdiff);
if(absdiff > maxdiff)
maxdiff = absdiff;
if(absdiff < mindiff)
mindiff = absdiff;
if( rmsdiff > diffthresh ){
// new
onset[event_count] = (realtime + bufsize) * 1000.0;
if(onset[event_count] < 0)
onset[event_count] = 0;
++event_count;
// post("rt %f diff %f",realtime * 1000.0,rmsdiff);
}
realtime += tadv;
}
// post("mindiff %f maxdiff %f",mindiff,maxdiff);
if(event_count == 0){
post("%s: no events found",OBJECT_NAME);
}
for(i = 0; i < event_count; i++){
SETFLOAT(listdata + i, onset[i]);
}
outlet_list(x->list, 0, event_count, listdata);
}
void buffet_internal_fadeout(t_buffet *x, t_symbol *msg, short argc, t_atom *argv)
{
long fadeframes;
long totalframes;
int i,j,k;
float env;
float *b_samples;
long b_nchans;
long startframe;
long endframe;
if( ! x->sr){
error("zero sample rate!");
return;
}
buffet_setbuf(x, x->wavename);
if(x->hosed){
return;
}
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
totalframes = x->wavebuf->b_frames;
if(argc < 2){
post("%s: internal_fadeout requires start and end times",OBJECT_NAME);
return;
}
startframe = .001 * x->sr * atom_getfloatarg(0,argc,argv);
endframe = .001 * x->sr * atom_getfloatarg(1,argc,argv);
if(startframe < 0 || endframe > totalframes || endframe <= startframe){
error("%s: bad frame boundaries to internal_fadeout: %ld and %ld",OBJECT_NAME,startframe, endframe);
return;
}
fadeframes = endframe - startframe;
for(i = (endframe-1) * b_nchans , k = 0; k < fadeframes ; i -= b_nchans, k++ ){
env = (float) k / (float) fadeframes;
for(j = 0; j < b_nchans; j++){
b_samples[i + j] *= env;
}
}
buffet_update(x);
}
void buffet_internal_fadein(t_buffet *x, t_symbol *msg, short argc, t_atom *argv)
{
long fadeframes;
long totalframes;
int i,j,k;
float env;
float *b_samples;
long b_nchans;
long startframe;
long endframe;
if( ! x->sr){
error("zero sample rate!");
return;
}
buffet_setbuf(x, x->wavename);
if(x->hosed){
return;
}
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
totalframes = x->wavebuf->b_frames;
if(argc < 2){
post("%s: internal_fadeout requires start and end times",OBJECT_NAME);
return;
}
startframe = .001 * x->sr * atom_getfloatarg(0,argc,argv);
endframe = .001 * x->sr * atom_getfloatarg(1,argc,argv);
if(startframe < 0 || endframe > totalframes || endframe <= startframe){
error("%s: bad frame boundaries to internal_fadein: %ld and %ld",OBJECT_NAME,startframe, endframe);
return;
}
fadeframes = endframe - startframe;
for(i = startframe * b_nchans , k = 0; k < fadeframes ; i += b_nchans, k++ ){
env = (float) k / (float) fadeframes;
for(j = 0; j < b_nchans; j++){
b_samples[i + j] *= env;
}
}
buffet_update(x);
}
void buffet_reverse(t_buffet *x)
{
int i,j;
// float env;
float *b_samples;
long b_nchans;
long b_frames;
float tmpsamp;
long lenm1;
buffet_setbuf(x, x->wavename);
if(!x->wavebuf->b_valid){
return;
}
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
b_frames = x->wavebuf->b_frames;
lenm1 = (b_frames - 1) * b_nchans;
for( i = 0; i < (b_frames * b_nchans) / 2; i += b_nchans){
for(j = 0; j < b_nchans; j++){
tmpsamp = b_samples[(lenm1 - i) + j];
b_samples[(lenm1 - i) + j] = b_samples[i + j];
b_samples[i + j] = tmpsamp;
}
}
buffet_update(x);
}
void buffet_update(t_buffet *x)
{
outlet_bang(x->bang);
if(x->autoredraw){
buffet_redraw(x);
}
}
void buffet_redraw_named(t_buffet *x, t_symbol *arrayname)
{
t_garray *a;
if (!(a = (t_garray *)pd_findbyclass(arrayname, garray_class))) {
if (*arrayname->s_name) pd_error(x, "%s: %s: no such array",OBJECT_NAME, arrayname->s_name);
}
else {
garray_redraw(a);
}
}
void buffet_redraw(t_buffet *x)
{
t_garray *a;
if (!(a = (t_garray *)pd_findbyclass(x->wavename, garray_class))) {
if (*x->wavename->s_name) pd_error(x, "%s: %s: no such array",OBJECT_NAME, x->wavename->s_name);
x->wavebuf->b_valid = 0;
}
else {
garray_redraw(a);
}
}
void buffet_copy_to_buffer(t_buffet *x, t_symbol *msg, short argc, t_atom *argv)
{
t_symbol *destname;
float *b_samples;
long b_nchans;
long b_frames;
float *b_dest_samples;
long b_dest_nchans;
long b_dest_frames;
long startframe;
long endframe;
long chunkframes;
float fadein,fadeout;
int fadeframes;
// totalframes;
float env;
int i,j,k;
buffet_setbuf(x, x->wavename);
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
b_frames = x->wavebuf->b_frames;
destname = atom_getsymbolarg(0,argc,argv);
if(! buffet_setdestbuf(x, destname)){
post("%s: could not find buffer %s",OBJECT_NAME,destname->s_name);
return;
}
b_dest_samples = x->destbuf->b_samples;
b_dest_nchans = x->destbuf->b_nchans;
b_dest_frames = x->destbuf->b_frames;
startframe = atom_getfloatarg(1,argc,argv) * .001 * x->sr;
endframe = atom_getfloatarg(2,argc,argv) * .001 * x->sr;
chunkframes = endframe - startframe;
if(chunkframes <= 0){
return;
}
if(b_nchans != b_dest_nchans){
error("%s: channel mismatch with buffer %s",OBJECT_NAME, destname->s_name);
return;
}
if(b_dest_frames < chunkframes ){
// post("%s: %s is too small, truncating copy region",OBJECT_NAME, destname->s_name);
chunkframes = b_dest_frames;
}
if(startframe < 0 || endframe >= b_frames){
error("%s: bad frame range for source buffer: %ld %ld",OBJECT_NAME, startframe,endframe);
return;
}
/* first clean out destination */
memset((char *)b_dest_samples, 0, b_dest_frames * b_dest_nchans * sizeof(float));
/* now copy segment */
memcpy(b_dest_samples, b_samples + (startframe * b_nchans),
chunkframes * b_nchans * sizeof(float) );
if(argc == 5){
// post("enveloping");
fadein = atom_getfloatarg(3,argc,argv);
fadeout = atom_getfloatarg(4,argc,argv);
if(fadein > 0){
// post("fading in");
fadeframes = fadein * .001 * x->sr;
if( fadeframes > b_dest_frames){
error("%s: fadein is too long",OBJECT_NAME);
return;
}
for(i = 0 , k = 0; k < fadeframes ; i += b_dest_nchans, k++ ){
env = (float) k / (float) fadeframes;
for(j = 0; j < b_dest_nchans; j++){
b_dest_samples[i + j] *= env;
}
}
}
if(fadeout > 0){
// post("fading out");
// totalframes = chunkframes;
fadeframes = fadeout * .001 * x->sr;
startframe = chunkframes - fadeframes;
endframe = chunkframes;
// endframe = .001 * x->sr * atom_getfloatarg(1,argc,argv); // stays the same
if(startframe < 0){
error("%s: bad frame boundaries to internal_fadeout: %ld and %ld",
OBJECT_NAME,startframe, endframe);
return;
}
// fadeframes = endframe - startframe; // we already know this
for(i = (chunkframes-1) * b_dest_nchans , k = 0; k < fadeframes ; i -= b_dest_nchans, k++ ){
env = (float) k / (float) fadeframes;
for(j = 0; j < b_dest_nchans; j++){
b_dest_samples[i + j] *= env;
}
}
}
}
buffet_redraw_named(x, destname);
outlet_bang(x->bang);
}
int buffet_setdestbuf(t_buffet *x, t_symbol *wavename)
{
t_garray *a;
int b_frames;
float *b_samples;
if (!(a = (t_garray *)pd_findbyclass(wavename, garray_class))) {
if (*wavename->s_name) pd_error(x, "%s: %s: no such array",OBJECT_NAME,wavename->s_name);
x->destbuf->b_valid = 0;
}
else if (!garray_getfloatarray(a, &b_frames, &b_samples)) {
pd_error(x, "%s: bad array for %s", wavename->s_name,OBJECT_NAME);
x->destbuf->b_valid = 0;
}
else {
x->destbuf->b_valid = 1;
x->destbuf->b_frames = (long)b_frames;
x->destbuf->b_nchans = 1;
x->destbuf->b_samples = b_samples;
garray_usedindsp(a);
}
/*We added this in linux - better check that it works */
return x->destbuf->b_valid;
}
void buffet_rmschunk(t_buffet *x, t_symbol *msg, short argc, t_atom *argv)
{
float *b_samples;
long b_nchans;
long b_frames;
long bufsamps;
long i;
float meansq;
float rmsval;
long bindex;
float buffer_duration;
long startframe;
long endframe;
buffet_setbuf(x, x->wavename);
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
b_frames = x->wavebuf->b_frames;
// duration in ms.
buffer_duration = 1000.0 * (float)b_frames / x->sr;
startframe = .001 * x->sr * atom_getfloatarg(0,argc,argv);
endframe = .001 * x->sr * atom_getfloatarg(1,argc,argv);
if(startframe < 0 || startframe >= b_frames - 1){
error("%s: naughty start frame: %ld",OBJECT_NAME,startframe);
return;
}
if(endframe < 2 || endframe >= b_frames){
error("%s: naughty start frame: %ld",OBJECT_NAME,startframe);
return;
}
bufsamps = (endframe - startframe);
// post("analyzing %d samples",bufsamps);
if(!bufsamps){
// post("%s: start and end are identical!",OBJECT_NAME);
// post("instantaneous sample value [ch 1 only]: %f", b_samples[startframe*b_nchans]);
return;
}
meansq = 0.0;
for(i = startframe; i < endframe; i++){
bindex = b_nchans * i; /* only analyze first channel */
meansq += b_samples[bindex] * b_samples[bindex];
}
meansq /= (float) bufsamps;
rmsval = sqrt(meansq);
x->rmschunk = rmsval;
outlet_float(x->floater, (double)x->rmschunk);
}
void buffet_dc_gain(t_buffet *x, t_floatarg f)
{
x->dc_gain = f;
}
void buffet_dc_coef(t_buffet *x, t_floatarg f)
{
x->dc_coef = f;
}
void buffet_autoredraw(t_buffet *x, t_floatarg f)
{
x->autoredraw = (short)f;
}
void buffet_minswap(t_buffet *x, t_floatarg f)
{
if(f < 2.0 * 1000.0 * x->fade / x->sr){
error("minimum must be at least twice fade time which is %f", 1000. * x->fade / x->sr);
return;
}
x->minframes = f * .001 * x->sr;
// post("min set to %f samples",x->minframes);
}
void buffet_maxswap(t_buffet *x, t_floatarg f)
{
long oldmem;
// long framelimit;
long newframes;
newframes = f * .001 * x->sr;
if(newframes <= x->minframes){
error("max blocksize must exceed minimum blocksize, which is %f", 1000. * x->minframes/ x->sr);
}
if(newframes > x->storage_maxframes ){
// post("extending memory");
oldmem = x->storage_bytes;
// post("old memory %d", oldmem);
x->storage_maxframes = newframes;
x->storage_bytes = (x->storage_maxframes + 1) * 2 * sizeof(float);
// post("new memory %d", x->storage_bytes);
x->storage = (float *) resizebytes((char *)x->storage, oldmem, x->storage_bytes);
}
x->maxframes = newframes;
}
void buffet_erase(t_buffet *x, t_symbol *msg, short argc, t_atom *argv)
{
float *b_samples;
long b_nchans;
long b_frames;
long startframe, endframe;
//int slab;
//int offset;
int i;
if(argc < 2){
post("%s: erase requires start and end times",OBJECT_NAME);
return;
}
buffet_setbuf(x, x->wavename);
if(x->hosed){
return;
}
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
b_frames = x->wavebuf->b_frames;
startframe = .001 * x->sr * atom_getfloatarg(0,argc,argv);
endframe = .001 * x->sr * atom_getfloatarg(1,argc,argv);
if(startframe < 0){
startframe = 0;
}
if(endframe > b_frames - 1){
endframe = b_frames - 1;
}
if(startframe >= b_frames - 1){
error("%s: naughty start frame: %ld",OBJECT_NAME,startframe);
return;
}
if(endframe < 2 || endframe <= startframe){
error("%s: naughty end frame: %ld",OBJECT_NAME,endframe);
return;
}
for(i = startframe * b_nchans; i < endframe * b_nchans; i++){
b_samples[i] = 0.0;
}
buffet_update(x);
}
void buffet_rotatetozero(t_buffet *x, t_floatarg f)
{
float target = (float) f;
long shiftframes = (long) (target * 0.001 * x->sr);
float *b_samples;
long b_nchans;
long b_frames;
float *tmpmem;
buffet_setbuf(x, x->wavename);
if(x->hosed){
return;
}
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
b_frames = x->wavebuf->b_frames;
if(shiftframes <= 0 || shiftframes >= b_frames){
error("%s: shift target %f is out of range",OBJECT_NAME,target);
return;
}
tmpmem = (float *) malloc(shiftframes * b_nchans * sizeof(float));
/* copy shift block to tmp */
memcpy(tmpmem, b_samples, shiftframes * b_nchans * sizeof(float));
/* now shift the rest to the top */
memmove(b_samples, b_samples + (shiftframes * b_nchans),
(b_frames - shiftframes) * b_nchans * sizeof(float));
/* finally copy tmp to the tail */
memcpy(b_samples + (b_frames - shiftframes) * b_nchans,tmpmem,
shiftframes * b_nchans * sizeof(float ));
free(tmpmem);
buffet_update(x);
}
void buffet_normalize(t_buffet *x, t_floatarg f)
{
float target = (float) f;
float *b_samples;
long b_nchans;
long b_frames;
long i;
float maxamp = 0.0;
float amptest;
float rescale;
if(target <= 0.0){
error("%s: normalize target %f is too low",OBJECT_NAME,target);
return;
}
buffet_setbuf(x, x->wavename);
if(x->hosed){
return;
}
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
b_frames = x->wavebuf->b_frames;
for(i = 0; i < b_frames * b_nchans; i++){
amptest = fabs(b_samples[i]);
if(maxamp < amptest)
maxamp = amptest;
}
if(maxamp < .000000001){
post("%s: amplitude zero or too low to normalize in \"%s\"",OBJECT_NAME,x->wavename->s_name);
return;
}
rescale = target / maxamp;
if(rescale > .99 && rescale < 1.01){
post("%s: \"%s\" already normalized to %f",OBJECT_NAME,x->wavename->s_name,target);
}
else {
for(i = 0; i < b_frames * b_nchans; i++){
b_samples[i] *= rescale;
}
}
buffet_update(x);
}
void buffet_fadein(t_buffet *x, t_floatarg f)
{
long fadeframes;
long totalframes;
int i,j,k;
float env;
float *b_samples;
long b_nchans;
if( ! x->sr){
error("zero sample rate!");
return;
}
fadeframes = f * .001 * x->sr;
//post("fading in %d frames",fadeframes);
buffet_setbuf(x, x->wavename);
if(x->hosed){
return;
}
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
totalframes = x->wavebuf->b_frames;
if( fadeframes > totalframes){
error("fadein is too long");
return;
}
for(i = 0, k = 0;i < fadeframes * b_nchans; i += b_nchans, k++ ){
env = (float) k / (float) fadeframes;
for(j = 0; j < b_nchans; j++){
b_samples[i + j] *= env;
}
}
buffet_update(x);
}
void buffet_fadeout(t_buffet *x, t_floatarg f)
{
long fadeframes;
long totalframes;
int i,j,k;
float env;
float *b_samples;
long b_nchans;
if( ! x->sr){
error("zero sample rate!");
return;
}
fadeframes = f * .001 * x->sr;
buffet_setbuf(x, x->wavename);
if(x->hosed){
return;
}
b_samples = x->wavebuf->b_samples;
b_nchans = x->wavebuf->b_nchans;
totalframes = x->wavebuf->b_frames;
if( fadeframes > totalframes){
error("%s: fadein is too long",OBJECT_NAME);
return;
}
for(i = (totalframes-1) * b_nchans , k = 0; k < fadeframes ; i -= b_nchans, k++ ){
env = (float) k / (float) fadeframes;
for(j = 0; j < b_nchans; j++){
b_samples[i + j] *= env;
}
}