summaryrefslogtreecommitdiffstats
path: root/arts/gui/kde/dbvolcalc.h
blob: a8ab0cf528bc4cec130f29d9b7f68593e170ddaa (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
/*
    Copyright (  C ) 2003 Arnold Krille <arnold@arnoldarts.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation;
    version 2 of the License.

    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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#ifndef ARTS_DB2VOL_CALC_H
#define ARTS_DB2VOL_CALC_H

#include <math.h>

class dB2VolCalc {
private:
	float _base;
public:
	dB2VolCalc( float _dbmin, float _dbmax )
	  : _base( 6/log10( double(2) ) ) // Depends on what you measure: intensity(10), pressure(20), old artscontrol(6/lg2 (near 20))
	  , dbmax( _dbmax )
	  , dbmin( _dbmin )
	{}

	float dbmax, dbmin;
	/**
		Logarithmic/decimal valuation:
		p = ampfactor ( linear )
		db = dezibel ( logarithmic )
		p = 10^( d/10 )
		db = 10*lg( p )

		artscontrol was using
			db = 6/ln( 2 ) * ln( p )
		which would be
			db = 6/lg( 2 ) * lg( p )
	*/
	float amptodb( float p ) {
		float db = _base*log10( p );
		if ( db < dbmin ) db = dbmin;
		if ( db > dbmax ) db = dbmax;
		return db;
	}
	float dbtoamp( float db ) {
		float amp = pow( 10, db/_base );
		if ( amp <= pow( 10, dbmin/_base ) ) amp = 0;
		return amp;
	}
	/// With ndb = normalized dB (between 0 and 1)
	float amptondb( float p ) {
		return  dbtondb( amptodb( p ) ); //- dbmin ) / ( dbmax - dbmin );
	}
	float ndbtoamp( float ndb ) {
		return dbtoamp( ndb * ( dbmax - dbmin ) + dbmin );
	}
	/// Normalizes a dezibel value.
	float dbtondb( float db ) {
		return ( db - dbmin )/( dbmax - dbmin );
	}
	/// Normalizes a volume to a logarithmic value between 0 and 1.
	float dbtovol( float db ) {
		return ( db -dbmin )/( 0-dbmin );
	}
	/// Unnormalizes a dezibel value.
	float ndbtodb( float ndb ) {
		return ( ndb * ( dbmax-dbmin ) +dbmin );
	}
};

#endif
// vim: sw=4 ts=4