~ubuntu-branches/ubuntu/precise/lmms/precise-updates

« back to all changes in this revision

Viewing changes to plugins/ladspa_effect/calf/src/calf/vumeter.h

  • Committer: Bazaar Package Importer
  • Author(s): Артём Попов
  • Date: 2011-02-14 20:58:36 UTC
  • mfrom: (1.1.10 upstream) (3.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110214205836-2u41xus1d2mj8nfz
Tags: 0.4.10-1ubuntu1
* Merge from debian unstable (LP: #718801).  Remaining changes:
  - Replace build-dep on libwine-dev with wine1.2-dev to build
    against the newer Wine.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Calf DSP Library 
 
2
 * Peak metering facilities.
 
3
 *
 
4
 * Copyright (C) 2007 Krzysztof Foltman
 
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, or (at your option)
 
9
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 
19
 * 02110-1301, USA.
 
20
 */
 
21
#ifndef __CALF_VUMETER_H
 
22
#define __CALF_VUMETER_H
 
23
 
 
24
#include <math.h>
 
25
 
 
26
namespace dsp {
 
27
 
 
28
/// Peak meter class
 
29
struct vumeter
 
30
{
 
31
    /// Measured signal level
 
32
    float level;
 
33
    /// Falloff of signal level (b1 coefficient of a 1-pole filter)
 
34
    float falloff;
 
35
    /// Clip indicator (set to 1 when |value| >= 1, fading otherwise)
 
36
    float clip;
 
37
    /// Falloff of clip indicator (b1 coefficient of a 1-pole filter); set to 1 if no falloff is required (manual reset of clip indicator)
 
38
    float clip_falloff;
 
39
    
 
40
    vumeter()
 
41
    {
 
42
        falloff = 0.999f;
 
43
        clip_falloff = 0.999f;
 
44
        reset();
 
45
    }
 
46
    
 
47
    void reset()
 
48
    {
 
49
        level = 0;
 
50
        clip = 0;
 
51
    }
 
52
    
 
53
    /// Set falloff so that the meter falls 20dB in time_20dB seconds, assuming sample rate of sample_rate
 
54
    /// @arg time_20dB time for the meter to move by 20dB (default 300ms if <= 0)
 
55
    void set_falloff(double time_20dB, double sample_rate)
 
56
    {
 
57
        if (time_20dB <= 0)
 
58
            time_20dB = 0.3;
 
59
        // 20dB = 10x +/- --> 0.1 = pow(falloff, sample_rate * time_20dB) = exp(sample_rate * ln(falloff))
 
60
        // ln(0.1) = sample_rate * ln(falloff)
 
61
        falloff = pow(0.1, 1 / (sample_rate * time_20dB));
 
62
        clip_falloff = falloff;
 
63
    }
 
64
    /// Copy falloff from another object
 
65
    void copy_falloff(const vumeter &src)
 
66
    {
 
67
        falloff = src.falloff;
 
68
        clip_falloff = src.clip_falloff;
 
69
    }
 
70
    
 
71
    /// Update peak meter based on input signal
 
72
    inline void update(const float *src, unsigned int len)
 
73
    {
 
74
        update_stereo(src, NULL, len);
 
75
    }
 
76
    /// Update peak meter based on louder of two input signals
 
77
    inline void update_stereo(const float *src1, const float *src2, unsigned int len)
 
78
    {
 
79
        // "Age" the old level by falloff^length
 
80
        level *= pow(falloff, len);
 
81
        // Same for clip level (using different fade constant)
 
82
        clip *= pow(clip_falloff, len);
 
83
        dsp::sanitize(level);
 
84
        dsp::sanitize(clip);
 
85
        // Process input samples - to get peak value, take a max of all values in the input signal and "aged" old peak
 
86
        // Clip is set to 1 if any sample is out-of-range, if no clip occurs, the "aged" value is assumed
 
87
        if (src1)
 
88
            run_sample_loop(src1, len);
 
89
        if (src2)
 
90
            run_sample_loop(src2, len);
 
91
    }
 
92
    inline void run_sample_loop(const float *src, unsigned int len)
 
93
    {
 
94
        float tmp = level;
 
95
        for (unsigned int i = 0; i < len; i++) {
 
96
            float sig = fabs(src[i]);
 
97
            tmp = std::max(tmp, sig);
 
98
            if (sig >= 1.f)
 
99
                clip = 1.f;
 
100
        }
 
101
        level = tmp;
 
102
    }
 
103
    /// Update clip meter as if update was called with all-zero input signal
 
104
    inline void update_zeros(unsigned int len)
 
105
    {
 
106
        level *= pow((double)falloff, (double)len);
 
107
        clip *= pow((double)clip_falloff, (double)len);
 
108
        dsp::sanitize(level);
 
109
        dsp::sanitize(clip);
 
110
    }
 
111
};
 
112
 
 
113
struct dual_vumeter
 
114
{
 
115
    vumeter left, right;
 
116
    
 
117
    inline void update_stereo(const float *src1, const float *src2, unsigned int len)
 
118
    {
 
119
        left.update_stereo(src1, NULL, len);
 
120
        right.update_stereo(NULL, src2, len);
 
121
    }
 
122
    inline void update_zeros(unsigned int len)
 
123
    {
 
124
        left.update_zeros(len);
 
125
        right.update_zeros(len);
 
126
    }
 
127
    inline void reset()
 
128
    {
 
129
        left.reset();
 
130
        right.reset();
 
131
    }
 
132
    inline void set_falloff(double time_20dB, double sample_rate)
 
133
    {
 
134
        left.set_falloff(time_20dB, sample_rate);
 
135
        right.copy_falloff(left);
 
136
    }
 
137
    inline void copy_falloff(const dual_vumeter &src)
 
138
    {
 
139
        left.copy_falloff(src.left);
 
140
        right.copy_falloff(src.right);
 
141
    }
 
142
 
 
143
};
 
144
 
 
145
};
 
146
 
 
147
#endif