/
pcm_ladspa.c
1808 lines (1684 loc) · 60.8 KB
/
pcm_ladspa.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
/**
* \file pcm/pcm_ladspa.c
* \ingroup PCM_Plugins
* \brief ALSA Plugin <-> LADSPA Plugin Interface
* \author Jaroslav Kysela <perex@perex.cz>
* \author Jaroslav Kysela <perex@perex.cz>
* \date 2001,2006
*/
/*
* PCM - LADSPA integration plugin
* Copyright (c) 2001-2006 by Jaroslav Kysela <perex@perex.cz>
* Copyright (c) 2005 by Jaroslav Kysela <perex@perex.cz>
*
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* perex@perex.cz 2005/12/13
* The LADSPA plugin rewrite was sponsored by MediaNet AG
* http://www.medianet.ag
*/
#include "pcm_local.h"
#include "pcm_plugin.h"
#include <dirent.h>
#include <locale.h>
#include <math.h>
#include "ladspa.h"
#ifndef PIC
/* entry for static linking */
const char *_snd_module_pcm_ladspa = "";
#endif
#ifndef DOC_HIDDEN
#define NO_ASSIGN 0xffffffff
typedef enum _snd_pcm_ladspa_policy {
SND_PCM_LADSPA_POLICY_NONE, /* use bindings only */
SND_PCM_LADSPA_POLICY_DUPLICATE /* duplicate bindings for all channels */
} snd_pcm_ladspa_policy_t;
typedef struct {
/* This field need to be the first */
snd_pcm_plugin_t plug;
/* Plugin custom fields */
struct list_head pplugins;
struct list_head cplugins;
unsigned int channels; /* forced input channels, 0 = auto */
unsigned int allocated; /* count of allocated samples */
LADSPA_Data *zero[2]; /* zero input or dummy output */
} snd_pcm_ladspa_t;
typedef struct {
unsigned int size;
unsigned int *array;
} snd_pcm_ladspa_array_t;
typedef struct {
snd_pcm_ladspa_array_t channels;
snd_pcm_ladspa_array_t ports;
LADSPA_Data **m_data;
LADSPA_Data **data;
} snd_pcm_ladspa_eps_t;
typedef struct snd_pcm_ladspa_instance {
struct list_head list;
const LADSPA_Descriptor *desc;
LADSPA_Handle *handle;
unsigned int depth;
snd_pcm_ladspa_eps_t input;
snd_pcm_ladspa_eps_t output;
struct snd_pcm_ladspa_instance *prev;
struct snd_pcm_ladspa_instance *next;
} snd_pcm_ladspa_instance_t;
typedef struct {
LADSPA_PortDescriptor pdesc; /* port description */
unsigned int port_bindings_size; /* size of array */
unsigned int *port_bindings; /* index = channel number, value = LADSPA port */
unsigned int controls_size; /* size of array */
unsigned char *controls_initialized; /* initialized by ALSA user */
LADSPA_Data *controls; /* index = LADSPA control port index */
} snd_pcm_ladspa_plugin_io_t;
typedef struct {
struct list_head list;
snd_pcm_ladspa_policy_t policy;
char *filename;
void *dl_handle;
const LADSPA_Descriptor *desc;
snd_pcm_ladspa_plugin_io_t input;
snd_pcm_ladspa_plugin_io_t output;
struct list_head instances; /* one LADSPA plugin might be used multiple times */
} snd_pcm_ladspa_plugin_t;
#endif /* DOC_HIDDEN */
static unsigned int snd_pcm_ladspa_count_ports(snd_pcm_ladspa_plugin_t *lplug,
LADSPA_PortDescriptor pdesc)
{
unsigned int res = 0, idx;
for (idx = 0; idx < lplug->desc->PortCount; idx++) {
if ((lplug->desc->PortDescriptors[idx] & pdesc) == pdesc)
res++;
}
return res;
}
static int snd_pcm_ladspa_find_port(unsigned int *res,
snd_pcm_ladspa_plugin_t *lplug,
LADSPA_PortDescriptor pdesc,
unsigned int port_idx)
{
unsigned long idx;
for (idx = 0; idx < lplug->desc->PortCount; idx++)
if ((lplug->desc->PortDescriptors[idx] & pdesc) == pdesc) {
if (port_idx == 0) {
*res = idx;
return 0;
}
port_idx--;
}
return -EINVAL;
}
static int snd_pcm_ladspa_find_sport(unsigned int *res,
snd_pcm_ladspa_plugin_t *lplug,
LADSPA_PortDescriptor pdesc,
const char *port_name)
{
unsigned long idx;
for (idx = 0; idx < lplug->desc->PortCount; idx++)
if ((lplug->desc->PortDescriptors[idx] & pdesc) == pdesc &&
!strcmp(lplug->desc->PortNames[idx], port_name)) {
*res = idx;
return 0;
}
return -EINVAL;
}
static int snd_pcm_ladspa_find_port_idx(unsigned int *res,
snd_pcm_ladspa_plugin_t *lplug,
LADSPA_PortDescriptor pdesc,
unsigned int port)
{
unsigned long idx;
unsigned int r = 0;
if (port >= lplug->desc->PortCount)
return -EINVAL;
for (idx = 0; idx < port; idx++)
if ((lplug->desc->PortDescriptors[idx] & pdesc) == pdesc)
r++;
*res = r;
return 0;
}
static void snd_pcm_ladspa_free_io(snd_pcm_ladspa_plugin_io_t *io)
{
free(io->controls);
free(io->controls_initialized);
}
static void snd_pcm_ladspa_free_plugins(struct list_head *plugins)
{
while (!list_empty(plugins)) {
snd_pcm_ladspa_plugin_t *plugin = list_entry(plugins->next, snd_pcm_ladspa_plugin_t, list);
snd_pcm_ladspa_free_io(&plugin->input);
snd_pcm_ladspa_free_io(&plugin->output);
if (plugin->dl_handle)
dlclose(plugin->dl_handle);
free(plugin->filename);
list_del(&plugin->list);
free(plugin);
}
}
static void snd_pcm_ladspa_free(snd_pcm_ladspa_t *ladspa)
{
unsigned int idx;
snd_pcm_ladspa_free_plugins(&ladspa->pplugins);
snd_pcm_ladspa_free_plugins(&ladspa->cplugins);
for (idx = 0; idx < 2; idx++) {
free(ladspa->zero[idx]);
ladspa->zero[idx] = NULL;
}
ladspa->allocated = 0;
}
static int snd_pcm_ladspa_close(snd_pcm_t *pcm)
{
snd_pcm_ladspa_t *ladspa = pcm->private_data;
snd_pcm_ladspa_free(ladspa);
return snd_pcm_generic_close(pcm);
}
static int snd_pcm_ladspa_hw_refine_cprepare(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params)
{
snd_pcm_ladspa_t *ladspa = pcm->private_data;
int err;
snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHMN };
err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
&access_mask);
if (err < 0)
return err;
err = _snd_pcm_hw_params_set_format(params, SND_PCM_FORMAT_FLOAT);
if (err < 0)
return err;
err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
if (err < 0)
return err;
if (ladspa->channels > 0 && pcm->stream == SND_PCM_STREAM_PLAYBACK) {
err = _snd_pcm_hw_param_set(params, SND_PCM_HW_PARAM_CHANNELS, ladspa->channels, 0);
if (err < 0)
return err;
}
params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
return 0;
}
static int snd_pcm_ladspa_hw_refine_sprepare(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *sparams)
{
snd_pcm_ladspa_t *ladspa = pcm->private_data;
snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAPN };
_snd_pcm_hw_params_any(sparams);
_snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
&saccess_mask);
_snd_pcm_hw_params_set_format(sparams, SND_PCM_FORMAT_FLOAT);
_snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
if (ladspa->channels > 0 && pcm->stream == SND_PCM_STREAM_CAPTURE)
_snd_pcm_hw_param_set(sparams, SND_PCM_HW_PARAM_CHANNELS, ladspa->channels, 0);
return 0;
}
static int snd_pcm_ladspa_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
snd_pcm_hw_params_t *sparams)
{
int err;
unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
SND_PCM_HW_PARBIT_RATE |
SND_PCM_HW_PARBIT_PERIOD_SIZE |
SND_PCM_HW_PARBIT_BUFFER_SIZE |
SND_PCM_HW_PARBIT_PERIODS |
SND_PCM_HW_PARBIT_PERIOD_TIME |
SND_PCM_HW_PARBIT_BUFFER_TIME |
SND_PCM_HW_PARBIT_TICK_TIME);
err = _snd_pcm_hw_params_refine(sparams, links, params);
if (err < 0)
return err;
return 0;
}
static int snd_pcm_ladspa_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
snd_pcm_hw_params_t *sparams)
{
int err;
unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
SND_PCM_HW_PARBIT_RATE |
SND_PCM_HW_PARBIT_PERIOD_SIZE |
SND_PCM_HW_PARBIT_BUFFER_SIZE |
SND_PCM_HW_PARBIT_PERIODS |
SND_PCM_HW_PARBIT_PERIOD_TIME |
SND_PCM_HW_PARBIT_BUFFER_TIME |
SND_PCM_HW_PARBIT_TICK_TIME);
err = _snd_pcm_hw_params_refine(params, links, sparams);
if (err < 0)
return err;
return 0;
}
static int snd_pcm_ladspa_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
{
return snd_pcm_hw_refine_slave(pcm, params,
snd_pcm_ladspa_hw_refine_cprepare,
snd_pcm_ladspa_hw_refine_cchange,
snd_pcm_ladspa_hw_refine_sprepare,
snd_pcm_ladspa_hw_refine_schange,
snd_pcm_generic_hw_refine);
}
static int snd_pcm_ladspa_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
{
// snd_pcm_ladspa_t *ladspa = pcm->private_data;
int err = snd_pcm_hw_params_slave(pcm, params,
snd_pcm_ladspa_hw_refine_cchange,
snd_pcm_ladspa_hw_refine_sprepare,
snd_pcm_ladspa_hw_refine_schange,
snd_pcm_generic_hw_params);
if (err < 0)
return err;
return 0;
}
static void snd_pcm_ladspa_free_eps(snd_pcm_ladspa_eps_t *eps)
{
free(eps->channels.array);
free(eps->ports.array);
}
static void snd_pcm_ladspa_free_instances(snd_pcm_t *pcm, snd_pcm_ladspa_t *ladspa, int cleanup)
{
struct list_head *list, *pos, *pos1, *next1;
unsigned int idx;
list = pcm->stream == SND_PCM_STREAM_PLAYBACK ? &ladspa->pplugins : &ladspa->cplugins;
list_for_each(pos, list) {
snd_pcm_ladspa_plugin_t *plugin = list_entry(pos, snd_pcm_ladspa_plugin_t, list);
list_for_each_safe(pos1, next1, &plugin->instances) {
snd_pcm_ladspa_instance_t *instance = list_entry(pos1, snd_pcm_ladspa_instance_t, list);
if (plugin->desc->deactivate)
plugin->desc->deactivate(instance->handle);
if (cleanup) {
if (plugin->desc->cleanup)
plugin->desc->cleanup(instance->handle);
if (instance->input.m_data) {
for (idx = 0; idx < instance->input.channels.size; idx++)
free(instance->input.m_data[idx]);
free(instance->input.m_data);
}
if (instance->output.m_data) {
for (idx = 0; idx < instance->output.channels.size; idx++)
free(instance->output.m_data[idx]);
free(instance->output.m_data);
}
free(instance->input.data);
free(instance->output.data);
list_del(&(instance->list));
snd_pcm_ladspa_free_eps(&instance->input);
snd_pcm_ladspa_free_eps(&instance->output);
free(instance);
} else {
if (plugin->desc->activate)
plugin->desc->activate(instance->handle);
}
}
if (cleanup) {
assert(list_empty(&plugin->instances));
}
}
}
static int snd_pcm_ladspa_add_to_carray(snd_pcm_ladspa_array_t *array,
unsigned int idx,
unsigned int val)
{
unsigned int *narray;
unsigned int idx1;
if (idx >= array->size) {
narray = realloc(array->array, sizeof(unsigned int) * (idx + 1));
if (narray == NULL)
return -ENOMEM;
for (idx1 = array->size; idx1 < idx; idx1++)
narray[idx1] = NO_ASSIGN;
array->array = narray;
array->size = idx + 1;
array->array[idx] = val;
return 0;
}
if (array->array[idx] == NO_ASSIGN)
array->array[idx] = val;
else
return -EINVAL;
return 0;
}
static int snd_pcm_ladspa_add_to_array(snd_pcm_ladspa_array_t *array,
unsigned int idx,
unsigned int val)
{
unsigned int *narray;
unsigned int idx1;
if (idx >= array->size) {
narray = realloc(array->array, sizeof(unsigned int) * (idx + 1));
if (narray == NULL)
return -ENOMEM;
for (idx1 = array->size; idx1 < idx; idx1++)
narray[idx1] = NO_ASSIGN;
array->array = narray;
array->size = idx + 1;
}
array->array[idx] = val;
return 0;
}
static int snd_pcm_ladspa_connect_plugin1(snd_pcm_ladspa_plugin_t *plugin,
snd_pcm_ladspa_plugin_io_t *io,
snd_pcm_ladspa_eps_t *eps)
{
unsigned int port, channels, idx, idx1;
int err;
assert(plugin->policy == SND_PCM_LADSPA_POLICY_NONE);
channels = io->port_bindings_size > 0 ?
io->port_bindings_size :
snd_pcm_ladspa_count_ports(plugin, io->pdesc | LADSPA_PORT_AUDIO);
for (idx = idx1 = 0; idx < channels; idx++) {
if (io->port_bindings_size > 0)
port = io->port_bindings[idx];
else {
err = snd_pcm_ladspa_find_port(&port, plugin, io->pdesc | LADSPA_PORT_AUDIO, idx);
if (err < 0) {
SNDERR("unable to find audio %s port %u plugin '%s'", io->pdesc & LADSPA_PORT_INPUT ? "input" : "output", idx, plugin->desc->Name);
return err;
}
}
if (port == NO_ASSIGN)
continue;
err = snd_pcm_ladspa_add_to_carray(&eps->channels, idx1, idx);
if (err < 0) {
SNDERR("unable to add channel %u for audio %s plugin '%s'", idx, io->pdesc & LADSPA_PORT_INPUT ? "input" : "output", plugin->desc->Name);
return err;
}
err = snd_pcm_ladspa_add_to_array(&eps->ports, idx1, port);
if (err < 0) {
SNDERR("unable to add port %u for audio %s plugin '%s'", port, io->pdesc & LADSPA_PORT_INPUT ? "input" : "output", plugin->desc->Name);
return err;
}
idx1++;
}
return 0;
}
static int snd_pcm_ladspa_connect_plugin(snd_pcm_ladspa_plugin_t *plugin,
snd_pcm_ladspa_instance_t *instance)
{
int err;
err = snd_pcm_ladspa_connect_plugin1(plugin, &plugin->input, &instance->input);
if (err < 0)
return err;
err = snd_pcm_ladspa_connect_plugin1(plugin, &plugin->output, &instance->output);
if (err < 0)
return err;
return 0;
}
static int snd_pcm_ladspa_connect_plugin_duplicate1(snd_pcm_ladspa_plugin_t *plugin,
snd_pcm_ladspa_plugin_io_t *io,
snd_pcm_ladspa_eps_t *eps,
unsigned int idx)
{
unsigned int port;
int err;
assert(plugin->policy == SND_PCM_LADSPA_POLICY_DUPLICATE);
if (io->port_bindings_size > 0) {
port = io->port_bindings[0];
} else {
err = snd_pcm_ladspa_find_port(&port, plugin, io->pdesc | LADSPA_PORT_AUDIO, 0);
if (err < 0) {
SNDERR("unable to find audio %s port %u plugin '%s'", io->pdesc & LADSPA_PORT_INPUT ? "input" : "output", (unsigned int)0, plugin->desc->Name);
return err;
}
}
err = snd_pcm_ladspa_add_to_carray(&eps->channels, 0, idx);
if (err < 0) {
SNDERR("unable to add channel %u for audio %s plugin '%s'", idx, io->pdesc & LADSPA_PORT_INPUT ? "input" : "output", plugin->desc->Name);
return err;
}
err = snd_pcm_ladspa_add_to_array(&eps->ports, 0, port);
if (err < 0) {
SNDERR("unable to add port %u for audio %s plugin '%s'", port, io->pdesc & LADSPA_PORT_INPUT ? "input" : "output", plugin->desc->Name);
return err;
}
return 0;
}
static int snd_pcm_ladspa_connect_plugin_duplicate(snd_pcm_ladspa_plugin_t *plugin,
snd_pcm_ladspa_plugin_io_t *in_io,
snd_pcm_ladspa_plugin_io_t *out_io,
snd_pcm_ladspa_instance_t *instance,
unsigned int idx)
{
int err;
err = snd_pcm_ladspa_connect_plugin_duplicate1(plugin, in_io, &instance->input, idx);
if (err < 0)
return err;
err = snd_pcm_ladspa_connect_plugin_duplicate1(plugin, out_io, &instance->output, idx);
if (err < 0)
return err;
return 0;
}
static void snd_pcm_ladspa_get_default_cvalue(const LADSPA_Descriptor * desc, unsigned int port, LADSPA_Data *val)
{
LADSPA_PortRangeHintDescriptor hdesc;
hdesc = desc->PortRangeHints[port].HintDescriptor;
switch (hdesc & LADSPA_HINT_DEFAULT_MASK) {
case LADSPA_HINT_DEFAULT_MINIMUM:
*val = desc->PortRangeHints[port].LowerBound;
break;
case LADSPA_HINT_DEFAULT_LOW:
if (LADSPA_IS_HINT_LOGARITHMIC(hdesc)) {
*val = exp(log(desc->PortRangeHints[port].LowerBound)
* 0.75
+ log(desc->PortRangeHints[port].UpperBound)
* 0.25);
} else {
*val = (desc->PortRangeHints[port].LowerBound * 0.75) +
(desc->PortRangeHints[port].UpperBound * 0.25);
}
break;
case LADSPA_HINT_DEFAULT_MIDDLE:
if (LADSPA_IS_HINT_LOGARITHMIC(hdesc)) {
*val = sqrt(desc->PortRangeHints[port].LowerBound *
desc->PortRangeHints[port].UpperBound);
} else {
*val = 0.5 *
(desc->PortRangeHints[port].LowerBound +
desc->PortRangeHints[port].UpperBound);
}
break;
case LADSPA_HINT_DEFAULT_HIGH:
if (LADSPA_IS_HINT_LOGARITHMIC(hdesc)) {
*val = exp(log(desc->PortRangeHints[port].LowerBound)
* 0.25
+ log(desc->PortRangeHints[port].UpperBound)
* 0.75);
} else {
*val = (desc->PortRangeHints[port].LowerBound * 0.25) +
(desc->PortRangeHints[port].UpperBound * 0.75);
}
break;
case LADSPA_HINT_DEFAULT_MAXIMUM:
*val = desc->PortRangeHints[port].UpperBound;
break;
case LADSPA_HINT_DEFAULT_0:
*val = 0;
break;
case LADSPA_HINT_DEFAULT_1:
*val = 1;
break;
case LADSPA_HINT_DEFAULT_100:
*val = 100;
break;
case LADSPA_HINT_DEFAULT_440:
*val = 440;
break;
default:
*val = 0; /* reasonable default, if everything fails */
break;
}
}
static int snd_pcm_ladspa_connect_controls(snd_pcm_ladspa_plugin_t *plugin,
snd_pcm_ladspa_plugin_io_t *io,
snd_pcm_ladspa_instance_t *instance)
{
unsigned long idx, midx;
for (idx = midx = 0; idx < plugin->desc->PortCount; idx++)
if ((plugin->desc->PortDescriptors[idx] & (io->pdesc | LADSPA_PORT_CONTROL)) == (io->pdesc | LADSPA_PORT_CONTROL)) {
if (io->controls_size > midx) {
if (!io->controls_initialized[midx])
snd_pcm_ladspa_get_default_cvalue(plugin->desc, idx, &io->controls[midx]);
plugin->desc->connect_port(instance->handle, idx, &io->controls[midx]);
} else {
return -EINVAL;
}
midx++;
}
return 0;
}
static int snd_pcm_ladspa_check_connect(snd_pcm_ladspa_plugin_t *plugin,
snd_pcm_ladspa_plugin_io_t *io,
snd_pcm_ladspa_eps_t *eps,
unsigned int depth)
{
unsigned int idx, midx;
int err = 0;
for (idx = midx = 0; idx < plugin->desc->PortCount; idx++)
if ((plugin->desc->PortDescriptors[idx] & (io->pdesc | LADSPA_PORT_AUDIO)) == (io->pdesc | LADSPA_PORT_AUDIO)) {
if (eps->channels.array[midx] == NO_ASSIGN) {
SNDERR("%s port for plugin %s depth %u is not connected", io->pdesc & LADSPA_PORT_INPUT ? "input" : "output", plugin->desc->Name, depth);
err++;
}
midx++;
}
if (err > 0) {
SNDERR("%i connection errors total", err);
return -EINVAL;
}
return 0;
}
static int snd_pcm_ladspa_allocate_instances(snd_pcm_t *pcm, snd_pcm_ladspa_t *ladspa)
{
struct list_head *list, *pos;
unsigned int depth, idx, count;
unsigned int in_channels;
unsigned int in_ports, out_ports;
snd_pcm_ladspa_instance_t *instance = NULL;
int err;
list = pcm->stream == SND_PCM_STREAM_PLAYBACK ? &ladspa->pplugins : &ladspa->cplugins;
in_channels = ladspa->channels > 0 ? ladspa->channels :
(pcm->stream == SND_PCM_STREAM_PLAYBACK ? pcm->channels : ladspa->plug.gen.slave->channels);
depth = 0;
list_for_each(pos, list) {
snd_pcm_ladspa_plugin_t *plugin = list_entry(pos, snd_pcm_ladspa_plugin_t, list);
in_ports = snd_pcm_ladspa_count_ports(plugin, LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO);
out_ports = snd_pcm_ladspa_count_ports(plugin, LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO);
count = 1;
if (plugin->policy == SND_PCM_LADSPA_POLICY_DUPLICATE) {
if (in_ports == 1 && out_ports == 1)
count = in_channels;
else
plugin->policy = SND_PCM_LADSPA_POLICY_NONE;
}
for (idx = 0; idx < count; idx++) {
instance = (snd_pcm_ladspa_instance_t *)calloc(1, sizeof(snd_pcm_ladspa_instance_t));
if (instance == NULL)
return -ENOMEM;
instance->desc = plugin->desc;
instance->handle = plugin->desc->instantiate(plugin->desc, pcm->rate);
instance->depth = depth;
if (instance->handle == NULL) {
SNDERR("Unable to create instance of LADSPA plugin '%s'", plugin->desc->Name);
free(instance);
return -EINVAL;
}
list_add_tail(&instance->list, &plugin->instances);
if (plugin->policy == SND_PCM_LADSPA_POLICY_DUPLICATE) {
err = snd_pcm_ladspa_connect_plugin_duplicate(plugin, &plugin->input, &plugin->output, instance, idx);
if (err < 0) {
SNDERR("Unable to connect duplicate port of plugin '%s' channel %u depth %u", plugin->desc->Name, idx, instance->depth);
return err;
}
} else {
err = snd_pcm_ladspa_connect_plugin(plugin, instance);
if (err < 0) {
SNDERR("Unable to connect plugin '%s' depth %u", plugin->desc->Name, depth);
return err;
}
}
err = snd_pcm_ladspa_connect_controls(plugin, &plugin->input, instance);
assert(err >= 0);
err = snd_pcm_ladspa_connect_controls(plugin, &plugin->output, instance);
assert(err >= 0);
if (plugin->desc->activate)
plugin->desc->activate(instance->handle);
}
err = snd_pcm_ladspa_check_connect(plugin, &plugin->input, &instance->input, depth);
if (err < 0)
return err;
err = snd_pcm_ladspa_check_connect(plugin, &plugin->output, &instance->output, depth);
if (err < 0)
return err;
depth++;
}
return 0;
}
static LADSPA_Data *snd_pcm_ladspa_allocate_zero(snd_pcm_ladspa_t *ladspa, unsigned int idx)
{
if (ladspa->zero[idx] == NULL)
ladspa->zero[idx] = calloc(ladspa->allocated, sizeof(LADSPA_Data));
return ladspa->zero[idx];
}
static int snd_pcm_ladspa_allocate_memory(snd_pcm_t *pcm, snd_pcm_ladspa_t *ladspa)
{
struct list_head *list, *pos, *pos1;
snd_pcm_ladspa_instance_t *instance;
unsigned int channels = 16, nchannels;
unsigned int ichannels, ochannels;
void **pchannels, **npchannels;
unsigned int idx, chn;
ladspa->allocated = 2048;
if (pcm->buffer_size > ladspa->allocated)
ladspa->allocated = pcm->buffer_size;
if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
ichannels = pcm->channels;
ochannels = ladspa->plug.gen.slave->channels;
} else {
ichannels = ladspa->plug.gen.slave->channels;
ochannels = pcm->channels;
}
pchannels = calloc(1, sizeof(void *) * channels);
if (pchannels == NULL)
return -ENOMEM;
list = pcm->stream == SND_PCM_STREAM_PLAYBACK ? &ladspa->pplugins : &ladspa->cplugins;
list_for_each(pos, list) {
snd_pcm_ladspa_plugin_t *plugin = list_entry(pos, snd_pcm_ladspa_plugin_t, list);
list_for_each(pos1, &plugin->instances) {
instance = list_entry(pos1, snd_pcm_ladspa_instance_t, list);
nchannels = channels;
for (idx = 0; idx < instance->input.channels.size; idx++) {
chn = instance->input.channels.array[idx];
assert(instance->input.ports.array[idx] != NO_ASSIGN);
if (chn >= nchannels)
nchannels = chn + 1;
}
for (idx = 0; idx < instance->output.channels.size; idx++) {
chn = instance->output.channels.array[idx];
assert(instance->output.ports.array[idx] != NO_ASSIGN);
if (chn >= nchannels)
nchannels = chn + 1;
}
if (nchannels != channels) {
npchannels = realloc(pchannels, nchannels * sizeof(void *));
if (npchannels == NULL) {
free(pchannels);
return -ENOMEM;
}
for (idx = channels; idx < nchannels; idx++)
npchannels[idx] = NULL;
pchannels = npchannels;
}
assert(instance->input.data == NULL);
assert(instance->input.m_data == NULL);
assert(instance->output.data == NULL);
assert(instance->output.m_data == NULL);
instance->input.data = calloc(instance->input.channels.size, sizeof(void *));
instance->input.m_data = calloc(instance->input.channels.size, sizeof(void *));
instance->output.data = calloc(instance->output.channels.size, sizeof(void *));
instance->output.m_data = calloc(instance->output.channels.size, sizeof(void *));
if (instance->input.data == NULL ||
instance->input.m_data == NULL ||
instance->output.data == NULL ||
instance->output.m_data == NULL) {
free(pchannels);
return -ENOMEM;
}
for (idx = 0; idx < instance->input.channels.size; idx++) {
chn = instance->input.channels.array[idx];
if (pchannels[chn] == NULL && chn < ichannels) {
instance->input.data[idx] = NULL;
continue;
}
instance->input.data[idx] = pchannels[chn];
if (instance->input.data[idx] == NULL) {
instance->input.data[idx] = snd_pcm_ladspa_allocate_zero(ladspa, 0);
if (instance->input.data[idx] == NULL) {
free(pchannels);
return -ENOMEM;
}
}
}
for (idx = 0; idx < instance->output.channels.size; idx++) {
chn = instance->output.channels.array[idx];
/* FIXME/OPTIMIZE: check if we can remove double alloc */
/* if LADSPA plugin has no broken inplace */
instance->output.data[idx] = malloc(sizeof(LADSPA_Data) * ladspa->allocated);
if (instance->output.data[idx] == NULL) {
free(pchannels);
return -ENOMEM;
}
pchannels[chn] = instance->output.m_data[idx] = instance->output.data[idx];
}
}
}
/* OPTIMIZE: we have already allocated areas for ALSA output channels */
/* next loop deallocates the last output LADSPA areas and connects */
/* them to ALSA areas (NULL) or dummy area ladpsa->free[1] ; */
/* this algorithm might be optimized to not allocate the last LADSPA outputs */
list_for_each(pos, list) {
snd_pcm_ladspa_plugin_t *plugin = list_entry(pos, snd_pcm_ladspa_plugin_t, list);
list_for_each(pos1, &plugin->instances) {
instance = list_entry(pos1, snd_pcm_ladspa_instance_t, list);
for (idx = 0; idx < instance->output.channels.size; idx++) {
chn = instance->output.channels.array[idx];
if (instance->output.data[idx] == pchannels[chn]) {
free(instance->output.m_data[idx]);
instance->output.m_data[idx] = NULL;
if (chn < ochannels) {
instance->output.data[idx] = NULL;
} else {
instance->output.data[idx] = snd_pcm_ladspa_allocate_zero(ladspa, 1);
if (instance->output.data[idx] == NULL) {
free(pchannels);
return -ENOMEM;
}
}
}
}
}
}
#if 0
printf("zero[0] = %p\n", ladspa->zero[0]);
printf("zero[1] = %p\n", ladspa->zero[1]);
list_for_each(pos, list) {
snd_pcm_ladspa_plugin_t *plugin = list_entry(pos, snd_pcm_ladspa_plugin_t, list);
list_for_each(pos1, &plugin->instances) {
instance = list_entry(pos1, snd_pcm_ladspa_instance_t, list);
for (idx = 0; idx < instance->input.channels.size; idx++)
printf("%i:alloc-input%i: data = %p, m_data = %p\n", instance->depth, idx, instance->input.data[idx], instance->input.m_data[idx]);
for (idx = 0; idx < instance->output.channels.size; idx++)
printf("%i:alloc-output%i: data = %p, m_data = %p\n", instance->depth, idx, instance->output.data[idx], instance->output.m_data[idx]);
}
}
#endif
free(pchannels);
return 0;
}
static int snd_pcm_ladspa_init(snd_pcm_t *pcm)
{
snd_pcm_ladspa_t *ladspa = pcm->private_data;
int err;
snd_pcm_ladspa_free_instances(pcm, ladspa, 1);
err = snd_pcm_ladspa_allocate_instances(pcm, ladspa);
if (err < 0) {
snd_pcm_ladspa_free_instances(pcm, ladspa, 1);
return err;
}
err = snd_pcm_ladspa_allocate_memory(pcm, ladspa);
if (err < 0) {
snd_pcm_ladspa_free_instances(pcm, ladspa, 1);
return err;
}
return 0;
}
static int snd_pcm_ladspa_hw_free(snd_pcm_t *pcm)
{
snd_pcm_ladspa_t *ladspa = pcm->private_data;
snd_pcm_ladspa_free_instances(pcm, ladspa, 1);
return snd_pcm_generic_hw_free(pcm);
}
static snd_pcm_uframes_t
snd_pcm_ladspa_write_areas(snd_pcm_t *pcm,
const snd_pcm_channel_area_t *areas,
snd_pcm_uframes_t offset,
snd_pcm_uframes_t size,
const snd_pcm_channel_area_t *slave_areas,
snd_pcm_uframes_t slave_offset,
snd_pcm_uframes_t *slave_sizep)
{
snd_pcm_ladspa_t *ladspa = pcm->private_data;
snd_pcm_ladspa_instance_t *instance;
struct list_head *pos, *pos1;
LADSPA_Data *data;
unsigned int idx, chn, size1, size2;
if (size > *slave_sizep)
size = *slave_sizep;
size2 = size;
#if 0 /* no processing - for testing purposes only */
snd_pcm_areas_copy(slave_areas, slave_offset,
areas, offset,
pcm->channels, size, pcm->format);
#else
while (size > 0) {
size1 = size;
if (size1 > ladspa->allocated)
size1 = ladspa->allocated;
list_for_each(pos, &ladspa->pplugins) {
snd_pcm_ladspa_plugin_t *plugin = list_entry(pos, snd_pcm_ladspa_plugin_t, list);
list_for_each(pos1, &plugin->instances) {
instance = list_entry(pos1, snd_pcm_ladspa_instance_t, list);
for (idx = 0; idx < instance->input.channels.size; idx++) {
chn = instance->input.channels.array[idx];
data = instance->input.data[idx];
if (data == NULL) {
data = (LADSPA_Data *)((char *)areas[chn].addr + (areas[chn].first / 8));
data += offset;
}
instance->desc->connect_port(instance->handle, instance->input.ports.array[idx], data);
}
for (idx = 0; idx < instance->output.channels.size; idx++) {
chn = instance->output.channels.array[idx];
data = instance->output.data[idx];
if (data == NULL) {
data = (LADSPA_Data *)((char *)slave_areas[chn].addr + (areas[chn].first / 8));
data += slave_offset;
}
instance->desc->connect_port(instance->handle, instance->output.ports.array[idx], data);
}
instance->desc->run(instance->handle, size1);
}
}
offset += size1;
slave_offset += size1;
size -= size1;
}
#endif
*slave_sizep = size2;
return size2;
}
static snd_pcm_uframes_t
snd_pcm_ladspa_read_areas(snd_pcm_t *pcm,
const snd_pcm_channel_area_t *areas,
snd_pcm_uframes_t offset,
snd_pcm_uframes_t size,
const snd_pcm_channel_area_t *slave_areas,
snd_pcm_uframes_t slave_offset,
snd_pcm_uframes_t *slave_sizep)
{
snd_pcm_ladspa_t *ladspa = pcm->private_data;
snd_pcm_ladspa_instance_t *instance;
struct list_head *pos, *pos1;
LADSPA_Data *data;
unsigned int idx, chn, size1, size2;;
if (size > *slave_sizep)
size = *slave_sizep;
size2 = size;
#if 0 /* no processing - for testing purposes only */
snd_pcm_areas_copy(areas, offset,
slave_areas, slave_offset,
pcm->channels, size, pcm->format);
#else
while (size > 0) {
size1 = size;
if (size1 > ladspa->allocated)
size1 = ladspa->allocated;
list_for_each(pos, &ladspa->cplugins) {
snd_pcm_ladspa_plugin_t *plugin = list_entry(pos, snd_pcm_ladspa_plugin_t, list);
list_for_each(pos1, &plugin->instances) {
instance = list_entry(pos1, snd_pcm_ladspa_instance_t, list);
for (idx = 0; idx < instance->input.channels.size; idx++) {
chn = instance->input.channels.array[idx];
data = instance->input.data[idx];
if (data == NULL) {
data = (LADSPA_Data *)((char *)slave_areas[chn].addr + (areas[chn].first / 8));
data += slave_offset;
}
instance->desc->connect_port(instance->handle, instance->input.ports.array[idx], data);
}
for (idx = 0; idx < instance->output.channels.size; idx++) {
chn = instance->output.channels.array[idx];
data = instance->output.data[idx];
if (data == NULL) {
data = (LADSPA_Data *)((char *)areas[chn].addr + (areas[chn].first / 8));
data += offset;
}
instance->desc->connect_port(instance->handle, instance->output.ports.array[idx], data);
}
instance->desc->run(instance->handle, size1);
}
}
offset += size1;
slave_offset += size1;
size -= size1;
}
#endif
*slave_sizep = size2;
return size2;
}
static void snd_pcm_ladspa_dump_direction(snd_pcm_ladspa_plugin_t *plugin,
snd_pcm_ladspa_plugin_io_t *io,
snd_output_t *out)
{
unsigned int idx, midx;
if (io->port_bindings_size == 0)
goto __control;
snd_output_printf(out, " Audio %s port bindings:\n", io->pdesc == LADSPA_PORT_INPUT ? "input" : "output");
for (idx = 0; idx < io->port_bindings_size; idx++) {
if (io->port_bindings[idx] == NO_ASSIGN)
snd_output_printf(out, " %i -> NONE\n", idx);
else
snd_output_printf(out, " %i -> %i\n", idx, io->port_bindings[idx]);
}
__control:
if (io->controls_size == 0)
return;
snd_output_printf(out, " Control %s port initial values:\n", io->pdesc == LADSPA_PORT_INPUT ? "input" : "output");
for (idx = midx = 0; idx < plugin->desc->PortCount; idx++) {
if ((plugin->desc->PortDescriptors[idx] & (io->pdesc | LADSPA_PORT_CONTROL)) == (io->pdesc | LADSPA_PORT_CONTROL)) {
snd_output_printf(out, " %i \"%s\" = %.8f\n", idx, plugin->desc->PortNames[idx], io->controls[midx]);
midx++;
}
}
}
static void snd_pcm_ladspa_dump_array(snd_output_t *out,
snd_pcm_ladspa_array_t *array,