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 if lash_client is None:
112 mixer_menu.append(gtk.SeparatorMenuItem())
113 open = gtk.ImageMenuItem(gtk.STOCK_OPEN)
114 mixer_menu.append(open)
115 open.connect('activate', self.on_open_cb)
116 save = gtk.ImageMenuItem(gtk.STOCK_SAVE)
117 mixer_menu.append(save)
118 save.connect('activate', self.on_save_cb)
119 save_as = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
120 mixer_menu.append(save_as)
121 save_as.connect('activate', self.on_save_as_cb)
123 mixer_menu.append(gtk.SeparatorMenuItem())
125 quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
126 mixer_menu.append(quit)
127 quit.connect('activate', self.on_quit_cb)
129 edit_menu = gtk.Menu()
130 edit_menu_item.set_submenu(edit_menu)
132 self.channel_remove_menu_item = gtk.ImageMenuItem(gtk.STOCK_REMOVE)
133 edit_menu.append(self.channel_remove_menu_item)
134 self.channel_remove_menu = gtk.Menu()
135 self.channel_remove_menu_item.set_submenu(self.channel_remove_menu)
137 channel_remove_all_menu_item = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
138 edit_menu.append(channel_remove_all_menu_item)
139 channel_remove_all_menu_item.connect("activate", self.on_channels_clear)
141 edit_menu.append(gtk.SeparatorMenuItem())
143 preferences = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
144 preferences.connect('activate', self.on_preferences_cb)
145 edit_menu.append(preferences)
147 help_menu = gtk.Menu()
148 help_menu_item.set_submenu(help_menu)
150 about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
151 help_menu.append(about)
152 about.connect("activate", self.on_about)
154 self.hbox_top = gtk.HBox()
155 self.vbox_top.pack_start(self.hbox_top, True)
157 self.scrolled_window = gtk.ScrolledWindow()
158 self.hbox_top.pack_start(self.scrolled_window, True)
160 self.hbox_inputs = gtk.HBox()
161 self.hbox_inputs.set_spacing(0)
162 self.hbox_inputs.set_border_width(0)
163 self.hbox_top.set_spacing(0)
164 self.hbox_top.set_border_width(0)
166 self.output_channels = []
168 self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
169 self.scrolled_window.add_with_viewport(self.hbox_inputs)
171 self.main_mix = MainMixChannel(self)
172 self.hbox_outputs = gtk.HBox()
173 self.hbox_outputs.set_spacing(0)
174 self.hbox_outputs.set_border_width(0)
176 frame.add(self.main_mix)
177 self.hbox_outputs.pack_start(frame, False)
178 self.hbox_top.pack_start(self.hbox_outputs, False)
180 self.window.connect("destroy", gtk.main_quit)
182 gobject.timeout_add(80, self.read_meters)
183 self.lash_client = lash_client
186 gobject.timeout_add(1000, self.lash_check_events)
189 print "Cleaning jack_mixer"
193 for channel in self.channels:
196 def on_open_cb(self, *args):
197 dlg = gtk.FileChooserDialog(title='Open', parent=self.window,
198 action=gtk.FILE_CHOOSER_ACTION_OPEN,
199 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
200 gtk.STOCK_OPEN, gtk.RESPONSE_OK))
201 dlg.set_default_response(gtk.RESPONSE_OK)
202 if dlg.run() == gtk.RESPONSE_OK:
203 filename = dlg.get_filename()
205 f = file(filename, 'r')
206 self.load_from_xml(f)
208 err = gtk.MessageDialog(self.window,
212 "Failed loading settings.")
216 self.current_filename = filename
221 def on_save_cb(self, *args):
222 if not self.current_filename:
223 return self.on_save_as_cb()
224 f = file(self.current_filename, 'w')
228 def on_save_as_cb(self, *args):
229 dlg = gtk.FileChooserDialog(title='Save', parent=self.window,
230 action=gtk.FILE_CHOOSER_ACTION_SAVE,
231 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
232 gtk.STOCK_SAVE, gtk.RESPONSE_OK))
233 dlg.set_default_response(gtk.RESPONSE_OK)
234 if dlg.run() == gtk.RESPONSE_OK:
235 self.current_filename = dlg.get_filename()
239 def on_quit_cb(self, *args):
242 preferences_dialog = None
243 def on_preferences_cb(self, widget):
244 if not self.preferences_dialog:
245 self.preferences_dialog = PreferencesDialog(self)
246 self.preferences_dialog.show()
247 self.preferences_dialog.present()
249 def on_add_input_channel(self, widget):
250 dialog = NewChannelDialog(app=self)
251 dialog.set_transient_for(self.window)
256 if ret == gtk.RESPONSE_OK:
257 result = dialog.get_result()
258 channel = self.add_channel(**result)
259 self.window.show_all()
261 def on_add_output_channel(self, widget):
262 dialog = NewOutputChannelDialog(app=self)
263 dialog.set_transient_for(self.window)
268 if ret == gtk.RESPONSE_OK:
269 result = dialog.get_result()
270 channel = self.add_output_channel(**result)
271 self.window.show_all()
273 def on_remove_channel(self, widget, channel):
274 print 'Removing channel "%s"' % channel.channel_name
275 self.channel_remove_menu.remove(widget)
276 if self.monitored_channel is channel:
277 channel.monitor_button.set_active(False)
278 for i in range(len(self.channels)):
279 if self.channels[i] is channel:
282 self.hbox_inputs.remove(channel.parent)
284 if len(self.channels) == 0:
285 self.channel_remove_menu_item.set_sensitive(False)
287 def on_channels_clear(self, widget):
288 for channel in self.channels:
290 self.hbox_inputs.remove(channel.parent)
292 self.channel_remove_menu = gtk.Menu()
293 self.channel_remove_menu_item.set_submenu(self.channel_remove_menu)
294 self.channel_remove_menu_item.set_sensitive(False)
296 def add_channel(self, name, stereo, volume_cc, balance_cc):
298 channel = InputChannel(self, name, stereo)
299 self.add_channel_precreated(channel)
301 err = gtk.MessageDialog(self.window,
302 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
305 "Channel creation failed")
310 channel.channel.volume_midi_cc = int(volume_cc)
312 channel.channel.balance_midi_cc = int(balance_cc)
313 if not (volume_cc or balance_cc):
314 channel.channel.autoset_midi_cc()
317 def add_channel_precreated(self, channel):
320 self.hbox_inputs.pack_start(frame, False)
322 channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
323 self.channel_remove_menu.append(channel_remove_menu_item)
324 channel_remove_menu_item.connect("activate", self.on_remove_channel, channel)
325 self.channel_remove_menu_item.set_sensitive(True)
326 self.channels.append(channel)
328 for outputchannel in self.output_channels:
329 channel.add_control_group(outputchannel)
331 def read_meters(self):
332 for channel in self.channels:
334 self.main_mix.read_meter()
335 for channel in self.output_channels:
339 def add_output_channel(self, name, stereo, volume_cc, balance_cc, display_solo_buttons):
341 channel = OutputChannel(self, name, stereo)
342 channel.display_solo_buttons = display_solo_buttons
343 self.add_output_channel_precreated(channel)
345 err = gtk.MessageDialog(self.window,
346 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
349 "Channel creation failed")
354 channel.channel.volume_midi_cc = int(volume_cc)
356 channel.channel.balance_midi_cc = int(balance_cc)
359 def add_output_channel_precreated(self, channel):
362 self.hbox_outputs.pack_start(frame, False)
364 # XXX: handle deletion of output channels
365 #channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
366 #self.channel_remove_menu.append(channel_remove_menu_item)
367 #channel_remove_menu_item.connect("activate", self.on_remove_channel, channel, channel_remove_menu_item)
368 #self.channel_remove_menu_item.set_sensitive(True)
369 self.output_channels.append(channel)
371 _monitored_channel = None
372 def get_monitored_channel(self):
373 return self._monitored_channel
375 def set_monitored_channel(self, channel):
376 if self._monitored_channel:
377 if channel.channel.name == self._monitored_channel.channel.name:
379 self._monitored_channel = channel
380 if type(channel) is InputChannel:
381 # reset all solo/mute settings
382 for in_channel in self.channels:
383 self.monitor_channel.set_solo(in_channel.channel, False)
384 self.monitor_channel.set_muted(in_channel.channel, False)
385 self.monitor_channel.set_solo(channel.channel, True)
386 self.monitor_channel.prefader = True
388 self.monitor_channel.prefader = False
389 self.update_monitor(channel)
390 monitored_channel = property(get_monitored_channel, set_monitored_channel)
392 def update_monitor(self, channel):
393 if self.monitored_channel is not channel:
395 self.monitor_channel.volume = channel.channel.volume
396 self.monitor_channel.balance = channel.channel.balance
397 if type(self.monitored_channel) is OutputChannel:
398 # sync solo/muted channels
399 for input_channel in self.channels:
400 self.monitor_channel.set_solo(input_channel.channel,
401 channel.channel.is_solo(input_channel.channel))
402 self.monitor_channel.set_muted(input_channel.channel,
403 channel.channel.is_muted(input_channel.channel))
404 elif type(self.monitored_channel) is MainMixChannel:
405 # sync solo/muted channels
406 for input_channel in self.channels:
407 self.monitor_channel.set_solo(input_channel.channel,
408 input_channel.channel.solo)
409 self.monitor_channel.set_muted(input_channel.channel,
410 input_channel.channel.mute)
412 def get_input_channel_by_name(self, name):
413 for input_channel in self.channels:
414 if input_channel.channel.name == name:
418 def on_about(self, *args):
419 about = gtk.AboutDialog()
420 about.set_name('jack_mixer')
421 about.set_copyright('Copyright © 2006-2009\nNedko Arnaudov, Frederic Peters')
422 about.set_license('''\
423 jack_mixer is free software; you can redistribute it and/or modify it
424 under the terms of the GNU General Public License as published by the
425 Free Software Foundation; either version 2 of the License, or (at your
426 option) any later version.
428 jack_mixer is distributed in the hope that it will be useful, but
429 WITHOUT ANY WARRANTY; without even the implied warranty of
430 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
431 General Public License for more details.
433 You should have received a copy of the GNU General Public License along
434 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
435 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
436 about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
437 'Frederic Peters <fpeters@0d.be>'])
438 about.set_logo_icon_name('jack_mixer')
439 about.set_website('http://home.gna.org/jackmixer/')
444 def lash_check_events(self):
445 while lash.lash_get_pending_event_count(self.lash_client):
446 event = lash.lash_get_event(self.lash_client)
450 event_type = lash.lash_event_get_type(event)
451 if event_type == lash.LASH_Quit:
452 print "jack_mixer: LASH ordered quit."
455 elif event_type == lash.LASH_Save_File:
456 directory = lash.lash_event_get_string(event)
457 print "jack_mixer: LASH ordered to save data in directory %s" % directory
458 filename = directory + os.sep + "jack_mixer.xml"
459 f = file(filename, "w")
462 lash.lash_send_event(self.lash_client, event) # we crash with double free
463 elif event_type == lash.LASH_Restore_File:
464 directory = lash.lash_event_get_string(event)
465 print "jack_mixer: LASH ordered to restore data from directory %s" % directory
466 filename = directory + os.sep + "jack_mixer.xml"
467 f = file(filename, "r")
468 self.load_from_xml(f, silence_errors=True)
470 lash.lash_send_event(self.lash_client, event)
472 print "jack_mixer: Got unhandled LASH event, type " + str(event_type)
475 #lash.lash_event_destroy(event)
479 def save_to_xml(self, file):
480 #print "Saving to XML..."
481 b = XmlSerialization()
486 def load_from_xml(self, file, silence_errors=False):
487 #print "Loading from XML..."
488 self.on_channels_clear(None)
489 self.unserialized_channels = []
490 b = XmlSerialization()
498 s.unserialize(self, b)
499 for channel in self.unserialized_channels:
500 if isinstance(channel, InputChannel):
501 self.add_channel_precreated(channel)
502 for channel in self.unserialized_channels:
503 if isinstance(channel, OutputChannel):
504 self.add_output_channel_precreated(channel)
505 del self.unserialized_channels
506 self.window.show_all()
508 def serialize(self, object_backend):
509 object_backend.add_property('geometry',
510 '%sx%s' % (self.window.allocation.width, self.window.allocation.height))
512 def unserialize_property(self, name, value):
513 if name == 'geometry':
514 width, height = value.split('x')
515 self.window.resize(int(width), int(height))
518 def unserialize_child(self, name):
519 if name == main_mix_serialization_name():
522 if name == input_channel_serialization_name():
523 channel = InputChannel(self, "", True)
524 self.unserialized_channels.append(channel)
527 if name == output_channel_serialization_name():
528 channel = OutputChannel(self, "", True)
529 self.unserialized_channels.append(channel)
532 def serialization_get_childs(self):
533 '''Get child objects tha required and support serialization'''
534 childs = self.channels[:] + self.output_channels[:]
535 childs.append(self.main_mix)
538 def serialization_name(self):
542 self.main_mix.realize()
543 self.main_mix.set_monitored()
548 self.window.show_all()
552 #f = file("/dev/stdout", "w")
557 print "Usage: %s [mixer_name]" % sys.argv[0]
560 if lash: # If LASH python bindings are available
561 # sys.argv is modified by this call
562 lash_client = lash.init(sys.argv, "jack_mixer", lash.LASH_Config_File)
566 parser = OptionParser()
567 parser.add_option('-c', '--config', dest='config')
568 options, args = parser.parse_args()
570 # Yeah , this sounds stupid, we connected earlier, but we dont want to show this if we got --help option
571 # This issue should be fixed in pylash, there is a reason for having two functions for initialization after all
573 print "Successfully connected to LASH server at " + lash.lash_get_server_name(lash_client)
581 name = "jack_mixer-%u" % os.getpid()
583 gtk.gdk.threads_init()
585 mixer = JackMixer(name, lash_client)
587 err = gtk.MessageDialog(None,
591 "Mixer creation failed (%s)" % str(e))
597 f = file(options.config)
598 mixer.current_filename = options.config
600 mixer.load_from_xml(f)
602 err = gtk.MessageDialog(mixer.window,
606 "Failed loading settings.")
609 mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
616 if __name__ == "__main__":