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_remove_input_menu_item = gtk.MenuItem('Remove Input Channel')
135 edit_menu.append(self.channel_remove_input_menu_item)
136 self.channel_remove_input_menu = gtk.Menu()
137 self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
139 self.channel_remove_output_menu_item = gtk.MenuItem('Remove Output Channel')
140 edit_menu.append(self.channel_remove_output_menu_item)
141 self.channel_remove_output_menu = gtk.Menu()
142 self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
144 channel_remove_all_menu_item = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
145 edit_menu.append(channel_remove_all_menu_item)
146 channel_remove_all_menu_item.connect("activate", self.on_channels_clear)
148 edit_menu.append(gtk.SeparatorMenuItem())
150 preferences = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
151 preferences.connect('activate', self.on_preferences_cb)
152 edit_menu.append(preferences)
154 help_menu = gtk.Menu()
155 help_menu_item.set_submenu(help_menu)
157 about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
158 help_menu.append(about)
159 about.connect("activate", self.on_about)
161 self.hbox_top = gtk.HBox()
162 self.vbox_top.pack_start(self.hbox_top, True)
164 self.scrolled_window = gtk.ScrolledWindow()
165 self.hbox_top.pack_start(self.scrolled_window, True)
167 self.hbox_inputs = gtk.HBox()
168 self.hbox_inputs.set_spacing(0)
169 self.hbox_inputs.set_border_width(0)
170 self.hbox_top.set_spacing(0)
171 self.hbox_top.set_border_width(0)
173 self.output_channels = []
175 self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
176 self.scrolled_window.add_with_viewport(self.hbox_inputs)
178 self.main_mix = MainMixChannel(self)
179 self.hbox_outputs = gtk.HBox()
180 self.hbox_outputs.set_spacing(0)
181 self.hbox_outputs.set_border_width(0)
183 frame.add(self.main_mix)
184 self.hbox_outputs.pack_start(frame, False)
185 self.hbox_top.pack_start(self.hbox_outputs, False)
187 self.window.connect("destroy", gtk.main_quit)
189 gobject.timeout_add(80, self.read_meters)
190 self.lash_client = lash_client
192 gobject.timeout_add(200, self.lash_check_events)
194 def sighandler(self, signum, frame):
195 print "Signal %d received" % signum
196 if signum == signal.SIGUSR1:
198 elif signum == signal.SIGTERM:
200 elif signum == signal.SIGINT:
203 print "Unknown signal %d received" % signum
206 print "Cleaning jack_mixer"
210 for channel in self.channels:
213 def on_open_cb(self, *args):
214 dlg = gtk.FileChooserDialog(title='Open', parent=self.window,
215 action=gtk.FILE_CHOOSER_ACTION_OPEN,
216 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
217 gtk.STOCK_OPEN, gtk.RESPONSE_OK))
218 dlg.set_default_response(gtk.RESPONSE_OK)
219 if dlg.run() == gtk.RESPONSE_OK:
220 filename = dlg.get_filename()
222 f = file(filename, 'r')
223 self.load_from_xml(f)
225 err = gtk.MessageDialog(self.window,
229 "Failed loading settings.")
233 self.current_filename = filename
238 def on_save_cb(self, *args):
239 if not self.current_filename:
240 return self.on_save_as_cb()
241 f = file(self.current_filename, 'w')
245 def on_save_as_cb(self, *args):
246 dlg = gtk.FileChooserDialog(title='Save', parent=self.window,
247 action=gtk.FILE_CHOOSER_ACTION_SAVE,
248 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
249 gtk.STOCK_SAVE, gtk.RESPONSE_OK))
250 dlg.set_default_response(gtk.RESPONSE_OK)
251 if dlg.run() == gtk.RESPONSE_OK:
252 self.current_filename = dlg.get_filename()
256 def on_quit_cb(self, *args):
259 preferences_dialog = None
260 def on_preferences_cb(self, widget):
261 if not self.preferences_dialog:
262 self.preferences_dialog = PreferencesDialog(self)
263 self.preferences_dialog.show()
264 self.preferences_dialog.present()
266 def on_add_input_channel(self, widget):
267 dialog = NewChannelDialog(app=self)
268 dialog.set_transient_for(self.window)
273 if ret == gtk.RESPONSE_OK:
274 result = dialog.get_result()
275 channel = self.add_channel(**result)
276 self.window.show_all()
278 def on_add_output_channel(self, widget):
279 dialog = NewOutputChannelDialog(app=self)
280 dialog.set_transient_for(self.window)
285 if ret == gtk.RESPONSE_OK:
286 result = dialog.get_result()
287 channel = self.add_output_channel(**result)
288 self.window.show_all()
290 def on_remove_input_channel(self, widget, channel):
291 print 'Removing channel "%s"' % channel.channel_name
292 self.channel_remove_input_menu.remove(widget)
293 if self.monitored_channel is channel:
294 channel.monitor_button.set_active(False)
295 for i in range(len(self.channels)):
296 if self.channels[i] is channel:
299 self.hbox_inputs.remove(channel.parent)
301 if len(self.channels) == 0:
302 self.channel_remove_input_menu_item.set_sensitive(False)
304 def on_remove_output_channel(self, widget, channel):
305 print 'Removing channel "%s"' % channel.channel_name
306 self.channel_remove_output_menu.remove(widget)
307 if self.monitored_channel is channel:
308 channel.monitor_button.set_active(False)
309 for i in range(len(self.channels)):
310 if self.output_channels[i] is channel:
312 del self.output_channels[i]
313 self.hbox_outputs.remove(channel.parent)
315 if len(self.output_channels) == 0:
316 self.channel_remove_output_menu_item.set_sensitive(False)
318 def on_channels_clear(self, widget):
319 for channel in self.output_channels:
321 self.hbox_outputs.remove(channel.parent)
322 for channel in self.channels:
324 self.hbox_inputs.remove(channel.parent)
326 self.output_channels = []
327 self.channel_remove_input_menu = gtk.Menu()
328 self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
329 self.channel_remove_input_menu_item.set_sensitive(False)
330 self.channel_remove_output_menu = gtk.Menu()
331 self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
332 self.channel_remove_output_menu_item.set_sensitive(False)
334 def add_channel(self, name, stereo, volume_cc, balance_cc):
336 channel = InputChannel(self, name, stereo)
337 self.add_channel_precreated(channel)
339 err = gtk.MessageDialog(self.window,
340 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
343 "Channel creation failed")
348 channel.channel.volume_midi_cc = int(volume_cc)
350 channel.channel.balance_midi_cc = int(balance_cc)
351 if not (volume_cc or balance_cc):
352 channel.channel.autoset_midi_cc()
356 def add_channel_precreated(self, channel):
359 self.hbox_inputs.pack_start(frame, False)
361 channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
362 self.channel_remove_input_menu.append(channel_remove_menu_item)
363 channel_remove_menu_item.connect("activate", self.on_remove_input_channel, channel)
364 self.channel_remove_input_menu_item.set_sensitive(True)
365 self.channels.append(channel)
367 for outputchannel in self.output_channels:
368 channel.add_control_group(outputchannel)
370 # create post fader output channel matching the input channel
371 channel.post_fader_output_channel = self.mixer.add_output_channel(
372 channel.channel.name + ' Out', channel.channel.is_stereo, True)
373 channel.post_fader_output_channel.volume = 0
374 channel.post_fader_output_channel.set_solo(channel.channel, True)
376 def read_meters(self):
377 for channel in self.channels:
379 self.main_mix.read_meter()
380 for channel in self.output_channels:
384 def add_output_channel(self, name, stereo, volume_cc, balance_cc, display_solo_buttons):
386 channel = OutputChannel(self, name, stereo)
387 channel.display_solo_buttons = display_solo_buttons
388 self.add_output_channel_precreated(channel)
390 err = gtk.MessageDialog(self.window,
391 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
394 "Channel creation failed")
399 channel.channel.volume_midi_cc = int(volume_cc)
401 channel.channel.balance_midi_cc = int(balance_cc)
404 def add_output_channel_precreated(self, channel):
407 self.hbox_outputs.pack_start(frame, False)
409 channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
410 self.channel_remove_output_menu.append(channel_remove_menu_item)
411 channel_remove_menu_item.connect("activate", self.on_remove_output_channel, channel)
412 self.channel_remove_output_menu_item.set_sensitive(True)
413 self.output_channels.append(channel)
415 _monitored_channel = None
416 def get_monitored_channel(self):
417 return self._monitored_channel
419 def set_monitored_channel(self, channel):
420 if self._monitored_channel:
421 if channel.channel.name == self._monitored_channel.channel.name:
423 self._monitored_channel = channel
424 if type(channel) is InputChannel:
425 # reset all solo/mute settings
426 for in_channel in self.channels:
427 self.monitor_channel.set_solo(in_channel.channel, False)
428 self.monitor_channel.set_muted(in_channel.channel, False)
429 self.monitor_channel.set_solo(channel.channel, True)
430 self.monitor_channel.prefader = True
432 self.monitor_channel.prefader = False
433 self.update_monitor(channel)
434 monitored_channel = property(get_monitored_channel, set_monitored_channel)
436 def update_monitor(self, channel):
437 if self.monitored_channel is not channel:
439 self.monitor_channel.volume = channel.channel.volume
440 self.monitor_channel.balance = channel.channel.balance
441 if type(self.monitored_channel) is OutputChannel:
442 # sync solo/muted channels
443 for input_channel in self.channels:
444 self.monitor_channel.set_solo(input_channel.channel,
445 channel.channel.is_solo(input_channel.channel))
446 self.monitor_channel.set_muted(input_channel.channel,
447 channel.channel.is_muted(input_channel.channel))
448 elif type(self.monitored_channel) is MainMixChannel:
449 # sync solo/muted channels
450 for input_channel in self.channels:
451 self.monitor_channel.set_solo(input_channel.channel,
452 input_channel.channel.solo)
453 self.monitor_channel.set_muted(input_channel.channel,
454 input_channel.channel.mute)
456 def get_input_channel_by_name(self, name):
457 for input_channel in self.channels:
458 if input_channel.channel.name == name:
462 def on_about(self, *args):
463 about = gtk.AboutDialog()
464 about.set_name('jack_mixer')
465 about.set_copyright('Copyright © 2006-2009\nNedko Arnaudov, Frederic Peters')
466 about.set_license('''\
467 jack_mixer is free software; you can redistribute it and/or modify it
468 under the terms of the GNU General Public License as published by the
469 Free Software Foundation; either version 2 of the License, or (at your
470 option) any later version.
472 jack_mixer is distributed in the hope that it will be useful, but
473 WITHOUT ANY WARRANTY; without even the implied warranty of
474 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
475 General Public License for more details.
477 You should have received a copy of the GNU General Public License along
478 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
479 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
480 about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
481 'Frederic Peters <fpeters@0d.be>'])
482 about.set_logo_icon_name('jack_mixer')
483 about.set_website('http://home.gna.org/jackmixer/')
488 def lash_check_events(self):
489 if self.current_filename and self.save:
490 print "saving on SIGUSR1 request"
496 if not self.lash_client:
499 while lash.lash_get_pending_event_count(self.lash_client):
500 event = lash.lash_get_event(self.lash_client)
504 event_type = lash.lash_event_get_type(event)
505 if event_type == lash.LASH_Quit:
506 print "jack_mixer: LASH ordered quit."
509 elif event_type == lash.LASH_Save_File:
510 directory = lash.lash_event_get_string(event)
511 print "jack_mixer: LASH ordered to save data in directory %s" % directory
512 filename = directory + os.sep + "jack_mixer.xml"
513 f = file(filename, "w")
516 lash.lash_send_event(self.lash_client, event) # we crash with double free
517 elif event_type == lash.LASH_Restore_File:
518 directory = lash.lash_event_get_string(event)
519 print "jack_mixer: LASH ordered to restore data from directory %s" % directory
520 filename = directory + os.sep + "jack_mixer.xml"
521 f = file(filename, "r")
522 self.load_from_xml(f, silence_errors=True)
524 lash.lash_send_event(self.lash_client, event)
526 print "jack_mixer: Got unhandled LASH event, type " + str(event_type)
529 #lash.lash_event_destroy(event)
533 def save_to_xml(self, file):
534 #print "Saving to XML..."
535 b = XmlSerialization()
540 def load_from_xml(self, file, silence_errors=False):
541 #print "Loading from XML..."
542 self.on_channels_clear(None)
543 self.unserialized_channels = []
544 b = XmlSerialization()
552 s.unserialize(self, b)
553 for channel in self.unserialized_channels:
554 if isinstance(channel, InputChannel):
555 self.add_channel_precreated(channel)
556 for channel in self.unserialized_channels:
557 if isinstance(channel, OutputChannel):
558 self.add_output_channel_precreated(channel)
559 del self.unserialized_channels
560 self.window.show_all()
562 def serialize(self, object_backend):
563 object_backend.add_property('geometry',
564 '%sx%s' % (self.window.allocation.width, self.window.allocation.height))
566 def unserialize_property(self, name, value):
567 if name == 'geometry':
568 width, height = value.split('x')
569 self.window.resize(int(width), int(height))
572 def unserialize_child(self, name):
573 if name == MainMixChannel.serialization_name():
576 if name == InputChannel.serialization_name():
577 channel = InputChannel(self, "", True)
578 self.unserialized_channels.append(channel)
581 if name == OutputChannel.serialization_name():
582 channel = OutputChannel(self, "", True)
583 self.unserialized_channels.append(channel)
586 def serialization_get_childs(self):
587 '''Get child objects tha required and support serialization'''
588 childs = self.channels[:] + self.output_channels[:]
589 childs.append(self.main_mix)
592 def serialization_name(self):
596 self.main_mix.realize()
597 self.main_mix.set_monitored()
602 self.window.show_all()
604 signal.signal(signal.SIGUSR1, self.sighandler)
605 signal.signal(signal.SIGTERM, self.sighandler)
606 signal.signal(signal.SIGINT, self.sighandler)
610 #f = file("/dev/stdout", "w")
615 print "Usage: %s [mixer_name]" % sys.argv[0]
618 # Connect to LASH if Python bindings are available, and the user did not
620 if lash and not '--no-lash' in sys.argv:
621 # sys.argv is modified by this call
622 lash_client = lash.init(sys.argv, "jack_mixer", lash.LASH_Config_File)
626 parser = OptionParser()
627 parser.add_option('-c', '--config', dest='config',
628 help='use a non default configuration file')
629 # --no-lash here is not acted upon, it is specified for completeness when
631 parser.add_option('--no-lash', dest='nolash', action='store_true',
632 help='do not connect to LASH')
633 options, args = parser.parse_args()
635 # Yeah , this sounds stupid, we connected earlier, but we dont want to show this if we got --help option
636 # This issue should be fixed in pylash, there is a reason for having two functions for initialization after all
638 print "Successfully connected to LASH server at " + lash.lash_get_server_name(lash_client)
646 name = "jack_mixer-%u" % os.getpid()
648 gtk.gdk.threads_init()
650 mixer = JackMixer(name, lash_client)
652 err = gtk.MessageDialog(None,
656 "Mixer creation failed (%s)" % str(e))
662 f = file(options.config)
663 mixer.current_filename = options.config
665 mixer.load_from_xml(f)
667 err = gtk.MessageDialog(mixer.window,
671 "Failed loading settings.")
674 mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
681 if __name__ == "__main__":