summaryrefslogtreecommitdiff
path: root/src/audio/testlimiter.cpp
blob: 5c3a515bfb7079f1a7cab44c00948bad872d1d3f (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
//
//  libavg - Media Playback Engine.
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "Dynamics.h"

#include "../base/TestSuite.h"
#include "../base/MathHelper.h"

#include <stdlib.h>
#include <iostream>

using namespace avg;
using namespace std;

class LimiterTest: public Test {
public:
    LimiterTest()
        : Test("LimiterTest", 2)
    {
    }

    void runTests()
    {
        const int CHANNELS = 2;
        float fs = 44100.f;
        int numSamples = int(fs * 0.1f);

        // Setup a brickwall limiter
        typedef Dynamics<float, CHANNELS> TStereoLimiter;
        TStereoLimiter* d = new TStereoLimiter(fs);
        d->setThreshold(0.f); // in dB
        d->setAttackTime(0.f); // in seconds
        d->setReleaseTime(0.05f); // in seconds
        d->setRmsTime(0.f); // in seconds
        d->setRatio(std::numeric_limits<float>::infinity());
        d->setMakeupGain(0.f); // in dB

        // Generate input and output test data
        float* pSamples = new float[CHANNELS*numSamples];
        for (int j = 0; j < numSamples; j++) {
            for (int i = 0; i < CHANNELS; i++) {
                pSamples[j*CHANNELS+i] = 2*sin(j*(440.f/44100)*float(M_PI));
            }
        }

        // Let the limiter work.
        for (int i=0; i<numSamples; ++i) {
            d->process(pSamples+i*CHANNELS);
        }

        // Check if everything is ok.
        bool bDiscontinuities = false;
        bool bAboveThreshold = false;
        for (int j = 1; j < numSamples; j++) {
            for (int i = 0; i < CHANNELS; i++) {
                // Test if anything is above the threshold.
                if (pSamples[j*CHANNELS+i] > 1) {
                    bAboveThreshold = true;
                }
                if (fabs(pSamples[j*CHANNELS+i]-pSamples[(j-1)*CHANNELS+i]) > 0.05f) {
                    bDiscontinuities = true;
//                    cerr << j << ": " << outSamples[j*OUT_CHANNELS+i] << ", " << 
//                            outSamples[(j-1)*OUT_CHANNELS+i] << endl;
                }
            }
        }
        TEST(!bAboveThreshold);
        TEST(!bDiscontinuities);
/*
        // Save data to ascii file.
        FILE * pFile = fopen("data.txt", "w");
        for (int j = 0; j < numSamples; j++) {
            fprintf(pFile, "%f\n", pSamples[j*OUT_CHANNELS]);
        }
        fclose(pFile);
*/
        // Free memory
        delete d;
        delete[] pSamples;
    }
};

int main(int nargs, char** args)
{
    LimiterTest test;
    test.runTests();
    bool bOK = test.isOk();

    if (bOK) {
        return 0;
    } else {
        return 1;
    }
}