Minor fix for flake8 complaint about comment whitespace
[jack_mixer.git] / abspeak.py
blob4c55ffe51cd48219bd34d60c6c103ed0ec55feb3
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
20 from gi.repository import Gtk
21 from gi.repository import Gdk
22 from gi.repository import GObject
25 class AbspeakWidget(Gtk.EventBox):
26 def __init__(self):
27 super().__init__()
28 self.label = Gtk.Label()
29 self.add(self.label)
30 self.connect("button-press-event", self.on_mouse)
31 self.peak = -math.inf
33 def get_style_context(self):
34 return self.label.get_style_context()
36 def on_mouse(self, widget, event):
37 if event.type == Gdk.EventType.BUTTON_PRESS:
38 if event.button == 1 or event.button == 2 or event.button == 3:
39 context = self.get_style_context()
40 context.remove_class("over_zero")
41 context.remove_class("is_nan")
43 if event.button == 1 or event.button == 3:
44 self.emit("reset")
45 elif event.button == 2:
46 adjust = -self.peak
48 if abs(adjust) < 30: # we better don't adjust more than +- 30 dB
49 self.emit("volume-adjust", adjust)
51 def set_peak(self, peak):
52 self.peak = peak
53 context = self.get_style_context()
55 if math.isnan(peak):
56 context.remove_class("over_zero")
57 context.add_class("is_nan")
58 self.label.set_text("NaN")
59 else:
60 text = "%+.1f" % peak
61 context.remove_class("is_nan")
63 if peak > 0:
64 context.add_class("over_zero")
66 self.label.set_text(text)
69 GObject.signal_new(
70 "reset", AbspeakWidget, GObject.SignalFlags.RUN_FIRST | GObject.SignalFlags.ACTION, None, []
72 GObject.signal_new(
73 "volume-adjust",
74 AbspeakWidget,
75 GObject.SignalFlags.RUN_FIRST | GObject.SignalFlags.ACTION,
76 None,
77 [GObject.TYPE_FLOAT],