kwave  18.07.70
Normalizer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Normalizer.cpp - simple normalizer with limiter
3  -------------------
4  begin : Sat May 09 2009
5  copyright : (C) 2009 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7 
8  limiter function : (C) 1999-2005 Chris Vaill <chrisvaill at gmail>
9  taken from "normalize-0.7.7"
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 
21 #include "config.h"
22 
23 #include <math.h>
24 
25 #include "libkwave/Sample.h"
26 
27 #include "Normalizer.h"
28 
29 //***************************************************************************
31  :Kwave::SampleSource(Q_NULLPTR), m_gain(1.0), m_limit(0.5)
32 {
33 }
34 
35 //***************************************************************************
37 {
38 }
39 
40 //***************************************************************************
42 {
43 }
44 
45 //***************************************************************************
46 /*
47  * Limiter function:
48  *
49  * / tanh((x + lev) / (1-lev)) * (1-lev) - lev (for x < -lev)
50  * |
51  * x' = | x (for |x| <= lev)
52  * |
53  * \ tanh((x - lev) / (1-lev)) * (1-lev) + lev (for x > lev)
54  *
55  * With limiter level = 0, this is equivalent to a tanh() function;
56  * with limiter level = 1, this is equivalent to clipping.
57  */
58 static inline double limiter(const double x, const double lmtr_lvl)
59 {
60  double xp;
61 
62  if (x < -lmtr_lvl)
63  xp = tanh((x + lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) - lmtr_lvl;
64  else if (x <= lmtr_lvl)
65  xp = x;
66  else
67  xp = tanh((x - lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) + lmtr_lvl;
68 
69  return xp;
70 }
71 
72 //***************************************************************************
74 {
75  const unsigned int len = data.size();
76  const bool use_limiter = (m_gain > 1.0);
77 
78  for (unsigned int i = 0; i < len; i++) {
79  double s = sample2double(data[i]);
80  s *= m_gain;
81  if (use_limiter) s = limiter(s, m_limit);
82  data[i] = double2sample(s);
83  }
84 
85  emit output(data);
86 }
87 
88 //***************************************************************************
89 void Kwave::Normalizer::setGain(const QVariant g)
90 {
91  m_gain = QVariant(g).toDouble();
92 }
93 
94 //***************************************************************************
95 void Kwave::Normalizer::setLimiterLevel(const QVariant l)
96 {
97  m_limit = QVariant(l).toDouble();
98 }
99 
100 //***************************************************************************
101 //***************************************************************************
Definition: App.h:33
void input(Kwave::SampleArray data)
Definition: Normalizer.cpp:73
virtual void goOn() Q_DECL_OVERRIDE
Definition: Normalizer.cpp:41
static double limiter(const double x, const double lmtr_lvl)
Definition: Normalizer.cpp:58
void setLimiterLevel(const QVariant l)
Definition: Normalizer.cpp:95
static double sample2double(const sample_t s)
Definition: Sample.h:73
virtual ~Normalizer() Q_DECL_OVERRIDE
Definition: Normalizer.cpp:36
static sample_t double2sample(const double f)
Definition: Sample.h:81
unsigned int size() const
void output(Kwave::SampleArray data)
void setGain(const QVariant g)
Definition: Normalizer.cpp:89