Move channel buffers from stack to structs so theyt can be reused when mixing with...
[jack_mixer.git] / meter.py
blob80c0783223c174e86c9c0068728fa665bb90f1de
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 gtk
22 class meter(gtk.DrawingArea):
23 def __init__(self, scale):
24 gtk.DrawingArea.__init__(self)
26 self.scale = scale
28 self.connect("expose-event", self.on_expose)
29 self.connect("size-request", self.on_size_request)
30 self.connect("size_allocate", self.on_size_allocate)
32 self.color_bg = gtk.gdk.Color(0,0,0)
33 self.color_value = gtk.gdk.Color(int(65535 * 0.8), int(65535 * 0.7), 0)
34 self.color_mark = gtk.gdk.Color(int(65535 * 0.2), int(65535 * 0.2), int(65535 * 0.2))
35 self.width = 0
36 self.height = 0
38 def on_expose(self, widget, event):
39 cairo_ctx = widget.window.cairo_create()
41 # set a clip region for the expose event
42 cairo_ctx.rectangle(event.area.x, event.area.y, event.area.width, event.area.height)
43 cairo_ctx.clip()
45 self.draw(cairo_ctx)
47 return False
49 def on_size_allocate(self, widget, allocation):
50 #print allocation.x, allocation.y, allocation.width, allocation.height
51 self.width = float(allocation.width)
52 self.height = float(allocation.height)
53 self.font_size = 10
55 def on_size_request(self, widget, requisition):
56 #print "size-request, %u x %u" % (requisition.width, requisition.height)
57 requisition.width = 20
58 return
60 def invalidate_all(self):
61 self.queue_draw_area(0, 0, int(self.width), int(self.height))
63 def draw_background(self, cairo_ctx):
64 cairo_ctx.set_source_color(self.color_bg)
65 cairo_ctx.rectangle(0, 0, self.width, self.height)
66 cairo_ctx.fill()
68 cairo_ctx.set_source_color(self.color_mark)
69 cairo_ctx.select_font_face("Fixed")
70 cairo_ctx.set_font_size(self.font_size)
71 glyph_width = self.font_size * 3 / 5 # avarage glyph ratio
72 for mark in self.scale.get_marks():
73 mark_position = int(self.height * (1 - mark.scale))
74 cairo_ctx.move_to(0, mark_position)
75 cairo_ctx.line_to(self.width, mark_position)
76 cairo_ctx.stroke()
77 x_correction = self.width / 2 - glyph_width * len(mark.text) / 2
78 cairo_ctx.move_to(x_correction, mark_position - 2)
79 cairo_ctx.show_text(mark.text)
81 def draw_value(self, cairo_ctx, value, x, width):
82 #return
83 cairo_ctx.set_source_color(self.color_value)
84 cairo_ctx.rectangle(x, self.height * (1 - value), width, self.height * value)
85 cairo_ctx.fill()
87 def set_scale(self, scale):
88 self.scale = scale
89 self.invalidate_all()
91 class mono(meter):
92 def __init__(self, scale):
93 meter.__init__(self, scale)
94 self.value = 0.0
96 def draw(self, cairo_ctx):
97 self.draw_background(cairo_ctx)
98 self.draw_value(cairo_ctx, self.value, self.width/4.0, self.width/2.0)
100 def set_value(self, value):
101 if value != self.value:
102 self.value = self.scale.db_to_scale(value)
103 self.invalidate_all()
105 class stereo(meter):
106 def __init__(self, scale):
107 meter.__init__(self, scale)
108 self.left = 0.0
109 self.right = 0.0
111 def draw(self, cairo_ctx):
112 self.draw_background(cairo_ctx)
113 self.draw_value(cairo_ctx, self.left, self.width/5.0, self.width/5.0)
114 self.draw_value(cairo_ctx, self.right, self.width/5.0 * 3.0, self.width/5.0)
116 def set_values(self, left, right):
117 if left != self.left or right != self.right:
118 self.left = self.scale.db_to_scale(left)
119 self.right = self.scale.db_to_scale(right)
120 self.invalidate_all()