/usr/share/perl5/Audio/Nama/Effect_subs.pm is in nama 1.078-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 | # ------ Effect Routines -------
package Audio::Nama;
use Modern::Perl;
use Carp;
use Audio::Nama::Util qw(round);
no warnings 'uninitialized';
use vars qw($cop_hints_yml);
our (
%tn,
%ti,
$this_track,
$this_op,
%cops,
%copp,
$debug,
$debug2,
$ui,
%effect_i,
@effects,
$regenerate_setup,
$cop_id,
$magical_cop_id,
%offset,
# automix()
%bn,
$main,
);
our (
%opts,
$effects_cache_file,
@effects_static_vars,
%effect_j,
@ladspa_sorted,
%e_bound,
# $cop_hints_yaml, # error explicit package name required at line 867
$ladspa_sample_rate,
%ladspa_help,
%effects_ladspa_file,
%ladspa_unique_id,
%ladspa_label,
%effects_ladspa,
@effects_help,
);
## high-level functions
sub add_effect {
$debug2 and print "&add_effect\n";
my %p = %{shift()};
my ($n,$code,$parent_id,$id,$parameter,$values) =
@p{qw( chain type parent_id cop_id parameter values)};
my $i = $effect_i{$code};
# don't create an existing vol or pan effect
return if $id and ($id eq $ti{$n}->vol
or $id eq $ti{$n}->pan);
$id = cop_add(\%p);
%p = ( %p, cop_id => $id); # replace chainop id
$ui->add_effect_gui(\%p) unless $ti{$n}->hide;
if( valid_engine_setup() ){
my $er = engine_running();
$ti{$n}->mute if $er;
apply_op($id);
$ti{$n}->unmute if $er;
}
$id;
}
sub modify_effect {
my ($op_id, $parameter, $sign, $value) = @_;
# $parameter: zero based
my $cop = $cops{$op_id}
or print("$op_id: non-existing effect id. Skipping\n"), return;
my $code = $cop->{type};
my $i = effect_index($code);
defined $i or croak "undefined effect code for $op_id: ",yaml_out($cop);
my $parameter_count = scalar @{ $effects[$i]->{params} };
#print "op_id: $op_id, code: ",$cops{$op_id}->{type}," parameter count: $parameter_count\n";
print("$op_id: effect does not exist, skipping\n"), return
unless $cops{$op_id};
print("$op_id: parameter (", $parameter + 1, ") out of range, skipping.\n"), return
unless ($parameter >= 0 and $parameter < $parameter_count);
my $new_value = $value;
if ($sign) {
$new_value =
eval (join " ",
$copp{$op_id}->[$parameter],
$sign,
$value);
};
$this_op = $op_id;
$debug and print "id $op_id p: $parameter, sign: $sign value: $value\n";
effect_update_copp_set(
$op_id,
$parameter,
$new_value);
}
sub modify_multiple_effects {
my ($op_ids, $parameters, $sign, $value) = @_;
map{ my $op_id = $_;
map{ my $parameter = $_;
$parameter--; # convert to zero-base
modify_effect($op_id, $parameter, $sign, $value);
} @$parameters;
$this_op = $op_id; # set current effect
} @$op_ids;
}
sub remove_effect {
$debug2 and print "&remove_effect\n";
my $id = shift;
carp("$id: does not exist, skipping...\n"), return unless $cops{$id};
my $n = $cops{$id}->{chain};
my $parent = $cops{$id}->{belongs_to} ;
$debug and print "id: $id, parent: $parent\n";
my $object = $parent ? q(controller) : q(chain operator);
$debug and print qq(ready to remove $object "$id" from track "$n"\n);
$ui->remove_effect_gui($id);
# recursively remove children
$debug and print "children found: ", join "|",@{$cops{$id}->{owns}},"\n";
map{remove_effect($_)}@{ $cops{$id}->{owns} }
if defined $cops{$id}->{owns};
;
if ( ! $parent ) { # i am a chain operator, have no parent
remove_op($id);
} else { # i am a controller
# remove the controller
remove_op($id);
# i remove ownership of deleted controller
$debug and print "parent $parent owns list: ", join " ",
@{ $cops{$parent}->{owns} }, "\n";
@{ $cops{$parent}->{owns} } = grep{ $_ ne $id}
@{ $cops{$parent}->{owns} } ;
$cops{$id}->{belongs_to} = undef;
$debug and print "parent $parent new owns list: ", join " ",
@{ $cops{$parent}->{owns} } ,$/;
}
$ti{$n}->remove_effect_from_track( $id );
delete $cops{$id}; # remove entry from chain operator list
delete $copp{$id}; # remove entry from chain operator parameters list
$this_op = undef;
}
sub position_effect {
my($op, $pos) = @_;
# we cannot handle controllers
print("$op or $pos: controller not allowed, skipping.\n"), return
if grep{ $cops{$_}->{belongs_to} } $op, $pos;
# first, modify track data structure
print("$op: effect does not exist, skipping.\n"), return unless $cops{$op};
my $track = $ti{$cops{$op}->{chain}};
my $op_index = nama_effect_index($op);
my @new_op_list = @{$track->ops};
# remove op
splice @new_op_list, $op_index, 1;
my $new_op_index;
if ( $pos eq 'ZZZ'){
# put it at the end
push @new_op_list, $op;
}
else {
my $track2 = $ti{$cops{$pos}->{chain}};
print("$pos: position belongs to a different track, skipping.\n"), return
unless $track eq $track2;
$new_op_index = nama_effect_index($pos);
# insert op
splice @new_op_list, $new_op_index, 0, $op;
}
# reconfigure the entire engine (inefficient, but easy to do)
#say join " - ",@new_op_list;
@{$track->ops} = @new_op_list;
$regenerate_setup++;
reconfigure_engine();
$this_track = $track;
command_process('show_track');
}
## array indices for Nama and Ecasound effects and controllers
sub nama_effect_index { # returns nama chain operator index
# does not distinguish op/ctrl
my $id = shift;
my $n = $cops{$id}->{chain};
my $arr = $ti{$n}->ops;
$debug and print "id: $id n: $n \n";
$debug and print join $/,@{ $ti{$n}->ops }, $/;
for my $pos ( 0.. scalar @{ $ti{$n}->ops } - 1 ) {
return $pos if $arr->[$pos] eq $id;
};
}
sub ecasound_effect_index {
my $id = shift;
my $n = $cops{$id}->{chain};
my $opcount; # one-based
$debug and print "id: $id n: $n \n",join $/,@{ $ti{$n}->ops }, $/;
for my $op (@{ $ti{$n}->ops }) {
# increment only for ops, not controllers
next if $cops{$op}->{belongs_to};
++$opcount;
last if $op eq $id
}
$offset{$n} + $opcount;
}
sub ctrl_index {
my $id = shift;
nama_effect_index($id) - nama_effect_index(root_parent($id));
}
sub ecasound_operator_index { # does not include offset
my $id = shift;
my $chain = $cops{$id}{chain};
my $track = $ti{$chain};
my @ops = @{$track->ops};
my $controller_count = 0;
my $position;
for my $i (0..scalar @ops - 1) {
$position = $i, last if $ops[$i] eq $id;
$controller_count++ if $cops{$ops[$i]}{belongs_to};
}
$position -= $controller_count; # skip controllers
++$position; # translates 0th to chain-position 1
}
sub ecasound_controller_index {
my $id = shift;
my $chain = $cops{$id}{chain};
my $track = $ti{$chain};
my @ops = @{$track->ops};
my $operator_count = 0;
my $position;
for my $i (0..scalar @ops - 1) {
$position = $i, last if $ops[$i] eq $id;
$operator_count++ if ! $cops{$ops[$i]}{belongs_to};
}
$position -= $operator_count; # skip operators
++$position; # translates 0th to chain-position 1
}
sub effect_code {
# get text effect code from user input, which could be
# - LADSPA Unique ID (number)
# - LADSPA Label (el:something)
# - abbreviated LADSPA label (something)
# - Ecasound operator (something)
# - abbreviated Ecasound preset (something)
# - Ecasound preset (pn:something)
# there is no interference in these labels at present,
# so we offer the convenience of using them without
# el: and pn: prefixes.
my $input = shift;
my $code;
if ($input !~ /\D/){ # i.e. $input is all digits
$code = $ladspa_label{$input}
or carp("$input: LADSPA plugin not found. Aborting.\n"), return;
}
elsif ( $effect_i{$input} ) { $code = $input }
elsif ( $effect_j{$input} ) { $code = $effect_j{$input} }
else { warn "$input: effect code not found\n";}
$code;
}
sub effect_index {
my $code = shift;
my $i = $effect_i{effect_code($code)};
defined $i or warn "$code: effect index not found\n";
$i
}
## Ecasound engine -- apply/remove chain operators
sub apply_ops { # in addition to operators in .ecs file
$debug2 and print "&apply_ops\n";
for my $n ( map{ $_->n } Audio::Nama::Track::all() ) {
$debug and print "chain: $n, offset: ", $offset{$n}, "\n";
next unless Audio::Nama::ChainSetup::is_ecasound_chain($n);
#next if $n == 2; # no volume control for mix track
#next if ! defined $offset{$n}; # for MIX
#next if ! $offset{$n} ;
# controllers will follow ops, so safe to apply all in order
for my $id ( @{ $ti{$n}->ops } ) {
apply_op($id);
}
}
ecasound_select_chain($this_track->n) if defined $this_track;
}
sub apply_op {
$debug2 and print "&apply_op\n";
my $id = shift;
my $selected = shift;
$debug and print "id: $id\n";
my $code = $cops{$id}->{type};
my $dad = $cops{$id}->{belongs_to};
$debug and print "chain: $cops{$id}->{chain} type: $cops{$id}->{type}, code: $code\n";
# if code contains colon, then follow with comma (preset, LADSPA)
# if code contains no colon, then follow with colon (ecasound, ctrl)
$code = '-' . $code . ($code =~ /:/ ? q(,) : q(:) );
my @vals = @{ $copp{$id} };
$debug and print "values: @vals\n";
# we start to build iam command
my $add = $dad ? "ctrl-add " : "cop-add ";
$add .= $code . join ",", @vals;
# if my parent has a parent then we need to append the -kx operator
$add .= " -kx" if $cops{$dad}->{belongs_to};
$debug and print "command: ", $add, "\n";
eval_iam("c-select $cops{$id}->{chain}")
if $selected != $cops{$id}->{chain};
if ( $dad ) {
eval_iam("cop-select " . ecasound_effect_index($dad));
}
eval_iam($add);
$debug and print "children found: ", join ",", "|",@{$cops{$id}->{owns}},"|\n";
my $ref = ref $cops{$id}->{owns} ;
$ref =~ /ARRAY/ or croak "expected array";
my @owns = @{ $cops{$id}->{owns} };
$debug and print "owns: @owns\n";
#map{apply_op($_)} @owns;
}
sub remove_op {
# remove chain operator from Ecasound engine
$debug2 and print "&remove_op\n";
# only if engine is configured
return unless eval_iam('cs-connected') and eval_iam('cs-is-valid');
my $id = shift;
my $n = $cops{$id}->{chain};
my $index;
my $parent = $cops{$id}->{belongs_to};
# select chain
return unless ecasound_select_chain($n);
# deal separately with controllers and chain operators
if ( ! $parent ){ # chain operator
$debug and print "no parent, assuming chain operator\n";
$index = ecasound_effect_index( $id );
$debug and print "ops list for chain $n: @{$ti{$n}->ops}\n";
$debug and print "operator id to remove: $id\n";
$debug and print "ready to remove from chain $n, operator id $id, index $index\n";
$debug and eval_iam("cs");
eval_iam("cop-select ". ecasound_effect_index($id) );
$debug and print "selected operator: ", eval_iam("cop-selected"), $/;
eval_iam("cop-remove");
$debug and eval_iam("cs");
} else { # controller
$debug and print "has parent, assuming controller\n";
my $ctrl_index = ctrl_index($id);
$debug and print eval_iam("cs");
eval_iam("cop-select ". ecasound_effect_index(root_parent($id)));
$debug and print "selected operator: ", eval_iam("cop-selected"), $/;
eval_iam("ctrl-select $ctrl_index");
eval_iam("ctrl-remove");
$debug and print eval_iam("cs");
}
}
# Track sax effects: A B C GG HH II D E F
# GG HH and II are controllers applied to chain operator C
#
# to remove controller HH:
#
# for Ecasound, chain op index = 3,
# ctrl index = 2
# = nama_effect_index HH - nama_effect_index C
#
#
# for Nama, chain op array index 2,
# ctrl arrray index = chain op array index + ctrl_index
# = effect index - 1 + ctrl_index
#
#
sub root_parent {
my $id = shift;
my $parent = $cops{$id}->{belongs_to};
carp("$id: has no parent, skipping...\n"),return unless $parent;
my $root_parent = $cops{$parent}->{belongs_to};
$parent = $root_parent || $parent;
$debug and print "$id: is a controller-controller, root parent: $parent\n";
$parent;
}
## manage Nama effects -- entries in %cops array
sub cop_add {
$debug2 and print "&cop_add\n";
my $p = shift;
my %p = %$p;
$debug and say yaml_out($p);
# return an existing id
return $p{cop_id} if $p{cop_id};
# use an externally provided (magical) id or the
# incrementing counter
my $id = $magical_cop_id || $cop_id;
# make entry in %cops with chain, code, display-type, children
my ($n, $type, $parent_id, $parameter) =
@p{qw(chain type parent_id parameter)};
my $i = $effect_i{$type};
$debug and print "Issuing a cop_id for track $n: $id\n";
$cops{$id} = {chain => $n,
type => $type,
display => $effects[$i]->{display},
owns => [] };
$p->{cop_id} = $id;
# set defaults
if (! $p{values}){
my @vals;
$debug and print "no settings found, loading defaults if present\n";
my $i = $effect_i{ $cops{$id}->{type} };
# don't initialize first parameter if operator has a parent
# i.e. if operator is a controller
for my $p ($parent_id ? 1 : 0..$effects[$i]->{count} - 1) {
my $default = $effects[$i]->{params}->[$p]->{default};
push @vals, $default;
}
$debug and print "copid: $id defaults: @vals \n";
$copp{$id} = \@vals;
}
if ($parent_id) {
$debug and print "parent found: $parent_id\n";
# store relationship
$debug and print "parent owns" , join " ",@{ $cops{$parent_id}->{owns}}, "\n";
push @{ $cops{$parent_id}->{owns}}, $id;
$debug and print join " ", "my attributes:", (keys %{ $cops{$id} }), "\n";
$cops{$id}->{belongs_to} = $parent_id;
$debug and print join " ", "my attributes again:", (keys %{ $cops{$id} }), "\n";
$debug and print "parameter: $parameter\n";
# set fx-param to the parameter number, which one
# above the zero-based array offset that $parameter represents
$copp{$id}->[0] = $parameter + 1;
# find position of parent and insert child immediately afterwards
my $end = scalar @{ $ti{$n}->ops } - 1 ;
for my $i (0..$end){
splice ( @{$ti{$n}->ops}, $i+1, 0, $id ), last
if $ti{$n}->ops->[$i] eq $parent_id
}
}
else { push @{$ti{$n}->ops }, $id; }
# set values if present
# ugly! The passed values ref may be used for multiple
# instances, so we copy it here [ @$values ]
$copp{$id} = [ @{$p{values}} ] if $p{values};
# make sure the counter $cop_id will not occupy an
# already used value
while( $cops{$cop_id}){$cop_id++};
$id;
}
## synchronize Ecasound chain operator parameters
# with Nama effect parameter
sub effect_update {
# update the parameters of the Ecasound chain operator
# referred to by a Nama operator_id
#$debug2 and print "&effect_update\n";
return unless valid_engine_setup();
#my $es = eval_iam("engine-status");
#$debug and print "engine is $es\n";
#return if $es !~ /not started|stopped|running/;
my ($id, $param, $val) = @_;
$param++; # so the value at $p[0] is applied to parameter 1
carp("$id: effect not found. skipping...\n"), return unless $cops{$id};
my $chain = $cops{$id}{chain};
return unless Audio::Nama::ChainSetup::is_ecasound_chain($chain);
$debug and print "chain $chain id $id param $param value $val\n";
# $param is zero-based.
# %copp is zero-based.
$debug and print join " ", @_, "\n";
my $old_chain = eval_iam('c-selected') if valid_engine_setup();
ecasound_select_chain($chain);
# update Ecasound's copy of the parameter
if( is_controller($id)){
my $i = ecasound_controller_index($id);
$debug and print
"controller $id: track: $chain, index: $i param: $param, value: $val\n";
eval_iam("ctrl-select $i");
eval_iam("ctrlp-select $param");
eval_iam("ctrlp-set $val");
}
else { # is operator
my $i = ecasound_operator_index($id);
$debug and print
"operator $id: track $chain, index: $i, offset: ",
$offset{$chain}, " param $param, value $val\n";
eval_iam("cop-select ". ($offset{$chain} + $i));
eval_iam("copp-select $param");
eval_iam("copp-set $val");
}
ecasound_select_chain($old_chain);
}
# set both Nama effect and Ecasound chain operator
# parameters
sub effect_update_copp_set {
my ($id, $param, $val) = @_;
effect_update( @_ );
$copp{$id}->[$param] = $val;
}
sub sync_effect_parameters {
# when a controller changes an effect parameter
# the effect state can differ from the state in
# %copp, Nama's effect parameter store
#
# this routine syncs them in prep for save_state()
return unless valid_engine_setup();
my $old_chain = eval_iam('c-selected');
map{ sync_one_effect($_) } ops_with_controller();
eval_iam("c-select $old_chain");
}
sub sync_one_effect {
my $id = shift;
my $chain = $cops{$id}{chain};
eval_iam("c-select $chain");
eval_iam("cop-select " . ( $offset{$chain} + ecasound_operator_index($id)));
$copp{$id} = get_cop_params( scalar @{$copp{$id}} );
}
sub get_cop_params {
my $count = shift;
my @params;
for (1..$count){
eval_iam("copp-select $_");
push @params, eval_iam("copp-get");
}
\@params
}
sub ops_with_controller {
grep{ ! is_controller($_) }
grep{ scalar @{$cops{$_}{owns}} }
map{ @{ $_->ops } }
Audio::Nama::ChainSetup::engine_tracks();
}
sub is_controller { my $id = shift; $cops{$id}{belongs_to} }
sub find_op_offsets {
$debug2 and print "&find_op_offsets\n";
my @op_offsets = grep{ /"\d+"/} split "\n",eval_iam("cs");
$debug and print join "\n\n",@op_offsets;
for my $output (@op_offsets){
my $chain_id;
($chain_id) = $output =~ m/Chain "(\w*\d+)"/;
# print "chain_id: $chain_id\n";
next if $chain_id =~ m/\D/; # skip id's containing non-digits
# i.e. M1
my $quotes = $output =~ tr/"//;
$debug and print "offset: $quotes in $output\n";
$offset{$chain_id} = $quotes/2 - 1;
}
}
## register data about LADSPA plugins, and Ecasound effects and
# presets (names, ids, parameters, hints)
sub prepare_static_effects_data{
$debug2 and print "&prepare_static_effects_data\n";
my $effects_cache = join_path(&project_root, $effects_cache_file);
#print "newplugins: ", new_plugins(), $/;
if ($opts{r} or new_plugins()){
eval { unlink $effects_cache};
print "Regenerating effects data cache\n";
}
if (-f $effects_cache and ! $opts{C}){
$debug and print "found effects cache: $effects_cache\n";
assign_var($effects_cache, @effects_static_vars);
} else {
$debug and print "reading in effects data, please wait...\n";
read_in_effects_data();
# cop-register, preset-register, ctrl-register, ladspa-register
get_ladspa_hints();
integrate_ladspa_hints();
integrate_cop_hints();
sort_ladspa_effects();
prepare_effects_help();
serialize (
file => $effects_cache,
vars => \@effects_static_vars,
class => 'Audio::Nama',
format => 'storable');
}
prepare_effect_index();
}
sub ladspa_plugin_list {
my @plugins;
my %seen;
for my $dir ( split ':', ladspa_path()){
next unless -d $dir;
opendir my ($dirh), $dir;
push @plugins,
map{"$dir/$_"} # full path
grep{ ! $seen{$_} and ++$seen{$_}} # skip seen plugins
grep{ /\.so$/} readdir $dirh; # get .so files
closedir $dirh;
}
@plugins
}
sub new_plugins {
my $effects_cache = join_path(&project_root, $effects_cache_file);
my @filenames = ladspa_plugin_list();
push @filenames, '/usr/local/share/ecasound/effect_presets',
'/usr/share/ecasound/effect_presets',
"$ENV{HOME}/.ecasound/effect_presets";
my $effects_cache_stamp = modified_stamp($effects_cache);
my $latest;
map{ my $mod = modified_stamp($_);
$latest = $mod if $mod > $latest } @filenames;
$latest > $effects_cache_stamp;
}
sub modified_stamp {
# timestamp that file was modified
my $filename = shift;
#print "file: $filename\n";
my @s = stat $filename;
$s[9];
}
sub prepare_effect_index {
$debug2 and print "&prepare_effect_index\n";
%effect_j = ();
map{
my $code = $_;
my ($short) = $code =~ /:([-\w]+)/;
if ( $short ) {
if ($effect_j{$short}) { warn "name collision: $_\n" }
else { $effect_j{$short} = $code }
}else{ $effect_j{$code} = $code };
} keys %effect_i;
#print yaml_out \%effect_j;
}
sub extract_effects_data {
$debug2 and print "&extract_effects_data\n";
my ($lower, $upper, $regex, $separator, @lines) = @_;
carp ("incorrect number of lines ", join ' ',$upper-$lower,scalar @lines)
if $lower + @lines - 1 != $upper;
$debug and print"lower: $lower upper: $upper separator: $separator\n";
#$debug and print "lines: ". join "\n",@lines, "\n";
$debug and print "regex: $regex\n";
for (my $j = $lower; $j <= $upper; $j++) {
my $line = shift @lines;
$line =~ /$regex/ or carp("bad effect data line: $line\n"),next;
my ($no, $name, $id, $rest) = ($1, $2, $3, $4);
$debug and print "Number: $no Name: $name Code: $id Rest: $rest\n";
my @p_names = split $separator,$rest;
map{s/'//g}@p_names; # remove leading and trailing q(') in ladspa strings
$debug and print "Parameter names: @p_names\n";
$effects[$j]={};
$effects[$j]->{number} = $no;
$effects[$j]->{code} = $id;
$effects[$j]->{name} = $name;
$effects[$j]->{count} = scalar @p_names;
$effects[$j]->{params} = [];
$effects[$j]->{display} = qq(field);
map{ push @{$effects[$j]->{params}}, {name => $_} } @p_names
if @p_names;
;
}
}
sub sort_ladspa_effects {
$debug2 and print "&sort_ladspa_effects\n";
# print yaml_out(\%e_bound);
my $aa = $e_bound{ladspa}{a};
my $zz = $e_bound{ladspa}{z};
# print "start: $aa end $zz\n";
map{push @ladspa_sorted, 0} ( 1 .. $aa ); # fills array slice [0..$aa-1]
splice @ladspa_sorted, $aa, 0,
sort { $effects[$a]->{name} cmp $effects[$b]->{name} } ($aa .. $zz) ;
$debug and print "sorted array length: ". scalar @ladspa_sorted, "\n";
}
sub read_in_effects_data {
$debug2 and print "&read_in_effects_data\n";
my $lr = eval_iam("ladspa-register");
#print $lr;
my @ladspa = split "\n", $lr;
# join the two lines of each entry
my @lad = map { join " ", splice(@ladspa,0,2) } 1..@ladspa/2;
my @preset = grep {! /^\w*$/ } split "\n", eval_iam("preset-register");
my @ctrl = grep {! /^\w*$/ } split "\n", eval_iam("ctrl-register");
my @cop = grep {! /^\w*$/ } split "\n", eval_iam("cop-register");
$debug and print "found ", scalar @cop, " Ecasound chain operators\n";
$debug and print "found ", scalar @preset, " Ecasound presets\n";
$debug and print "found ", scalar @ctrl, " Ecasound controllers\n";
$debug and print "found ", scalar @lad, " LADSPA effects\n";
# index boundaries we need to make effects list and menus
$e_bound{cop}{a} = 1;
$e_bound{cop}{z} = @cop; # scalar
$e_bound{ladspa}{a} = $e_bound{cop}{z} + 1;
$e_bound{ladspa}{b} = $e_bound{cop}{z} + int(@lad/4);
$e_bound{ladspa}{c} = $e_bound{cop}{z} + 2*int(@lad/4);
$e_bound{ladspa}{d} = $e_bound{cop}{z} + 3*int(@lad/4);
$e_bound{ladspa}{z} = $e_bound{cop}{z} + @lad;
$e_bound{preset}{a} = $e_bound{ladspa}{z} + 1;
$e_bound{preset}{b} = $e_bound{ladspa}{z} + int(@preset/2);
$e_bound{preset}{z} = $e_bound{ladspa}{z} + @preset;
$e_bound{ctrl}{a} = $e_bound{preset}{z} + 1;
$e_bound{ctrl}{z} = $e_bound{preset}{z} + @ctrl;
my $cop_re = qr/
^(\d+) # number
\. # dot
\s+ # spaces+
(\w.+?) # name, starting with word-char, non-greedy
# (\w+) # name
,\s* # comma spaces*
-(\w+) # cop_id
:? # maybe colon (if parameters)
(.*$) # rest
/x;
my $preset_re = qr/
^(\d+) # number
\. # dot
\s+ # spaces+
(\w+) # name
,\s* # comma spaces*
-(pn:\w+) # preset_id
:? # maybe colon (if parameters)
(.*$) # rest
/x;
my $ladspa_re = qr/
^(\d+) # number
\. # dot
\s+ # spaces
(.+?) # name, starting with word-char, non-greedy
\s+ # spaces
-(el:[-\w]+),? # ladspa_id maybe followed by comma
(.*$) # rest
/x;
my $ctrl_re = qr/
^(\d+) # number
\. # dot
\s+ # spaces
(\w.+?) # name, starting with word-char, non-greedy
,\s* # comma, zero or more spaces
-(k\w+):? # ktrl_id maybe followed by colon
(.*$) # rest
/x;
extract_effects_data(
$e_bound{cop}{a},
$e_bound{cop}{z},
$cop_re,
q(','),
@cop,
);
extract_effects_data(
$e_bound{ladspa}{a},
$e_bound{ladspa}{z},
$ladspa_re,
q(','),
@lad,
);
extract_effects_data(
$e_bound{preset}{a},
$e_bound{preset}{z},
$preset_re,
q(,),
@preset,
);
extract_effects_data(
$e_bound{ctrl}{a},
$e_bound{ctrl}{z},
$ctrl_re,
q(,),
@ctrl,
);
for my $i (0..$#effects){
$effect_i{ $effects[$i]->{code} } = $i;
$debug and print "i: $i code: $effects[$i]->{code} display: $effects[$i]->{display}\n";
}
$debug and print "\@effects\n======\n", yaml_out(\@effects); ;
}
sub integrate_cop_hints {
my @cop_hints = @{ yaml_in( $cop_hints_yml ) };
for my $hashref ( @cop_hints ){
#print "cop hints ref type is: ",ref $hashref, $/;
my $code = $hashref->{code};
$effects[ $effect_i{ $code } ] = $hashref;
}
}
sub ladspa_path {
$ENV{LADSPA_PATH} || q(/usr/lib/ladspa);
}
sub get_ladspa_hints{
$debug2 and print "&get_ladspa_hints\n";
my @dirs = split ':', ladspa_path();
my $data = '';
my %seen = ();
my @plugins = ladspa_plugin_list();
#pager join $/, @plugins;
# use these regexes to snarf data
my $pluginre = qr/
Plugin\ Name: \s+ "([^"]+)" \s+
Plugin\ Label: \s+ "([^"]+)" \s+
Plugin\ Unique\ ID: \s+ (\d+) \s+
[^\x00]+(?=Ports) # swallow maximum up to Ports
Ports: \s+ ([^\x00]+) # swallow all
/x;
my $paramre = qr/
"([^"]+)" # name inside quotes
\s+
(.+) # rest
/x;
my $i;
for my $file (@plugins){
my @stanzas = split "\n\n", qx(analyseplugin $file);
for my $stanza (@stanzas) {
my ($plugin_name, $plugin_label, $plugin_unique_id, $ports)
= $stanza =~ /$pluginre/
or carp "*** couldn't match plugin stanza $stanza ***";
$debug and print "plugin label: $plugin_label $plugin_unique_id\n";
my @lines = grep{ /input/ and /control/ } split "\n",$ports;
my @params; # data
my @names;
for my $p (@lines) {
next if $p =~ /^\s*$/;
$p =~ s/\.{3}/10/ if $p =~ /amplitude|gain/i;
$p =~ s/\.{3}/60/ if $p =~ /delay|decay/i;
$p =~ s(\.{3})($ladspa_sample_rate/2) if $p =~ /frequency/i;
$p =~ /$paramre/;
my ($name, $rest) = ($1, $2);
my ($dir, $type, $range, $default, $hint) =
split /\s*,\s*/ , $rest, 5;
$debug and print join(
"|",$name, $dir, $type, $range, $default, $hint) , $/;
# if $hint =~ /logarithmic/;
if ( $range =~ /toggled/i ){
$range = q(0 to 1);
$hint .= q(toggled);
}
my %p;
$p{name} = $name;
$p{dir} = $dir;
$p{hint} = $hint;
my ($beg, $end, $default_val, $resolution)
= range($name, $range, $default, $hint, $plugin_label);
$p{begin} = $beg;
$p{end} = $end;
$p{default} = $default_val;
$p{resolution} = $resolution;
push @params, { %p };
}
$plugin_label = "el:" . $plugin_label;
$ladspa_help{$plugin_label} = $stanza;
$effects_ladspa_file{$plugin_unique_id} = $file;
$ladspa_unique_id{$plugin_label} = $plugin_unique_id;
$ladspa_unique_id{$plugin_name} = $plugin_unique_id;
$ladspa_label{$plugin_unique_id} = $plugin_label;
$effects_ladspa{$plugin_label}->{name} = $plugin_name;
$effects_ladspa{$plugin_label}->{id} = $plugin_unique_id;
$effects_ladspa{$plugin_label}->{params} = [ @params ];
$effects_ladspa{$plugin_label}->{count} = scalar @params;
$effects_ladspa{$plugin_label}->{display} = 'scale';
} # pager( join "\n======\n", @stanzas);
#last if ++$i > 10;
}
$debug and print yaml_out(\%effects_ladspa);
}
sub srate_val {
my $input = shift;
my $val_re = qr/(
[+-]? # optional sign
\d+ # one or more digits
(\.\d+)? # optional decimal
(e[+-]?\d+)? # optional exponent
)/ix; # case insensitive e/E
my ($val) = $input =~ /$val_re/; # or carp "no value found in input: $input\n";
$val * ( $input =~ /srate/ ? $ladspa_sample_rate : 1 )
}
sub range {
my ($name, $range, $default, $hint, $plugin_label) = @_;
my $multiplier = 1;;
my ($beg, $end) = split /\s+to\s+/, $range;
$beg = srate_val( $beg );
$end = srate_val( $end );
$default = srate_val( $default );
$default = $default || $beg;
$debug and print "beg: $beg, end: $end, default: $default\n";
if ( $name =~ /gain|amplitude/i ){
$beg = 0.01 unless $beg;
$end = 0.01 unless $end;
}
my $resolution = ($end - $beg) / 100;
if ($hint =~ /integer|toggled/i ) { $resolution = 1; }
elsif ($hint =~ /logarithmic/ ) {
$beg = round ( log $beg ) if $beg;
$end = round ( log $end ) if $end;
$resolution = ($end - $beg) / 100;
$default = $default ? round (log $default) : $default;
}
$resolution = d2( $resolution + 0.002) if $resolution < 1 and $resolution > 0.01;
$resolution = dn ( $resolution, 3 ) if $resolution < 0.01;
$resolution = int ($resolution + 0.1) if $resolution > 1 ;
($beg, $end, $default, $resolution)
}
sub integrate_ladspa_hints {
$debug2 and print "&integrate_ladspa_hints\n";
map{
my $i = $effect_i{$_};
# print("$_ not found\n"),
if ($i) {
$effects[$i]->{params} = $effects_ladspa{$_}->{params};
# we revise the number of parameters read in from ladspa-register
$effects[$i]->{count} = scalar @{$effects_ladspa{$_}->{params}};
$effects[$i]->{display} = $effects_ladspa{$_}->{display};
}
} keys %effects_ladspa;
my %L;
my %M;
map { $L{$_}++ } keys %effects_ladspa;
map { $M{$_}++ } grep {/el:/} keys %effect_i;
for my $k (keys %L) {
$M{$k} or $debug and print "$k not found in ecasound listing\n";
}
for my $k (keys %M) {
$L{$k} or $debug and print "$k not found in ladspa listing\n";
}
$debug and print join "\n", sort keys %effects_ladspa;
$debug and print '-' x 60, "\n";
$debug and print join "\n", grep {/el:/} sort keys %effect_i;
#print yaml_out \@effects; exit;
}
## generate effects help data
sub prepare_effects_help {
# presets
map{ s/^.*? //; # remove initial number
$_ .= "\n"; # add newline
my ($id) = /(pn:\w+)/; # find id
s/,/, /g; # to help line breaks
push @effects_help, $_; #store help
} split "\n",eval_iam("preset-register");
# LADSPA
my $label;
map{
if ( my ($_label) = /-(el:[-\w]+)/ ){
$label = $_label;
s/^\s+/ /; # trim spaces
s/'//g; # remove apostrophes
$_ .="\n"; # add newline
push @effects_help, $_; # store help
} else {
# replace leading number with LADSPA Unique ID
s/^\d+/$ladspa_unique_id{$label}/;
s/\s+$/ /; # remove trailing spaces
substr($effects_help[-1],0,0) = $_; # join lines
$effects_help[-1] =~ s/,/, /g; #
$effects_help[-1] =~ s/,\s+$//;
}
} reverse split "\n",eval_iam("ladspa-register");
#my @lines = reverse split "\n",eval_iam("ladspa-register");
#pager( scalar @lines, $/, join $/,@lines);
#my @crg = map{s/^.*? -//; $_ .= "\n" }
# split "\n",eval_iam("control-register");
#pager (@lrg, @prg); exit;
}
sub automix {
# get working track set
my @tracks = grep{
$tn{$_}->rec_status eq 'MON' or
$bn{$_} and $tn{$_}->rec_status eq 'REC'
} $main->tracks;
say "tracks: @tracks";
## we do not allow automix if inserts are present
say("Cannot perform automix if inserts are present. Skipping."), return
if grep{$tn{$_}->prefader_insert || $tn{$_}->postfader_insert} @tracks;
#use Smart::Comments '###';
# add -ev to summed signal
my $ev = add_effect( { chain => $tn{Master}->n, type => 'ev' } );
### ev id: $ev
# turn off audio output
$tn{Master}->set(rw => 'OFF');
### Status before mixdown:
command_process('show');
### reduce track volume levels to 10%
## accommodate ea and eadb volume controls
my $vol_operator = $cops{$tn{$tracks[0]}->vol}{type};
my $reduce_vol_command = $vol_operator eq 'ea' ? 'vol / 10' : 'vol - 10';
my $restore_vol_command = $vol_operator eq 'ea' ? 'vol * 10' : 'vol + 10';
### reduce vol command: $reduce_vol_command
for (@tracks){ command_process("$_ $reduce_vol_command") }
command_process('show');
generate_setup('automix') # pass a bit of magic
or say("automix: generate_setup failed!"), return;
connect_transport();
# start_transport() does a rec_cleanup() on transport stop
eval_iam('start'); # don't use heartbeat
sleep 2; # time for engine to stabilize
while( eval_iam('engine-status') ne 'finished'){
print q(.); sleep 1; update_clock_display()};
print " Done\n";
# parse cop status
my $cs = eval_iam('cop-status');
### cs: $cs
my $cs_re = qr/Chain "1".+?result-max-multiplier ([\.\d]+)/s;
my ($multiplier) = $cs =~ /$cs_re/;
### multiplier: $multiplier
remove_effect($ev);
# deal with all silence case, where multiplier is 0.00000
if ( $multiplier < 0.00001 ){
say "Signal appears to be silence. Skipping.";
for (@tracks){ command_process("$_ $restore_vol_command") }
$tn{Master}->set(rw => 'MON');
return;
}
### apply multiplier to individual tracks
for (@tracks){ command_process( "$_ vol*$multiplier" ) }
### mixdown
command_process('mixdown; arm; start');
### turn on audio output
# command_process('mixplay'); # rec_cleanup does this automatically
#no Smart::Comments;
}
1;
__END__
|