+ DSP primitives: fix a rather stupid bug in clamping functions
[calf.git] / src / modmatrix.cpp
blobcba6ced8b7bbe9ee6d5243fc3e4dd90197083099
1 /* Calf DSP Library
2 * Modulation matrix boilerplate code.
4 * Copyright (C) 2001-2007 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 #include <assert.h>
22 #include <sstream>
23 #include <calf/modmatrix.h>
25 using namespace std;
26 using namespace dsp;
27 using namespace calf_plugins;
29 const char *mod_mapping_names[] = { "0..1", "-1..1", "-1..0", "x^2", "2x^2-1", "ASqr", "ASqrBip", "Para", NULL };
31 const float mod_matrix::scaling_coeffs[dsp::map_type_count][3] = {
32 { 0, 1, 0 },
33 { -1, 2, 0 },
34 { -1, 1, 0 },
35 { 0, 0, 1 },
36 { -1, 0, 1 },
37 { 0, 2, -1 },
38 { -1, 4, -2 },
39 { 0, 4, -4 },
42 mod_matrix::mod_matrix(modulation_entry *_matrix, unsigned int _rows, const char **_src_names, const char **_dest_names)
43 : matrix(_matrix)
44 , matrix_rows(_rows)
45 , mod_src_names(_src_names)
46 , mod_dest_names(_dest_names)
48 table_column_info tci[6] = {
49 { "Source", TCT_ENUM, 0, 0, 0, mod_src_names },
50 { "Mapping", TCT_ENUM, 0, 0, 0, mod_mapping_names },
51 { "Modulator", TCT_ENUM, 0, 0, 0, mod_src_names },
52 { "Amount", TCT_FLOAT, 0, 1, 1, NULL},
53 { "Destination", TCT_ENUM, 0, 0, 0, mod_dest_names },
54 { NULL }
56 assert(sizeof(table_columns) == sizeof(tci));
57 memcpy(table_columns, tci, sizeof(table_columns));
58 for (unsigned int i = 0; i < matrix_rows; i++)
59 matrix[i].reset();
62 const table_column_info *mod_matrix::get_table_columns(int param)
64 return table_columns;
67 uint32_t mod_matrix::get_table_rows(int param)
69 return matrix_rows;
72 std::string mod_matrix::get_cell(int param, int row, int column)
74 assert(row >= 0 && row < (int)matrix_rows);
75 modulation_entry &slot = matrix[row];
76 switch(column) {
77 case 0: // source 1
78 return mod_src_names[slot.src1];
79 case 1: // mapping mode
80 return mod_mapping_names[slot.mapping];
81 case 2: // source 2
82 return mod_src_names[slot.src2];
83 case 3: // amount
84 return calf_utils::f2s(slot.amount);
85 case 4: // destination
86 return mod_dest_names[slot.dest];
87 default:
88 assert(0);
89 return "";
93 void mod_matrix::set_cell(int param, int row, int column, const std::string &src, std::string &error)
95 assert(row >= 0 && row < (int)matrix_rows);
96 modulation_entry &slot = matrix[row];
97 const char **arr = mod_src_names;
98 if (column == 1)
99 arr = mod_mapping_names;
100 if (column == 4)
101 arr = mod_dest_names;
102 switch(column) {
103 case 0:
104 case 1:
105 case 2:
106 case 4:
108 for (int i = 0; arr[i]; i++)
110 if (src == arr[i])
112 if (column == 0)
113 slot.src1 = i;
114 else if (column == 1)
115 slot.mapping = (mapping_mode)i;
116 else if (column == 2)
117 slot.src2 = i;
118 else if (column == 4)
119 slot.dest = i;
120 error.clear();
121 return;
124 error = "Invalid name: " + src;
125 return;
127 case 3:
129 stringstream ss(src);
130 ss >> slot.amount;
131 error.clear();
132 return;