]> git.0d.be Git - jack_mixer.git/blob - channel.py
Renamed bootstrap to autogen.sh to match what is being done in other projects
[jack_mixer.git] / channel.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 gtk
21 import gobject
22 import glib
23 import slider
24 import meter
25 import abspeak
26 from serialization import serialized_object
27
28 try:
29     import phat
30 except:
31     phat = None
32
33
34 class channel(gtk.VBox, serialized_object):
35     '''Widget with slider and meter used as base class for more specific channel widgets'''
36     monitor_button = None
37
38     def __init__(self, app, name, stereo):
39         gtk.VBox.__init__(self)
40         self.app = app
41         self.mixer = app.mixer
42         self.gui_factory = app.gui_factory
43         self._channel_name = name
44         self.stereo = stereo
45         self.meter_scale = self.gui_factory.get_default_meter_scale()
46         self.slider_scale = self.gui_factory.get_default_slider_scale()
47         self.slider_adjustment = slider.adjustment_dBFS(self.slider_scale, 0.0)
48         self.balance_adjustment = gtk.Adjustment(0.0, -1.0, 1.0, 0.02)
49         self.future_volume_midi_cc = None
50         self.future_balance_midi_cc = None
51
52     def get_channel_name(self):
53         return self._channel_name
54
55     label_name = None
56     channel = None
57     def set_channel_name(self, name):
58         self._channel_name = name
59         if self.label_name:
60             self.label_name.set_text(name)
61         if self.channel:
62             self.channel.name = name
63     channel_name = property(get_channel_name, set_channel_name)
64
65     def realize(self):
66         #print "Realizing channel \"%s\"" % self.channel_name
67
68         self.slider_adjustment.connect("volume-changed", self.on_volume_changed)
69         self.balance_adjustment.connect("value-changed", self.on_balance_changed)
70         self.connect('midi-event-received', self.on_midi_event_received)
71
72         self.slider = None
73         self.create_slider_widget()
74
75         if self.stereo:
76             self.meter = meter.stereo(self.meter_scale)
77         else:
78             self.meter = meter.mono(self.meter_scale)
79         self.on_vumeter_color_changed(self.gui_factory)
80
81         self.meter.set_events(gtk.gdk.SCROLL_MASK)
82
83         self.gui_factory.connect("default-meter-scale-changed", self.on_default_meter_scale_changed)
84         self.gui_factory.connect("default-slider-scale-changed", self.on_default_slider_scale_changed)
85         self.gui_factory.connect('vumeter-color-changed', self.on_vumeter_color_changed)
86         self.gui_factory.connect('vumeter-color-scheme-changed', self.on_vumeter_color_changed)
87         self.gui_factory.connect('use-custom-widgets-changed', self.on_custom_widgets_changed)
88
89         self.abspeak = abspeak.widget()
90         self.abspeak.connect("reset", self.on_abspeak_reset)
91         self.abspeak.connect("volume-adjust", self.on_abspeak_adjust)
92
93         self.volume_digits = gtk.Entry()
94         self.volume_digits.connect("key-press-event", self.on_volume_digits_key_pressed)
95         self.volume_digits.connect("focus-out-event", self.on_volume_digits_focus_out)
96
97         self.connect("key-press-event", self.on_key_pressed)
98         self.connect("scroll-event", self.on_scroll)
99
100     def unrealize(self):
101         #print "Unrealizing channel \"%s\"" % self.channel_name
102         pass
103
104     def create_balance_widget(self):
105         if self.gui_factory.use_custom_widgets and phat:
106             self.balance = phat.HFanSlider()
107             self.balance.set_default_value(0)
108             self.balance.set_adjustment(self.balance_adjustment)
109         else:
110             self.balance = gtk.HScale(self.balance_adjustment)
111             self.balance.set_draw_value(False)
112         self.pack_start(self.balance, False)
113         if self.monitor_button:
114             self.reorder_child(self.monitor_button, -1)
115         self.balance.show()
116
117     def create_slider_widget(self):
118         parent = None
119         if self.slider:
120             parent = self.slider.get_parent()
121             self.slider.destroy()
122         if self.gui_factory.use_custom_widgets:
123             self.slider = slider.CustomSliderWidget(self.slider_adjustment)
124         else:
125             self.slider = slider.GtkSlider(self.slider_adjustment)
126         if parent:
127             parent.pack_start(self.slider)
128             parent.reorder_child(self.slider, 0)
129         self.slider.show()
130
131     def on_default_meter_scale_changed(self, gui_factory, scale):
132         #print "Default meter scale change detected."
133         self.meter.set_scale(scale)
134
135     def on_default_slider_scale_changed(self, gui_factory, scale):
136         #print "Default slider scale change detected."
137         self.slider_scale = scale
138         self.slider_adjustment.set_scale(scale)
139         self.channel.set_midi_scale(self.slider_scale.scale)
140
141     def on_vumeter_color_changed(self, gui_factory, *args):
142         color = gui_factory.get_vumeter_color()
143         color_scheme = gui_factory.get_vumeter_color_scheme()
144         if color_scheme != 'solid':
145             self.meter.set_color(None)
146         else:
147             self.meter.set_color(gtk.gdk.color_parse(color))
148
149     def on_custom_widgets_changed(self, gui_factory, value):
150         self.balance.destroy()
151         self.create_balance_widget()
152         self.create_slider_widget()
153
154     def on_abspeak_adjust(self, abspeak, adjust):
155         #print "abspeak adjust %f" % adjust
156         self.slider_adjustment.set_value_db(self.slider_adjustment.get_value_db() + adjust)
157         self.channel.abspeak = None
158         #self.update_volume(False)   # We want to update gui even if actual decibels have not changed (scale wrap for example)
159
160     def on_abspeak_reset(self, abspeak):
161         #print "abspeak reset"
162         self.channel.abspeak = None
163
164     def on_volume_digits_key_pressed(self, widget, event):
165         if (event.keyval == gtk.keysyms.Return or event.keyval == gtk.keysyms.KP_Enter):
166             db_text = self.volume_digits.get_text()
167             try:
168                 db = float(db_text)
169                 #print "Volume digits confirmation \"%f dBFS\"" % db
170             except (ValueError), e:
171                 #print "Volume digits confirmation ignore, reset to current"
172                 self.update_volume(False)
173                 return
174             self.slider_adjustment.set_value_db(db)
175             #self.grab_focus()
176             #self.update_volume(False)   # We want to update gui even if actual decibels have not changed (scale wrap for example)
177
178     def on_volume_digits_focus_out(self, widget, event):
179         #print "volume digits focus out detected"
180         self.update_volume(False)
181
182     def read_meter(self):
183         if self.stereo:
184             meter_left, meter_right = self.channel.meter
185             self.meter.set_values(meter_left, meter_right)
186         else:
187             self.meter.set_value(self.channel.meter[0])
188
189         self.abspeak.set_peak(self.channel.abspeak)
190
191     def on_scroll(self, widget, event):
192         if event.direction == gtk.gdk.SCROLL_DOWN:
193             self.slider_adjustment.step_down()
194         elif event.direction == gtk.gdk.SCROLL_UP:
195             self.slider_adjustment.step_up()
196         return True
197
198     def update_volume(self, update_engine):
199         db = self.slider_adjustment.get_value_db()
200
201         db_text = "%.2f" % db
202         self.volume_digits.set_text(db_text)
203
204         if update_engine:
205             #print "Setting engine volume to " + db_text
206             self.channel.volume = db
207             self.app.update_monitor(self)
208
209     def on_volume_changed(self, adjustment):
210         self.update_volume(True)
211
212     def on_balance_changed(self, adjustment):
213         balance = self.balance_adjustment.get_value()
214         #print "%s balance: %f" % (self.channel_name, balance)
215         self.channel.balance = balance
216         self.app.update_monitor(self)
217
218     def on_key_pressed(self, widget, event):
219         if (event.keyval == gtk.keysyms.Up):
220             #print self.channel_name + " Up"
221             self.slider_adjustment.step_up()
222             return True
223         elif (event.keyval == gtk.keysyms.Down):
224             #print self.channel_name + " Down"
225             self.slider_adjustment.step_down()
226             return True
227
228         return False
229
230     def serialize(self, object_backend):
231         object_backend.add_property("volume", "%f" % self.slider_adjustment.get_value_db())
232         object_backend.add_property("balance", "%f" % self.balance_adjustment.get_value())
233
234         if self.channel.volume_midi_cc:
235             object_backend.add_property('volume_midi_cc', str(self.channel.volume_midi_cc))
236         if self.channel.balance_midi_cc:
237             object_backend.add_property('balance_midi_cc', str(self.channel.balance_midi_cc))
238
239     def unserialize_property(self, name, value):
240         if name == "volume":
241             self.slider_adjustment.set_value_db(float(value))
242             return True
243         if name == "balance":
244             self.balance_adjustment.set_value(float(value))
245             return True
246         if name == 'volume_midi_cc':
247             self.future_volume_midi_cc = int(value)
248             return True
249         if name == 'balance_midi_cc':
250             self.future_balance_midi_cc = int(value)
251             return True
252         return False
253
254     def on_midi_event_received(self, *args):
255         self.slider_adjustment.set_value_db(self.channel.volume)
256         self.balance_adjustment.set_value(self.channel.balance)
257
258     def midi_change_callback(self, *args):
259         # the changes are not applied directly to the widgets as they
260         # absolutely have to be done from the gtk thread.
261         self.emit('midi-event-received')
262
263     def on_monitor_button_toggled(self, button):
264         if not button.get_active():
265             self.app.main_mix.monitor_button.set_active(True)
266         else:
267             for channel in self.app.channels + self.app.output_channels + [self.app.main_mix]:
268                 if channel.monitor_button.get_active() and channel.monitor_button is not button:
269                     channel.monitor_button.handler_block_by_func(
270                                 channel.on_monitor_button_toggled)
271                     channel.monitor_button.set_active(False)
272                     channel.monitor_button.handler_unblock_by_func(
273                                 channel.on_monitor_button_toggled)
274             self.app.set_monitored_channel(self)
275
276     def set_monitored(self):
277         if self.channel:
278             self.app.set_monitored_channel(self)
279         self.monitor_button.set_active(True)
280
281 gobject.signal_new('midi-event-received', channel,
282                 gobject.SIGNAL_RUN_FIRST | gobject.SIGNAL_ACTION,
283                 gobject.TYPE_NONE, ())
284
285 class input_channel(channel):
286     def __init__(self, app, name, stereo):
287         channel.__init__(self, app, name, stereo)
288
289     def realize(self):
290         self.channel = self.mixer.add_channel(self.channel_name, self.stereo)
291         if self.channel == None:
292             raise Exception,"Cannot create a channel"
293         channel.realize(self)
294         if self.future_volume_midi_cc:
295             self.channel.volume_midi_cc = self.future_volume_midi_cc
296         if self.future_balance_midi_cc:
297             self.channel.balance_midi_cc = self.future_balance_midi_cc
298         self.channel.midi_scale = self.slider_scale.scale
299         self.channel.midi_change_callback = self.midi_change_callback
300
301         self.on_volume_changed(self.slider_adjustment)
302         self.on_balance_changed(self.balance_adjustment)
303
304         # vbox child at upper part
305         self.vbox = gtk.VBox()
306         self.pack_start(self.vbox, False)
307         self.label_name = gtk.Label()
308         self.label_name.set_text(self.channel_name)
309         self.label_name.set_size_request(0, -1)
310         self.label_name_event_box = gtk.EventBox()
311         self.label_name_event_box.connect("button-press-event", self.on_label_mouse)
312         self.label_name_event_box.add(self.label_name)
313         self.vbox.pack_start(self.label_name_event_box, True)
314 #         self.label_stereo = gtk.Label()
315 #         if self.stereo:
316 #             self.label_stereo.set_text("stereo")
317 #         else:
318 #             self.label_stereo.set_text("mono")
319 #         self.label_stereo.set_size_request(0, -1)
320 #         self.vbox.pack_start(self.label_stereo, True)
321
322         # hbox for mute and solo buttons
323         self.hbox_mutesolo = gtk.HBox()
324
325         self.mute = gtk.ToggleButton()
326         self.mute.set_label("M")
327         self.mute.set_active(self.channel.mute)
328         self.mute.connect("button-press-event", self.on_mute_button_pressed)
329         self.mute.connect("toggled", self.on_mute_toggled)
330         self.hbox_mutesolo.pack_start(self.mute, True)
331
332         self.solo = gtk.ToggleButton()
333         self.solo.set_label("S")
334         self.solo.set_active(self.channel.solo)
335         self.solo.connect("button-press-event", self.on_solo_button_pressed)
336         self.solo.connect("toggled", self.on_solo_toggled)
337         self.hbox_mutesolo.pack_start(self.solo, True)
338
339         self.vbox.pack_start(self.hbox_mutesolo, False)
340
341         frame = gtk.Frame()
342         frame.set_shadow_type(gtk.SHADOW_IN)
343         frame.add(self.abspeak);
344         self.pack_start(frame, False)
345
346         # hbox child at lower part
347         self.hbox = gtk.HBox()
348         self.hbox.pack_start(self.slider, True)
349         frame = gtk.Frame()
350         frame.set_shadow_type(gtk.SHADOW_IN)
351         frame.add(self.meter);
352         self.hbox.pack_start(frame, True)
353         frame = gtk.Frame()
354         frame.set_shadow_type(gtk.SHADOW_IN)
355         frame.add(self.hbox);
356         self.pack_start(frame, True)
357
358         self.volume_digits.set_size_request(0, -1)
359         self.pack_start(self.volume_digits, False)
360
361         self.create_balance_widget()
362
363         self.monitor_button = gtk.ToggleButton('MON')
364         self.monitor_button.connect('toggled', self.on_monitor_button_toggled)
365         self.pack_start(self.monitor_button, False, False)
366
367     def add_control_group(self, channel):
368         control_group = ControlGroup(channel, self)
369         control_group.show_all()
370         self.vbox.pack_start(control_group, False)
371         return control_group
372
373     def update_control_group(self, channel):
374         for control_group in self.vbox.get_children():
375             if isinstance(control_group, ControlGroup):
376                 if control_group.output_channel is channel:
377                     control_group.update()
378
379     def get_control_group(self, channel):
380         for control_group in self.vbox.get_children():
381             if isinstance(control_group, ControlGroup):
382                 if control_group.output_channel is channel:
383                     return control_group
384         return None
385
386     def unrealize(self):
387         channel.unrealize(self)
388         self.channel.remove()
389         self.channel = False
390
391     channel_properties_dialog = None
392     def on_channel_properties(self):
393         if not self.channel_properties_dialog:
394             self.channel_properties_dialog = ChannelPropertiesDialog(self, self.app)
395         self.channel_properties_dialog.show()
396         self.channel_properties_dialog.present()
397
398     def on_label_mouse(self, widget, event):
399         if event.type == gtk.gdk._2BUTTON_PRESS:
400             if event.button == 1:
401                 self.on_channel_properties()
402
403     def on_mute_toggled(self, button):
404         self.channel.mute = self.mute.get_active()
405         self.app.update_monitor(self.app.main_mix)
406
407     def on_mute_button_pressed(self, button, event, *args):
408         if event.button == 3:
409             # right click on the mute button, act on all output channels
410             if button.get_active(): # was muted
411                 button.set_active(False)
412                 if hasattr(button, 'touched_channels'):
413                     touched_channels = button.touched_channels
414                     for chan in touched_channels:
415                         ctlgroup = self.get_control_group(chan)
416                         ctlgroup.mute.set_active(False)
417                     del button.touched_channels
418             else: # was not muted
419                 button.set_active(True)
420                 touched_channels = []
421                 for chan in self.app.output_channels:
422                     ctlgroup = self.get_control_group(chan)
423                     if not ctlgroup.mute.get_active():
424                         ctlgroup.mute.set_active(True)
425                         touched_channels.append(chan)
426                 button.touched_channels = touched_channels
427             return True
428         return False
429
430     def on_solo_toggled(self, button):
431         self.channel.solo = self.solo.get_active()
432         self.app.update_monitor(self.app.main_mix)
433
434     def on_solo_button_pressed(self, button, event, *args):
435         if event.button == 3:
436             # right click on the solo button, act on all output channels
437             if button.get_active(): # was soloed
438                 button.set_active(False)
439                 if hasattr(button, 'touched_channels'):
440                     touched_channels = button.touched_channels
441                     for chan in touched_channels:
442                         ctlgroup = self.get_control_group(chan)
443                         ctlgroup.solo.set_active(False)
444                     del button.touched_channels
445             else: # was not soloed
446                 button.set_active(True)
447                 touched_channels = []
448                 for chan in self.app.output_channels:
449                     ctlgroup = self.get_control_group(chan)
450                     if not ctlgroup.solo.get_active():
451                         ctlgroup.solo.set_active(True)
452                         touched_channels.append(chan)
453                 button.touched_channels = touched_channels
454             return True
455         return False
456
457     def serialization_name(self):
458         return input_channel_serialization_name()
459
460     def serialize(self, object_backend):
461         object_backend.add_property("name", self.channel_name)
462         if self.stereo:
463             object_backend.add_property("type", "stereo")
464         else:
465             object_backend.add_property("type", "mono")
466         channel.serialize(self, object_backend)
467
468     def unserialize_property(self, name, value):
469         if name == "name":
470             self.channel_name = str(value)
471             return True
472         if name == "type":
473             if value == "stereo":
474                 self.stereo = True
475                 return True
476             if value == "mono":
477                 self.stereo = False
478                 return True
479         return channel.unserialize_property(self, name, value)
480
481 def input_channel_serialization_name():
482     return "input_channel"
483
484
485 available_colours = [
486     ('#ef2929', '#cc0000', '#840000'),
487     ('#729fcf', '#3465a4', '#204a67'),
488     ('#8aa234', '#73d216', '#4e7a06'),
489     ('#fce84f', '#edd400', '#c48000'),
490     ('#fcaf3e', '#f57900', '#ae5c00'),
491     ('#ad7fa8', '#75507b', '#4c3556'),
492     ('#e9b96e', '#c17d11', '#6f4902'),
493 ]
494
495 class output_channel(channel):
496     colours = available_colours[:]
497     _display_solo_buttons = False
498
499     _init_muted_channels = None
500     _init_solo_channels = None
501
502     def __init__(self, app, name, stereo):
503         channel.__init__(self, app, name, stereo)
504
505     def get_display_solo_buttons(self):
506         return self._display_solo_buttons
507
508     def set_display_solo_buttons(self, value):
509         self._display_solo_buttons = value
510         # notifying control groups
511         for inputchannel in self.app.channels:
512             inputchannel.update_control_group(self)
513
514     display_solo_buttons = property(get_display_solo_buttons, set_display_solo_buttons)
515
516     def realize(self):
517         channel.realize(self)
518         self.channel = self.mixer.add_output_channel(self.channel_name, self.stereo)
519         if self.channel == None:
520             raise Exception,"Cannot create a channel"
521         channel.realize(self)
522
523         self.channel.midi_scale = self.slider_scale.scale
524         self.channel.midi_change_callback = self.midi_change_callback
525
526         self.on_volume_changed(self.slider_adjustment)
527         self.on_balance_changed(self.balance_adjustment)
528
529         # vbox child at upper part
530         self.vbox = gtk.VBox()
531         self.pack_start(self.vbox, False)
532         self.label_name = gtk.Label()
533         self.label_name.set_text(self.channel_name)
534         self.label_name.set_size_request(0, -1)
535         self.label_name_event_box = gtk.EventBox()
536         self.label_name_event_box.connect('button-press-event', self.on_label_mouse)
537         self.label_name_event_box.add(self.label_name)
538         if not self.colours:
539             self.colours = available_colours[:]
540         for color in self.colours:
541             self.color_tuple = [gtk.gdk.color_parse(color[x]) for x in range(3)]
542             self.colours.remove(color)
543             break
544         self.label_name_event_box.modify_bg(gtk.STATE_NORMAL, self.color_tuple[1])
545         self.vbox.pack_start(self.label_name_event_box, True)
546         frame = gtk.Frame()
547         frame.set_shadow_type(gtk.SHADOW_IN)
548         frame.add(self.abspeak);
549         self.vbox.pack_start(frame, False)
550
551         # hbox child at lower part
552         self.hbox = gtk.HBox()
553         self.hbox.pack_start(self.slider, True)
554         frame = gtk.Frame()
555         frame.set_shadow_type(gtk.SHADOW_IN)
556         frame.add(self.meter);
557         self.hbox.pack_start(frame, True)
558         frame = gtk.Frame()
559         frame.set_shadow_type(gtk.SHADOW_IN)
560         frame.add(self.hbox);
561         self.pack_start(frame, True)
562
563         self.volume_digits.set_size_request(0, -1)
564         self.pack_start(self.volume_digits, False)
565
566         self.create_balance_widget()
567
568         self.monitor_button = gtk.ToggleButton('MON')
569         self.monitor_button.connect('toggled', self.on_monitor_button_toggled)
570         self.pack_start(self.monitor_button, False, False)
571
572         # add control groups to the input channels, and initialize them
573         # appropriately
574         for input_channel in self.app.channels:
575             ctlgroup = input_channel.add_control_group(self)
576             if self._init_muted_channels and input_channel.channel.name in self._init_muted_channels:
577                 ctlgroup.mute.set_active(True)
578             if self._init_solo_channels and input_channel.channel.name in self._init_solo_channels:
579                 ctlgroup.solo.set_active(True)
580         self._init_muted_channels = None
581         self._init_solo_channels = None
582
583     channel_properties_dialog = None
584     def on_channel_properties(self):
585         if not self.channel_properties_dialog:
586             self.channel_properties_dialog = OutputChannelPropertiesDialog(self, self.app)
587         self.channel_properties_dialog.show()
588         self.channel_properties_dialog.present()
589
590     def on_label_mouse(self, widget, event):
591         if event.type == gtk.gdk._2BUTTON_PRESS:
592             if event.button == 1:
593                 self.on_channel_properties()
594
595     def unrealize(self):
596         channel.unrealize(self)
597         self.channel = False
598
599     def serialization_name(self):
600         return output_channel_serialization_name()
601
602     def serialize(self, object_backend):
603         object_backend.add_property("name", self.channel_name)
604         if self.stereo:
605             object_backend.add_property("type", "stereo")
606         else:
607             object_backend.add_property("type", "mono")
608         if self.display_solo_buttons:
609             object_backend.add_property("solo_buttons", "true")
610         muted_channels = []
611         solo_channels = []
612         for input_channel in self.app.channels:
613             if self.channel.is_muted(input_channel.channel):
614                 muted_channels.append(input_channel)
615             if self.channel.is_solo(input_channel.channel):
616                 solo_channels.append(input_channel)
617         if muted_channels:
618             object_backend.add_property('muted_channels', '|'.join([x.channel.name for x in muted_channels]))
619         if solo_channels:
620             object_backend.add_property('solo_channels', '|'.join([x.channel.name for x in solo_channels]))
621         channel.serialize(self, object_backend)
622
623     def unserialize_property(self, name, value):
624         if name == "name":
625             self.channel_name = str(value)
626             return True
627         if name == "type":
628             if value == "stereo":
629                 self.stereo = True
630                 return True
631             if value == "mono":
632                 self.stereo = False
633                 return True
634         if name == "solo_buttons":
635             if value == "true":
636                 self.display_solo_buttons = True
637                 return True
638         if name == 'muted_channels':
639             self._init_muted_channels = value.split('|')
640             return True
641         if name == 'solo_channels':
642             self._init_solo_channels = value.split('|')
643             return True
644         return channel.unserialize_property(self, name, value)
645
646 def output_channel_serialization_name():
647     return "output_channel"
648
649 class main_mix(channel):
650     _init_muted_channels = None
651     _init_solo_channels = None
652
653     def __init__(self, app):
654         channel.__init__(self, app, "MAIN", True)
655
656     def realize(self):
657         channel.realize(self)
658         self.channel = self.mixer.main_mix_channel
659         self.channel.midi_scale = self.slider_scale.scale
660         self.channel.midi_change_callback = self.midi_change_callback
661
662         self.on_volume_changed(self.slider_adjustment)
663         self.on_balance_changed(self.balance_adjustment)
664
665         # vbox child at upper part
666         self.vbox = gtk.VBox()
667         self.pack_start(self.vbox, False)
668         self.label_name = gtk.Label()
669         self.label_name.set_text(self.channel_name)
670         self.label_name.set_size_request(0, -1)
671         self.vbox.pack_start(self.label_name, False)
672         frame = gtk.Frame()
673         frame.set_shadow_type(gtk.SHADOW_IN)
674         frame.add(self.abspeak);
675         self.vbox.pack_start(frame, False)
676
677         # hbox child at lower part
678         self.hbox = gtk.HBox()
679         self.hbox.pack_start(self.slider, True)
680         frame = gtk.Frame()
681         frame.set_shadow_type(gtk.SHADOW_IN)
682         frame.add(self.meter);
683         self.hbox.pack_start(frame, True)
684         frame = gtk.Frame()
685         frame.set_shadow_type(gtk.SHADOW_IN)
686         frame.add(self.hbox);
687         self.pack_start(frame, True)
688
689         self.volume_digits.set_size_request(0, -1)
690         self.pack_start(self.volume_digits, False)
691
692         self.create_balance_widget()
693
694         self.monitor_button = gtk.ToggleButton('MON')
695         self.monitor_button.connect('toggled', self.on_monitor_button_toggled)
696         self.pack_start(self.monitor_button, False, False)
697
698         for input_channel in self.app.channels:
699             if self._init_muted_channels and input_channel.channel.name in self._init_muted_channels:
700                 input_channel.mute.set_active(True)
701             if self._init_solo_channels and input_channel.channel.name in self._init_solo_channels:
702                 input_channel.solo.set_active(True)
703         self._init_muted_channels = None
704         self._init_solo_channels = None
705
706     def unrealize(self):
707         channel.unrealize(self)
708         self.channel = False
709
710     def serialization_name(self):
711         return main_mix_serialization_name()
712
713     def serialize(self, object_backend):
714         muted_channels = []
715         solo_channels = []
716         for input_channel in self.app.channels:
717             if input_channel.channel.mute:
718                 muted_channels.append(input_channel)
719             if input_channel.channel.solo:
720                 solo_channels.append(input_channel)
721         if muted_channels:
722             object_backend.add_property('muted_channels', '|'.join([x.channel.name for x in muted_channels]))
723         if solo_channels:
724             object_backend.add_property('solo_channels', '|'.join([x.channel.name for x in solo_channels]))
725         channel.serialize(self, object_backend)
726
727     def unserialize_property(self, name, value):
728         if name == 'muted_channels':
729             self._init_muted_channels = value.split('|')
730             return True
731         if name == 'solo_channels':
732             self._init_solo_channels = value.split('|')
733             return True
734         return channel.unserialize_property(self, name, value)
735
736 def main_mix_serialization_name():
737     return "main_mix_channel"
738
739
740 class ChannelPropertiesDialog(gtk.Dialog):
741     channel = None
742
743     def __init__(self, parent, app):
744         self.channel = parent
745         self.app = app
746         self.mixer = self.channel.mixer
747         gtk.Dialog.__init__(self,
748                         'Channel "%s" Properties' % self.channel.channel_name,
749                         self.channel.gui_factory.topwindow)
750
751         self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
752         self.ok_button = self.add_button(gtk.STOCK_APPLY, gtk.RESPONSE_APPLY)
753
754         self.create_ui()
755         self.fill_ui()
756
757         self.connect('response', self.on_response_cb)
758         self.connect('delete-event', self.on_response_cb)
759
760     def create_frame(self, label, child):
761         frame = gtk.Frame('')
762         frame.set_border_width(3)
763         #frame.set_shadow_type(gtk.SHADOW_NONE)
764         frame.get_label_widget().set_markup('<b>%s</b>' % label)
765
766         alignment = gtk.Alignment(0, 0, 1, 1)
767         alignment.set_padding(0, 0, 12, 0)
768         frame.add(alignment)
769         alignment.add(child)
770
771         return frame
772
773     def create_ui(self):
774         vbox = gtk.VBox()
775         self.vbox.add(vbox)
776
777         table = gtk.Table(2, 2, False)
778         vbox.pack_start(self.create_frame('Properties', table))
779         table.set_row_spacings(5)
780         table.set_col_spacings(5)
781
782         table.attach(gtk.Label('Name'), 0, 1, 0, 1)
783         self.entry_name = gtk.Entry()
784         self.entry_name.set_activates_default(True)
785         self.entry_name.connect('changed', self.on_entry_name_changed)
786         table.attach(self.entry_name, 1, 2, 0, 1)
787
788         table.attach(gtk.Label('Mode'), 0, 1, 1, 2)
789         self.mode_hbox = gtk.HBox()
790         table.attach(self.mode_hbox, 1, 2, 1, 2)
791         self.mono = gtk.RadioButton(label='Mono')
792         self.stereo = gtk.RadioButton(label='Stereo', group=self.mono)
793         self.mode_hbox.pack_start(self.mono)
794         self.mode_hbox.pack_start(self.stereo)
795
796         table = gtk.Table(2, 3, False)
797         vbox.pack_start(self.create_frame('MIDI Control Channels', table))
798         table.set_row_spacings(5)
799         table.set_col_spacings(5)
800
801         table.attach(gtk.Label('Volume'), 0, 1, 0, 1)
802         self.entry_volume_cc = gtk.Entry()
803         self.entry_volume_cc.set_activates_default(True)
804         self.entry_volume_cc.set_editable(False)
805         self.entry_volume_cc.set_width_chars(3)
806         table.attach(self.entry_volume_cc, 1, 2, 0, 1)
807         self.button_sense_midi_volume = gtk.Button('Autoset')
808         self.button_sense_midi_volume.connect('clicked',
809                         self.on_sense_midi_volume_clicked)
810         table.attach(self.button_sense_midi_volume, 2, 3, 0, 1)
811
812         table.attach(gtk.Label('Balance'), 0, 1, 1, 2)
813         self.entry_balance_cc = gtk.Entry()
814         self.entry_balance_cc.set_activates_default(True)
815         self.entry_balance_cc.set_width_chars(3)
816         self.entry_balance_cc.set_editable(False)
817         table.attach(self.entry_balance_cc, 1, 2, 1, 2)
818         self.button_sense_midi_balance = gtk.Button('Autoset')
819         self.button_sense_midi_balance.connect('clicked',
820                         self.on_sense_midi_balance_clicked)
821         table.attach(self.button_sense_midi_balance, 2, 3, 1, 2)
822
823         self.vbox.show_all()
824
825     def fill_ui(self):
826         self.entry_name.set_text(self.channel.channel_name)
827         if self.channel.channel.is_stereo:
828             self.stereo.set_active(True)
829         else:
830             self.mono.set_active(True)
831         self.mode_hbox.set_sensitive(False)
832         self.entry_volume_cc.set_text('%s' % self.channel.channel.volume_midi_cc)
833         self.entry_balance_cc.set_text('%s' % self.channel.channel.balance_midi_cc)
834
835     def sense_popup_dialog(self, entry):
836         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
837         window.set_destroy_with_parent(True)
838         window.set_transient_for(self)
839         window.set_decorated(False)
840         window.set_modal(True)
841         window.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
842         window.set_border_width(10)
843
844         vbox = gtk.VBox(10)
845         window.add(vbox)
846         window.timeout = 5
847         vbox.pack_start(gtk.Label('Please move the MIDI control you want to use for this function.'))
848         timeout_label = gtk.Label('This window will close in 5 seconds')
849         vbox.pack_start(timeout_label)
850         def close_sense_timeout(window, entry):
851             window.timeout -= 1
852             timeout_label.set_text('This window will close in %d seconds.' % window.timeout)
853             if window.timeout == 0:
854                 window.destroy()
855                 entry.set_text('%s' % self.mixer.last_midi_channel)
856                 return False
857             return True
858         window.show_all()
859         glib.timeout_add_seconds(1, close_sense_timeout, window, entry)
860
861     def on_sense_midi_volume_clicked(self, *args):
862         self.sense_popup_dialog(self.entry_volume_cc)
863
864     def on_sense_midi_balance_clicked(self, *args):
865         self.sense_popup_dialog(self.entry_balance_cc)
866
867     def on_response_cb(self, dlg, response_id, *args):
868         self.channel.channel_properties_dialog = None
869         self.destroy()
870         if response_id == gtk.RESPONSE_APPLY:
871             name = self.entry_name.get_text()
872             self.channel.channel_name = name
873             self.channel.channel.volume_midi_cc = int(self.entry_volume_cc.get_text())
874             self.channel.channel.balance_midi_cc = int(self.entry_balance_cc.get_text())
875
876     def on_entry_name_changed(self, entry):
877         sensitive = False
878         if len(entry.get_text()):
879             if self.channel and self.channel.channel.name == entry.get_text():
880                 sensitive = True
881             elif entry.get_text() not in [x.channel.name for x in self.app.channels] + \
882                         [x.channel.name for x in self.app.output_channels] + ['MAIN']:
883                 sensitive = True
884         self.ok_button.set_sensitive(sensitive)
885
886
887 class NewChannelDialog(ChannelPropertiesDialog):
888     def __init__(self, app):
889         gtk.Dialog.__init__(self, 'New Channel', app.window)
890         self.mixer = app.mixer
891         self.app = app
892         self.create_ui()
893
894         self.stereo.set_active(True) # default to stereo
895
896         self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
897         self.ok_button = self.add_button(gtk.STOCK_ADD, gtk.RESPONSE_OK)
898         self.ok_button.set_sensitive(False)
899         self.set_default_response(gtk.RESPONSE_OK);
900
901     def get_result(self):
902         return {'name': self.entry_name.get_text(),
903                 'stereo': self.stereo.get_active(),
904                 'volume_cc': self.entry_volume_cc.get_text(),
905                 'balance_cc': self.entry_balance_cc.get_text()
906                }
907
908 class OutputChannelPropertiesDialog(ChannelPropertiesDialog):
909     def create_ui(self):
910         ChannelPropertiesDialog.create_ui(self)
911
912         vbox = gtk.VBox()
913         self.vbox.pack_start(self.create_frame('Input Channels', vbox))
914
915         self.display_solo_buttons = gtk.CheckButton('Display solo buttons')
916         vbox.pack_start(self.display_solo_buttons)
917
918         self.vbox.show_all()
919
920     def fill_ui(self):
921         ChannelPropertiesDialog.fill_ui(self)
922         self.display_solo_buttons.set_active(self.channel.display_solo_buttons)
923
924     def on_response_cb(self, dlg, response_id, *args):
925         ChannelPropertiesDialog.on_response_cb(self, dlg, response_id, *args)
926         if response_id == gtk.RESPONSE_APPLY:
927             self.channel.display_solo_buttons = self.display_solo_buttons.get_active()
928
929
930 class NewOutputChannelDialog(OutputChannelPropertiesDialog):
931     def __init__(self, app):
932         gtk.Dialog.__init__(self, 'New Output Channel', app.window)
933         self.mixer = app.mixer
934         self.app = app
935         self.create_ui()
936
937         # TODO: disable mode for output channels as mono output channels may
938         # not be correctly handled yet.
939         self.mode_hbox.set_sensitive(False)
940         self.stereo.set_active(True) # default to stereo
941
942         self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
943         self.ok_button = self.add_button(gtk.STOCK_ADD, gtk.RESPONSE_OK)
944         self.ok_button.set_sensitive(False)
945         self.set_default_response(gtk.RESPONSE_OK);
946
947     def get_result(self):
948         return {'name': self.entry_name.get_text(),
949                 'stereo': self.stereo.get_active(),
950                 'volume_cc': self.entry_volume_cc.get_text(),
951                 'balance_cc': self.entry_balance_cc.get_text(),
952                 'display_solo_buttons': self.display_solo_buttons.get_active(),
953                }
954
955
956 class ControlGroup(gtk.Alignment):
957     def __init__(self, output_channel, input_channel):
958         gtk.Alignment.__init__(self, 0.5, 0.5, 0, 0)
959         self.output_channel = output_channel
960         self.input_channel = input_channel
961         self.app = input_channel.app
962
963         hbox = gtk.HBox()
964         self.hbox = hbox
965         self.add(hbox)
966
967         mute = gtk.ToggleButton()
968         self.mute = mute
969         mute.set_label("M")
970         mute.connect("toggled", self.on_mute_toggled)
971         hbox.pack_start(mute, False)
972
973         solo = gtk.ToggleButton()
974         self.solo = solo
975         solo.set_label("S")
976         solo.connect("toggled", self.on_solo_toggled)
977         if self.output_channel.display_solo_buttons:
978             hbox.pack_start(solo, True)
979
980         mute.modify_bg(gtk.STATE_PRELIGHT, output_channel.color_tuple[0])
981         mute.modify_bg(gtk.STATE_NORMAL, output_channel.color_tuple[1])
982         mute.modify_bg(gtk.STATE_ACTIVE, output_channel.color_tuple[2])
983         solo.modify_bg(gtk.STATE_PRELIGHT, output_channel.color_tuple[0])
984         solo.modify_bg(gtk.STATE_NORMAL, output_channel.color_tuple[1])
985         solo.modify_bg(gtk.STATE_ACTIVE, output_channel.color_tuple[2])
986
987     def update(self):
988         if self.output_channel.display_solo_buttons:
989             if not self.solo in self.hbox.get_children():
990                 self.hbox.pack_start(self.solo, True)
991                 self.solo.show()
992         else:
993             if self.solo in self.hbox.get_children():
994                 self.hbox.remove(self.solo)
995
996     def on_mute_toggled(self, button):
997         self.output_channel.channel.set_muted(self.input_channel.channel, button.get_active())
998         self.app.update_monitor(self)
999
1000     def on_solo_toggled(self, button):
1001         self.output_channel.channel.set_solo(self.input_channel.channel, button.get_active())
1002         self.app.update_monitor(self)