+ Mod Matrix: add mapping type (max. 2nd order polynomials), normalize sources to...
[calf.git] / src / calf / modmatrix.h
blob96f7d16447a8e276a013868adabd9679c2558962
1 /* Calf DSP Library
2 * Modulation matrix boilerplate code.
4 * Copyright (C) 2009 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 02111-1307, USA.
21 #ifndef __CALF_MODMATRIX_H
22 #define __CALF_MODMATRIX_H
24 #include "giface.h"
26 namespace dsp {
28 struct modulation_entry
30 int src1, src2;
31 int mapping;
32 float amount;
33 int dest;
38 namespace calf_plugins {
40 class mod_matrix: public table_edit_iface
42 protected:
43 enum modulation_mode { mod_positive, mod_bipolar, mod_negative, mod_squared, mod_squared_bipolar, mod_antisquared, mod_antisquared_bipolar, mod_parabola, mod_type_count };
44 /// Polynomials for different scaling modes (1, x, x^2)
45 static const float scaling_coeffs[mod_type_count][3];
46 /// Column descriptions for table widget
47 table_column_info table_columns[6];
49 dsp::modulation_entry *matrix;
50 unsigned int matrix_rows;
51 const char **mod_src_names, **mod_dest_names;
53 mod_matrix(dsp::modulation_entry *_matrix, unsigned int _rows, const char **_src_names, const char **_dest_names);
54 public:
55 virtual const table_column_info *get_table_columns(int param);
56 virtual uint32_t get_table_rows(int param);
57 virtual std::string get_cell(int param, int row, int column);
58 virtual void set_cell(int param, int row, int column, const std::string &src, std::string &error);
60 /// Process modulation matrix, calculate outputs from inputs
61 inline void calculate_modmatrix(float *moddest, int moddest_count, float *modsrc)
63 for (int i = 0; i < moddest_count; i++)
64 moddest[i] = 0;
65 for (unsigned int i = 0; i < matrix_rows; i++)
67 dsp::modulation_entry &slot = matrix[i];
68 if (slot.dest) {
69 float value = modsrc[slot.src1] * modsrc[slot.src2];
70 value = dsp::clip<float>(value, 0, 1);
71 const float *c = scaling_coeffs[slot.mapping];
72 value = c[0] + c[1] * value + c[2] * value * value;
73 moddest[slot.dest] += value * slot.amount;
81 #endif