]> git.0d.be Git - jack_mixer.git/blob - channel.py
Remove shebang lines from Python plain modules
[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     def __init__(self, app, name, stereo):
288         Channel.__init__(self, app, name, stereo)
289
290     def realize(self):
291         self.channel = self.mixer.add_channel(self.channel_name, self.stereo)
292         if self.channel == None:
293             raise Exception,"Cannot create a channel"
294         Channel.realize(self)
295         if self.future_volume_midi_cc:
296             self.channel.volume_midi_cc = self.future_volume_midi_cc
297         if self.future_balance_midi_cc:
298             self.channel.balance_midi_cc = self.future_balance_midi_cc
299         self.channel.midi_scale = self.slider_scale.scale
300         self.channel.midi_change_callback = self.midi_change_callback
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         self.channel.remove()
394         self.channel = None
395
396     channel_properties_dialog = None
397     def on_channel_properties(self):
398         if not self.channel_properties_dialog:
399             self.channel_properties_dialog = ChannelPropertiesDialog(self, self.app)
400         self.channel_properties_dialog.show()
401         self.channel_properties_dialog.present()
402
403     def on_label_mouse(self, widget, event):
404         if event.type == gtk.gdk._2BUTTON_PRESS:
405             if event.button == 1:
406                 self.on_channel_properties()
407
408     def on_mute_toggled(self, button):
409         self.channel.mute = self.mute.get_active()
410         self.app.update_monitor(self.app.main_mix)
411
412     def on_mute_button_pressed(self, button, event, *args):
413         if event.button == 3:
414             # right click on the mute button, act on all output channels
415             if button.get_active(): # was muted
416                 button.set_active(False)
417                 if hasattr(button, 'touched_channels'):
418                     touched_channels = button.touched_channels
419                     for chan in touched_channels:
420                         ctlgroup = self.get_control_group(chan)
421                         ctlgroup.mute.set_active(False)
422                     del button.touched_channels
423             else: # was not muted
424                 button.set_active(True)
425                 touched_channels = []
426                 for chan in self.app.output_channels:
427                     ctlgroup = self.get_control_group(chan)
428                     if not ctlgroup.mute.get_active():
429                         ctlgroup.mute.set_active(True)
430                         touched_channels.append(chan)
431                 button.touched_channels = touched_channels
432             return True
433         return False
434
435     def on_solo_toggled(self, button):
436         self.channel.solo = self.solo.get_active()
437         self.app.update_monitor(self.app.main_mix)
438
439     def on_solo_button_pressed(self, button, event, *args):
440         if event.button == 3:
441             # right click on the solo button, act on all output channels
442             if button.get_active(): # was soloed
443                 button.set_active(False)
444                 if hasattr(button, 'touched_channels'):
445                     touched_channels = button.touched_channels
446                     for chan in touched_channels:
447                         ctlgroup = self.get_control_group(chan)
448                         ctlgroup.solo.set_active(False)
449                     del button.touched_channels
450             else: # was not soloed
451                 button.set_active(True)
452                 touched_channels = []
453                 for chan in self.app.output_channels:
454                     ctlgroup = self.get_control_group(chan)
455                     if not ctlgroup.solo.get_active():
456                         ctlgroup.solo.set_active(True)
457                         touched_channels.append(chan)
458                 button.touched_channels = touched_channels
459             return True
460         return False
461
462     def serialization_name(self):
463         return input_channel_serialization_name()
464
465     def serialize(self, object_backend):
466         object_backend.add_property("name", self.channel_name)
467         if self.stereo:
468             object_backend.add_property("type", "stereo")
469         else:
470             object_backend.add_property("type", "mono")
471         Channel.serialize(self, object_backend)
472
473     def unserialize_property(self, name, value):
474         if name == "name":
475             self.channel_name = str(value)
476             return True
477         if name == "type":
478             if value == "stereo":
479                 self.stereo = True
480                 return True
481             if value == "mono":
482                 self.stereo = False
483                 return True
484         return Channel.unserialize_property(self, name, value)
485
486 def input_channel_serialization_name():
487     return "input_channel"
488
489
490 available_colours = [
491     ('#ef2929', '#cc0000', '#840000'),
492     ('#729fcf', '#3465a4', '#204a67'),
493     ('#8aa234', '#73d216', '#4e7a06'),
494     ('#fce84f', '#edd400', '#c48000'),
495     ('#fcaf3e', '#f57900', '#ae5c00'),
496     ('#ad7fa8', '#75507b', '#4c3556'),
497     ('#e9b96e', '#c17d11', '#6f4902'),
498 ]
499
500 class OutputChannel(Channel):
501     colours = available_colours[:]
502     _display_solo_buttons = False
503
504     _init_muted_channels = None
505     _init_solo_channels = None
506
507     def __init__(self, app, name, stereo):
508         Channel.__init__(self, app, name, stereo)
509
510     def get_display_solo_buttons(self):
511         return self._display_solo_buttons
512
513     def set_display_solo_buttons(self, value):
514         self._display_solo_buttons = value
515         # notifying control groups
516         for inputchannel in self.app.channels:
517             inputchannel.update_control_group(self)
518
519     display_solo_buttons = property(get_display_solo_buttons, set_display_solo_buttons)
520
521     def realize(self):
522         Channel.realize(self)
523         self.channel = self.mixer.add_output_channel(self.channel_name, self.stereo)
524         if self.channel == None:
525             raise Exception,"Cannot create a channel"
526         Channel.realize(self)
527
528         self.channel.midi_scale = self.slider_scale.scale
529         self.channel.midi_change_callback = self.midi_change_callback
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     def serialization_name(self):
610         return output_channel_serialization_name()
611
612     def serialize(self, object_backend):
613         object_backend.add_property("name", self.channel_name)
614         if self.stereo:
615             object_backend.add_property("type", "stereo")
616         else:
617             object_backend.add_property("type", "mono")
618         if self.display_solo_buttons:
619             object_backend.add_property("solo_buttons", "true")
620         muted_channels = []
621         solo_channels = []
622         for input_channel in self.app.channels:
623             if self.channel.is_muted(input_channel.channel):
624                 muted_channels.append(input_channel)
625             if self.channel.is_solo(input_channel.channel):
626                 solo_channels.append(input_channel)
627         if muted_channels:
628             object_backend.add_property('muted_channels', '|'.join([x.channel.name for x in muted_channels]))
629         if solo_channels:
630             object_backend.add_property('solo_channels', '|'.join([x.channel.name for x in solo_channels]))
631         Channel.serialize(self, object_backend)
632
633     def unserialize_property(self, name, value):
634         if name == "name":
635             self.channel_name = str(value)
636             return True
637         if name == "type":
638             if value == "stereo":
639                 self.stereo = True
640                 return True
641             if value == "mono":
642                 self.stereo = False
643                 return True
644         if name == "solo_buttons":
645             if value == "true":
646                 self.display_solo_buttons = True
647                 return True
648         if name == 'muted_channels':
649             self._init_muted_channels = value.split('|')
650             return True
651         if name == 'solo_channels':
652             self._init_solo_channels = value.split('|')
653             return True
654         return Channel.unserialize_property(self, name, value)
655
656 def output_channel_serialization_name():
657     return "output_channel"
658
659 class MainMixChannel(Channel):
660     _init_muted_channels = None
661     _init_solo_channels = None
662
663     def __init__(self, app):
664         Channel.__init__(self, app, "MAIN", True)
665
666     def realize(self):
667         Channel.realize(self)
668         self.channel = self.mixer.main_mix_channel
669         self.channel.midi_scale = self.slider_scale.scale
670         self.channel.midi_change_callback = self.midi_change_callback
671
672         self.on_volume_changed(self.slider_adjustment)
673         self.on_balance_changed(self.balance_adjustment)
674
675         # vbox child at upper part
676         self.vbox = gtk.VBox()
677         self.pack_start(self.vbox, False)
678         self.label_name = gtk.Label()
679         self.label_name.set_text(self.channel_name)
680         self.label_name.set_size_request(0, -1)
681         self.vbox.pack_start(self.label_name, False)
682         frame = gtk.Frame()
683         frame.set_shadow_type(gtk.SHADOW_IN)
684         frame.add(self.abspeak);
685         self.vbox.pack_start(frame, False)
686
687         # hbox child at lower part
688         self.hbox = gtk.HBox()
689         self.hbox.pack_start(self.slider, True)
690         frame = gtk.Frame()
691         frame.set_shadow_type(gtk.SHADOW_IN)
692         frame.add(self.meter);
693         self.hbox.pack_start(frame, True)
694         frame = gtk.Frame()
695         frame.set_shadow_type(gtk.SHADOW_IN)
696         frame.add(self.hbox);
697         self.pack_start(frame, True)
698
699         self.volume_digits.set_size_request(0, -1)
700         self.pack_start(self.volume_digits, False)
701
702         self.create_balance_widget()
703
704         self.monitor_button = gtk.ToggleButton('MON')
705         self.monitor_button.connect('toggled', self.on_monitor_button_toggled)
706         self.pack_start(self.monitor_button, False, False)
707
708         for input_channel in self.app.channels:
709             if self._init_muted_channels and input_channel.channel.name in self._init_muted_channels:
710                 input_channel.mute.set_active(True)
711             if self._init_solo_channels and input_channel.channel.name in self._init_solo_channels:
712                 input_channel.solo.set_active(True)
713         self._init_muted_channels = None
714         self._init_solo_channels = None
715
716     def unrealize(self):
717         Channel.unrealize(self)
718         self.channel = False
719
720     def serialization_name(self):
721         return main_mix_serialization_name()
722
723     def serialize(self, object_backend):
724         muted_channels = []
725         solo_channels = []
726         for input_channel in self.app.channels:
727             if input_channel.channel.mute:
728                 muted_channels.append(input_channel)
729             if input_channel.channel.solo:
730                 solo_channels.append(input_channel)
731         if muted_channels:
732             object_backend.add_property('muted_channels', '|'.join([x.channel.name for x in muted_channels]))
733         if solo_channels:
734             object_backend.add_property('solo_channels', '|'.join([x.channel.name for x in solo_channels]))
735         Channel.serialize(self, object_backend)
736
737     def unserialize_property(self, name, value):
738         if name == 'muted_channels':
739             self._init_muted_channels = value.split('|')
740             return True
741         if name == 'solo_channels':
742             self._init_solo_channels = value.split('|')
743             return True
744         return Channel.unserialize_property(self, name, value)
745
746 def main_mix_serialization_name():
747     return "main_mix_channel"
748
749
750 class ChannelPropertiesDialog(gtk.Dialog):
751     channel = None
752
753     def __init__(self, parent, app):
754         self.channel = parent
755         self.app = app
756         self.mixer = self.channel.mixer
757         gtk.Dialog.__init__(self,
758                         'Channel "%s" Properties' % self.channel.channel_name,
759                         self.channel.gui_factory.topwindow)
760
761         self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
762         self.ok_button = self.add_button(gtk.STOCK_APPLY, gtk.RESPONSE_APPLY)
763
764         self.create_ui()
765         self.fill_ui()
766
767         self.connect('response', self.on_response_cb)
768         self.connect('delete-event', self.on_response_cb)
769
770     def create_frame(self, label, child):
771         frame = gtk.Frame('')
772         frame.set_border_width(3)
773         #frame.set_shadow_type(gtk.SHADOW_NONE)
774         frame.get_label_widget().set_markup('<b>%s</b>' % label)
775
776         alignment = gtk.Alignment(0, 0, 1, 1)
777         alignment.set_padding(0, 0, 12, 0)
778         frame.add(alignment)
779         alignment.add(child)
780
781         return frame
782
783     def create_ui(self):
784         vbox = gtk.VBox()
785         self.vbox.add(vbox)
786
787         table = gtk.Table(2, 2, False)
788         vbox.pack_start(self.create_frame('Properties', table))
789         table.set_row_spacings(5)
790         table.set_col_spacings(5)
791
792         table.attach(gtk.Label('Name'), 0, 1, 0, 1)
793         self.entry_name = gtk.Entry()
794         self.entry_name.set_activates_default(True)
795         self.entry_name.connect('changed', self.on_entry_name_changed)
796         table.attach(self.entry_name, 1, 2, 0, 1)
797
798         table.attach(gtk.Label('Mode'), 0, 1, 1, 2)
799         self.mode_hbox = gtk.HBox()
800         table.attach(self.mode_hbox, 1, 2, 1, 2)
801         self.mono = gtk.RadioButton(label='Mono')
802         self.stereo = gtk.RadioButton(label='Stereo', group=self.mono)
803         self.mode_hbox.pack_start(self.mono)
804         self.mode_hbox.pack_start(self.stereo)
805
806         table = gtk.Table(2, 3, False)
807         vbox.pack_start(self.create_frame('MIDI Control Channels', table))
808         table.set_row_spacings(5)
809         table.set_col_spacings(5)
810
811         table.attach(gtk.Label('Volume'), 0, 1, 0, 1)
812         self.entry_volume_cc = gtk.Entry()
813         self.entry_volume_cc.set_activates_default(True)
814         self.entry_volume_cc.set_editable(False)
815         self.entry_volume_cc.set_width_chars(3)
816         table.attach(self.entry_volume_cc, 1, 2, 0, 1)
817         self.button_sense_midi_volume = gtk.Button('Autoset')
818         self.button_sense_midi_volume.connect('clicked',
819                         self.on_sense_midi_volume_clicked)
820         table.attach(self.button_sense_midi_volume, 2, 3, 0, 1)
821
822         table.attach(gtk.Label('Balance'), 0, 1, 1, 2)
823         self.entry_balance_cc = gtk.Entry()
824         self.entry_balance_cc.set_activates_default(True)
825         self.entry_balance_cc.set_width_chars(3)
826         self.entry_balance_cc.set_editable(False)
827         table.attach(self.entry_balance_cc, 1, 2, 1, 2)
828         self.button_sense_midi_balance = gtk.Button('Autoset')
829         self.button_sense_midi_balance.connect('clicked',
830                         self.on_sense_midi_balance_clicked)
831         table.attach(self.button_sense_midi_balance, 2, 3, 1, 2)
832
833         self.vbox.show_all()
834
835     def fill_ui(self):
836         self.entry_name.set_text(self.channel.channel_name)
837         if self.channel.channel.is_stereo:
838             self.stereo.set_active(True)
839         else:
840             self.mono.set_active(True)
841         self.mode_hbox.set_sensitive(False)
842         self.entry_volume_cc.set_text('%s' % self.channel.channel.volume_midi_cc)
843         self.entry_balance_cc.set_text('%s' % self.channel.channel.balance_midi_cc)
844
845     def sense_popup_dialog(self, entry):
846         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
847         window.set_destroy_with_parent(True)
848         window.set_transient_for(self)
849         window.set_decorated(False)
850         window.set_modal(True)
851         window.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
852         window.set_border_width(10)
853
854         vbox = gtk.VBox(10)
855         window.add(vbox)
856         window.timeout = 5
857         vbox.pack_start(gtk.Label('Please move the MIDI control you want to use for this function.'))
858         timeout_label = gtk.Label('This window will close in 5 seconds')
859         vbox.pack_start(timeout_label)
860         def close_sense_timeout(window, entry):
861             window.timeout -= 1
862             timeout_label.set_text('This window will close in %d seconds.' % window.timeout)
863             if window.timeout == 0:
864                 window.destroy()
865                 entry.set_text('%s' % self.mixer.last_midi_channel)
866                 return False
867             return True
868         window.show_all()
869         glib.timeout_add_seconds(1, close_sense_timeout, window, entry)
870
871     def on_sense_midi_volume_clicked(self, *args):
872         self.sense_popup_dialog(self.entry_volume_cc)
873
874     def on_sense_midi_balance_clicked(self, *args):
875         self.sense_popup_dialog(self.entry_balance_cc)
876
877     def on_response_cb(self, dlg, response_id, *args):
878         self.channel.channel_properties_dialog = None
879         self.destroy()
880         if response_id == gtk.RESPONSE_APPLY:
881             name = self.entry_name.get_text()
882             self.channel.channel_name = name
883             self.channel.channel.volume_midi_cc = int(self.entry_volume_cc.get_text())
884             self.channel.channel.balance_midi_cc = int(self.entry_balance_cc.get_text())
885
886     def on_entry_name_changed(self, entry):
887         sensitive = False
888         if len(entry.get_text()):
889             if self.channel and self.channel.channel.name == entry.get_text():
890                 sensitive = True
891             elif entry.get_text() not in [x.channel.name for x in self.app.channels] + \
892                         [x.channel.name for x in self.app.output_channels] + ['MAIN']:
893                 sensitive = True
894         self.ok_button.set_sensitive(sensitive)
895
896
897 class NewChannelDialog(ChannelPropertiesDialog):
898     def __init__(self, app):
899         gtk.Dialog.__init__(self, 'New Channel', app.window)
900         self.mixer = app.mixer
901         self.app = app
902         self.create_ui()
903
904         self.stereo.set_active(True) # default to stereo
905
906         self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
907         self.ok_button = self.add_button(gtk.STOCK_ADD, gtk.RESPONSE_OK)
908         self.ok_button.set_sensitive(False)
909         self.set_default_response(gtk.RESPONSE_OK);
910
911     def get_result(self):
912         return {'name': self.entry_name.get_text(),
913                 'stereo': self.stereo.get_active(),
914                 'volume_cc': self.entry_volume_cc.get_text(),
915                 'balance_cc': self.entry_balance_cc.get_text()
916                }
917
918 class OutputChannelPropertiesDialog(ChannelPropertiesDialog):
919     def create_ui(self):
920         ChannelPropertiesDialog.create_ui(self)
921
922         vbox = gtk.VBox()
923         self.vbox.pack_start(self.create_frame('Input Channels', vbox))
924
925         self.display_solo_buttons = gtk.CheckButton('Display solo buttons')
926         vbox.pack_start(self.display_solo_buttons)
927
928         self.vbox.show_all()
929
930     def fill_ui(self):
931         ChannelPropertiesDialog.fill_ui(self)
932         self.display_solo_buttons.set_active(self.channel.display_solo_buttons)
933
934     def on_response_cb(self, dlg, response_id, *args):
935         ChannelPropertiesDialog.on_response_cb(self, dlg, response_id, *args)
936         if response_id == gtk.RESPONSE_APPLY:
937             self.channel.display_solo_buttons = self.display_solo_buttons.get_active()
938
939
940 class NewOutputChannelDialog(OutputChannelPropertiesDialog):
941     def __init__(self, app):
942         gtk.Dialog.__init__(self, 'New Output Channel', app.window)
943         self.mixer = app.mixer
944         self.app = app
945         self.create_ui()
946
947         # TODO: disable mode for output channels as mono output channels may
948         # not be correctly handled yet.
949         self.mode_hbox.set_sensitive(False)
950         self.stereo.set_active(True) # default to stereo
951
952         self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
953         self.ok_button = self.add_button(gtk.STOCK_ADD, gtk.RESPONSE_OK)
954         self.ok_button.set_sensitive(False)
955         self.set_default_response(gtk.RESPONSE_OK);
956
957     def get_result(self):
958         return {'name': self.entry_name.get_text(),
959                 'stereo': self.stereo.get_active(),
960                 'volume_cc': self.entry_volume_cc.get_text(),
961                 'balance_cc': self.entry_balance_cc.get_text(),
962                 'display_solo_buttons': self.display_solo_buttons.get_active(),
963                }
964
965
966 class ControlGroup(gtk.Alignment):
967     def __init__(self, output_channel, input_channel):
968         gtk.Alignment.__init__(self, 0.5, 0.5, 0, 0)
969         self.output_channel = output_channel
970         self.input_channel = input_channel
971         self.app = input_channel.app
972
973         hbox = gtk.HBox()
974         self.hbox = hbox
975         self.add(hbox)
976
977         mute = gtk.ToggleButton()
978         self.mute = mute
979         mute.set_label("M")
980         mute.connect("toggled", self.on_mute_toggled)
981         hbox.pack_start(mute, False)
982
983         solo = gtk.ToggleButton()
984         self.solo = solo
985         solo.set_label("S")
986         solo.connect("toggled", self.on_solo_toggled)
987         if self.output_channel.display_solo_buttons:
988             hbox.pack_start(solo, True)
989
990         mute.modify_bg(gtk.STATE_PRELIGHT, output_channel.color_tuple[0])
991         mute.modify_bg(gtk.STATE_NORMAL, output_channel.color_tuple[1])
992         mute.modify_bg(gtk.STATE_ACTIVE, output_channel.color_tuple[2])
993         solo.modify_bg(gtk.STATE_PRELIGHT, output_channel.color_tuple[0])
994         solo.modify_bg(gtk.STATE_NORMAL, output_channel.color_tuple[1])
995         solo.modify_bg(gtk.STATE_ACTIVE, output_channel.color_tuple[2])
996
997     def update(self):
998         if self.output_channel.display_solo_buttons:
999             if not self.solo in self.hbox.get_children():
1000                 self.hbox.pack_start(self.solo, True)
1001                 self.solo.show()
1002         else:
1003             if self.solo in self.hbox.get_children():
1004                 self.hbox.remove(self.solo)
1005
1006     def on_mute_toggled(self, button):
1007         self.output_channel.channel.set_muted(self.input_channel.channel, button.get_active())
1008         self.app.update_monitor(self)
1009
1010     def on_solo_toggled(self, button):
1011         self.output_channel.channel.set_solo(self.input_channel.channel, button.get_active())
1012         self.app.update_monitor(self)
1013