]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.py
Edit Python sys.path earlier
[jack_mixer.git] / jack_mixer.py
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 #
4 # This file is part of jack_mixer
5 #
6 # Copyright (C) 2006-2009 Nedko Arnaudov <nedko@arnaudov.name>
7 # Copyright (C) 2009 Frederic Peters <fpeters@0d.be>
8 #
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
12 #
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.
17 #
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.
21
22 from optparse import OptionParser
23
24 import gtk
25 import gobject
26 import sys
27 import os
28
29 old_path = sys.path
30 sys.path.insert(0, os.path.dirname(sys.argv[0]) + os.sep + ".." + os.sep + "share"+ os.sep + "jack_mixer")
31
32 import jack_mixer_c
33 import scale
34
35 try:
36     import lash
37 except:
38     lash = None
39
40 from channel import *
41 import gui
42 from preferences import PreferencesDialog
43
44 sys.path = old_path
45
46 from serialization_xml import xml_serialization
47 from serialization import serialized_object, serializator
48
49 if lash is None:
50     print >> sys.stderr, "Cannot load LASH python bindings, you want them unless you enjoy manual jack plumbing each time you use this app"
51
52 class jack_mixer(serialized_object):
53
54     # scales suitable as meter scales
55     meter_scales = [scale.iec_268(), scale.linear_70dB(), scale.iec_268_minimalistic()]
56
57     # scales suitable as volume slider scales
58     slider_scales = [scale.linear_30dB(), scale.linear_70dB()]
59
60     # name of settngs file that is currently open
61     current_filename = None
62
63     def __init__(self, name, lash_client):
64         self.mixer = jack_mixer_c.Mixer(name)
65         if not self.mixer:
66             return
67         self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
68
69         if lash_client:
70             # Send our client name to server
71             lash_event = lash.lash_event_new_with_type(lash.LASH_Client_Name)
72             lash.lash_event_set_string(lash_event, name)
73             lash.lash_send_event(lash_client, lash_event)
74
75             lash.lash_jack_client_name(lash_client, name)
76
77         gtk.window_set_default_icon_name('jack_mixer')
78
79         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
80         self.window.set_title(name)
81
82         self.gui_factory = gui.factory(self.window, self.meter_scales, self.slider_scales)
83
84         self.vbox_top = gtk.VBox()
85         self.window.add(self.vbox_top)
86
87         self.menubar = gtk.MenuBar()
88         self.vbox_top.pack_start(self.menubar, False)
89
90         mixer_menu_item = gtk.MenuItem("_Mixer")
91         self.menubar.append(mixer_menu_item)
92         edit_menu_item = gtk.MenuItem('_Edit')
93         self.menubar.append(edit_menu_item)
94         help_menu_item = gtk.MenuItem('_Help')
95         self.menubar.append(help_menu_item)
96
97         self.window.set_default_size(120,300)
98
99         mixer_menu = gtk.Menu()
100         mixer_menu_item.set_submenu(mixer_menu)
101
102         add_input_channel = gtk.ImageMenuItem('New _Input Channel')
103         mixer_menu.append(add_input_channel)
104         add_input_channel.connect("activate", self.on_add_input_channel)
105
106         add_output_channel = gtk.ImageMenuItem('New _Output Channel')
107         mixer_menu.append(add_output_channel)
108         add_output_channel.connect("activate", self.on_add_output_channel)
109
110         if lash_client is None and xml_serialization is not None:
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)
121
122         mixer_menu.append(gtk.SeparatorMenuItem())
123
124         quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
125         mixer_menu.append(quit)
126         quit.connect('activate', self.on_quit_cb)
127
128         edit_menu = gtk.Menu()
129         edit_menu_item.set_submenu(edit_menu)
130
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)
135
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)
139
140         edit_menu.append(gtk.SeparatorMenuItem())
141
142         preferences = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
143         preferences.connect('activate', self.on_preferences_cb)
144         edit_menu.append(preferences)
145
146         help_menu = gtk.Menu()
147         help_menu_item.set_submenu(help_menu)
148
149         about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
150         help_menu.append(about)
151         about.connect("activate", self.on_about)
152
153         self.hbox_top = gtk.HBox()
154         self.vbox_top.pack_start(self.hbox_top, True)
155
156         self.scrolled_window = gtk.ScrolledWindow()
157         self.hbox_top.pack_start(self.scrolled_window, True)
158
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)
164         self.channels = []
165         self.output_channels = []
166
167         self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
168         self.scrolled_window.add_with_viewport(self.hbox_inputs)
169
170         self.main_mix = main_mix(self)
171         self.hbox_outputs = gtk.HBox()
172         self.hbox_outputs.set_spacing(0)
173         self.hbox_outputs.set_border_width(0)
174         frame = gtk.Frame()
175         frame.add(self.main_mix)
176         self.hbox_outputs.pack_start(frame, False)
177         self.hbox_top.pack_start(self.hbox_outputs, False)
178
179         self.window.connect("destroy", gtk.main_quit)
180
181         gobject.timeout_add(80, self.read_meters)
182         self.lash_client = lash_client
183
184         if lash_client:
185             gobject.timeout_add(1000, self.lash_check_events)
186
187     def cleanup(self):
188         print "Cleaning jack_mixer"
189         if not self.mixer:
190             return
191
192         for channel in self.channels:
193             channel.unrealize()
194
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()
203             try:
204                 f = file(filename, 'r')
205                 self.load_from_xml(f)
206             except:
207                 # TODO: display error in a dialog box
208                 print >> sys.stderr, 'Failed to read', filename
209             else:
210                 self.current_filename = filename
211             finally:
212                 f.close()
213         dlg.destroy()
214
215     def on_save_cb(self, *args):
216         if not self.current_filename:
217             return self.on_save_as_cb()
218         f = file(self.current_filename, 'w')
219         self.save_to_xml(f)
220         f.close()
221
222     def on_save_as_cb(self, *args):
223         dlg = gtk.FileChooserDialog(title='Save', parent=self.window,
224                         action=gtk.FILE_CHOOSER_ACTION_SAVE,
225                         buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
226                                  gtk.STOCK_SAVE, gtk.RESPONSE_OK))
227         dlg.set_default_response(gtk.RESPONSE_OK)
228         if dlg.run() == gtk.RESPONSE_OK:
229             self.current_filename = dlg.get_filename()
230             self.on_save_cb()
231         dlg.destroy()
232
233     def on_quit_cb(self, *args):
234         gtk.main_quit()
235
236     preferences_dialog = None
237     def on_preferences_cb(self, widget):
238         if not self.preferences_dialog:
239             self.preferences_dialog = PreferencesDialog(self)
240         self.preferences_dialog.show()
241         self.preferences_dialog.present()
242
243     def on_add_input_channel(self, widget):
244         dialog = NewChannelDialog(app=self)
245         dialog.set_transient_for(self.window)
246         dialog.show()
247         ret = dialog.run()
248         dialog.hide()
249
250         if ret == gtk.RESPONSE_OK:
251             result = dialog.get_result()
252             channel = self.add_channel(**result)
253             self.window.show_all()
254
255     def on_add_output_channel(self, widget):
256         dialog = NewOutputChannelDialog(app=self)
257         dialog.set_transient_for(self.window)
258         dialog.show()
259         ret = dialog.run()
260         dialog.hide()
261
262         if ret == gtk.RESPONSE_OK:
263             result = dialog.get_result()
264             channel = self.add_output_channel(**result)
265             self.window.show_all()
266
267     def on_remove_channel(self, widget, channel):
268         print 'Removing channel "%s"' % channel.channel_name
269         self.channel_remove_menu.remove(widget)
270         if self.monitored_channel is channel:
271             channel.monitor_button.set_active(False)
272         for i in range(len(self.channels)):
273             if self.channels[i] is channel:
274                 channel.unrealize()
275                 del self.channels[i]
276                 self.hbox_inputs.remove(channel.parent)
277                 break
278         if len(self.channels) == 0:
279             self.channel_remove_menu_item.set_sensitive(False)
280
281     def on_channels_clear(self, widget):
282         for channel in self.channels:
283             channel.unrealize()
284             self.hbox_inputs.remove(channel.parent)
285         self.channels = []
286         self.channel_remove_menu = gtk.Menu()
287         self.channel_remove_menu_item.set_submenu(self.channel_remove_menu)
288         self.channel_remove_menu_item.set_sensitive(False)
289
290     def add_channel(self, name, stereo, volume_cc, balance_cc):
291         try:
292             channel = input_channel(self, name, stereo)
293             self.add_channel_precreated(channel)
294         except Exception:
295             err = gtk.MessageDialog(self.window,
296                             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
297                             gtk.MESSAGE_ERROR,
298                             gtk.BUTTONS_OK,
299                             "Channel creation failed")
300             err.run()
301             err.destroy()
302             return
303         if volume_cc:
304             channel.channel.volume_midi_cc = int(volume_cc)
305         if balance_cc:
306             channel.channel.balance_midi_cc = int(balance_cc)
307         if not (volume_cc or balance_cc):
308             channel.channel.autoset_midi_cc()
309         return channel
310
311     def add_channel_precreated(self, channel):
312         frame = gtk.Frame()
313         frame.add(channel)
314         self.hbox_inputs.pack_start(frame, False)
315         channel.realize()
316         channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
317         self.channel_remove_menu.append(channel_remove_menu_item)
318         channel_remove_menu_item.connect("activate", self.on_remove_channel, channel)
319         self.channel_remove_menu_item.set_sensitive(True)
320         self.channels.append(channel)
321
322         for outputchannel in self.output_channels:
323             channel.add_control_group(outputchannel)
324
325     def read_meters(self):
326         for channel in self.channels:
327             channel.read_meter()
328         self.main_mix.read_meter()
329         for channel in self.output_channels:
330             channel.read_meter()
331         return True
332
333     def add_output_channel(self, name, stereo, volume_cc, balance_cc, display_solo_buttons):
334         try:
335             channel = output_channel(self, name, stereo)
336             channel.display_solo_buttons = display_solo_buttons
337             self.add_output_channel_precreated(channel)
338         except Exception:
339             raise
340             err = gtk.MessageDialog(self.window,
341                             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
342                             gtk.MESSAGE_ERROR,
343                             gtk.BUTTONS_OK,
344                             "Channel creation failed")
345             err.run()
346             err.destroy()
347             return
348         if volume_cc:
349             channel.channel.volume_midi_cc = int(volume_cc)
350         if balance_cc:
351             channel.channel.balance_midi_cc = int(balance_cc)
352         return channel
353
354     def add_output_channel_precreated(self, channel):
355         frame = gtk.Frame()
356         frame.add(channel)
357         self.hbox_outputs.pack_start(frame, False)
358         channel.realize()
359         # XXX: handle deletion of output channels
360         #channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
361         #self.channel_remove_menu.append(channel_remove_menu_item)
362         #channel_remove_menu_item.connect("activate", self.on_remove_channel, channel, channel_remove_menu_item)
363         #self.channel_remove_menu_item.set_sensitive(True)
364         self.output_channels.append(channel)
365
366     _monitored_channel = None
367     def get_monitored_channel(self):
368         return self._monitored_channel
369
370     def set_monitored_channel(self, channel):
371         if self._monitored_channel:
372             if channel.channel.name == self._monitored_channel.channel.name:
373                 return
374         self._monitored_channel = channel
375         if type(channel) is input_channel:
376             # reset all solo/mute settings
377             for in_channel in self.channels:
378                 self.monitor_channel.set_solo(in_channel.channel, False)
379                 self.monitor_channel.set_muted(in_channel.channel, False)
380             self.monitor_channel.set_solo(channel.channel, True)
381             self.monitor_channel.prefader = True
382         else:
383             self.monitor_channel.prefader = False
384         self.update_monitor(channel)
385     monitored_channel = property(get_monitored_channel, set_monitored_channel)
386
387     def update_monitor(self, channel):
388         if self.monitored_channel is not channel:
389             return
390         self.monitor_channel.volume = channel.channel.volume
391         self.monitor_channel.balance = channel.channel.balance
392         if type(self.monitored_channel) is output_channel:
393             # sync solo/muted channels
394             for input_channel in self.channels:
395                 self.monitor_channel.set_solo(input_channel.channel,
396                                 channel.channel.is_solo(input_channel.channel))
397                 self.monitor_channel.set_muted(input_channel.channel,
398                                 channel.channel.is_muted(input_channel.channel))
399         elif type(self.monitored_channel) is main_mix:
400             # sync solo/muted channels
401             for input_channel in self.channels:
402                 self.monitor_channel.set_solo(input_channel.channel,
403                                 input_channel.channel.solo)
404                 self.monitor_channel.set_muted(input_channel.channel,
405                                 input_channel.channel.mute)
406
407     def get_input_channel_by_name(self, name):
408         for input_channel in self.channels:
409             if input_channel.channel.name == name:
410                 return input_channel
411         return None
412
413     def on_about(self, *args):
414         about = gtk.AboutDialog()
415         about.set_name('jack_mixer')
416         about.set_copyright('Copyright © 2006-2009\nNedko Arnaudov, Frederic Peters')
417         about.set_license('''\
418 jack_mixer is free software; you can redistribute it and/or modify it
419 under the terms of the GNU General Public License as published by the
420 Free Software Foundation; either version 2 of the License, or (at your
421 option) any later version.
422
423 jack_mixer is distributed in the hope that it will be useful, but
424 WITHOUT ANY WARRANTY; without even the implied warranty of
425 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
426 General Public License for more details.
427
428 You should have received a copy of the GNU General Public License along
429 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
430 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
431         about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
432                            'Frederic Peters <fpeters@0d.be>'])
433         about.set_logo_icon_name('jack_mixer')
434         about.set_website('http://home.gna.org/jackmixer/')
435
436         about.run()
437         about.destroy()
438
439     def lash_check_events(self):
440         while lash.lash_get_pending_event_count(self.lash_client):
441             event = lash.lash_get_event(self.lash_client)
442
443             #print repr(event)
444
445             event_type = lash.lash_event_get_type(event)
446             if event_type == lash.LASH_Quit:
447                 print "jack_mixer: LASH ordered quit."
448                 gtk.main_quit()
449                 return False
450             elif event_type == lash.LASH_Save_File:
451                 directory = lash.lash_event_get_string(event)
452                 print "jack_mixer: LASH ordered to save data in directory %s" % directory
453                 filename = directory + os.sep + "jack_mixer.xml"
454                 f = file(filename, "w")
455                 self.save_to_xml(f)
456                 f.close()
457                 lash.lash_send_event(self.lash_client, event) # we crash with double free
458             elif event_type == lash.LASH_Restore_File:
459                 directory = lash.lash_event_get_string(event)
460                 print "jack_mixer: LASH ordered to restore data from directory %s" % directory
461                 filename = directory + os.sep + "jack_mixer.xml"
462                 f = file(filename, "r")
463                 self.load_from_xml(f)
464                 f.close()
465                 lash.lash_send_event(self.lash_client, event)
466             else:
467                 print "jack_mixer: Got unhandled LASH event, type " + str(event_type)
468                 return True
469
470             #lash.lash_event_destroy(event)
471
472         return True
473
474     def save_to_xml(self, file):
475         #print "Saving to XML..."
476         b = xml_serialization()
477         s = serializator()
478         s.serialize(self, b)
479         b.save(file)
480
481     def load_from_xml(self, file):
482         #print "Loading from XML..."
483         self.on_channels_clear(None)
484         self.unserialized_channels = []
485         b = xml_serialization()
486         b.load(file)
487         s = serializator()
488         s.unserialize(self, b)
489         for channel in self.unserialized_channels:
490             if isinstance(channel, input_channel):
491                 self.add_channel_precreated(channel)
492         for channel in self.unserialized_channels:
493             if isinstance(channel, output_channel):
494                 self.add_output_channel_precreated(channel)
495         del self.unserialized_channels
496         self.window.show_all()
497
498     def serialize(self, object_backend):
499         object_backend.add_property('geometry',
500                         '%sx%s' % (self.window.allocation.width, self.window.allocation.height))
501
502     def unserialize_property(self, name, value):
503         if name == 'geometry':
504             width, height = value.split('x')
505             self.window.resize(int(width), int(height))
506             return True
507
508     def unserialize_child(self, name):
509         if name == main_mix_serialization_name():
510             return self.main_mix
511
512         if name == input_channel_serialization_name():
513             channel = input_channel(self, "", True)
514             self.unserialized_channels.append(channel)
515             return channel
516
517         if name == output_channel_serialization_name():
518             channel = output_channel(self, "", True)
519             self.unserialized_channels.append(channel)
520             return channel
521
522     def serialization_get_childs(self):
523         '''Get child objects tha required and support serialization'''
524         childs = self.channels[:] + self.output_channels[:]
525         childs.append(self.main_mix)
526         return childs
527
528     def serialization_name(self):
529         return "jack_mixer"
530
531     def main(self):
532         self.main_mix.realize()
533         self.main_mix.set_monitored()
534
535         if not self.mixer:
536             return
537
538         self.window.show_all()
539
540         gtk.main()
541
542         #f = file("/dev/stdout", "w")
543         #self.save_to_xml(f)
544         #f.close
545
546 def help():
547     print "Usage: %s [mixer_name]" % sys.argv[0]
548
549 def main():
550     if lash:                        # If LASH python bindings are available
551         # sys.argv is modified by this call
552         lash_client = lash.init(sys.argv, "jack_mixer", lash.LASH_Config_File)
553     else:
554         lash_client = None
555
556     parser = OptionParser()
557     parser.add_option('-c', '--config', dest='config')
558     options, args = parser.parse_args()
559
560     # Yeah , this sounds stupid, we connected earlier, but we dont want to show this if we got --help option
561     # This issue should be fixed in pylash, there is a reason for having two functions for initialization after all
562     if lash_client:
563         print "Successfully connected to LASH server at " +  lash.lash_get_server_name(lash_client)
564
565     if len(args) == 1:
566         name = args[0]
567     else:
568         name = None
569
570     if not name:
571         name = "jack_mixer-%u" % os.getpid()
572
573     gtk.gdk.threads_init()
574     try:
575         mixer = jack_mixer(name, lash_client)
576     except Exception, e:
577         err = gtk.MessageDialog(None,
578                             gtk.DIALOG_MODAL,
579                             gtk.MESSAGE_ERROR,
580                             gtk.BUTTONS_OK,
581                             "Mixer creation failed (%s)" % str(e))
582         err.run()
583         err.destroy()
584         sys.exit(1)
585
586     if options.config:
587         f = file(options.config)
588         mixer.current_filename = options.config
589         mixer.load_from_xml(f)
590         mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)),300)
591         f.close()
592
593     mixer.main()
594
595     mixer.cleanup()
596
597 if __name__ == "__main__":
598     main()