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
33 print >> sys.stderr, "Cannot load LASH python bindings, you want them unless you enjoy manual jack plumbing each time you use this app"
35 # temporary change Python modules lookup path to look into installation
36 # directory ($prefix/share/jack_mixer/)
38 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..', 'share', 'jack_mixer'))
45 from preferences import PreferencesDialog
47 from serialization_xml import XmlSerialization
48 from serialization import SerializedObject, Serializator
50 # restore Python modules lookup path
53 class JackMixer(SerializedObject):
55 # scales suitable as meter scales
56 meter_scales = [scale.IEC268(), scale.Linear70dB(), scale.IEC268Minimalistic()]
58 # scales suitable as volume slider scales
59 slider_scales = [scale.Linear30dB(), scale.Linear70dB()]
61 # name of settngs file that is currently open
62 current_filename = None
64 def __init__(self, name, lash_client):
65 self.mixer = jack_mixer_c.Mixer(name)
68 self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
71 # Send our client name to server
72 lash_event = lash.lash_event_new_with_type(lash.LASH_Client_Name)
73 lash.lash_event_set_string(lash_event, name)
74 lash.lash_send_event(lash_client, lash_event)
76 lash.lash_jack_client_name(lash_client, name)
78 gtk.window_set_default_icon_name('jack_mixer')
80 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
81 self.window.set_title(name)
83 self.gui_factory = gui.Factory(self.window, self.meter_scales, self.slider_scales)
85 self.vbox_top = gtk.VBox()
86 self.window.add(self.vbox_top)
88 self.menubar = gtk.MenuBar()
89 self.vbox_top.pack_start(self.menubar, False)
91 mixer_menu_item = gtk.MenuItem("_Mixer")
92 self.menubar.append(mixer_menu_item)
93 edit_menu_item = gtk.MenuItem('_Edit')
94 self.menubar.append(edit_menu_item)
95 help_menu_item = gtk.MenuItem('_Help')
96 self.menubar.append(help_menu_item)
98 self.window.set_default_size(120, 300)
100 mixer_menu = gtk.Menu()
101 mixer_menu_item.set_submenu(mixer_menu)
103 add_input_channel = gtk.ImageMenuItem('New _Input Channel')
104 mixer_menu.append(add_input_channel)
105 add_input_channel.connect("activate", self.on_add_input_channel)
107 add_output_channel = gtk.ImageMenuItem('New _Output Channel')
108 mixer_menu.append(add_output_channel)
109 add_output_channel.connect("activate", self.on_add_output_channel)
111 mixer_menu.append(gtk.SeparatorMenuItem())
112 open = gtk.ImageMenuItem(gtk.STOCK_OPEN)
113 mixer_menu.append(open)
114 open.connect('activate', self.on_open_cb)
115 save = gtk.ImageMenuItem(gtk.STOCK_SAVE)
116 mixer_menu.append(save)
117 save.connect('activate', self.on_save_cb)
118 save_as = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
119 mixer_menu.append(save_as)
120 save_as.connect('activate', self.on_save_as_cb)
122 mixer_menu.append(gtk.SeparatorMenuItem())
124 quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
125 mixer_menu.append(quit)
126 quit.connect('activate', self.on_quit_cb)
128 edit_menu = gtk.Menu()
129 edit_menu_item.set_submenu(edit_menu)
131 self.channel_remove_menu_item = gtk.ImageMenuItem(gtk.STOCK_REMOVE)
132 edit_menu.append(self.channel_remove_menu_item)
133 self.channel_remove_menu = gtk.Menu()
134 self.channel_remove_menu_item.set_submenu(self.channel_remove_menu)
136 channel_remove_all_menu_item = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
137 edit_menu.append(channel_remove_all_menu_item)
138 channel_remove_all_menu_item.connect("activate", self.on_channels_clear)
140 edit_menu.append(gtk.SeparatorMenuItem())
142 preferences = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
143 preferences.connect('activate', self.on_preferences_cb)
144 edit_menu.append(preferences)
146 help_menu = gtk.Menu()
147 help_menu_item.set_submenu(help_menu)
149 about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
150 help_menu.append(about)
151 about.connect("activate", self.on_about)
153 self.hbox_top = gtk.HBox()
154 self.vbox_top.pack_start(self.hbox_top, True)
156 self.scrolled_window = gtk.ScrolledWindow()
157 self.hbox_top.pack_start(self.scrolled_window, True)
159 self.hbox_inputs = gtk.HBox()
160 self.hbox_inputs.set_spacing(0)
161 self.hbox_inputs.set_border_width(0)
162 self.hbox_top.set_spacing(0)
163 self.hbox_top.set_border_width(0)
165 self.output_channels = []
167 self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
168 self.scrolled_window.add_with_viewport(self.hbox_inputs)
170 self.main_mix = MainMixChannel(self)
171 self.hbox_outputs = gtk.HBox()
172 self.hbox_outputs.set_spacing(0)
173 self.hbox_outputs.set_border_width(0)
175 frame.add(self.main_mix)
176 self.hbox_outputs.pack_start(frame, False)
177 self.hbox_top.pack_start(self.hbox_outputs, False)
179 self.window.connect("destroy", gtk.main_quit)
181 gobject.timeout_add(80, self.read_meters)
182 self.lash_client = lash_client
185 gobject.timeout_add(1000, self.lash_check_events)
188 print "Cleaning jack_mixer"
192 for channel in self.channels:
195 def on_open_cb(self, *args):
196 dlg = gtk.FileChooserDialog(title='Open', parent=self.window,
197 action=gtk.FILE_CHOOSER_ACTION_OPEN,
198 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
199 gtk.STOCK_OPEN, gtk.RESPONSE_OK))
200 dlg.set_default_response(gtk.RESPONSE_OK)
201 if dlg.run() == gtk.RESPONSE_OK:
202 filename = dlg.get_filename()
204 f = file(filename, 'r')
205 self.load_from_xml(f)
207 err = gtk.MessageDialog(self.window,
211 "Failed loading settings.")
215 self.current_filename = filename
220 def on_save_cb(self, *args):
221 if not self.current_filename:
222 return self.on_save_as_cb()
223 f = file(self.current_filename, 'w')
227 def on_save_as_cb(self, *args):
228 dlg = gtk.FileChooserDialog(title='Save', parent=self.window,
229 action=gtk.FILE_CHOOSER_ACTION_SAVE,
230 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
231 gtk.STOCK_SAVE, gtk.RESPONSE_OK))
232 dlg.set_default_response(gtk.RESPONSE_OK)
233 if dlg.run() == gtk.RESPONSE_OK:
234 self.current_filename = dlg.get_filename()
238 def on_quit_cb(self, *args):
241 preferences_dialog = None
242 def on_preferences_cb(self, widget):
243 if not self.preferences_dialog:
244 self.preferences_dialog = PreferencesDialog(self)
245 self.preferences_dialog.show()
246 self.preferences_dialog.present()
248 def on_add_input_channel(self, widget):
249 dialog = NewChannelDialog(app=self)
250 dialog.set_transient_for(self.window)
255 if ret == gtk.RESPONSE_OK:
256 result = dialog.get_result()
257 channel = self.add_channel(**result)
258 self.window.show_all()
260 def on_add_output_channel(self, widget):
261 dialog = NewOutputChannelDialog(app=self)
262 dialog.set_transient_for(self.window)
267 if ret == gtk.RESPONSE_OK:
268 result = dialog.get_result()
269 channel = self.add_output_channel(**result)
270 self.window.show_all()
272 def on_remove_channel(self, widget, channel):
273 print 'Removing channel "%s"' % channel.channel_name
274 self.channel_remove_menu.remove(widget)
275 if self.monitored_channel is channel:
276 channel.monitor_button.set_active(False)
277 for i in range(len(self.channels)):
278 if self.channels[i] is channel:
281 self.hbox_inputs.remove(channel.parent)
283 if len(self.channels) == 0:
284 self.channel_remove_menu_item.set_sensitive(False)
286 def on_channels_clear(self, widget):
287 for channel in self.channels:
289 self.hbox_inputs.remove(channel.parent)
291 self.channel_remove_menu = gtk.Menu()
292 self.channel_remove_menu_item.set_submenu(self.channel_remove_menu)
293 self.channel_remove_menu_item.set_sensitive(False)
295 def add_channel(self, name, stereo, volume_cc, balance_cc):
297 channel = InputChannel(self, name, stereo)
298 self.add_channel_precreated(channel)
300 err = gtk.MessageDialog(self.window,
301 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
304 "Channel creation failed")
309 channel.channel.volume_midi_cc = int(volume_cc)
311 channel.channel.balance_midi_cc = int(balance_cc)
312 if not (volume_cc or balance_cc):
313 channel.channel.autoset_midi_cc()
316 def add_channel_precreated(self, channel):
319 self.hbox_inputs.pack_start(frame, False)
321 channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
322 self.channel_remove_menu.append(channel_remove_menu_item)
323 channel_remove_menu_item.connect("activate", self.on_remove_channel, channel)
324 self.channel_remove_menu_item.set_sensitive(True)
325 self.channels.append(channel)
327 for outputchannel in self.output_channels:
328 channel.add_control_group(outputchannel)
330 def read_meters(self):
331 for channel in self.channels:
333 self.main_mix.read_meter()
334 for channel in self.output_channels:
338 def add_output_channel(self, name, stereo, volume_cc, balance_cc, display_solo_buttons):
340 channel = OutputChannel(self, name, stereo)
341 channel.display_solo_buttons = display_solo_buttons
342 self.add_output_channel_precreated(channel)
344 err = gtk.MessageDialog(self.window,
345 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
348 "Channel creation failed")
353 channel.channel.volume_midi_cc = int(volume_cc)
355 channel.channel.balance_midi_cc = int(balance_cc)
358 def add_output_channel_precreated(self, channel):
361 self.hbox_outputs.pack_start(frame, False)
363 # XXX: handle deletion of output channels
364 #channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
365 #self.channel_remove_menu.append(channel_remove_menu_item)
366 #channel_remove_menu_item.connect("activate", self.on_remove_channel, channel, channel_remove_menu_item)
367 #self.channel_remove_menu_item.set_sensitive(True)
368 self.output_channels.append(channel)
370 _monitored_channel = None
371 def get_monitored_channel(self):
372 return self._monitored_channel
374 def set_monitored_channel(self, channel):
375 if self._monitored_channel:
376 if channel.channel.name == self._monitored_channel.channel.name:
378 self._monitored_channel = channel
379 if type(channel) is InputChannel:
380 # reset all solo/mute settings
381 for in_channel in self.channels:
382 self.monitor_channel.set_solo(in_channel.channel, False)
383 self.monitor_channel.set_muted(in_channel.channel, False)
384 self.monitor_channel.set_solo(channel.channel, True)
385 self.monitor_channel.prefader = True
387 self.monitor_channel.prefader = False
388 self.update_monitor(channel)
389 monitored_channel = property(get_monitored_channel, set_monitored_channel)
391 def update_monitor(self, channel):
392 if self.monitored_channel is not channel:
394 self.monitor_channel.volume = channel.channel.volume
395 self.monitor_channel.balance = channel.channel.balance
396 if type(self.monitored_channel) is OutputChannel:
397 # sync solo/muted channels
398 for input_channel in self.channels:
399 self.monitor_channel.set_solo(input_channel.channel,
400 channel.channel.is_solo(input_channel.channel))
401 self.monitor_channel.set_muted(input_channel.channel,
402 channel.channel.is_muted(input_channel.channel))
403 elif type(self.monitored_channel) is MainMixChannel:
404 # sync solo/muted channels
405 for input_channel in self.channels:
406 self.monitor_channel.set_solo(input_channel.channel,
407 input_channel.channel.solo)
408 self.monitor_channel.set_muted(input_channel.channel,
409 input_channel.channel.mute)
411 def get_input_channel_by_name(self, name):
412 for input_channel in self.channels:
413 if input_channel.channel.name == name:
417 def on_about(self, *args):
418 about = gtk.AboutDialog()
419 about.set_name('jack_mixer')
420 about.set_copyright('Copyright © 2006-2009\nNedko Arnaudov, Frederic Peters')
421 about.set_license('''\
422 jack_mixer is free software; you can redistribute it and/or modify it
423 under the terms of the GNU General Public License as published by the
424 Free Software Foundation; either version 2 of the License, or (at your
425 option) any later version.
427 jack_mixer is distributed in the hope that it will be useful, but
428 WITHOUT ANY WARRANTY; without even the implied warranty of
429 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
430 General Public License for more details.
432 You should have received a copy of the GNU General Public License along
433 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
434 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
435 about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
436 'Frederic Peters <fpeters@0d.be>'])
437 about.set_logo_icon_name('jack_mixer')
438 about.set_website('http://home.gna.org/jackmixer/')
443 def lash_check_events(self):
444 while lash.lash_get_pending_event_count(self.lash_client):
445 event = lash.lash_get_event(self.lash_client)
449 event_type = lash.lash_event_get_type(event)
450 if event_type == lash.LASH_Quit:
451 print "jack_mixer: LASH ordered quit."
454 elif event_type == lash.LASH_Save_File:
455 directory = lash.lash_event_get_string(event)
456 print "jack_mixer: LASH ordered to save data in directory %s" % directory
457 filename = directory + os.sep + "jack_mixer.xml"
458 f = file(filename, "w")
461 lash.lash_send_event(self.lash_client, event) # we crash with double free
462 elif event_type == lash.LASH_Restore_File:
463 directory = lash.lash_event_get_string(event)
464 print "jack_mixer: LASH ordered to restore data from directory %s" % directory
465 filename = directory + os.sep + "jack_mixer.xml"
466 f = file(filename, "r")
467 self.load_from_xml(f, silence_errors=True)
469 lash.lash_send_event(self.lash_client, event)
471 print "jack_mixer: Got unhandled LASH event, type " + str(event_type)
474 #lash.lash_event_destroy(event)
478 def save_to_xml(self, file):
479 #print "Saving to XML..."
480 b = XmlSerialization()
485 def load_from_xml(self, file, silence_errors=False):
486 #print "Loading from XML..."
487 self.on_channels_clear(None)
488 self.unserialized_channels = []
489 b = XmlSerialization()
497 s.unserialize(self, b)
498 for channel in self.unserialized_channels:
499 if isinstance(channel, InputChannel):
500 self.add_channel_precreated(channel)
501 for channel in self.unserialized_channels:
502 if isinstance(channel, OutputChannel):
503 self.add_output_channel_precreated(channel)
504 del self.unserialized_channels
505 self.window.show_all()
507 def serialize(self, object_backend):
508 object_backend.add_property('geometry',
509 '%sx%s' % (self.window.allocation.width, self.window.allocation.height))
511 def unserialize_property(self, name, value):
512 if name == 'geometry':
513 width, height = value.split('x')
514 self.window.resize(int(width), int(height))
517 def unserialize_child(self, name):
518 if name == main_mix_serialization_name():
521 if name == input_channel_serialization_name():
522 channel = InputChannel(self, "", True)
523 self.unserialized_channels.append(channel)
526 if name == output_channel_serialization_name():
527 channel = OutputChannel(self, "", True)
528 self.unserialized_channels.append(channel)
531 def serialization_get_childs(self):
532 '''Get child objects tha required and support serialization'''
533 childs = self.channels[:] + self.output_channels[:]
534 childs.append(self.main_mix)
537 def serialization_name(self):
541 self.main_mix.realize()
542 self.main_mix.set_monitored()
547 self.window.show_all()
551 #f = file("/dev/stdout", "w")
556 print "Usage: %s [mixer_name]" % sys.argv[0]
559 if lash: # If LASH python bindings are available
560 # sys.argv is modified by this call
561 lash_client = lash.init(sys.argv, "jack_mixer", lash.LASH_Config_File)
565 parser = OptionParser()
566 parser.add_option('-c', '--config', dest='config')
567 options, args = parser.parse_args()
569 # Yeah , this sounds stupid, we connected earlier, but we dont want to show this if we got --help option
570 # This issue should be fixed in pylash, there is a reason for having two functions for initialization after all
572 print "Successfully connected to LASH server at " + lash.lash_get_server_name(lash_client)
580 name = "jack_mixer-%u" % os.getpid()
582 gtk.gdk.threads_init()
584 mixer = JackMixer(name, lash_client)
586 err = gtk.MessageDialog(None,
590 "Mixer creation failed (%s)" % str(e))
596 f = file(options.config)
597 mixer.current_filename = options.config
599 mixer.load_from_xml(f)
601 err = gtk.MessageDialog(mixer.window,
605 "Failed loading settings.")
608 mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
615 if __name__ == "__main__":