2 # -*- coding: utf-8 -*-
4 # This file is part of jack_mixer
6 # Copyright (C) 2006-2009 Nedko Arnaudov <nedko@arnaudov.name>
7 # Copyright (C) 2009 Frederic Peters <fpeters@0d.be>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; version 2 of the License
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 from optparse import OptionParser
34 print >> sys.stderr, "Cannot load LASH python bindings, you want them unless you enjoy manual jack plumbing each time you use this app"
36 # temporary change Python modules lookup path to look into installation
37 # directory ($prefix/share/jack_mixer/)
39 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..', 'share', 'jack_mixer'))
46 from preferences import PreferencesDialog
48 from serialization_xml import XmlSerialization
49 from serialization import SerializedObject, Serializator
51 # restore Python modules lookup path
54 class JackMixer(SerializedObject):
56 # scales suitable as meter scales
57 meter_scales = [scale.IEC268(), scale.Linear70dB(), scale.IEC268Minimalistic()]
59 # scales suitable as volume slider scales
60 slider_scales = [scale.Linear30dB(), scale.Linear70dB()]
62 # name of settngs file that is currently open
63 current_filename = None
65 def __init__(self, name, lash_client):
66 self.mixer = jack_mixer_c.Mixer(name)
69 self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
74 # Send our client name to server
75 lash_event = lash.lash_event_new_with_type(lash.LASH_Client_Name)
76 lash.lash_event_set_string(lash_event, name)
77 lash.lash_send_event(lash_client, lash_event)
79 lash.lash_jack_client_name(lash_client, name)
81 gtk.window_set_default_icon_name('jack_mixer')
83 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
84 self.window.set_title(name)
86 self.gui_factory = gui.Factory(self.window, self.meter_scales, self.slider_scales)
88 self.vbox_top = gtk.VBox()
89 self.window.add(self.vbox_top)
91 self.menubar = gtk.MenuBar()
92 self.vbox_top.pack_start(self.menubar, False)
94 mixer_menu_item = gtk.MenuItem("_Mixer")
95 self.menubar.append(mixer_menu_item)
96 edit_menu_item = gtk.MenuItem('_Edit')
97 self.menubar.append(edit_menu_item)
98 help_menu_item = gtk.MenuItem('_Help')
99 self.menubar.append(help_menu_item)
101 self.window.set_default_size(120, 300)
103 mixer_menu = gtk.Menu()
104 mixer_menu_item.set_submenu(mixer_menu)
106 add_input_channel = gtk.ImageMenuItem('New _Input Channel')
107 mixer_menu.append(add_input_channel)
108 add_input_channel.connect("activate", self.on_add_input_channel)
110 add_output_channel = gtk.ImageMenuItem('New _Output Channel')
111 mixer_menu.append(add_output_channel)
112 add_output_channel.connect("activate", self.on_add_output_channel)
114 mixer_menu.append(gtk.SeparatorMenuItem())
115 open = gtk.ImageMenuItem(gtk.STOCK_OPEN)
116 mixer_menu.append(open)
117 open.connect('activate', self.on_open_cb)
118 save = gtk.ImageMenuItem(gtk.STOCK_SAVE)
119 mixer_menu.append(save)
120 save.connect('activate', self.on_save_cb)
121 save_as = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
122 mixer_menu.append(save_as)
123 save_as.connect('activate', self.on_save_as_cb)
125 mixer_menu.append(gtk.SeparatorMenuItem())
127 quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
128 mixer_menu.append(quit)
129 quit.connect('activate', self.on_quit_cb)
131 edit_menu = gtk.Menu()
132 edit_menu_item.set_submenu(edit_menu)
134 self.channel_edit_input_menu_item = gtk.MenuItem('_Edit Input Channel')
135 edit_menu.append(self.channel_edit_input_menu_item)
136 self.channel_edit_input_menu = gtk.Menu()
137 self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
139 self.channel_edit_output_menu_item = gtk.MenuItem('Edit _Output Channel')
140 edit_menu.append(self.channel_edit_output_menu_item)
141 self.channel_edit_output_menu = gtk.Menu()
142 self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
144 self.channel_remove_input_menu_item = gtk.MenuItem('Remove _Input Channel')
145 edit_menu.append(self.channel_remove_input_menu_item)
146 self.channel_remove_input_menu = gtk.Menu()
147 self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
149 self.channel_remove_output_menu_item = gtk.MenuItem('_Remove Output Channel')
150 edit_menu.append(self.channel_remove_output_menu_item)
151 self.channel_remove_output_menu = gtk.Menu()
152 self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
154 channel_remove_all_menu_item = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
155 edit_menu.append(channel_remove_all_menu_item)
156 channel_remove_all_menu_item.connect("activate", self.on_channels_clear)
158 edit_menu.append(gtk.SeparatorMenuItem())
160 preferences = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
161 preferences.connect('activate', self.on_preferences_cb)
162 edit_menu.append(preferences)
164 help_menu = gtk.Menu()
165 help_menu_item.set_submenu(help_menu)
167 about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
168 help_menu.append(about)
169 about.connect("activate", self.on_about)
171 self.hbox_top = gtk.HBox()
172 self.vbox_top.pack_start(self.hbox_top, True)
174 self.scrolled_window = gtk.ScrolledWindow()
175 self.hbox_top.pack_start(self.scrolled_window, True)
177 self.hbox_inputs = gtk.HBox()
178 self.hbox_inputs.set_spacing(0)
179 self.hbox_inputs.set_border_width(0)
180 self.hbox_top.set_spacing(0)
181 self.hbox_top.set_border_width(0)
183 self.output_channels = []
185 self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
186 self.scrolled_window.add_with_viewport(self.hbox_inputs)
188 self.main_mix = MainMixChannel(self)
189 self.hbox_outputs = gtk.HBox()
190 self.hbox_outputs.set_spacing(0)
191 self.hbox_outputs.set_border_width(0)
193 frame.add(self.main_mix)
194 self.hbox_outputs.pack_start(frame, False)
195 self.hbox_top.pack_start(self.hbox_outputs, False)
197 self.window.connect("destroy", gtk.main_quit)
199 gobject.timeout_add(80, self.read_meters)
200 self.lash_client = lash_client
202 gobject.timeout_add(200, self.lash_check_events)
204 def sighandler(self, signum, frame):
205 #print "Signal %d received" % signum
206 if signum == signal.SIGUSR1:
208 elif signum == signal.SIGTERM:
210 elif signum == signal.SIGINT:
213 print "Unknown signal %d received" % signum
216 print "Cleaning jack_mixer"
220 for channel in self.channels:
223 def on_open_cb(self, *args):
224 dlg = gtk.FileChooserDialog(title='Open', parent=self.window,
225 action=gtk.FILE_CHOOSER_ACTION_OPEN,
226 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
227 gtk.STOCK_OPEN, gtk.RESPONSE_OK))
228 dlg.set_default_response(gtk.RESPONSE_OK)
229 if dlg.run() == gtk.RESPONSE_OK:
230 filename = dlg.get_filename()
232 f = file(filename, 'r')
233 self.load_from_xml(f)
235 err = gtk.MessageDialog(self.window,
239 "Failed loading settings.")
243 self.current_filename = filename
248 def on_save_cb(self, *args):
249 if not self.current_filename:
250 return self.on_save_as_cb()
251 f = file(self.current_filename, 'w')
255 def on_save_as_cb(self, *args):
256 dlg = gtk.FileChooserDialog(title='Save', parent=self.window,
257 action=gtk.FILE_CHOOSER_ACTION_SAVE,
258 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
259 gtk.STOCK_SAVE, gtk.RESPONSE_OK))
260 dlg.set_default_response(gtk.RESPONSE_OK)
261 if dlg.run() == gtk.RESPONSE_OK:
262 self.current_filename = dlg.get_filename()
266 def on_quit_cb(self, *args):
269 preferences_dialog = None
270 def on_preferences_cb(self, widget):
271 if not self.preferences_dialog:
272 self.preferences_dialog = PreferencesDialog(self)
273 self.preferences_dialog.show()
274 self.preferences_dialog.present()
276 def on_add_input_channel(self, widget):
277 dialog = NewChannelDialog(app=self)
278 dialog.set_transient_for(self.window)
283 if ret == gtk.RESPONSE_OK:
284 result = dialog.get_result()
285 channel = self.add_channel(**result)
286 self.window.show_all()
288 def on_add_output_channel(self, widget):
289 dialog = NewOutputChannelDialog(app=self)
290 dialog.set_transient_for(self.window)
295 if ret == gtk.RESPONSE_OK:
296 result = dialog.get_result()
297 channel = self.add_output_channel(**result)
298 self.window.show_all()
300 def on_edit_input_channel(self, widget, channel):
301 print 'Editing channel "%s"' % channel.channel_name
302 channel.on_channel_properties()
304 def remove_channel_edit_input_menuitem_by_label(self, widget, label):
305 if (widget.get_label() == label):
306 self.channel_edit_input_menu.remove(widget)
308 def on_remove_input_channel(self, widget, channel):
309 print 'Removing channel "%s"' % channel.channel_name
310 self.channel_remove_input_menu.remove(widget)
311 self.channel_edit_input_menu.foreach(
312 self.remove_channel_edit_input_menuitem_by_label,
313 channel.channel_name);
314 if self.monitored_channel is channel:
315 channel.monitor_button.set_active(False)
316 for i in range(len(self.channels)):
317 if self.channels[i] is channel:
320 self.hbox_inputs.remove(channel.parent)
322 if len(self.channels) == 0:
323 self.channel_remove_input_menu_item.set_sensitive(False)
325 def on_edit_output_channel(self, widget, channel):
326 print 'Editing channel "%s"' % channel.channel_name
327 channel.on_channel_properties()
329 def remove_channel_edit_output_menuitem_by_label(self, widget, label):
330 if (widget.get_label() == label):
331 self.channel_edit_output_menu.remove(widget)
333 def on_remove_output_channel(self, widget, channel):
334 print 'Removing channel "%s"' % channel.channel_name
335 self.channel_remove_output_menu.remove(widget)
336 self.channel_edit_output_menu.foreach(
337 self.remove_channel_edit_output_menuitem_by_label,
338 channel.channel_name);
339 if self.monitored_channel is channel:
340 channel.monitor_button.set_active(False)
341 for i in range(len(self.channels)):
342 if self.output_channels[i] is channel:
344 del self.output_channels[i]
345 self.hbox_outputs.remove(channel.parent)
347 if len(self.output_channels) == 0:
348 self.channel_remove_output_menu_item.set_sensitive(False)
350 def rename_channels(self, container, parameters):
351 if (container.get_label() == parameters['oldname']):
352 container.set_label(parameters['newname'])
354 def on_channel_rename(self, oldname, newname):
355 rename_parameters = { 'oldname' : oldname, 'newname' : newname }
356 self.channel_edit_input_menu.foreach(self.rename_channels,
358 self.channel_edit_output_menu.foreach(self.rename_channels,
360 self.channel_remove_input_menu.foreach(self.rename_channels,
362 self.channel_remove_output_menu.foreach(self.rename_channels,
364 print "Renaming channel from %s to %s\n" % (oldname, newname)
367 def on_channels_clear(self, widget):
368 for channel in self.output_channels:
370 self.hbox_outputs.remove(channel.parent)
371 for channel in self.channels:
373 self.hbox_inputs.remove(channel.parent)
375 self.output_channels = []
376 self.channel_edit_input_menu = gtk.Menu()
377 self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
378 self.channel_edit_input_menu_item.set_sensitive(False)
379 self.channel_remove_input_menu = gtk.Menu()
380 self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
381 self.channel_remove_input_menu_item.set_sensitive(False)
382 self.channel_edit_output_menu = gtk.Menu()
383 self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
384 self.channel_edit_output_menu_item.set_sensitive(False)
385 self.channel_remove_output_menu = gtk.Menu()
386 self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
387 self.channel_remove_output_menu_item.set_sensitive(False)
389 def add_channel(self, name, stereo, volume_cc, balance_cc):
391 channel = InputChannel(self, name, stereo)
392 self.add_channel_precreated(channel)
394 err = gtk.MessageDialog(self.window,
395 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
398 "Channel creation failed")
403 channel.channel.volume_midi_cc = int(volume_cc)
405 channel.channel.balance_midi_cc = int(balance_cc)
406 if not (volume_cc or balance_cc):
407 channel.channel.autoset_midi_cc()
411 def add_channel_precreated(self, channel):
414 self.hbox_inputs.pack_start(frame, False)
417 channel_edit_menu_item = gtk.MenuItem(channel.channel_name)
418 self.channel_edit_input_menu.append(channel_edit_menu_item)
419 channel_edit_menu_item.connect("activate", self.on_edit_input_channel, channel)
420 self.channel_edit_input_menu_item.set_sensitive(True)
422 channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
423 self.channel_remove_input_menu.append(channel_remove_menu_item)
424 channel_remove_menu_item.connect("activate", self.on_remove_input_channel, channel)
425 self.channel_remove_input_menu_item.set_sensitive(True)
427 self.channels.append(channel)
429 for outputchannel in self.output_channels:
430 channel.add_control_group(outputchannel)
432 # create post fader output channel matching the input channel
433 channel.post_fader_output_channel = self.mixer.add_output_channel(
434 channel.channel.name + ' Out', channel.channel.is_stereo, True)
435 channel.post_fader_output_channel.volume = 0
436 channel.post_fader_output_channel.set_solo(channel.channel, True)
438 def read_meters(self):
439 for channel in self.channels:
441 self.main_mix.read_meter()
442 for channel in self.output_channels:
446 def add_output_channel(self, name, stereo, volume_cc, balance_cc, display_solo_buttons):
448 channel = OutputChannel(self, name, stereo)
449 channel.display_solo_buttons = display_solo_buttons
450 self.add_output_channel_precreated(channel)
452 err = gtk.MessageDialog(self.window,
453 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
456 "Channel creation failed")
461 channel.channel.volume_midi_cc = int(volume_cc)
463 channel.channel.balance_midi_cc = int(balance_cc)
466 def add_output_channel_precreated(self, channel):
469 self.hbox_outputs.pack_start(frame, False)
472 channel_edit_menu_item = gtk.MenuItem(channel.channel_name)
473 self.channel_edit_output_menu.append(channel_edit_menu_item)
474 channel_edit_menu_item.connect("activate", self.on_edit_output_channel, channel)
475 self.channel_edit_output_menu_item.set_sensitive(True)
477 channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
478 self.channel_remove_output_menu.append(channel_remove_menu_item)
479 channel_remove_menu_item.connect("activate", self.on_remove_output_channel, channel)
480 self.channel_remove_output_menu_item.set_sensitive(True)
482 self.output_channels.append(channel)
484 _monitored_channel = None
485 def get_monitored_channel(self):
486 return self._monitored_channel
488 def set_monitored_channel(self, channel):
489 if self._monitored_channel:
490 if channel.channel.name == self._monitored_channel.channel.name:
492 self._monitored_channel = channel
493 if type(channel) is InputChannel:
494 # reset all solo/mute settings
495 for in_channel in self.channels:
496 self.monitor_channel.set_solo(in_channel.channel, False)
497 self.monitor_channel.set_muted(in_channel.channel, False)
498 self.monitor_channel.set_solo(channel.channel, True)
499 self.monitor_channel.prefader = True
501 self.monitor_channel.prefader = False
502 self.update_monitor(channel)
503 monitored_channel = property(get_monitored_channel, set_monitored_channel)
505 def update_monitor(self, channel):
506 if self.monitored_channel is not channel:
508 self.monitor_channel.volume = channel.channel.volume
509 self.monitor_channel.balance = channel.channel.balance
510 if type(self.monitored_channel) is OutputChannel:
511 # sync solo/muted channels
512 for input_channel in self.channels:
513 self.monitor_channel.set_solo(input_channel.channel,
514 channel.channel.is_solo(input_channel.channel))
515 self.monitor_channel.set_muted(input_channel.channel,
516 channel.channel.is_muted(input_channel.channel))
517 elif type(self.monitored_channel) is MainMixChannel:
518 # sync solo/muted channels
519 for input_channel in self.channels:
520 self.monitor_channel.set_solo(input_channel.channel,
521 input_channel.channel.solo)
522 self.monitor_channel.set_muted(input_channel.channel,
523 input_channel.channel.mute)
525 def get_input_channel_by_name(self, name):
526 for input_channel in self.channels:
527 if input_channel.channel.name == name:
531 def on_about(self, *args):
532 about = gtk.AboutDialog()
533 about.set_name('jack_mixer')
534 about.set_copyright('Copyright © 2006-2009\nNedko Arnaudov, Frederic Peters')
535 about.set_license('''\
536 jack_mixer is free software; you can redistribute it and/or modify it
537 under the terms of the GNU General Public License as published by the
538 Free Software Foundation; either version 2 of the License, or (at your
539 option) any later version.
541 jack_mixer is distributed in the hope that it will be useful, but
542 WITHOUT ANY WARRANTY; without even the implied warranty of
543 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
544 General Public License for more details.
546 You should have received a copy of the GNU General Public License along
547 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
548 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
549 about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
550 'Frederic Peters <fpeters@0d.be>'])
551 about.set_logo_icon_name('jack_mixer')
552 about.set_website('http://home.gna.org/jackmixer/')
557 def lash_check_events(self):
560 if self.current_filename:
561 print "saving on SIGUSR1 request"
565 print "not saving because filename is not known"
568 if not self.lash_client:
571 while lash.lash_get_pending_event_count(self.lash_client):
572 event = lash.lash_get_event(self.lash_client)
576 event_type = lash.lash_event_get_type(event)
577 if event_type == lash.LASH_Quit:
578 print "jack_mixer: LASH ordered quit."
581 elif event_type == lash.LASH_Save_File:
582 directory = lash.lash_event_get_string(event)
583 print "jack_mixer: LASH ordered to save data in directory %s" % directory
584 filename = directory + os.sep + "jack_mixer.xml"
585 f = file(filename, "w")
588 lash.lash_send_event(self.lash_client, event) # we crash with double free
589 elif event_type == lash.LASH_Restore_File:
590 directory = lash.lash_event_get_string(event)
591 print "jack_mixer: LASH ordered to restore data from directory %s" % directory
592 filename = directory + os.sep + "jack_mixer.xml"
593 f = file(filename, "r")
594 self.load_from_xml(f, silence_errors=True)
596 lash.lash_send_event(self.lash_client, event)
598 print "jack_mixer: Got unhandled LASH event, type " + str(event_type)
601 #lash.lash_event_destroy(event)
605 def save_to_xml(self, file):
606 #print "Saving to XML..."
607 b = XmlSerialization()
612 def load_from_xml(self, file, silence_errors=False):
613 #print "Loading from XML..."
614 self.on_channels_clear(None)
615 self.unserialized_channels = []
616 b = XmlSerialization()
624 s.unserialize(self, b)
625 for channel in self.unserialized_channels:
626 if isinstance(channel, InputChannel):
627 self.add_channel_precreated(channel)
628 for channel in self.unserialized_channels:
629 if isinstance(channel, OutputChannel):
630 self.add_output_channel_precreated(channel)
631 del self.unserialized_channels
632 self.window.show_all()
634 def serialize(self, object_backend):
635 object_backend.add_property('geometry',
636 '%sx%s' % (self.window.allocation.width, self.window.allocation.height))
638 def unserialize_property(self, name, value):
639 if name == 'geometry':
640 width, height = value.split('x')
641 self.window.resize(int(width), int(height))
644 def unserialize_child(self, name):
645 if name == MainMixChannel.serialization_name():
648 if name == InputChannel.serialization_name():
649 channel = InputChannel(self, "", True)
650 self.unserialized_channels.append(channel)
653 if name == OutputChannel.serialization_name():
654 channel = OutputChannel(self, "", True)
655 self.unserialized_channels.append(channel)
658 def serialization_get_childs(self):
659 '''Get child objects tha required and support serialization'''
660 childs = self.channels[:] + self.output_channels[:]
661 childs.append(self.main_mix)
664 def serialization_name(self):
668 self.main_mix.realize()
669 self.main_mix.set_monitored()
674 self.window.show_all()
676 signal.signal(signal.SIGUSR1, self.sighandler)
677 signal.signal(signal.SIGTERM, self.sighandler)
678 signal.signal(signal.SIGINT, self.sighandler)
682 #f = file("/dev/stdout", "w")
687 print "Usage: %s [mixer_name]" % sys.argv[0]
690 # Connect to LASH if Python bindings are available, and the user did not
692 if lash and not '--no-lash' in sys.argv:
693 # sys.argv is modified by this call
694 lash_client = lash.init(sys.argv, "jack_mixer", lash.LASH_Config_File)
698 parser = OptionParser()
699 parser.add_option('-c', '--config', dest='config',
700 help='use a non default configuration file')
701 # --no-lash here is not acted upon, it is specified for completeness when
703 parser.add_option('--no-lash', dest='nolash', action='store_true',
704 help='do not connect to LASH')
705 options, args = parser.parse_args()
707 # Yeah , this sounds stupid, we connected earlier, but we dont want to show this if we got --help option
708 # This issue should be fixed in pylash, there is a reason for having two functions for initialization after all
710 print "Successfully connected to LASH server at " + lash.lash_get_server_name(lash_client)
720 gtk.gdk.threads_init() # if this function is called, when SIGUSR1 is received, we enter tight loop...
722 mixer = JackMixer(name, lash_client)
724 err = gtk.MessageDialog(None,
728 "Mixer creation failed (%s)" % str(e))
734 f = file(options.config)
735 mixer.current_filename = options.config
737 mixer.load_from_xml(f)
739 err = gtk.MessageDialog(mixer.window,
743 "Failed loading settings.")
746 mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
753 if __name__ == "__main__":