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
38 sys.path.insert(0, os.path.dirname(sys.argv[0]) + os.sep + ".." + os.sep + "share"+ os.sep + "jack_mixer")
41 from preferences import PreferencesDialog
45 from serialization_xml import xml_serialization
46 from serialization import serialized_object, serializator
49 print >> sys.stderr, "Cannot load LASH python bindings, you want them unless you enjoy manual jack plumbing each time you use this app"
51 class jack_mixer(serialized_object):
53 # scales suitable as meter scales
54 meter_scales = [scale.iec_268(), scale.linear_70dB(), scale.iec_268_minimalistic()]
56 # scales suitable as volume slider scales
57 slider_scales = [scale.linear_30dB(), scale.linear_70dB()]
59 # name of settngs file that is currently open
60 current_filename = None
62 def __init__(self, name, lash_client):
63 self.mixer = jack_mixer_c.Mixer(name)
66 self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
69 # Send our client name to server
70 lash_event = lash.lash_event_new_with_type(lash.LASH_Client_Name)
71 lash.lash_event_set_string(lash_event, name)
72 lash.lash_send_event(lash_client, lash_event)
74 lash.lash_jack_client_name(lash_client, name)
76 gtk.window_set_default_icon_name('jack_mixer')
78 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
79 self.window.set_title(name)
81 self.gui_factory = gui.factory(self.window, self.meter_scales, self.slider_scales)
83 self.vbox_top = gtk.VBox()
84 self.window.add(self.vbox_top)
86 self.menubar = gtk.MenuBar()
87 self.vbox_top.pack_start(self.menubar, False)
89 mixer_menu_item = gtk.MenuItem("_Mixer")
90 self.menubar.append(mixer_menu_item)
91 edit_menu_item = gtk.MenuItem('_Edit')
92 self.menubar.append(edit_menu_item)
93 help_menu_item = gtk.MenuItem('_Help')
94 self.menubar.append(help_menu_item)
96 self.window.set_default_size(120,300)
98 mixer_menu = gtk.Menu()
99 mixer_menu_item.set_submenu(mixer_menu)
101 add_input_channel = gtk.ImageMenuItem('New _Input Channel')
102 mixer_menu.append(add_input_channel)
103 add_input_channel.connect("activate", self.on_add_input_channel)
105 add_output_channel = gtk.ImageMenuItem('New _Output Channel')
106 mixer_menu.append(add_output_channel)
107 add_output_channel.connect("activate", self.on_add_output_channel)
109 if lash_client is None and xml_serialization is not None:
110 mixer_menu.append(gtk.SeparatorMenuItem())
111 open = gtk.ImageMenuItem(gtk.STOCK_OPEN)
112 mixer_menu.append(open)
113 open.connect('activate', self.on_open_cb)
114 save = gtk.ImageMenuItem(gtk.STOCK_SAVE)
115 mixer_menu.append(save)
116 save.connect('activate', self.on_save_cb)
117 save_as = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
118 mixer_menu.append(save_as)
119 save_as.connect('activate', self.on_save_as_cb)
121 mixer_menu.append(gtk.SeparatorMenuItem())
123 quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
124 mixer_menu.append(quit)
125 quit.connect('activate', self.on_quit_cb)
127 edit_menu = gtk.Menu()
128 edit_menu_item.set_submenu(edit_menu)
130 self.channel_remove_menu_item = gtk.ImageMenuItem(gtk.STOCK_REMOVE)
131 edit_menu.append(self.channel_remove_menu_item)
132 self.channel_remove_menu = gtk.Menu()
133 self.channel_remove_menu_item.set_submenu(self.channel_remove_menu)
135 channel_remove_all_menu_item = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
136 edit_menu.append(channel_remove_all_menu_item)
137 channel_remove_all_menu_item.connect("activate", self.on_channels_clear)
139 edit_menu.append(gtk.SeparatorMenuItem())
141 preferences = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
142 preferences.connect('activate', self.on_preferences_cb)
143 edit_menu.append(preferences)
145 help_menu = gtk.Menu()
146 help_menu_item.set_submenu(help_menu)
148 about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
149 help_menu.append(about)
150 about.connect("activate", self.on_about)
152 self.hbox_top = gtk.HBox()
153 self.vbox_top.pack_start(self.hbox_top, True)
155 self.scrolled_window = gtk.ScrolledWindow()
156 self.hbox_top.pack_start(self.scrolled_window, True)
158 self.hbox_inputs = gtk.HBox()
159 self.hbox_inputs.set_spacing(0)
160 self.hbox_inputs.set_border_width(0)
161 self.hbox_top.set_spacing(0)
162 self.hbox_top.set_border_width(0)
164 self.output_channels = []
166 self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
167 self.scrolled_window.add_with_viewport(self.hbox_inputs)
169 self.main_mix = main_mix(self)
170 self.hbox_outputs = gtk.HBox()
171 self.hbox_outputs.set_spacing(0)
172 self.hbox_outputs.set_border_width(0)
174 frame.add(self.main_mix)
175 self.hbox_outputs.pack_start(frame, False)
176 self.hbox_top.pack_start(self.hbox_outputs, False)
178 self.window.connect("destroy", gtk.main_quit)
180 gobject.timeout_add(80, self.read_meters)
181 self.lash_client = lash_client
184 gobject.timeout_add(1000, self.lash_check_events)
187 print "Cleaning jack_mixer"
191 for channel in self.channels:
194 def on_open_cb(self, *args):
195 dlg = gtk.FileChooserDialog(title='Open', parent=self.window,
196 action=gtk.FILE_CHOOSER_ACTION_OPEN,
197 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
198 gtk.STOCK_OPEN, gtk.RESPONSE_OK))
199 dlg.set_default_response(gtk.RESPONSE_OK)
200 if dlg.run() == gtk.RESPONSE_OK:
201 filename = dlg.get_filename()
203 f = file(filename, 'r')
204 self.load_from_xml(f)
206 # TODO: display error in a dialog box
207 print >> sys.stderr, 'Failed to read', filename
209 self.current_filename = filename
214 def on_save_cb(self, *args):
215 if not self.current_filename:
216 return self.on_save_as_cb()
217 f = file(self.current_filename, 'w')
221 def on_save_as_cb(self, *args):
222 dlg = gtk.FileChooserDialog(title='Save', parent=self.window,
223 action=gtk.FILE_CHOOSER_ACTION_SAVE,
224 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
225 gtk.STOCK_SAVE, gtk.RESPONSE_OK))
226 dlg.set_default_response(gtk.RESPONSE_OK)
227 if dlg.run() == gtk.RESPONSE_OK:
228 self.current_filename = dlg.get_filename()
232 def on_quit_cb(self, *args):
235 preferences_dialog = None
236 def on_preferences_cb(self, widget):
237 if not self.preferences_dialog:
238 self.preferences_dialog = PreferencesDialog(self)
239 self.preferences_dialog.show()
240 self.preferences_dialog.present()
242 def on_add_input_channel(self, widget):
243 dialog = NewChannelDialog(app=self)
244 dialog.set_transient_for(self.window)
249 if ret == gtk.RESPONSE_OK:
250 result = dialog.get_result()
251 channel = self.add_channel(**result)
252 self.window.show_all()
254 def on_add_output_channel(self, widget):
255 dialog = NewOutputChannelDialog(app=self)
256 dialog.set_transient_for(self.window)
261 if ret == gtk.RESPONSE_OK:
262 result = dialog.get_result()
263 channel = self.add_output_channel(**result)
264 self.window.show_all()
266 def on_remove_channel(self, widget, channel):
267 print 'Removing channel "%s"' % channel.channel_name
268 self.channel_remove_menu.remove(widget)
269 if self.monitored_channel is channel:
270 channel.monitor_button.set_active(False)
271 for i in range(len(self.channels)):
272 if self.channels[i] is channel:
275 self.hbox_inputs.remove(channel.parent)
277 if len(self.channels) == 0:
278 self.channel_remove_menu_item.set_sensitive(False)
280 def on_channels_clear(self, widget):
281 for channel in self.channels:
283 self.hbox_inputs.remove(channel.parent)
285 self.channel_remove_menu = gtk.Menu()
286 self.channel_remove_menu_item.set_submenu(self.channel_remove_menu)
287 self.channel_remove_menu_item.set_sensitive(False)
289 def add_channel(self, name, stereo, volume_cc, balance_cc):
291 channel = input_channel(self, name, stereo)
292 self.add_channel_precreated(channel)
294 err = gtk.MessageDialog(self.window,
295 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
298 "Channel creation failed")
303 channel.channel.volume_midi_cc = int(volume_cc)
305 channel.channel.balance_midi_cc = int(balance_cc)
306 if not (volume_cc or balance_cc):
307 channel.channel.autoset_midi_cc()
310 def add_channel_precreated(self, channel):
313 self.hbox_inputs.pack_start(frame, False)
315 channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
316 self.channel_remove_menu.append(channel_remove_menu_item)
317 channel_remove_menu_item.connect("activate", self.on_remove_channel, channel)
318 self.channel_remove_menu_item.set_sensitive(True)
319 self.channels.append(channel)
321 for outputchannel in self.output_channels:
322 channel.add_control_group(outputchannel)
324 def read_meters(self):
325 for channel in self.channels:
327 self.main_mix.read_meter()
328 for channel in self.output_channels:
332 def add_output_channel(self, name, stereo, volume_cc, balance_cc, display_solo_buttons):
334 channel = output_channel(self, name, stereo)
335 channel.display_solo_buttons = display_solo_buttons
336 self.add_output_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)
353 def add_output_channel_precreated(self, channel):
356 self.hbox_outputs.pack_start(frame, False)
358 # XXX: handle deletion of output channels
359 #channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
360 #self.channel_remove_menu.append(channel_remove_menu_item)
361 #channel_remove_menu_item.connect("activate", self.on_remove_channel, channel, channel_remove_menu_item)
362 #self.channel_remove_menu_item.set_sensitive(True)
363 self.output_channels.append(channel)
365 _monitored_channel = None
366 def get_monitored_channel(self):
367 return self._monitored_channel
369 def set_monitored_channel(self, channel):
370 if self._monitored_channel:
371 if channel.channel.name == self._monitored_channel.channel.name:
373 self._monitored_channel = channel
374 if type(channel) is input_channel:
375 # reset all solo/mute settings
376 for in_channel in self.channels:
377 self.monitor_channel.set_solo(in_channel.channel, False)
378 self.monitor_channel.set_muted(in_channel.channel, False)
379 self.monitor_channel.set_solo(channel.channel, True)
380 self.monitor_channel.prefader = True
382 self.monitor_channel.prefader = False
383 self.update_monitor(channel)
384 monitored_channel = property(get_monitored_channel, set_monitored_channel)
386 def update_monitor(self, channel):
387 if self.monitored_channel is not channel:
389 self.monitor_channel.volume = channel.channel.volume
390 self.monitor_channel.balance = channel.channel.balance
391 if type(self.monitored_channel) is output_channel:
392 # sync solo/muted channels
393 for input_channel in self.channels:
394 self.monitor_channel.set_solo(input_channel.channel,
395 channel.channel.is_solo(input_channel.channel))
396 self.monitor_channel.set_muted(input_channel.channel,
397 channel.channel.is_muted(input_channel.channel))
398 elif type(self.monitored_channel) is main_mix:
399 # sync solo/muted channels
400 for input_channel in self.channels:
401 self.monitor_channel.set_solo(input_channel.channel,
402 input_channel.channel.solo)
403 self.monitor_channel.set_muted(input_channel.channel,
404 input_channel.channel.mute)
406 def get_input_channel_by_name(self, name):
407 for input_channel in self.channels:
408 if input_channel.channel.name == name:
412 def on_about(self, *args):
413 about = gtk.AboutDialog()
414 about.set_name('jack_mixer')
415 about.set_copyright('Copyright © 2006-2009\nNedko Arnaudov, Frederic Peters')
416 about.set_license('''\
417 jack_mixer is free software; you can redistribute it and/or modify it
418 under the terms of the GNU General Public License as published by the
419 Free Software Foundation; either version 2 of the License, or (at your
420 option) any later version.
422 jack_mixer is distributed in the hope that it will be useful, but
423 WITHOUT ANY WARRANTY; without even the implied warranty of
424 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
425 General Public License for more details.
427 You should have received a copy of the GNU General Public License along
428 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
429 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
430 about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
431 'Frederic Peters <fpeters@0d.be>'])
432 about.set_logo_icon_name('jack_mixer')
433 about.set_website('http://home.gna.org/jackmixer/')
438 def lash_check_events(self):
439 while lash.lash_get_pending_event_count(self.lash_client):
440 event = lash.lash_get_event(self.lash_client)
444 event_type = lash.lash_event_get_type(event)
445 if event_type == lash.LASH_Quit:
446 print "jack_mixer: LASH ordered quit."
449 elif event_type == lash.LASH_Save_File:
450 directory = lash.lash_event_get_string(event)
451 print "jack_mixer: LASH ordered to save data in directory %s" % directory
452 filename = directory + os.sep + "jack_mixer.xml"
453 f = file(filename, "w")
456 lash.lash_send_event(self.lash_client, event) # we crash with double free
457 elif event_type == lash.LASH_Restore_File:
458 directory = lash.lash_event_get_string(event)
459 print "jack_mixer: LASH ordered to restore data from directory %s" % directory
460 filename = directory + os.sep + "jack_mixer.xml"
461 f = file(filename, "r")
462 self.load_from_xml(f)
464 lash.lash_send_event(self.lash_client, event)
466 print "jack_mixer: Got unhandled LASH event, type " + str(event_type)
469 #lash.lash_event_destroy(event)
473 def save_to_xml(self, file):
474 #print "Saving to XML..."
475 b = xml_serialization()
480 def load_from_xml(self, file):
481 #print "Loading from XML..."
482 self.on_channels_clear(None)
483 self.unserialized_channels = []
484 b = xml_serialization()
487 s.unserialize(self, b)
488 for channel in self.unserialized_channels:
489 if isinstance(channel, input_channel):
490 self.add_channel_precreated(channel)
491 for channel in self.unserialized_channels:
492 if isinstance(channel, output_channel):
493 self.add_output_channel_precreated(channel)
494 del self.unserialized_channels
495 self.window.show_all()
497 def serialize(self, object_backend):
498 object_backend.add_property('geometry',
499 '%sx%s' % (self.window.allocation.width, self.window.allocation.height))
501 def unserialize_property(self, name, value):
502 if name == 'geometry':
503 width, height = value.split('x')
504 self.window.resize(int(width), int(height))
507 def unserialize_child(self, name):
508 if name == main_mix_serialization_name():
511 if name == input_channel_serialization_name():
512 channel = input_channel(self, "", True)
513 self.unserialized_channels.append(channel)
516 if name == output_channel_serialization_name():
517 channel = output_channel(self, "", True)
518 self.unserialized_channels.append(channel)
521 def serialization_get_childs(self):
522 '''Get child objects tha required and support serialization'''
523 childs = self.channels[:] + self.output_channels[:]
524 childs.append(self.main_mix)
527 def serialization_name(self):
531 self.main_mix.realize()
532 self.main_mix.set_monitored()
537 self.window.show_all()
541 #f = file("/dev/stdout", "w")
546 print "Usage: %s [mixer_name]" % sys.argv[0]
549 if lash: # If LASH python bindings are available
550 # sys.argv is modified by this call
551 lash_client = lash.init(sys.argv, "jack_mixer", lash.LASH_Config_File)
555 parser = OptionParser()
556 parser.add_option('-c', '--config', dest='config')
557 options, args = parser.parse_args()
559 # Yeah , this sounds stupid, we connected earlier, but we dont want to show this if we got --help option
560 # This issue should be fixed in pylash, there is a reason for having two functions for initialization after all
562 print "Successfully connected to LASH server at " + lash.lash_get_server_name(lash_client)
570 name = "jack_mixer-%u" % os.getpid()
572 gtk.gdk.threads_init()
574 mixer = jack_mixer(name, lash_client)
576 err = gtk.MessageDialog(None,
580 "Mixer creation failed (%s)" % str(e))
586 f = file(options.config)
587 mixer.current_filename = options.config
588 mixer.load_from_xml(f)
589 mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)),300)
596 if __name__ == "__main__":