File: generic-controller.cpp

package info (click to toggle)
ecasound 2.9.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,292 kB
  • sloc: cpp: 39,475; sh: 4,335; lisp: 1,918; ansic: 1,883; makefile: 888; python: 617; ruby: 202
file content (158 lines) | stat: -rw-r--r-- 4,083 bytes parent folder | download | duplicates (6)
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
// ------------------------------------------------------------------------
// generic_controller.cpp: General sources for control signals
// Copyright (C) 1999-2002,2005,2008 Kai Vehmanen
//
// Attributes:
//     eca-style-version: 3
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 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 General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
// ------------------------------------------------------------------------

#include <cmath>
#include <iostream>
#include <string>

#include <kvu_numtostr.h>
#include <kvu_dbc.h>

#include "generic-controller.h"
#include "eca-chainop.h"

#include "eca-logger.h"

/* Debug controller source values */ 
// #define DEBUG_CONTROLLERS 1

#ifdef DEBUG_CONTROLLERS
#define DEBUG_CTRL_STATEMENT(x) x
#else
#define DEBUG_CTRL_STATEMENT(x) ((void)0)
#endif

GENERIC_CONTROLLER::GENERIC_CONTROLLER(CONTROLLER_SOURCE* src, OPERATOR* dobj, int par_id, double range_low, double range_high)
{
  source = src;
  target = dobj;
  init_called_rep = false;
  param_id_rep = par_id;
  rangelow_rep = range_low;
  rangehigh_rep = range_high;
  last_value_pos_rep = -1;
}

void GENERIC_CONTROLLER::init(void)
{ 
  source->init();

  double init_value = target->get_parameter(param_id_rep);
  init_value = (init_value - rangelow_rep) / (rangehigh_rep - rangelow_rep);
  source->set_initial_value(init_value);

  DEBUG_CTRL_STATEMENT(std::cerr << "generic-controller: init type '"
		       << source->name() << "', init_value " 
		       << init_value << "." << std::endl);

  init_called_rep = true;
  last_value_pos_rep = -1;
}

CONTROLLER_SOURCE::parameter_t GENERIC_CONTROLLER::value(double pos)
{
  // --------
  DBC_REQUIRE(is_valid() == true);
  // --------

  double new_value = rangelow_rep +
    (source->value(pos) * (rangehigh_rep - rangelow_rep));

  DEBUG_CTRL_STATEMENT(std::cerr << "generic-controller: type '"
		       << source->name() << "', pos_sec " << pos 
		       << ", source_value " << source->value(pos) 
		       << ", scaled_value " << new_value << "." << std::endl);

  target->set_parameter(param_id_rep, new_value);

  last_value_pos_rep = pos;

  return new_value;
}

string GENERIC_CONTROLLER::status(void) const
{
  if (is_valid() == true) {
    double value = -1.0f;

    if (last_value_pos_rep > 0) {
      value = source->value(last_value_pos_rep);
    }

    return "Source \"" + source->name() + 
	   "\" connected to target \"" +
	   target->name() + "\"." +
	   " Current source value is " + 
	   kvu_numtostr(value) + 
	   " and target " + 
	   kvu_numtostr(target->get_parameter(param_id_rep)) + ".";
  }
  else {
    return "Controller not valid.";
  }
}

void GENERIC_CONTROLLER::assign_target(OPERATOR* obj)
{ 
  target  = obj; 
}

void GENERIC_CONTROLLER::assign_source(CONTROLLER_SOURCE* obj)
{ 
  if (init_called_rep == true &&
      source != obj)
    init();

  source = obj;
}

void GENERIC_CONTROLLER::set_parameter(int param, CHAIN_OPERATOR::parameter_t v)
{
  switch (param) {
  case 1: 
    param_id_rep = static_cast<int>(v);
    break;
  case 2: 
    rangelow_rep = v;
    break;
  case 3: 
    rangehigh_rep = v;
    break;
  default:
    source->set_parameter(param - 3, v);
  }
}

CHAIN_OPERATOR::parameter_t GENERIC_CONTROLLER::get_parameter(int param) const
{
  switch (param) {
  case 1: 
    return static_cast<parameter_t>(param_id_rep);
  case 2: 
    return rangelow_rep;
  case 3: 
    return rangehigh_rep;
  default:
    return source->get_parameter(param - 3);
  }
  return 0.0;
}