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