Cleanup scale code by removing threshold objects in python part (replaced with mark...
[jack_mixer.git] / scale.py
blob9d0dd3d75dea03961e2d2431fc434120805e5b2f
1 #!/usr/bin/env python
3 # This file is part of jack_mixer
5 # Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; version 2 of the License
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.
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 St, Fifth Floor, Boston, MA 02110-1301 USA.
20 import math
21 import fpconst
22 import jack_mixer_c
24 class mark:
25 '''Encapsulates scale linear function edge and coefficients for scale = a * dB + b formula'''
26 def __init__(self, db, scale):
27 self.db = db
28 self.scale = scale
29 self.text = "%.0f" % math.fabs(db)
31 class base:
32 '''Scale abstraction, various scale implementation derive from this class'''
33 def __init__(self, scale_id, description):
34 self.marks = []
35 self.scale_id = scale_id
36 self.description = description
37 self.scale = jack_mixer_c.scale_create()
39 def add_threshold(self, db, scale, is_mark):
40 jack_mixer_c.scale_add_threshold(self.scale, db, scale)
41 if is_mark:
42 self.marks.append(mark(db, scale))
44 def calculate_coefficients(self):
45 jack_mixer_c.scale_calculate_coefficients(self.scale)
47 def db_to_scale(self, db):
48 '''Convert dBFS value to number in range 0.0-1.0 used in GUI'''
49 #print "db_to_scale(%f)" % db
50 return jack_mixer_c.scale_db_to_scale(self.scale, db)
52 def scale_to_db(self, scale):
53 '''Convert number in range 0.0-1.0 used in GUI to dBFS value'''
54 return jack_mixer_c.scale_scale_to_db(self.scale, scale)
56 def add_mark(self, db):
57 self.marks.append(mark(db, -1.0))
59 def get_marks(self):
60 return self.marks
62 def scale_marks(self):
63 for i in self.marks:
64 if i.scale == -1.0:
65 i.scale = self.db_to_scale(i.db)
67 # IEC 60268-18 Peak programme level meters - Digital audio peak level meter
68 # Adapted from meterpridge, may be wrong, I'm not buying standards, event if they cost $45
69 # If someone has the standart, please eighter share it with me or fix the code.
70 class iec_268(base):
71 '''IEC 60268-18 Peak programme level meters - Digital audio peak level meter'''
72 def __init__(self):
73 base.__init__(self, "iec_268", "IEC 60268-18 Peak programme level meters - Digital audio peak level meter")
74 self.add_threshold(-70.0, 0.0, False)
75 self.add_threshold(-60.0, 0.05, True)
76 self.add_threshold(-50.0, 0.075, True)
77 self.add_threshold(-40.0, 0.15, True)
78 self.add_mark(-35.0)
79 self.add_threshold(-30.0, 0.3, True)
80 self.add_mark(-25.0)
81 self.add_threshold(-20.0, 0.5, True)
82 self.add_mark(-15.0)
83 self.add_mark(-10.0)
84 self.add_mark(-5.0)
85 self.add_threshold(0.0, 1.0, True)
86 self.calculate_coefficients()
87 self.scale_marks()
89 class iec_268_minimalistic(base):
90 '''IEC 60268-18 Peak programme level meters - Digital audio peak level meter, fewer marks'''
91 def __init__(self):
92 base.__init__(self, "iec_268_minimalistic", "IEC 60268-18 Peak programme level meters - Digital audio peak level meter, fewer marks")
93 self.add_threshold(-70.0, 0.0, False)
94 self.add_threshold(-60.0, 0.05, True)
95 self.add_threshold(-50.0, 0.075, False)
96 self.add_threshold(-40.0, 0.15, True)
97 self.add_threshold(-30.0, 0.3, True)
98 self.add_threshold(-20.0, 0.5, True)
99 self.add_mark(-10.0)
100 self.add_threshold(0.0, 1.0, True)
101 self.calculate_coefficients()
102 self.scale_marks()
104 class linear_70dB(base):
105 '''Linear scale with range from -70 to 0 dBFS'''
106 def __init__(self):
107 base.__init__(self, "linear_70dB", "Linear scale with range from -70 to 0 dBFS")
108 self.add_threshold(-70.0, 0.0, False)
109 self.add_mark(-60.0)
110 self.add_mark(-50.0)
111 self.add_mark(-40.0)
112 self.add_mark(-35.0)
113 self.add_mark(-30.0)
114 self.add_mark(-25.0)
115 self.add_mark(-20.0)
116 self.add_mark(-15.0)
117 self.add_mark(-10.0)
118 self.add_mark(-5.0)
119 self.add_threshold(0.0, 1.0, True)
120 self.calculate_coefficients()
121 self.scale_marks()
123 class linear_30dB(base):
124 '''Linear scale with range from -30 to +30 dBFS'''
125 def __init__(self):
126 base.__init__(self, "linear_30dB", "Linear scale with range from -30 to +30 dBFS")
127 self.add_threshold(-30.0, 0.0, False)
128 self.add_threshold(+30.0, 1.0, True)
129 self.calculate_coefficients()
130 self.scale_marks()
132 def scale_test1(scale):
133 for i in range(-97 * 2, 1, 1):
134 db = float(i)/2.0
135 print "%5.1f dB maps to %f" % (db, scale.db_to_scale(db))
137 def scale_test2(scale):
138 for i in range(101):
139 s = float(i)/100.0
140 print "%.2f maps to %.1f dB" % (s, scale.scale_to_db(s))
142 def print_db_to_scale(db):
143 print "%-.1f dB maps to %f" % (db, scale.db_to_scale(db))
145 def scale_test3(scale):
146 print_db_to_scale(+77.0)
147 print_db_to_scale(+7.0)
148 print_db_to_scale(0.0)
149 print_db_to_scale(-107.0)
151 #scale = linear_30dB()
152 #scale_test2(scale)
153 #scale_test3(scale)