Cleanup
[calf.git] / src / modmatrix.cpp
blobae71cd843b9f24eb3b3a623591ad107187dea102
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 <calf/modmatrix.h>
22 #include <calf/utils.h>
23 #include <memory.h>
24 #include <sstream>
26 using namespace std;
27 using namespace dsp;
28 using namespace calf_plugins;
29 using namespace calf_utils;
31 mod_matrix_impl::mod_matrix_impl(dsp::modulation_entry *_matrix, mod_matrix_metadata *_metadata)
32 : matrix(_matrix)
33 , metadata(_metadata)
35 matrix_rows = metadata->get_table_rows();
36 for (unsigned int i = 0; i < matrix_rows; i++)
37 matrix[i].reset();
40 const float mod_matrix_impl::scaling_coeffs[mod_matrix_metadata::map_type_count][3] = {
41 { 0, 1, 0 },
42 { -1, 2, 0 },
43 { -1, 1, 0 },
44 { 0, 0, 1 },
45 { -1, 0, 1 },
46 { 0, 2, -1 },
47 { -1, 4, -2 },
48 { 0, 4, -4 },
51 std::string mod_matrix_impl::get_cell(int row, int column) const
53 assert(row >= 0 && row < (int)matrix_rows);
54 modulation_entry &slot = matrix[row];
55 const char **arr = metadata->get_table_columns()[column].values;
56 switch(column) {
57 case 0: // source 1
58 return arr[slot.src1];
59 case 1: // mapping mode
60 return arr[slot.mapping];
61 case 2: // source 2
62 return arr[slot.src2];
63 case 3: // amount
64 return calf_utils::f2s(slot.amount);
65 case 4: // destination
66 return arr[slot.dest];
67 default:
68 assert(0);
69 return "";
73 void mod_matrix_impl::set_cell(int row, int column, const std::string &src, std::string &error)
75 assert(row >= 0 && row < (int)matrix_rows);
76 modulation_entry &slot = matrix[row];
77 const char **arr = metadata->get_table_columns()[column].values;
78 switch(column) {
79 case 0:
80 case 1:
81 case 2:
82 case 4:
84 for (int i = 0; arr[i]; i++)
86 if (src == arr[i])
88 if (column == 0)
89 slot.src1 = i;
90 else if (column == 1)
91 slot.mapping = (mod_matrix_metadata::mapping_mode)i;
92 else if (column == 2)
93 slot.src2 = i;
94 else if (column == 4)
95 slot.dest = i;
96 error.clear();
97 return;
100 error = "Invalid name: " + src;
101 return;
103 case 3:
105 stringstream ss(src);
106 ss >> slot.amount;
107 error.clear();
108 return;
113 void mod_matrix_impl::send_configures(send_configure_iface *sci)
115 for (int i = 0; i < (int)matrix_rows; i++)
117 for (int j = 0; j < 5; j++)
119 string key = "mod_matrix:" + i2s(i) + "," + i2s(j);
120 sci->send_configure(key.c_str(), get_cell(i, j).c_str());
125 char *mod_matrix_impl::configure(const char *key, const char *value)
127 bool is_rows;
128 int row, column;
129 if (!parse_table_key(key, "mod_matrix:", is_rows, row, column))
130 return NULL;
131 if (is_rows)
132 return strdup("Unexpected key");
134 if (row != -1 && column != -1)
136 string error;
137 string value_text;
138 if (value == NULL)
140 const modulation_entry *src = get_default_mod_matrix_value(row);
141 if (src)
143 modulation_entry &slot = matrix[row];
144 switch(column)
146 case 0: slot.src1 = src->src1; break;
147 case 1: slot.mapping = src->mapping; break;
148 case 2: slot.src2 = src->src2; break;
149 case 3: slot.amount = src->amount; break;
150 case 4: slot.dest = src->dest; break;
152 return NULL;
154 const table_column_info &ci = metadata->get_table_columns()[column];
155 if (ci.type == TCT_ENUM)
156 value_text = ci.values[(int)ci.def_value];
157 else
158 if (ci.type == TCT_FLOAT)
159 value_text = f2s(ci.def_value);
160 value = value_text.c_str();
162 set_cell(row, column, value, error);
163 if (!error.empty())
164 return strdup(error.c_str());
166 return NULL;