]> git.0d.be Git - jack_mixer.git/blob - scale.py
Updated and documented a little bit the sys.path alteration
[jack_mixer.git] / scale.py
1 #!/usr/bin/env python
2 #
3 # This file is part of jack_mixer
4 #
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
10 #
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.
15 #
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.
19
20 import math
21 import jack_mixer_c
22
23 class mark:
24     '''Encapsulates scale linear function edge and coefficients for scale = a * dB + b formula'''
25     def __init__(self, db, scale):
26         self.db = db
27         self.scale = scale
28         self.text = "%.0f" % math.fabs(db)
29
30 class base:
31     '''Scale abstraction, various scale implementation derive from this class'''
32     def __init__(self, scale_id, description):
33         self.marks = []
34         self.scale_id = scale_id
35         self.description = description
36         self.scale = jack_mixer_c.Scale()
37
38     def add_threshold(self, db, scale, is_mark):
39         self.scale.add_threshold(db, scale)
40         if is_mark:
41             self.marks.append(mark(db, scale))
42
43     def calculate_coefficients(self):
44         self.scale.calculate_coefficients()
45
46     def db_to_scale(self, db):
47         '''Convert dBFS value to number in range 0.0-1.0 used in GUI'''
48         #print "db_to_scale(%f)" % db
49         return self.scale.db_to_scale(db)
50
51     def scale_to_db(self, scale):
52         '''Convert number in range 0.0-1.0 used in GUI to dBFS value'''
53         return self.scale.scale_to_db(scale)
54
55     def add_mark(self, db):
56         self.marks.append(mark(db, -1.0))
57
58     def get_marks(self):
59         return self.marks
60
61     def scale_marks(self):
62         for i in self.marks:
63             if i.scale == -1.0:
64                 i.scale = self.db_to_scale(i.db)
65
66 # IEC 60268-18 Peak programme level meters - Digital audio peak level meter
67 # Adapted from meterpridge, may be wrong, I'm not buying standards, event if they cost $45
68 # If someone has the standart, please eighter share it with me or fix the code.
69 class iec_268(base):
70     '''IEC 60268-18 Peak programme level meters - Digital audio peak level meter'''
71     def __init__(self):
72         base.__init__(self, "iec_268", "IEC 60268-18 Peak programme level meters - Digital audio peak level meter")
73         self.add_threshold(-70.0, 0.0, False)
74         self.add_threshold(-60.0, 0.05, True)
75         self.add_threshold(-50.0, 0.075, True)
76         self.add_threshold(-40.0, 0.15, True)
77         self.add_mark(-35.0)
78         self.add_threshold(-30.0, 0.3, True)
79         self.add_mark(-25.0)
80         self.add_threshold(-20.0, 0.5, True)
81         self.add_mark(-15.0)
82         self.add_mark(-10.0)
83         self.add_mark(-5.0)
84         self.add_threshold(0.0, 1.0, True)
85         self.calculate_coefficients()
86         self.scale_marks()
87
88 class iec_268_minimalistic(base):
89     '''IEC 60268-18 Peak programme level meters - Digital audio peak level meter, fewer marks'''
90     def __init__(self):
91         base.__init__(self, "iec_268_minimalistic", "IEC 60268-18 Peak programme level meters - Digital audio peak level meter, fewer marks")
92         self.add_threshold(-70.0, 0.0, False)
93         self.add_threshold(-60.0, 0.05, True)
94         self.add_threshold(-50.0, 0.075, False)
95         self.add_threshold(-40.0, 0.15, True)
96         self.add_threshold(-30.0, 0.3, True)
97         self.add_threshold(-20.0, 0.5, True)
98         self.add_mark(-10.0)
99         self.add_threshold(0.0, 1.0, True)
100         self.calculate_coefficients()
101         self.scale_marks()
102
103 class linear_70dB(base):
104     '''Linear scale with range from -70 to 0 dBFS'''
105     def __init__(self):
106         base.__init__(self, "linear_70dB", "Linear scale with range from -70 to 0 dBFS")
107         self.add_threshold(-70.0, 0.0, False)
108         self.add_mark(-60.0)
109         self.add_mark(-50.0)
110         self.add_mark(-40.0)
111         self.add_mark(-35.0)
112         self.add_mark(-30.0)
113         self.add_mark(-25.0)
114         self.add_mark(-20.0)
115         self.add_mark(-15.0)
116         self.add_mark(-10.0)
117         self.add_mark(-5.0)
118         self.add_threshold(0.0, 1.0, True)
119         self.calculate_coefficients()
120         self.scale_marks()
121
122 class linear_30dB(base):
123     '''Linear scale with range from -30 to +30 dBFS'''
124     def __init__(self):
125         base.__init__(self, "linear_30dB", "Linear scale with range from -30 to +30 dBFS")
126         self.add_threshold(-30.0, 0.0, False)
127         self.add_threshold(+30.0, 1.0, True)
128         self.calculate_coefficients()
129         self.scale_marks()
130
131 def scale_test1(scale):
132     for i in range(-97 * 2, 1, 1):
133         db = float(i)/2.0
134         print "%5.1f dB maps to %f" % (db, scale.db_to_scale(db))
135
136 def scale_test2(scale):
137     for i in range(101):
138         s = float(i)/100.0
139         print "%.2f maps to %.1f dB" % (s, scale.scale_to_db(s))
140
141 def print_db_to_scale(db):
142     print "%-.1f dB maps to %f" % (db, scale.db_to_scale(db))
143
144 def scale_test3(scale):
145     print_db_to_scale(+77.0)
146     print_db_to_scale(+7.0)
147     print_db_to_scale(0.0)
148     print_db_to_scale(-107.0)
149
150 #scale = linear_30dB()
151 #scale_test2(scale)
152 #scale_test3(scale)