release: 13
[jack_mixer.git] / abspeak.py
blob171e38f615922046ed6977697b9937c02c070ba0
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 from gi.repository import Gtk
19 from gi.repository import Gdk
20 from gi.repository import Pango
21 from gi.repository import GObject
22 import math
24 css = b"""
25 .over_zero {
26 background-color: #cc4c00;
29 .is_nan {
30 background-color: #b20000;
32 """
33 css_provider = Gtk.CssProvider()
34 css_provider.load_from_data(css)
35 context = Gtk.StyleContext()
36 screen = Gdk.Screen.get_default()
37 context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
39 class AbspeakWidget(Gtk.EventBox):
40 def __init__(self):
41 GObject.GObject.__init__(self)
42 self.label = Gtk.Label()
43 #attrs = Pango.AttrList()
44 #font_attr = Pango.AttrFamily("monospace")
45 #attrs.insert(font_attr)
46 #self.label.set_attributes(attrs)
47 self.add(self.label)
48 self.connect("button-press-event", self.on_mouse)
49 self.peak = -math.inf
51 def on_mouse(self, widget, event):
52 if event.type == Gdk.EventType.BUTTON_PRESS:
53 if event.button == 1 or event.button == 2 or event.button == 3:
54 context = self.get_style_context()
55 context.remove_class('over_zero')
56 context.remove_class('is_nan')
57 if event.button == 1 or event.button == 3:
58 self.emit("reset")
59 elif event.button == 2:
60 adjust = -self.peak
61 if abs(adjust) < 30: # we better don't adjust more than +- 30 dB
62 self.emit("volume-adjust", adjust)
64 def set_peak(self, peak):
65 self.peak = peak
66 if math.isnan(peak):
67 self.get_style_context().add_class('is_nan')
68 self.label.set_text("NaN")
69 else:
70 text = "%+.1f" % peak
72 if peak > 0:
73 self.get_style_context().add_class('over_zero')
74 else:
75 pass
77 self.label.set_text(text)
79 GObject.signal_new("reset", AbspeakWidget,
80 GObject.SignalFlags.RUN_FIRST | GObject.SignalFlags.ACTION, None, [])
81 GObject.signal_new("volume-adjust", AbspeakWidget,
82 GObject.SignalFlags.RUN_FIRST | GObject.SignalFlags.ACTION, None, [GObject.TYPE_FLOAT])