~ubuntu-branches/ubuntu/hardy/ecasound2.2/hardy

« back to all changes in this revision

Viewing changes to libecasound/audiofx_envelope_modulation.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Junichi Uekawa
  • Date: 2005-04-14 09:15:48 UTC
  • Revision ID: james.westby@ubuntu.com-20050414091548-o7kgb47z0tcunh0s
Tags: upstream-2.4.1
ImportĀ upstreamĀ versionĀ 2.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ------------------------------------------------------------------------
 
2
// audiofx_envelope-modulation.cpp: Effects which modify/modulate signal
 
3
//                                  envelope
 
4
// Copyright (C) 2000 Rob Coker 
 
5
//
 
6
// This program is free software; you can redistribute it and/or modify
 
7
// it under the terms of the GNU General Public License as published by
 
8
// the Free Software Foundation; either version 2 of the License, or
 
9
// (at your option) any later version.
 
10
// 
 
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
 
14
// GNU General Public License for more details.
 
15
// 
 
16
// You should have received a copy of the GNU General Public License
 
17
// along with this program; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
19
//
 
20
// ------------------------------------------------------------------------
 
21
// 
 
22
// History: 
 
23
//
 
24
// 2003-08-21 Kai Vehmanen
 
25
//     - Fixed a timing resolution bug in the envelope modulation code.
 
26
//       This involved changing from float to fixed point presentation for 
 
27
//       the position counters to avoid loss of precision (led to unexpected
 
28
//       drift in pulse timing).
 
29
// 2000-11-01 Rob Coker
 
30
//     - Initial version.
 
31
//
 
32
// ------------------------------------------------------------------------
 
33
 
 
34
#include <cmath>
 
35
 
 
36
#include <kvu_message_item.h>
 
37
 
 
38
#include "samplebuffer_iterators.h"
 
39
#include "audiofx_envelope_modulation.h"
 
40
 
 
41
#include "eca-logger.h"
 
42
#include "eca-error.h"
 
43
 
 
44
EFFECT_ENV_MOD::~EFFECT_ENV_MOD(void)
 
45
{
 
46
}
 
47
 
 
48
EFFECT_PULSE_GATE::EFFECT_PULSE_GATE (parameter_t freq_Hz,
 
49
                                      parameter_t onTime_percent)
 
50
{
 
51
  set_parameter(1, freq_Hz);
 
52
  set_parameter(2, onTime_percent);
 
53
 
 
54
  freq_rep = 1.0;
 
55
  on_time_rep = 0;
 
56
  current_rep = 0;
 
57
  period_rep = 0;
 
58
  on_from_rep = 0;
 
59
}
 
60
 
 
61
EFFECT_PULSE_GATE::~EFFECT_PULSE_GATE(void)
 
62
{
 
63
}
 
64
 
 
65
void EFFECT_PULSE_GATE::set_samples_per_second(SAMPLE_SPECS::sample_rate_t v)
 
66
{
 
67
  /* NOP, see audiofx.cpp:set_samples_per_second(); */
 
68
  EFFECT_ENV_MOD::set_samples_per_second(v);
 
69
}
 
70
 
 
71
void EFFECT_PULSE_GATE::set_parameter(int param, parameter_t value)
 
72
{
 
73
  switch (param) {
 
74
  case 1: 
 
75
    if (value > 0)
 
76
      {
 
77
        freq_rep = value;
 
78
        period_rep = static_cast<long int>(1.0f / freq_rep * samples_per_second() + 0.5f); // samples
 
79
      }
 
80
    else
 
81
      {
 
82
        MESSAGE_ITEM otemp;
 
83
        otemp << "(audiofx_envelope_modulation) WARNING! Frequency must be greater than 0! ";
 
84
        ECA_LOG_MSG(ECA_LOGGER::user_objects, otemp.to_string());
 
85
      }
 
86
    break;
 
87
 
 
88
  case 2:
 
89
    if ((value > 0) && (value < 100))
 
90
      {
 
91
        on_time_rep = value;
 
92
        on_from_rep = static_cast<long int>((on_time_rep / 100.0) * period_rep + 0.5f);
 
93
      }
 
94
    else
 
95
      {
 
96
        MESSAGE_ITEM otemp;
 
97
        otemp << "(audiofx_envelope_modulation) WARNING! on time must be between 0 and 100 inclusive! ";
 
98
        ECA_LOG_MSG(ECA_LOGGER::user_objects, otemp.to_string());
 
99
      }
 
100
    break;
 
101
  }
 
102
}
 
103
 
 
104
CHAIN_OPERATOR::parameter_t EFFECT_PULSE_GATE::get_parameter(int param) const
 
105
{
 
106
  switch (param) {
 
107
  case 1: 
 
108
    return(freq_rep);
 
109
 
 
110
  case 2:
 
111
    return (on_time_rep);
 
112
  }
 
113
  return(0.0);
 
114
}
 
115
 
 
116
void EFFECT_PULSE_GATE::init(SAMPLE_BUFFER* sbuf)
 
117
 
118
  i.init(sbuf); 
 
119
  set_channels(sbuf->number_of_channels());
 
120
  EFFECT_ENV_MOD::init(sbuf);
 
121
}
 
122
 
 
123
void EFFECT_PULSE_GATE::process(void)
 
124
{
 
125
  i.begin(); // iterate through all samples, one sample-frame at a
 
126
             // time (interleaved)
 
127
  while(!i.end()) {
 
128
    ++current_rep;
 
129
    if (current_rep >= period_rep) {
 
130
        current_rep = 0;
 
131
    }
 
132
    if (current_rep > on_from_rep) {
 
133
      for(int n = 0; n < channels(); n++) {
 
134
        *i.current(n) = 0.0;
 
135
      }
 
136
    }
 
137
    i.next();
 
138
  }
 
139
}
 
140
 
 
141
EFFECT_PULSE_GATE_BPM::EFFECT_PULSE_GATE_BPM (parameter_t bpm,
 
142
                                              parameter_t ontime_percent)
 
143
{
 
144
  set_parameter(1, bpm);
 
145
  set_parameter(2, ontime_percent);
 
146
}
 
147
 
 
148
EFFECT_PULSE_GATE_BPM::~EFFECT_PULSE_GATE_BPM(void)
 
149
{
 
150
}
 
151
 
 
152
void EFFECT_PULSE_GATE_BPM::set_parameter(int param, parameter_t value)
 
153
{
 
154
  switch (param) {
 
155
  case 1: 
 
156
    pulsegate_rep.set_parameter(1, value / 60.0f);
 
157
    break;
 
158
  case 2:
 
159
    pulsegate_rep.set_parameter(2, value);
 
160
    break;
 
161
  }
 
162
}
 
163
 
 
164
CHAIN_OPERATOR::parameter_t EFFECT_PULSE_GATE_BPM::get_parameter(int param) const {
 
165
  switch (param) {
 
166
  case 1: 
 
167
    return (pulsegate_rep.get_parameter(1) * 60.0f);
 
168
    break;
 
169
  case 2:
 
170
    return (pulsegate_rep.get_parameter(2));
 
171
    break;
 
172
  }
 
173
  return(0.0);
 
174
}
 
175
 
 
176
void EFFECT_PULSE_GATE_BPM::init(SAMPLE_BUFFER* sbuf)
 
177
 
178
  pulsegate_rep.init(sbuf); 
 
179
  EFFECT_ENV_MOD::init(sbuf);
 
180
}
 
181
 
 
182
void EFFECT_PULSE_GATE_BPM::process(void) { pulsegate_rep.process(); }
 
183
 
 
184
void EFFECT_PULSE_GATE_BPM::set_samples_per_second(SAMPLE_SPECS::sample_rate_t v)
 
185
{
 
186
  pulsegate_rep.set_samples_per_second(v);
 
187
  EFFECT_ENV_MOD::set_samples_per_second(v);
 
188
}
 
189
 
 
190
EFFECT_TREMOLO::EFFECT_TREMOLO (parameter_t freq_bpm,
 
191
                                parameter_t depth_percent)
 
192
{
 
193
  set_parameter(1, freq_bpm);
 
194
  set_parameter(2, depth_percent);
 
195
  currentTime = 0.0;
 
196
}
 
197
 
 
198
EFFECT_TREMOLO::~EFFECT_TREMOLO(void)
 
199
{
 
200
}
 
201
 
 
202
void EFFECT_TREMOLO::set_parameter(int param, parameter_t value)
 
203
{
 
204
  switch (param) {
 
205
  case 1:
 
206
    if (value > 0)
 
207
      {
 
208
        freq = value/(2*60); // convert from bpm to Hz
 
209
      }
 
210
    else
 
211
      {
 
212
        MESSAGE_ITEM otemp;
 
213
        otemp << "(audiofx_envelope_modulation) WARNING! bpm must be greater than 0! ";
 
214
        ECA_LOG_MSG(ECA_LOGGER::info, otemp.to_string());
 
215
      }
 
216
    break;
 
217
  case 2:
 
218
        depth = (value/100.0); // from percent to fraction
 
219
    break;
 
220
  }
 
221
}
 
222
 
 
223
CHAIN_OPERATOR::parameter_t EFFECT_TREMOLO::get_parameter(int param) const
 
224
{
 
225
  switch (param) {
 
226
  case 1:
 
227
    return freq*120;
 
228
    break;
 
229
  case 2:
 
230
    return depth*100.0;
 
231
    break;
 
232
  }
 
233
  return(0.0);
 
234
}
 
235
 
 
236
void EFFECT_TREMOLO::init(SAMPLE_BUFFER* sbuf)
 
237
{
 
238
  i.init(sbuf);
 
239
  set_channels(sbuf->number_of_channels());
 
240
  incrTime = 1.0/samples_per_second();
 
241
  EFFECT_ENV_MOD::init(sbuf);
 
242
}
 
243
 
 
244
void EFFECT_TREMOLO::process(void)
 
245
{
 
246
  i.begin(); // iterate through all samples, one sample-frame at a
 
247
             // time (interleaved)
 
248
  while(!i.end())
 
249
  {
 
250
    currentTime += incrTime;
 
251
    double envelope = (1-depth)+depth*fabs(sin(2*3.1416*currentTime*freq));
 
252
    if (envelope < 0)
 
253
    {
 
254
       envelope = 0;
 
255
    }
 
256
        for(int n = 0; n < channels(); n++)
 
257
        {
 
258
            *i.current(n) *= envelope;
 
259
    }
 
260
    i.next();
 
261
  }
 
262
}