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