File: osc-gen.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 (271 lines) | stat: -rw-r--r-- 7,174 bytes parent folder | download | duplicates (5)
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
// ------------------------------------------------------------------------
// osc-gen.cpp: Generic oscillator
// Copyright (C) 1999-2002,2008,2012 Kai Vehmanen
//
// Attributes:
//     eca-style-version: 3
//
// This program is fre 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 <vector>
#include <string>

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

#include "eca-object-factory.h"
#include "osc-gen.h"
#include "oscillator.h"
#include "eca-logger.h"

/* For hunting bugs in envelope code */
//#define DEBUG_ENVELOPE_POINTS 

#ifdef DEBUG_ENVELOPE_POINTS
#define _DEBUG_ENVELOPE(x) do { x; } while(0)
#include <iostream>
#else
#define _DEBUG_ENVELOPE(x)
#endif

size_t GENERIC_OSCILLATOR::current_stage(double pos)
{
  size_t start, n;

  // note: like 'std::fmod(pos, loop_length_rep)' but faster
  double loop_pos = pos - static_cast<int>(pos / loop_length_rep) * loop_length_rep;
    
  // note: optimize for the case where position changes
  //       linearly
  start = last_stage_rep;

  for(n = start;;) {

    double pos_scaled = loop_pos / loop_length_rep;
    double p1 = envtable_rep[n].pos;
    double p2 = envtable_rep[n + 1].pos;

    _DEBUG_ENVELOPE(std::cout << 
                    "looking for stage " + kvu_numtostr(n) +
                    ", stages " + kvu_numtostr(envtable_rep.size()) +
                    ", pos " + kvu_numtostr(pos) +
                    ", looppos " + kvu_numtostr(loop_pos) +
                    ", scaled " + kvu_numtostr(pos_scaled) +
                    ", p1 " + kvu_numtostr(p1) +
                    ", p2 " + kvu_numtostr(p2) << "\n");

    if (pos_scaled >= p1 && pos_scaled < p2) {
      _DEBUG_ENVELOPE(std::cout << 
                      "found stage " + kvu_numtostr(n) << "\n");
      last_pos_scaled_rep = pos_scaled;
      break;
    }

    n++;
    if (n + 1 == envtable_rep.size())
      n = 0;

    if (n == start) {
      static bool once = true;
      if (once) {
        ECA_LOG_MSG(ECA_LOGGER::info,
                    "ERROR: invalid envelop");
        once = false;
      }
      break;
    }
  }

  _DEBUG_ENVELOPE(if (last_stage_rep != n) { \
      std::cout << "stage change from " \
                << kvu_numtostr(last_stage_rep)  \
                <<" to " << kvu_numtostr(n) << "\n"; } );
  
  last_stage_rep = n;
  
  return n;
}

CONTROLLER_SOURCE::parameter_t GENERIC_OSCILLATOR::value(double pos)
{
  size_t stage = current_stage(pos);
  double retval = 0.0f;

  /* case: hold/step */
  if (mode_rep == 0) {
    retval = envtable_rep[stage].val;
    _DEBUG_ENVELOPE(std::cout
                    << "hold value " << retval
                    << ", stage " << stage << "\n");

  }
  /* case: linear interpolation */
  else {
    double p1 = envtable_rep[stage].pos;
    double p2 = envtable_rep[stage + 1].pos;
    double v1 = envtable_rep[stage].val;
    double v2 = envtable_rep[stage + 1].val;
    double mult = 1.0 - ((p2 - last_pos_scaled_rep) / (p2 - p1));
    retval = v1 + (mult * (v2 - v1));
    _DEBUG_ENVELOPE(std::cout
                    << "linear value " << retval
                    << ", stage " << stage 
                    << ", v1 " << v1 << ", v2 " << v2 << ", scaledpos " << last_pos_scaled_rep
                    << "\n");
  }

  return retval;
}

GENERIC_OSCILLATOR::GENERIC_OSCILLATOR(double freq, int mode)
  : OSCILLATOR(freq, 0.0)
{
  last_stage_rep = 0;

  set_param_count(0);

  set_parameter(1, get_parameter(1));
  set_parameter(2, mode);
}

void GENERIC_OSCILLATOR::init(void)
{
  ECA_LOG_MSG(ECA_LOGGER::user_objects,
              "Generic oscillator init with params: "
              + ECA_OBJECT_FACTORY::operator_parameters_to_eos(this));
  if (envtable_rep.size() < 2)
    envtable_rep.resize(2);
}

GENERIC_OSCILLATOR::~GENERIC_OSCILLATOR (void)
{
}

void GENERIC_OSCILLATOR::set_param_count(int params)
{
  param_names_rep = "freq,mode,pcount,start_val,end_val";
  if (params > 0) {
    for(int n = 0; n < params; n++) {
      std::string num = kvu_numtostr(n + 1);
      param_names_rep += ",pos";
      param_names_rep += num;
      param_names_rep += ",val";
      param_names_rep += num;
    }
  }
}

std::string GENERIC_OSCILLATOR::parameter_names(void) const
{
  return param_names_rep;
}

void GENERIC_OSCILLATOR::prepare_envelope(void)
{
  size_t len = 2 + (params_rep.size() + 1) / 2;
  envtable_rep.resize(len);

  envtable_rep[0].pos = 0.0;
  envtable_rep[0].val = first_value_rep;
  size_t n = 0;
  size_t p1_offset = 1;
  size_t p_end_offset = params_rep.size() / 2 + p1_offset;
  for(; n < params_rep.size(); n++) {
    envtable_rep[n / 2 + p1_offset].pos = params_rep[n];
    if (++n == params_rep.size())
      break;
    envtable_rep[n / 2 + p1_offset].val = params_rep[n];
  } 
  DBC_CHECK(p_end_offset < envtable_rep.size());
  envtable_rep[p_end_offset].pos = 1.0;
  envtable_rep[p_end_offset].val = last_value_rep;

}

void GENERIC_OSCILLATOR::set_parameter(int param, CONTROLLER_SOURCE::parameter_t value)
{
  ECA_LOG_MSG(ECA_LOGGER::user_objects,
              "setting param " + kvu_numtostr(param) 
              + " (" + get_parameter_name(param) + ")"
              + " => " + kvu_numtostr(value));

  switch (param) {
  case 1: 
    frequency(value);
    loop_length_rep = 1.0f / frequency(); // length of one wave in seconds
    break;

  case 2: 
    mode_rep = static_cast<int>(value);
    break;

  case 3: 
    set_param_count(static_cast<int>(value));
    break;

  case 4:
    first_value_rep = value;
    break;

  case 5:
    last_value_rep = value;
    break;

  default: {
      int pointnum = param - 5;
      if (pointnum > 0) {

        if (pointnum > static_cast<int>(params_rep.size()))
          params_rep.resize(pointnum);
        
        params_rep[pointnum - 1] = value;
      }

      prepare_envelope();

      break;
    }
  }
}

CONTROLLER_SOURCE::parameter_t GENERIC_OSCILLATOR::get_parameter(int param) const
{ 
  switch (param) {
  case 1: 
    return frequency();

  case 2:
    return static_cast<parameter_t>(mode_rep);

  case 3:
    return static_cast<parameter_t>((number_of_params() - 5) / 2);

  case 4:
    return static_cast<parameter_t>(first_value_rep);

  case 5:
    return static_cast<parameter_t>(last_value_rep);

  default:
    int pointnum = param - 5;
    if (pointnum > 0) {
      if (pointnum <= static_cast<int>(params_rep.size())) {
        return static_cast<parameter_t>(params_rep[pointnum - 1]);
      }
    }
  }
  return 0.0;
}