add Daniel Sheeler to authors
[jack_mixer.git] / scale.py
blob9afe2403f22a5eb7c85448a2f5f283f2ac05c8a9
1 # This file is part of jack_mixer
3 # Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 import math
19 import jack_mixer_c
21 class Mark:
22 '''Encapsulates scale linear function edge and coefficients for scale = a * dB + b formula'''
23 def __init__(self, db, scale):
24 self.db = db
25 self.scale = scale
26 self.text = "%.0f" % math.fabs(db)
28 class Base:
29 '''Scale abstraction, various scale implementation derive from this class'''
30 def __init__(self, scale_id, description):
31 self.marks = []
32 self.scale_id = scale_id
33 self.description = description
34 self.scale = jack_mixer_c.Scale()
36 def add_threshold(self, db, scale, is_mark):
37 self.scale.add_threshold(db, scale)
38 if is_mark:
39 self.marks.append(Mark(db, scale))
41 def calculate_coefficients(self):
42 self.scale.calculate_coefficients()
44 def db_to_scale(self, db):
45 '''Convert dBFS value to number in range 0.0-1.0 used in GUI'''
46 #print "db_to_scale(%f)" % db
47 return self.scale.db_to_scale(db)
49 def scale_to_db(self, scale):
50 '''Convert number in range 0.0-1.0 used in GUI to dBFS value'''
51 return self.scale.scale_to_db(scale)
53 def add_mark(self, db):
54 self.marks.append(Mark(db, -1.0))
56 def get_marks(self):
57 return self.marks
59 def scale_marks(self):
60 for i in self.marks:
61 if i.scale == -1.0:
62 i.scale = self.db_to_scale(i.db)
64 # IEC 60268-18 Peak programme level meters - Digital audio peak level meter
65 # Adapted from meterpridge, may be wrong, I'm not buying standards, event if they cost $45
66 # If someone has the standart, please eighter share it with me or fix the code.
67 class IEC268(Base):
68 '''IEC 60268-18 Peak programme level meters - Digital audio peak level meter'''
69 def __init__(self):
70 Base.__init__(self, "iec_268",
71 "IEC 60268-18 Peak programme level meters - Digital audio peak level meter")
72 self.add_threshold(-70.0, 0.0, False)
73 self.add_threshold(-60.0, 0.05, True)
74 self.add_threshold(-50.0, 0.075, True)
75 self.add_threshold(-40.0, 0.15, True)
76 self.add_mark(-35.0)
77 self.add_threshold(-30.0, 0.3, True)
78 self.add_mark(-25.0)
79 self.add_threshold(-20.0, 0.5, True)
80 self.add_mark(-15.0)
81 self.add_mark(-10.0)
82 self.add_mark(-5.0)
83 self.add_threshold(0.0, 1.0, True)
84 self.calculate_coefficients()
85 self.scale_marks()
87 class IEC268Minimalistic(Base):
88 '''IEC 60268-18 Peak programme level meters - Digital audio peak level meter,
89 fewer marks'''
90 def __init__(self):
91 Base.__init__(self, 'iec_268_minimalistic',
92 '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 Linear70dB(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 Linear30dB(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)