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

« back to all changes in this revision

Viewing changes to libecasound/audiofx_misc.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_misc.cpp: Miscellanous effect processing routines.
 
3
// Copyright (C) 1999-2003,2005 Kai Vehmanen
 
4
//
 
5
// Attributes:
 
6
//     eca-style-version: 3
 
7
//
 
8
// This program is free software; you can redistribute it and/or modify
 
9
// it under the terms of the GNU General Public License as published by
 
10
// the Free Software Foundation; either version 2 of the License, or
 
11
// (at your option) any later version.
 
12
// 
 
13
// This program is distributed in the hope that it will be useful,
 
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
// GNU General Public License for more details.
 
17
// 
 
18
// You should have received a copy of the GNU General Public License
 
19
// along with this program; if not, write to the Free Software
 
20
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
21
// ------------------------------------------------------------------------
 
22
 
 
23
#include <iostream>
 
24
#include <cmath>
 
25
 
 
26
#include <kvu_dbc.h>
 
27
#include <kvu_numtostr.h>
 
28
#include <kvu_utils.h>
 
29
 
 
30
#include "samplebuffer_iterators.h"
 
31
#include "eca-operator.h"
 
32
#include "audiofx_misc.h"
 
33
#include "eca-logger.h"
 
34
#include "eca-error.h"
 
35
 
 
36
EFFECT_DCFIX::EFFECT_DCFIX (void)
 
37
{
 
38
}
 
39
 
 
40
EFFECT_DCFIX::EFFECT_DCFIX (const EFFECT_DCFIX& x)
 
41
{
 
42
  deltafixes_rep = x.deltafixes_rep;
 
43
  i_rep = x.i_rep;
 
44
}
 
45
 
 
46
void EFFECT_DCFIX::set_parameter(int param, CHAIN_OPERATOR::parameter_t value)
 
47
{
 
48
  if (param == 1) {
 
49
    deltafixes_rep.resize(static_cast<int>(value));
 
50
  }
 
51
  else if (param > 1 && param - 2 < static_cast<int>(deltafixes_rep.size())) {
 
52
    deltafixes_rep[param - 2] = value;
 
53
  }
 
54
}
 
55
 
 
56
CHAIN_OPERATOR::parameter_t EFFECT_DCFIX::get_parameter(int param) const
 
57
{
 
58
  parameter_t result = 0.0f;
 
59
 
 
60
  if (param == 1) {
 
61
    result = static_cast<parameter_t>(deltafixes_rep.size());
 
62
  }
 
63
  else if (param > 1 && param - 2 < static_cast<int>(deltafixes_rep.size())) {
 
64
    result = deltafixes_rep[param - 2];
 
65
  }
 
66
 
 
67
  return result;
 
68
}
 
69
 
 
70
string EFFECT_DCFIX::parameter_names(void) const
 
71
{
 
72
  std::vector<std::string> t;
 
73
 
 
74
  t.push_back("channel-count");
 
75
 
 
76
  for(int n = 0; n < static_cast<int>(deltafixes_rep.size()); n++) {
 
77
    t.push_back("delta-ch" + kvu_numtostr(n + 1));
 
78
  }
 
79
 
 
80
  return kvu_vector_to_string(t, ",");
 
81
}
 
82
 
 
83
void EFFECT_DCFIX::parameter_description(int param, struct PARAM_DESCRIPTION *pd) const
 
84
{
 
85
  OPERATOR::parameter_description(param, pd);
 
86
}
 
87
 
 
88
void EFFECT_DCFIX::init(SAMPLE_BUFFER *insample)
 
89
{
 
90
  i_rep.init(insample);
 
91
  set_channels(insample->number_of_channels());
 
92
  if (channels() > static_cast<int>(deltafixes_rep.size())) {
 
93
    deltafixes_rep.resize(channels());
 
94
  }
 
95
}
 
96
 
 
97
void EFFECT_DCFIX::process(void)
 
98
{
 
99
  for(int n = 0; n < channels(); n++) {
 
100
    i_rep.begin(n);
 
101
    while(!i_rep.end()) {
 
102
      *i_rep.current() = *i_rep.current() + deltafixes_rep[n];
 
103
      i_rep.next();
 
104
    }
 
105
  }
 
106
}
 
107
 
 
108
const int EFFECT_PITCH_SHIFT::resample_low_limit = 8;
 
109
 
 
110
EFFECT_PITCH_SHIFT::EFFECT_PITCH_SHIFT (const EFFECT_PITCH_SHIFT& x)
 
111
{
 
112
  pmod_rep = x.pmod_rep;
 
113
  target_rate_rep = x.target_rate_rep;
 
114
  sbuf_repp = 0;
 
115
}
 
116
 
 
117
void EFFECT_PITCH_SHIFT::set_parameter(int param, CHAIN_OPERATOR::parameter_t value)
 
118
{
 
119
  switch (param) {
 
120
  case 1: 
 
121
    double lowlimit = 1.0f / EFFECT_PITCH_SHIFT::resample_low_limit * 100.0f;
 
122
 
 
123
    if (value <= lowlimit) {
 
124
      ECA_LOG_MSG(ECA_LOGGER::info, 
 
125
                  "WARNING! Shift-% must be greater than " +
 
126
                  kvu_numtostr(lowlimit) + 
 
127
                  "%! Limiting to the low-limit.");
 
128
      pmod_rep = lowlimit;
 
129
    }
 
130
    else {
 
131
      pmod_rep = value;
 
132
    }
 
133
    if (sbuf_repp != 0) {
 
134
      target_rate_rep = static_cast<long int>(samples_per_second() * 100.0 / pmod_rep);
 
135
    }
 
136
    else {
 
137
      target_rate_rep = 0;
 
138
    }
 
139
    break;
 
140
  }
 
141
}
 
142
 
 
143
CHAIN_OPERATOR::parameter_t EFFECT_PITCH_SHIFT::get_parameter(int param) const
 
144
{
 
145
  switch (param) {
 
146
  case 1: 
 
147
    return pmod_rep;
 
148
  }
 
149
  return 0.0;
 
150
}
 
151
 
 
152
void EFFECT_PITCH_SHIFT::parameter_description(int param, struct PARAM_DESCRIPTION *pd) const
 
153
{
 
154
  OPERATOR::parameter_description(param, pd);
 
155
 
 
156
  switch(param) 
 
157
    {
 
158
    case 1: 
 
159
      pd->default_value = 100.0f;
 
160
      pd->bounded_above = true;
 
161
      /* 100x speed-up is a sensible upper limit */
 
162
      pd->upper_bound = 10000.0f; 
 
163
      pd->bounded_below = true;
 
164
      /* ~8x slow-down is a sensible lower limit: */
 
165
      pd->lower_bound = 1.0f / EFFECT_PITCH_SHIFT::resample_low_limit * 100.0f; 
 
166
      break;
 
167
    }
 
168
}
 
169
 
 
170
void EFFECT_PITCH_SHIFT::init(SAMPLE_BUFFER *insample)
 
171
{
 
172
  sbuf_repp = insample;
 
173
  // truncate, not round, to integer
 
174
  target_rate_rep = static_cast<long int>((samples_per_second() * 100.0 / pmod_rep));
 
175
 
 
176
  long int lowlimit = sbuf_repp->length_in_samples() * EFFECT_PITCH_SHIFT::resample_low_limit; 
 
177
  sbuf_repp->reserve_length_in_samples(lowlimit);
 
178
  ECA_LOG_MSG(ECA_LOGGER::system_objects, 
 
179
              "Setting resampling lowlimit to " + 
 
180
              kvu_numtostr(lowlimit) + " bytes.");
 
181
 
 
182
  sbuf_repp->resample_init_memory(samples_per_second(), target_rate_rep);
 
183
  sbuf_repp->resample_set_quality(50);
 
184
  ECA_LOG_MSG(ECA_LOGGER::user_objects, "Resampling from " +
 
185
              kvu_numtostr(samples_per_second()) + 
 
186
              " to " + 
 
187
              kvu_numtostr(target_rate_rep) + "."); 
 
188
}
 
189
 
 
190
void EFFECT_PITCH_SHIFT::release(void)
 
191
{
 
192
  sbuf_repp = 0;
 
193
}
 
194
 
 
195
void EFFECT_PITCH_SHIFT::process(void)
 
196
{
 
197
  sbuf_repp->resample(samples_per_second(), target_rate_rep); 
 
198
}
 
199
 
 
200
long int EFFECT_PITCH_SHIFT::max_output_samples(long int i_samples) const
 
201
{
 
202
  DBC_CHECK(sbuf_repp != 0);
 
203
  return static_cast<long int>(static_cast<double>(target_rate_rep) /
 
204
                               samples_per_second() *
 
205
                               i_samples);
 
206
}
 
207
 
 
208
EFFECT_AUDIO_STAMP::EFFECT_AUDIO_STAMP(void) 
 
209
  : sbuf_repp(0) 
 
210
{
 
211
}
 
212
 
 
213
EFFECT_AUDIO_STAMP::EFFECT_AUDIO_STAMP(const EFFECT_AUDIO_STAMP& arg) 
 
214
  : sbuf_repp(0) 
 
215
{
 
216
}
 
217
 
 
218
void EFFECT_AUDIO_STAMP::set_parameter(int param, CHAIN_OPERATOR::parameter_t value)
 
219
{
 
220
  switch (param) {
 
221
  case 1: 
 
222
    set_id(static_cast<int>(value));
 
223
    break;
 
224
  }
 
225
}
 
226
 
 
227
CHAIN_OPERATOR::parameter_t EFFECT_AUDIO_STAMP::get_parameter(int param) const
 
228
{
 
229
  switch (param) {
 
230
  case 1: 
 
231
    return static_cast<parameter_t>(id());
 
232
  }
 
233
  return 0.0;
 
234
}
 
235
 
 
236
void EFFECT_AUDIO_STAMP::parameter_description(int param, struct PARAM_DESCRIPTION *pd) const
 
237
{
 
238
  OPERATOR::parameter_description(param, pd);
 
239
  switch(param) 
 
240
    {
 
241
    case 1: 
 
242
      pd->default_value = 1;
 
243
      pd->description = get_parameter_name(param);
 
244
      pd->bounded_above = false;
 
245
      pd->bounded_below = true;
 
246
      pd->lower_bound = 1;
 
247
      pd->integer = true;
 
248
      break;
 
249
    }
 
250
}
 
251
 
 
252
void EFFECT_AUDIO_STAMP::init(SAMPLE_BUFFER *insample)
 
253
{
 
254
  sbuf_repp = insample;
 
255
}
 
256
 
 
257
void EFFECT_AUDIO_STAMP::release(void)
 
258
{
 
259
  sbuf_repp = 0;
 
260
}
 
261
 
 
262
void EFFECT_AUDIO_STAMP::process(void)
 
263
{
 
264
  store(sbuf_repp);
 
265
  // std::cerr << "Storing audio stamp with id " << id() << "." << std::endl;
 
266
}