]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.py
Renamed bootstrap to autogen.sh to match what is being done in other projects
[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 import jack_mixer_c
30 import scale
31
32 try:
33     import lash
34 except:
35     lash = None
36
37 old_path = sys.path
38 sys.path.insert(0, os.path.dirname(sys.argv[0]) + os.sep + ".." + os.sep + "share"+ os.sep + "jack_mixer")
39 from channel import *
40 import gui
41 from preferences import PreferencesDialog
42
43 sys.path = old_path
44
45 from serialization_xml import xml_serialization
46 from serialization import serialized_object, serializator
47
48 if lash is None:
49     print >> sys.stderr, "Cannot load LASH python bindings, you want them unless you enjoy manual jack plumbing each time you use this app"
50
51 class jack_mixer(serialized_object):
52
53     # scales suitable as meter scales
54     meter_scales = [scale.iec_268(), scale.linear_70dB(), scale.iec_268_minimalistic()]
55
56     # scales suitable as volume slider scales
57     slider_scales = [scale.linear_30dB(), scale.linear_70dB()]
58
59     # name of settngs file that is currently open
60     current_filename = None
61
62     def __init__(self, name, lash_client):
63         self.mixer = jack_mixer_c.Mixer(name)
64         if not self.mixer:
65             return
66         self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
67
68         if lash_client:
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)
73
74             lash.lash_jack_client_name(lash_client, name)
75
76         gtk.window_set_default_icon_name('jack_mixer')
77
78         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
79         self.window.set_title(name)
80
81         self.gui_factory = gui.factory(self.window, self.meter_scales, self.slider_scales)
82
83         self.vbox_top = gtk.VBox()
84         self.window.add(self.vbox_top)
85
86         self.menubar = gtk.MenuBar()
87         self.vbox_top.pack_start(self.menubar, False)
88
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)
95
96         self.window.set_default_size(120,300)
97
98         mixer_menu = gtk.Menu()
99         mixer_menu_item.set_submenu(mixer_menu)
100
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)
104
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)
108
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)
120
121         mixer_menu.append(gtk.SeparatorMenuItem())
122
123         quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
124         mixer_menu.append(quit)
125         quit.connect('activate', self.on_quit_cb)
126
127         edit_menu = gtk.Menu()
128         edit_menu_item.set_submenu(edit_menu)
129
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)
134
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)
138
139         edit_menu.append(gtk.SeparatorMenuItem())
140
141         preferences = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
142         preferences.connect('activate', self.on_preferences_cb)
143         edit_menu.append(preferences)
144
145         help_menu = gtk.Menu()
146         help_menu_item.set_submenu(help_menu)
147
148         about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
149         help_menu.append(about)
150         about.connect("activate", self.on_about)
151
152         self.hbox_top = gtk.HBox()
153         self.vbox_top.pack_start(self.hbox_top, True)
154
155         self.scrolled_window = gtk.ScrolledWindow()
156         self.hbox_top.pack_start(self.scrolled_window, True)
157
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)
163         self.channels = []
164         self.output_channels = []
165
166         self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
167         self.scrolled_window.add_with_viewport(self.hbox_inputs)
168
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)
173         frame = gtk.Frame()
174         frame.add(self.main_mix)
175         self.hbox_outputs.pack_start(frame, False)
176         self.hbox_top.pack_start(self.hbox_outputs, False)
177
178         self.window.connect("destroy", gtk.main_quit)
179
180         gobject.timeout_add(80, self.read_meters)
181         self.lash_client = lash_client
182
183         if lash_client:
184             gobject.timeout_add(1000, self.lash_check_events)
185
186     def cleanup(self):
187         print "Cleaning jack_mixer"
188         if not self.mixer:
189             return
190
191         for channel in self.channels:
192             channel.unrealize()
193
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()
202             try:
203                 f = file(filename, 'r')
204                 self.load_from_xml(f)
205             except:
206                 # TODO: display error in a dialog box
207                 print >> sys.stderr, 'Failed to read', filename
208             else:
209                 self.current_filename = filename
210             finally:
211                 f.close()
212         dlg.destroy()
213
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')
218         self.save_to_xml(f)
219         f.close()
220
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()
229             self.on_save_cb()
230         dlg.destroy()
231
232     def on_quit_cb(self, *args):
233         gtk.main_quit()
234
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()
241
242     def on_add_input_channel(self, widget):
243         dialog = NewChannelDialog(app=self)
244         dialog.set_transient_for(self.window)
245         dialog.show()
246         ret = dialog.run()
247         dialog.hide()
248
249         if ret == gtk.RESPONSE_OK:
250             result = dialog.get_result()
251             channel = self.add_channel(**result)
252             self.window.show_all()
253
254     def on_add_output_channel(self, widget):
255         dialog = NewOutputChannelDialog(app=self)
256         dialog.set_transient_for(self.window)
257         dialog.show()
258         ret = dialog.run()
259         dialog.hide()
260
261         if ret == gtk.RESPONSE_OK:
262             result = dialog.get_result()
263             channel = self.add_output_channel(**result)
264             self.window.show_all()
265
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:
273                 channel.unrealize()
274                 del self.channels[i]
275                 self.hbox_inputs.remove(channel.parent)
276                 break
277         if len(self.channels) == 0:
278             self.channel_remove_menu_item.set_sensitive(False)
279
280     def on_channels_clear(self, widget):
281         for channel in self.channels:
282             channel.unrealize()
283             self.hbox_inputs.remove(channel.parent)
284         self.channels = []
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)
288
289     def add_channel(self, name, stereo, volume_cc, balance_cc):
290         try:
291             channel = input_channel(self, name, stereo)
292             self.add_channel_precreated(channel)
293         except Exception:
294             err = gtk.MessageDialog(self.window,
295                             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
296                             gtk.MESSAGE_ERROR,
297                             gtk.BUTTONS_OK,
298                             "Channel creation failed")
299             err.run()
300             err.destroy()
301             return
302         if volume_cc:
303             channel.channel.volume_midi_cc = int(volume_cc)
304         if balance_cc:
305             channel.channel.balance_midi_cc = int(balance_cc)
306         if not (volume_cc or balance_cc):
307             channel.channel.autoset_midi_cc()
308         return channel
309
310     def add_channel_precreated(self, channel):
311         frame = gtk.Frame()
312         frame.add(channel)
313         self.hbox_inputs.pack_start(frame, False)
314         channel.realize()
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)
320
321         for outputchannel in self.output_channels:
322             channel.add_control_group(outputchannel)
323
324     def read_meters(self):
325         for channel in self.channels:
326             channel.read_meter()
327         self.main_mix.read_meter()
328         for channel in self.output_channels:
329             channel.read_meter()
330         return True
331
332     def add_output_channel(self, name, stereo, volume_cc, balance_cc, display_solo_buttons):
333         try:
334             channel = output_channel(self, name, stereo)
335             channel.display_solo_buttons = display_solo_buttons
336             self.add_output_channel_precreated(channel)
337         except Exception:
338             raise
339             err = gtk.MessageDialog(self.window,
340                             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
341                             gtk.MESSAGE_ERROR,
342                             gtk.BUTTONS_OK,
343                             "Channel creation failed")
344             err.run()
345             err.destroy()
346             return
347         if volume_cc:
348             channel.channel.volume_midi_cc = int(volume_cc)
349         if balance_cc:
350             channel.channel.balance_midi_cc = int(balance_cc)
351         return channel
352
353     def add_output_channel_precreated(self, channel):
354         frame = gtk.Frame()
355         frame.add(channel)
356         self.hbox_outputs.pack_start(frame, False)
357         channel.realize()
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)
364
365     _monitored_channel = None
366     def get_monitored_channel(self):
367         return self._monitored_channel
368
369     def set_monitored_channel(self, channel):
370         if self._monitored_channel:
371             if channel.channel.name == self._monitored_channel.channel.name:
372                 return
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
381         else:
382             self.monitor_channel.prefader = False
383         self.update_monitor(channel)
384     monitored_channel = property(get_monitored_channel, set_monitored_channel)
385
386     def update_monitor(self, channel):
387         if self.monitored_channel is not channel:
388             return
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)
405
406     def get_input_channel_by_name(self, name):
407         for input_channel in self.channels:
408             if input_channel.channel.name == name:
409                 return input_channel
410         return None
411
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.
421
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.
426
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/')
434
435         about.run()
436         about.destroy()
437
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)
441
442             #print repr(event)
443
444             event_type = lash.lash_event_get_type(event)
445             if event_type == lash.LASH_Quit:
446                 print "jack_mixer: LASH ordered quit."
447                 gtk.main_quit()
448                 return False
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")
454                 self.save_to_xml(f)
455                 f.close()
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)
463                 f.close()
464                 lash.lash_send_event(self.lash_client, event)
465             else:
466                 print "jack_mixer: Got unhandled LASH event, type " + str(event_type)
467                 return True
468
469             #lash.lash_event_destroy(event)
470
471         return True
472
473     def save_to_xml(self, file):
474         #print "Saving to XML..."
475         b = xml_serialization()
476         s = serializator()
477         s.serialize(self, b)
478         b.save(file)
479
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()
485         b.load(file)
486         s = serializator()
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()
496
497     def serialize(self, object_backend):
498         object_backend.add_property('geometry',
499                         '%sx%s' % (self.window.allocation.width, self.window.allocation.height))
500
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))
505             return True
506
507     def unserialize_child(self, name):
508         if name == main_mix_serialization_name():
509             return self.main_mix
510
511         if name == input_channel_serialization_name():
512             channel = input_channel(self, "", True)
513             self.unserialized_channels.append(channel)
514             return channel
515
516         if name == output_channel_serialization_name():
517             channel = output_channel(self, "", True)
518             self.unserialized_channels.append(channel)
519             return channel
520
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)
525         return childs
526
527     def serialization_name(self):
528         return "jack_mixer"
529
530     def main(self):
531         self.main_mix.realize()
532         self.main_mix.set_monitored()
533
534         if not self.mixer:
535             return
536
537         self.window.show_all()
538
539         gtk.main()
540
541         #f = file("/dev/stdout", "w")
542         #self.save_to_xml(f)
543         #f.close
544
545 def help():
546     print "Usage: %s [mixer_name]" % sys.argv[0]
547
548 def main():
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)
552     else:
553         lash_client = None
554
555     parser = OptionParser()
556     parser.add_option('-c', '--config', dest='config')
557     options, args = parser.parse_args()
558
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
561     if lash_client:
562         print "Successfully connected to LASH server at " +  lash.lash_get_server_name(lash_client)
563
564     if len(args) == 1:
565         name = args[0]
566     else:
567         name = None
568
569     if not name:
570         name = "jack_mixer-%u" % os.getpid()
571
572     gtk.gdk.threads_init()
573     try:
574         mixer = jack_mixer(name, lash_client)
575     except Exception, e:
576         err = gtk.MessageDialog(None,
577                             gtk.DIALOG_MODAL,
578                             gtk.MESSAGE_ERROR,
579                             gtk.BUTTONS_OK,
580                             "Mixer creation failed (%s)" % str(e))
581         err.run()
582         err.destroy()
583         sys.exit(1)
584
585     if options.config:
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)
590         f.close()
591
592     mixer.main()
593
594     mixer.cleanup()
595
596 if __name__ == "__main__":
597     main()