-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbe-conv.cc
1115 lines (918 loc) · 24.1 KB
/
be-conv.cc
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
// -----------------------------------------------------------------------------
//
// Copyright (C) 2006-2009 Fons Adriaensen <[email protected]>
//
// 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.
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// -----------------------------------------------------------------------------
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <sndfile.h>
//#include "config.h" // jconv
#include <time.h>
#include <math.h>
#include "common.h"
#include "common-string.h"
#include "builtin-effect.h"
#include "zita-convolver.h"
/**************
greathall.conf
/cd /audio/reverbs
# in out partition maxsize
# -----------------------------------------------
/convolver/new 1 2 512 120000
# num port name connect to
# -----------------------------------------------
/input/name 1 Input
/output/name 1 Output.L
/output/name 2 Output.R
# in out gain delay offset length chan file
# -------------------------------------------------------------------------
/impulse/read 1 1 0.2 0 0 0 1 greathall.wav
/impulse/read 1 2 0.2 0 0 0 2 greathall.wav
********************/
#define MAXSIZE 0x00100000
#define BSIZE 0x4000
static const char *label = "conv";
static const char *description = "impulse response based on Fons Adriaensen's work";
static const char *inputNames[] = { "impulse file" };
static int inputType[] = { INPUT_FILE };
static int iHasMin[] = { FALSE };
static int iHasMax[] = { FALSE };
static float iDefault[] = { NULL };
static int iIsLog[] = { FALSE };
static int iUsesSampleRate[] = { FALSE };
static int nInputs = 1;
static int nOutputs = 0;
static int nInputBuffers = 1;
static int nOutputBuffers = 1;
typedef struct data_s data_t;
struct data_s {
float *input_buffer_left;
float *output_buffer_left;
int initialized;
unsigned long sampleCount; // used to init zita lib
unsigned long sampleRate;
unsigned long rate;
unsigned int ninp;
unsigned int nout;
unsigned int size;
unsigned int part;
unsigned int frag;
unsigned int latency;
unsigned int err;
char *impulse_file_name;
char currentFilename[MAX_CHARS];
Convproc *conv;
};
class Impdata
{
public:
enum
{
TYPE_RAW = 0x00010000,
TYPE_SWEEP = 0x00020002,
TYPE_LR = 0x00030002,
TYPE_MS = 0x00040002,
TYPE_AMB_A = 0x00050004,
TYPE_AMB_B3 = 0x00060003,
TYPE_AMB_B4 = 0x00070004,
TYPE_LA_OCT = 0x0008000C,
TYPE__CHAN = 0x0000ffff,
TYPE__TYPE = 0xffff0000
};
enum
{
ERR_NONE = 0,
ERR_MODE = -1,
ERR_DATA = -2,
ERR_OPEN = -3,
ERR_FORM = -4,
ERR_SEEK = -5,
ERR_READ = -6,
ERR_WRITE = -7
};
enum
{
MODE_NONE = 0,
MODE_READ = 1,
MODE_WRITE = 2
};
Impdata (void);
~Impdata (void);
uint32_t mode (void) const { return _mode; }
uint32_t vers (void) const { return _vers; }
uint32_t type (void) const { return _type; }
uint32_t rate_n (void) const { return _rate_n; }
uint32_t rate_d (void) const { return _rate_d; }
uint32_t n_chan (void) const { return _type & TYPE__CHAN; }
uint32_t n_fram (void) const { return _n_fram; }
uint32_t n_sect (void) const { return _n_sect; }
uint32_t i_fram (void) const { return _i_fram; }
uint32_t i_sect (void) const { return _i_sect; }
int32_t tref_i (void) const { return _tref_i; }
uint32_t tref_n (void) const { return _tref_n; }
uint32_t tref_d (void) const { return _tref_d; }
int set_type (uint32_t type);
int set_n_fram (uint32_t n_fram);
int set_n_sect (uint32_t n_sect);
void set_rate (uint32_t rate_n, uint32_t rate_d = 1) { _rate_n = rate_n; _rate_d = rate_d; }
void set_tref (int32_t i, uint32_t n = 0, uint32_t d = 1) { _tref_i = i; _tref_n = n; _tref_d = d; }
void set_bits (uint32_t bits) { _bits = bits; }
double drate (void) const { return (double) _rate_n / (double) _rate_d; }
double dtref (void) const { return (double) _tref_n / (double) _tref_d + _tref_i; }
float *data (int c) const { return _data ? (_data + c) : 0; }
int alloc (void);
int deall (void);
int clear (void);
int open_read (const char *name);
int open_write (const char *name);
int sf_open_read (const char *name);
int sf_open_write (const char *name);
int close (void);
int read_sect (uint32_t i_sect);
int write_sect (uint32_t i_sect);
int seek_sect (uint32_t i_sect);
int read_ext (uint32_t k, float *p);
int write_ext (uint32_t k, float *p);
int seek_ext (uint32_t k);
int locate (uint32_t i_sect) { return seek_sect (i_sect); }
static void checkext (char *name);
static const char *channame (uint32_t type, uint32_t chan);
private:
uint32_t _vers;
uint32_t _type;
uint32_t _rate_n;
uint32_t _rate_d;
uint32_t _n_fram;
uint32_t _n_sect;
int32_t _tref_i;
uint32_t _tref_n;
uint32_t _tref_d;
uint32_t _bits;
uint32_t _i_fram;
uint32_t _i_sect;
uint32_t _mode;
uint32_t _size;
float *_data;
FILE *_aldfile;
SNDFILE *_sndfile;
static uint32_t mapvers1 (uint32_t type);
static char _suffix [12];
static const char *_suffix2 [2];
static const char *_suffix3 [2];
static const char *_suffix4 [2];
static const char *_suffix5 [4];
static const char *_suffix7 [4];
static const char *_suffix8 [12];
};
#define HDRSIZE 256
//------------------------------------------------------------------------------------------------------------
Impdata::Impdata (void) :
_vers (0),
_type (0),
_rate_n (0),
_rate_d (1),
_n_fram (0),
_n_sect (0),
_tref_i (0),
_tref_n (0),
_tref_d (1),
_bits (0),
_i_fram (0),
_i_sect (0),
_mode (0),
_size (0),
_data (0),
_aldfile (0),
_sndfile (0)
{
}
Impdata::~Impdata (void)
{
close ();
delete[] _data;
}
int Impdata::set_type (uint32_t type)
{
if (_mode) return ERR_MODE;
_type = type;
deall ();
return 0;
}
int Impdata::set_n_fram (uint32_t n_fram)
{
if (_aldfile) return ERR_MODE;
_n_fram = n_fram;
deall ();
return 0;
}
int Impdata::set_n_sect (uint32_t n_sect)
{
if (_aldfile) return ERR_MODE;
_n_sect = n_sect;
return 0;
}
int Impdata::alloc (void)
{
uint32_t k;
k = (_type & TYPE__CHAN) * _n_fram;
if (k != _size)
{
delete[] _data;
if (k)
{
_size = k;
_data = new float [k];
memset (_data, 0, _size * sizeof (float));
}
else
{
_size = 0;
_data = 0;
}
}
return 0;
}
int Impdata::deall (void)
{
delete[] _data;
_data = 0;
_size = 0;
return 0;
}
int Impdata::clear (void)
{
if (_data) memset (_data, 0, _size * sizeof (float));
return 0;
}
//------------------------------------------------------------------------------------------------------------
int Impdata::open_read (const char *name)
{
char p [HDRSIZE];
if (_mode) return ERR_MODE;
deall ();
_type = 0;
_n_fram = 0;
_n_sect = 0;
if ((_aldfile = fopen (name, "r")) == 0) return ERR_OPEN;
if (fread (p, 1, HDRSIZE, _aldfile) != HDRSIZE)
{
fclose (_aldfile);
_aldfile = 0;
return ERR_READ;
}
if (strcmp (p, "aliki") || p [6] || p [7])
{
fclose (_aldfile);
_aldfile = 0;
return ERR_FORM;
}
_vers = *(uint32_t *)(p + 8);
if (_vers == 1)
{
_type = mapvers1 (*(uint32_t *)(p + 12));
_rate_n = *(uint32_t *)(p + 16);
_rate_d = 1;
_n_sect = *(uint32_t *)(p + 20);
_n_fram = *(uint32_t *)(p + 24);
_tref_i = *(uint32_t *)(p + 28);
_tref_n = 0;
_tref_d = 1;
_bits = 0;
}
else if (_vers == 2)
{
_type = *(uint32_t *)(p + 12);
_rate_n = *(uint32_t *)(p + 16);
_rate_d = *(uint32_t *)(p + 20);
_n_fram = *(uint32_t *)(p + 24);
_n_sect = *(uint32_t *)(p + 28);
_tref_i = *(uint32_t *)(p + 32);
_tref_n = *(uint32_t *)(p + 36);
_tref_d = *(uint32_t *)(p + 40);
_bits = *(uint32_t *)(p + 44);
}
else
{
fclose (_aldfile);
_aldfile = 0;
return ERR_FORM;
}
if (!_type || !_n_fram || !_n_sect)
{
fclose (_aldfile);
_aldfile = 0;
return ERR_FORM;
}
_mode = MODE_READ;
_i_sect = 0;
_i_fram = 0;
return 0;
}
int Impdata::open_write (const char *name)
{
char p [HDRSIZE];
if (_mode || !_type || !_n_fram || !_n_sect) return ERR_MODE;
if ((_aldfile = fopen (name, "w")) == 0) return ERR_OPEN;
strcpy (p, "aliki");
p [6] = p [7] = 0;
*(uint32_t *)(p + 8) = _vers = 2;
*(uint32_t *)(p + 12) = _type;
*(uint32_t *)(p + 16) = _rate_n;
*(uint32_t *)(p + 20) = _rate_d;
*(uint32_t *)(p + 24) = _n_fram;
*(uint32_t *)(p + 28) = _n_sect;
*(uint32_t *)(p + 32) = _tref_i;
*(uint32_t *)(p + 36) = _tref_n;
*(uint32_t *)(p + 40) = _tref_d;
*(uint32_t *)(p + 44) = _bits;
memset (p + 48, 0, HDRSIZE - 48);
if (fwrite (p, 1, HDRSIZE, _aldfile) != HDRSIZE)
{
fclose (_aldfile);
_aldfile = 0;
return ERR_WRITE;
}
_mode = MODE_WRITE;
_i_sect = 0;
_i_fram = 0;
return 0;
}
int Impdata::sf_open_read (const char *name)
{
SF_INFO I;
if (_mode) return ERR_MODE;
deall ();
_type = 0;
_n_fram = 0;
_n_sect = 0;
if ((_sndfile = sf_open (name, SFM_READ, &I)) == 0) return ERR_OPEN;
_type = TYPE_RAW + I.channels;
_rate_n = I.samplerate;
_rate_d = 1;
_n_sect = 1;
_n_fram = I.frames;
_tref_i = 1;
_tref_n = 0;
_tref_d = 1;
_bits = 0;
#ifndef NOAMBIS
if (sf_command (_sndfile, SFC_WAVEX_GET_AMBISONIC, 0, 0) == SF_AMBISONIC_B_FORMAT)
{
switch (I.channels)
{
case 3: _type = TYPE_AMB_B3; break;
case 4: _type = TYPE_AMB_B4; break;
}
}
#endif
if (!_n_fram)
{
sf_close (_sndfile);
_sndfile = 0;
return ERR_FORM;
}
_mode = MODE_READ;
return 0;
}
int Impdata::sf_open_write (const char *name)
{
SF_INFO I;
if (_mode || !_type || !_n_fram || !_n_sect) return ERR_MODE;
I.channels = _type & TYPE__CHAN;
I.format = (I.channels > 2) ? SF_FORMAT_WAVEX : SF_FORMAT_WAV;
I.format |= SF_FORMAT_FLOAT;
I.samplerate = _rate_n / _rate_d;
I.sections = 1;
if ((_sndfile = sf_open (name, SFM_WRITE, &I)) == 0) return ERR_OPEN;
#ifndef NOAMBIS
switch (_type)
{
case TYPE_AMB_B3:
case TYPE_AMB_B4:
sf_command (_sndfile, SFC_WAVEX_SET_AMBISONIC, 0, SF_AMBISONIC_B_FORMAT);
break;
}
#endif
_mode = MODE_WRITE;
_i_sect = 0;
_i_fram = 0;
return 0;
}
int Impdata::close (void)
{
if (_aldfile)
{
fclose (_aldfile);
_aldfile = 0;
}
else if (_sndfile)
{
sf_close (_sndfile);
_sndfile = 0;
}
_mode = 0;
return 0;
}
//------------------------------------------------------------------------------------------------------------
int Impdata::read_sect (uint32_t i_sect)
{
int r;
r = locate (i_sect);
if (r) return r;
return _data ? read_ext (_n_fram, _data) : ERR_DATA;
}
int Impdata::write_sect (uint32_t i_sect)
{
int r;
r = locate (i_sect);
if (r) return r;
return _data ? write_ext (_n_fram, _data) : ERR_DATA;
}
int Impdata::seek_sect (uint32_t i_sect)
{
if (_aldfile)
{
if (fseek (_aldfile, HDRSIZE + (_type & TYPE__CHAN) * _n_fram * i_sect * sizeof (float), SEEK_SET)) return ERR_SEEK;
_i_sect = i_sect;
_i_fram = 0;
return 0;
}
else if (_sndfile)
{
if (sf_seek (_sndfile, _n_fram * i_sect, SEEK_SET) != _n_fram * i_sect) return ERR_SEEK;
_i_sect = i_sect;
_i_fram = 0;
return 0;
}
else return ERR_MODE;
}
int Impdata::read_ext (uint32_t k, float *p)
{
if (_mode != MODE_READ) return ERR_MODE;
if (k > _n_fram - _i_fram) k = _n_fram - _i_fram;
if (_aldfile)
{
k = fread (p, (_type & TYPE__CHAN) * sizeof (float), k, _aldfile);
}
else if (_sndfile)
{
k = sf_readf_float (_sndfile, p, k);
}
else return ERR_MODE;
_i_fram += k;
return k;
}
int Impdata::write_ext (uint32_t k, float *p)
{
if (_mode != MODE_WRITE) return ERR_MODE;
if (k > _n_fram - _i_fram) k = _n_fram - _i_fram;
if (_aldfile)
{
k = fwrite (p, (_type & TYPE__CHAN) * sizeof (float), k, _aldfile);
}
else if (_sndfile)
{
k = sf_writef_float (_sndfile, p, k);
}
else return ERR_MODE;
_i_fram += k;
return k;
}
int Impdata::seek_ext (uint32_t k)
{
if (_aldfile)
{
if (fseek (_aldfile, HDRSIZE + (_type & TYPE__CHAN) * k * sizeof (float), SEEK_SET)) return ERR_SEEK;
_i_fram = k;
return 0;
}
else if (_sndfile)
{
if (sf_seek (_sndfile, k, SEEK_SET) != k) return ERR_SEEK;
_i_fram = k;
return 0;
}
else return ERR_MODE;
}
//------------------------------------------------------------------------------------------------------------
uint32_t Impdata::mapvers1 (uint32_t type)
{
if (type < 256) return TYPE_RAW | type;
switch (type)
{
case 0x0102: return TYPE_MS;
case 0x0202: return TYPE_SWEEP;
case 0x0103: return TYPE_AMB_B3;
case 0x0104: return TYPE_AMB_B4;
}
return TYPE_RAW | type;
}
void Impdata::checkext (char *name)
{
char *p;
p = strrchr (name, '.');
if (p && ! strcmp (p, ".ald")) return;
strcat (name, ".ald");
}
char Impdata::_suffix [12];
const char *Impdata::_suffix2 [] = { "F", "I" };
const char *Impdata::_suffix3 [] = { "L", "R" };
const char *Impdata::_suffix4 [] = { "M", "S" };
const char *Impdata::_suffix5 [] = { "LF", "RF", "LB", "RB" };
const char *Impdata::_suffix7 [] = { "W", "X", "Y", "Z" };
const char *Impdata::_suffix8 [] = { "Lin", "A-W", "32", "63", "125", "250", "500", "1k", "2k", "4k", "8k", "16k" };
const char *Impdata::channame (uint32_t type, uint32_t chan)
{
const char **p;
if (chan >= (type & TYPE__CHAN)) return 0;
switch (type & TYPE__TYPE)
{
case 0x00020000: p = _suffix2; break;
case 0x00030000: p = _suffix3; break;
case 0x00040000: p = _suffix4; break;
case 0x00050000: p = _suffix5; break;
case 0x00060000:
case 0x00070000: p = _suffix7; break;
case 0x00080000: p = _suffix8; break;
default: p = 0;
}
if (p) return p [chan];
sprintf (_suffix, "%d", chan + 1);
return _suffix;
}
//------------------------------------------------------------------------------------------------------------
static void *instantiate (unsigned long sampleRate)
{
void *r;
r = calloc(1, sizeof(data_t));
if (!r) {
xerror();
}
((data_t *)r)->sampleRate = sampleRate;
((data_t *)r)->rate = sampleRate;
return r;
}
static void cleanup (void *instance)
{
data_t *ins = (data_t *)instance;
if (!instance) {
xerror();
}
ins->conv->cleanup();
free(instance);
}
static void activate (void *instance)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
}
static void deactivate (void *instance)
{
data_t *ins = (data_t *)instance;
if (!ins)
return;
if (!ins->conv)
return;
//ins->conv->cleanup();
ins->conv->stop_process();
}
static void connect_input_buffer (void *instance, int num, float *buffer)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (!buffer) {
xerror();
}
if (num >= nInputBuffers) {
xerror();
}
if (num == 0) {
ins->input_buffer_left = buffer;
} else {
//ins->input_buffer_right = buffer;
xerror();
}
}
static void connect_output_buffer (void *instance, int num, float *buffer)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (!buffer) {
xerror();
}
if (num >= nOutputBuffers) {
xerror();
}
if (num == 0) {
ins->output_buffer_left = buffer;
} else {
//ins->output_buffer_right = buffer;
xerror();
}
}
static void connect_input (void *instance, int num, void *f)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (!f) {
xerror();
}
if (num >= nInputs) {
xerror();
}
if (num == 0) {
ins->impulse_file_name = (char *)f;
} else {
xerror();
}
}
static void init_instance (data_t *ins)
{
unsigned int ip1, op1;
float gain;
unsigned int delay;
unsigned int offset;
unsigned int length;
unsigned int ichan, nchan;
int ifram, nfram; //, err;
char file [1024];
char path [1024];
Impdata impd;
float *buff, *p;
int lnum = 0; // just to compile ok
ins->initialized = TRUE;
//FIXME
ins->err = TRUE;
// frag = periodsize >> 2;
ins->frag = ins->sampleCount;
ins->latency = 0; //FIXME pass in
ins->conv = new Convproc;
//if (M_opt) conv->set_fftwopt(FFTW_MEASURE);
/* config(av[0], 0) */
// convnew(q)
/*
# in out partition maxsize
# -----------------------------------------------
/convolver/new 1 2 512 120000
*/
ins->ninp = 1;
ins->nout = 1;
ins->part = 512;
ins->size = 120000;
//FIXME checks
if (ins->part > Convproc::MAXDIVIS * ins->frag) {
ins->part = Convproc::MAXDIVIS * ins->frag;
fprintf (stderr, "Warning: partition size adjusted to %d\n", ins->part);
}
if (ins->part < ins->frag) {
ins->part = ins->frag;
fprintf (stderr, "Warning: partition size adjusted to %d\n", ins->part);
}
if (ins->conv->configure(ins->ninp, ins->nout, ins->size, ins->frag, ins->part, Convproc::MAXPART)) {
fprintf(stderr, "Couldn't initialize convolver engine.\n");
//FIXME wat
return;
}
// end convnew()
// read_sndfile(q, lnum, cdir, latency)
/*
# in out gain delay offset length chan file
# ------------------------------------------------------------------
/impulse/read 1 1 0.2 0 0 0 1 greathall.wav
/impulse/read 1 2 0.2 0 0 0 2 greathall.wav
*/
ip1 = 1;
op1 = 1;
gain = 0.1; //0.2;
delay = 0;
offset = 0;
length = 0;
ichan = 1;
//snprintf(file, 1024, "/home/acano/tmp5/greathall.wav");
snprintf(file, 1024, ins->impulse_file_name);
if (ins->latency)
{
if (delay >= ins->latency)
{
delay -= ins->latency;
}
else
{
ins->latency -= delay;
delay = 0;
offset += ins->latency;
fprintf (stderr, "Warning: first %d frames removed by latency compensation,\n", ins->latency);
}
}
//err = check1 (ip1, op1);
//if (err) return err;
//if (*file == '/') strcpy (path, file);
//else
// {
// strcpy (path, cdir);
// strcat (path, "/");
// strcat (path, file);
// }
strcpy(path, file);
if (impd.sf_open_read (path))
{
fprintf (stderr, "Unable to open '%s'\n", path);
return;
}
if (impd.rate_n () != (unsigned int) ins->rate)
{
fprintf (stderr, "Warning: sample rate (%d) of '%s' does not match.\n",
impd.rate_n (), path);
}
nchan = impd.n_chan ();
nfram = impd.n_fram ();
if ((ichan < 1) || (ichan > nchan))
{
fprintf (stderr, "Channel not available in line %d\n", lnum);
impd.close ();
return;
}
if (offset && impd.seek_ext (offset))
{
fprintf (stderr, "Can't seek to offset in line %d\n", lnum);
impd.close ();
return;
}
if (! length) length = nfram - offset;
if (length > ins->size - delay)
{
length = ins->size - delay;
fprintf (stderr, "Warning: data truncated in line %d\n", lnum);
}
try
{
buff = new float [BSIZE * nchan];
}
catch (...)
{
fprintf(stderr, "covn couldn't allocate buffer\n");
impd.close ();
return;
}
while (length)
{
nfram = (length > BSIZE) ? BSIZE : length;
nfram = impd.read_ext (nfram, buff);
if (nfram < 0)
{
fprintf (stderr, "Error reading file in line %d\n", lnum);
impd.close ();
delete[] buff;
return;
}
if (nfram)
{
p = buff + ichan - 1;
for (ifram = 0; ifram < nfram; ifram++) p [ifram * nchan] *= gain;
if (ins->conv->impdata_create (ip1 - 1, op1 - 1, nchan, p, delay, delay + nfram))
{
fprintf(stderr, "Error creating impulse data\n");
impd.close ();
delete[] buff;
return;
}
delay += nfram;
length -= nfram;
}
}
impd.close ();
delete[] buff;
//return 0;
// end read_sndfile()
//FIXME /impulse/copy
// done config()
//////////////////////////////////////////////////
ins->conv->print();
ins->conv->start_process(0, 0);
ins->err = FALSE;
printf("conv initialized\n");
}