1
// ------------------------------------------------------------------------
2
// audiofx_analysis.cpp: Classes for signal analysis
3
// Copyright (C) 1999-2002,2008 Kai Vehmanen
6
// eca-style-version: 3 (see Ecasound Programmer's Guide)
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.
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.
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
// ------------------------------------------------------------------------
27
#include <kvu_message_item.h>
28
#include <kvu_numtostr.h>
30
#include "samplebuffer_iterators.h"
31
#include "audiofx_analysis.h"
32
#include "audiofx_amplitude.h"
34
#include "eca-logger.h"
35
#include "eca-error.h"
39
static string priv_align_right(const string txt, int width, char padchar)
42
int pad = width - static_cast<int>(txt.size());
44
res.resize(pad, padchar);
51
SAMPLE_SPECS::sample_t threshold;
54
#define BUCKET_ENTRY_DB(x) \
55
{ #x, EFFECT_AMPLITUDE::db_to_linear(x) }
57
static struct bucket bucket_table[] =
60
BUCKET_ENTRY_DB(-0.1),
70
EFFECT_ANALYSIS::~EFFECT_ANALYSIS(void)
75
EFFECT_VOLUME_BUCKETS::EFFECT_VOLUME_BUCKETS (void)
78
int res = pthread_mutex_init(&lock_rep, NULL);
82
EFFECT_VOLUME_BUCKETS::~EFFECT_VOLUME_BUCKETS (void)
86
void EFFECT_VOLUME_BUCKETS::status_entry(const std::vector<unsigned long int>& buckets, std::string& otemp) const
88
/* note: is called with 'lock_rep' taken */
90
for(unsigned int n = 0; n < buckets.size(); n++) {
91
string samples = kvu_numtostr(buckets[n]);
93
otemp += priv_align_right(samples, 8, '_');
95
#if NEVER_USED_PRINT_PERCENTAGE
97
otemp += priv_align_right(kvu_numtostr(100.0f *
99
num_of_samples[n], 2),
103
if (n != buckets.size())
109
void EFFECT_VOLUME_BUCKETS::reset_all_stats(void)
111
reset_period_stats();
112
max_pos = max_neg = 0.0f;
115
void EFFECT_VOLUME_BUCKETS::reset_period_stats(void)
117
for(unsigned int nm = 0; nm < pos_samples_db.size(); nm++)
118
for(unsigned int ch = 0; ch < pos_samples_db[nm].size(); ch++)
119
pos_samples_db[nm][ch] = 0;
121
for(unsigned int nm = 0; nm < neg_samples_db.size(); nm++)
122
for(unsigned int ch = 0; ch < neg_samples_db[nm].size(); ch++)
123
neg_samples_db[nm][ch] = 0;
125
for(unsigned int nm = 0; nm < num_of_samples.size(); nm++)
126
num_of_samples[nm] = 0;
129
string EFFECT_VOLUME_BUCKETS::status(void) const
131
int res = pthread_mutex_lock(&lock_rep);
134
std::string status_str;
136
status_str = "-- Amplitude statistics --\n";
137
status_str += "Pos/neg, count,(%), ch1...n";
139
for(unsigned j = 0; j < pos_samples_db.size(); j++) {
140
status_str += std::string("\nPos ")
141
+ priv_align_right(bucket_table[j].name, 4, ' ')
143
status_entry(pos_samples_db[j], status_str);
146
for(unsigned int j = neg_samples_db.size(); j > 0; j--) {
148
status_str += std::string("\nNeg ")
149
+ priv_align_right(bucket_table[j-1].name, 4, ' ')
151
status_entry(neg_samples_db[j-1], status_str);
154
status_str += std::string("\nTotal.....: ");
155
status_entry(num_of_samples, status_str);
158
status_str += "(audiofx) Peak amplitude: pos=" + kvu_numtostr(max_pos,5) + " neg=" + kvu_numtostr(max_neg,5) + ".\n";
159
status_str += "(audiofx) Max gain without clipping: " + kvu_numtostr(max_multiplier(),5) + ".\n";
161
status_str += "(audiofx) -- End of statistics --\n";
163
res = pthread_mutex_unlock(&lock_rep);
169
void EFFECT_VOLUME_BUCKETS::parameter_description(int param,
170
struct PARAM_DESCRIPTION *pd) const
174
pd->default_value = 0;
175
pd->description = get_parameter_name(param);
176
pd->bounded_above = true;
177
pd->upper_bound = 1.0;
178
pd->bounded_below = true;
179
pd->lower_bound = 0.0f;
182
pd->logarithmic = false;
187
pd->default_value = 1.0f;
188
pd->description = get_parameter_name(param);
189
pd->bounded_above = false;
190
pd->upper_bound = 0.0f;
191
pd->bounded_below = false;
192
pd->lower_bound = 0.0f;
195
pd->logarithmic = false;
201
void EFFECT_VOLUME_BUCKETS::set_parameter(int param, CHAIN_OPERATOR::parameter_t value)
206
CHAIN_OPERATOR::parameter_t EFFECT_VOLUME_BUCKETS::get_parameter(int param) const
210
/* note: always enabled since 2.7.0, but keeping the parameter
211
* still for backwards compatibility */
215
return max_multiplier();
220
CHAIN_OPERATOR::parameter_t EFFECT_VOLUME_BUCKETS::max_multiplier(void) const
223
SAMPLE_SPECS::sample_t max_peak = max_pos;
225
if (max_neg > max_pos)
227
if (max_peak != 0.0f)
228
k = SAMPLE_SPECS::max_amplitude / max_peak;
235
void EFFECT_VOLUME_BUCKETS::init(SAMPLE_BUFFER* insample)
237
int res = pthread_mutex_lock(&lock_rep);
241
set_channels(insample->number_of_channels());
242
DBC_CHECK(channels() == insample->number_of_channels());
243
num_of_samples.resize(insample->number_of_channels(), 0);
245
int entries = sizeof(bucket_table) / sizeof(struct bucket);
247
pos_samples_db.resize(entries, std::vector<unsigned long int> (channels()));
248
neg_samples_db.resize(entries, std::vector<unsigned long int> (channels()));
252
res = pthread_mutex_unlock(&lock_rep);
255
EFFECT_ANALYSIS::init(insample);
258
void EFFECT_VOLUME_BUCKETS::process(void)
260
DBC_CHECK(static_cast<int>(num_of_samples.size()) == channels());
262
int res = pthread_mutex_trylock(&lock_rep);
267
DBC_CHECK(num_of_samples.size() > static_cast<unsigned>(i.channel()));
268
num_of_samples[i.channel()]++;
270
if (*i.current() >= 0) {
271
if (*i.current() > max_pos) max_pos = *i.current();
273
for(unsigned j = 0; j < pos_samples_db.size(); j++) {
274
if (*i.current() > bucket_table[j].threshold) {
275
pos_samples_db[j][i.channel()]++;
281
if (-(*i.current()) > max_neg) max_neg = -(*i.current());
283
for(unsigned j = 0; j < neg_samples_db.size(); j++) {
284
if (*i.current() < -bucket_table[j].threshold) {
285
neg_samples_db[j][i.channel()]++;
293
res = pthread_mutex_unlock(&lock_rep);
296
// else { std::cerr << "(audiofx_analysis) lock taken, skipping process().\n"; }
299
EFFECT_VOLUME_PEAK::EFFECT_VOLUME_PEAK (void)
301
max_amplitude_repp = 0;
304
EFFECT_VOLUME_PEAK::~EFFECT_VOLUME_PEAK (void)
306
if (max_amplitude_repp != 0) {
307
delete[] max_amplitude_repp;
308
max_amplitude_repp = 0;
312
void EFFECT_VOLUME_PEAK::parameter_description(int param,
313
struct PARAM_DESCRIPTION *pd) const
315
if (param > 0 && param <= channels()) {
316
pd->default_value = 0;
317
pd->description = get_parameter_name(param);
318
pd->bounded_above = false;
319
pd->bounded_below = true;
320
pd->lower_bound = 0.0f;
323
pd->logarithmic = false;
328
std::string EFFECT_VOLUME_PEAK::parameter_names(void) const
331
for(int n = 0; n < channels(); n++) {
332
params += "peak-amplitude-ch" + kvu_numtostr(n + 1);
333
if (n != channels()) params += ",";
338
void EFFECT_VOLUME_PEAK::set_parameter(int param, CHAIN_OPERATOR::parameter_t value)
342
CHAIN_OPERATOR::parameter_t EFFECT_VOLUME_PEAK::get_parameter(int param) const
344
if (param > 0 && param <= channels()) {
345
parameter_t temp = max_amplitude_repp[param - 1];
346
max_amplitude_repp[param - 1] = 0.0f;
352
void EFFECT_VOLUME_PEAK::init(SAMPLE_BUFFER* insample)
355
if (max_amplitude_repp != 0) {
356
delete[] max_amplitude_repp;
357
max_amplitude_repp = 0;
359
max_amplitude_repp = new parameter_t [insample->number_of_channels()];
360
set_channels(insample->number_of_channels());
363
void EFFECT_VOLUME_PEAK::process(void)
367
SAMPLE_SPECS::sample_t abscurrent = std::fabs(*i.current());
368
DBC_CHECK(i.channel() >= 0);
369
DBC_CHECK(i.channel() < channels());
370
if (abscurrent > max_amplitude_repp[i.channel()]) {
371
max_amplitude_repp[i.channel()] = std::fabs(*i.current());
377
EFFECT_DCFIND::EFFECT_DCFIND (void)
381
string EFFECT_DCFIND::status(void) const
384
mitem.setprecision(5);
385
mitem << "(audiofx) Optimal value for DC-adjust: ";
386
mitem << get_deltafix(SAMPLE_SPECS::ch_left) << " (left), ";
387
mitem << get_deltafix(SAMPLE_SPECS::ch_right) << " (right).";
388
return mitem.to_string();
391
string EFFECT_DCFIND::parameter_names(void) const
393
std::vector<std::string> t;
394
for(int n = 0; n < channels(); n++) {
395
t.push_back("result-offset-ch" + kvu_numtostr(n + 1));
397
return kvu_vector_to_string(t, ",");
400
CHAIN_OPERATOR::parameter_t EFFECT_DCFIND::get_deltafix(int channel) const
402
SAMPLE_SPECS::sample_t deltafix;
405
channel >= static_cast<int>(pos_sum.size()) ||
406
channel >= static_cast<int>(neg_sum.size())) return 0.0;
408
if (pos_sum[channel] > neg_sum[channel]) deltafix = -(pos_sum[channel] - neg_sum[channel]) / num_of_samples[channel];
409
else deltafix = (neg_sum[channel] - pos_sum[channel]) / num_of_samples[channel];
411
return (CHAIN_OPERATOR::parameter_t)deltafix;
414
void EFFECT_DCFIND::parameter_description(int param,
415
struct PARAM_DESCRIPTION *pd) const
417
pd->default_value = 0.0f;
418
pd->description = get_parameter_name(param);
419
pd->bounded_above = false;
420
pd->upper_bound = 0.0f;
421
pd->bounded_below = false;
422
pd->lower_bound = 0.0f;
425
pd->logarithmic = false;
429
void EFFECT_DCFIND::set_parameter(int param,
430
CHAIN_OPERATOR::parameter_t value)
434
CHAIN_OPERATOR::parameter_t EFFECT_DCFIND::get_parameter(int param) const
436
return get_deltafix(param-1);
439
void EFFECT_DCFIND::init(SAMPLE_BUFFER *insample)
442
set_channels(insample->number_of_channels());
443
pos_sum.resize(channels());
444
neg_sum.resize(channels());
445
num_of_samples.resize(channels());
448
void EFFECT_DCFIND::process(void)
452
tempval = *i.current();
453
if (tempval > SAMPLE_SPECS::silent_value)
454
pos_sum[i.channel()] += tempval;
456
neg_sum[i.channel()] += fabs(tempval);
457
num_of_samples[i.channel()]++;