summaryrefslogtreecommitdiff
path: root/libs/vamp-plugins/ebu_r128_proc.h
blob: 126be8b0f254ee0f4dcfd7d36f950847653dea6a (plain)
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
// ------------------------------------------------------------------------
//
//  Copyright (C) 2010-2011 Fons Adriaensen <fons@linuxaudio.org>
//    
//  This program is free 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ------------------------------------------------------------------------


#ifndef _EBU_R128_PROC_H
#define _EBU_R128_PROC_H

#define MAXCH 5

namespace Fons {

class Ebu_r128_fst
{
private:

    friend class Ebu_r128_proc;

    void reset (void) { _z1 = _z2 = _z3 = _z4 = 0; }

    float _z1, _z2, _z3, _z4;
};


class Ebu_r128_hist
{
private:

    Ebu_r128_hist (void);
    ~Ebu_r128_hist (void);

    friend class Ebu_r128_proc;

    void  reset (void);
    void  initstat (void);
    void  addpoint (float v);
    float integrate (int ind);
    void  calc_integ (float *vi, float *th);
    void  calc_range (float *v0, float *v1, float *th);

    int  *_histc;
    int   _count;
    int   _error;

    static float _bin_power [100];
};



class Ebu_r128_proc
{
public:

    Ebu_r128_proc (void);
    ~Ebu_r128_proc (void);

    void  init (int nchan, float fsamp);
    void  reset (void);
    void  process (int nfram, const float *const *input);
    void  integr_reset (void);
    void  integr_pause (void) { _integr = false; }
    void  integr_start (void) { _integr = true; }

    float loudness_M (void) const { return _loudness_M; }
    float maxloudn_M (void) const { return _maxloudn_M; }
    float loudness_S (void) const { return _loudness_S; }
    float maxloudn_S (void) const { return _maxloudn_S; }
    float integrated (void) const { return _integrated; }
    float integ_thr (void) const { return _integ_thr; }
    float range_min (void) const { return _range_min; }
    float range_max (void) const { return _range_max; }
    float range_thr (void) const { return _range_thr; }

    const int *histogram_M (void) const { return _hist_M._histc; }
    const int *histogram_S (void) const { return _hist_S._histc; }
    int hist_M_count (void) const { return _hist_M._count; }
    int hist_S_count (void) const { return _hist_S._count; }

private:

    float addfrags (int nfrag);
    void  detect_init (float fsamp);
    void  detect_reset (void);
    float detect_process (int nfram);

    bool              _integr;       // Integration on/off.
    int               _nchan;        // Number of channels, 2 or 5.
    float             _fsamp;        // Sample rate.
    int               _fragm;        // Fragmenst size, 1/20 second.
    int               _frcnt;        // Number of samples remaining in current fragment.
    float             _frpwr;        // Power accumulated for current fragment.
    float             _power [64];   // Array of fragment powers.
    int               _wrind;        // Write index into _frpwr 
    int               _div1;         // M period counter, 200 ms;
    int               _div2;         // S period counter, 1s;
    float             _loudness_M;
    float             _maxloudn_M;
    float             _loudness_S;
    float             _maxloudn_S;
    float             _integrated;
    float             _integ_thr;
    float             _range_min;
    float             _range_max;
    float             _range_thr;
    
    // Filter coefficients and states.
    float             _a0, _a1, _a2;
    float             _b1, _b2;
    float             _c3, _c4;
    float const      *_ipp [MAXCH];
    Ebu_r128_fst      _fst [MAXCH];
    Ebu_r128_hist     _hist_M;
    Ebu_r128_hist     _hist_S;

    // Default channel gains.
    static float      _chan_gain [5];
};

};

#endif