]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.py
Fixed rotation of output channel colours
[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 try:
30     import lash
31 except:
32     lash = None
33     print >> sys.stderr, "Cannot load LASH python bindings, you want them unless you enjoy manual jack plumbing each time you use this app"
34
35 # temporary change Python modules lookup path to look into installation
36 # directory ($prefix/share/jack_mixer/)
37 old_path = sys.path
38 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..', 'share', 'jack_mixer'))
39
40 import jack_mixer_c
41 import scale
42 from channel import *
43
44 import gui
45 from preferences import PreferencesDialog
46
47 from serialization_xml import XmlSerialization
48 from serialization import SerializedObject, Serializator
49
50 # restore Python modules lookup path
51 sys.path = old_path
52
53 class JackMixer(SerializedObject):
54
55     # scales suitable as meter scales
56     meter_scales = [scale.IEC268(), scale.Linear70dB(), scale.IEC268Minimalistic()]
57
58     # scales suitable as volume slider scales
59     slider_scales = [scale.Linear30dB(), scale.Linear70dB()]
60
61     # name of settngs file that is currently open
62     current_filename = None
63
64     def __init__(self, name, lash_client):
65         self.mixer = jack_mixer_c.Mixer(name)
66         if not self.mixer:
67             return
68         self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
69
70         if lash_client:
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)
75
76             lash.lash_jack_client_name(lash_client, name)
77
78         gtk.window_set_default_icon_name('jack_mixer')
79
80         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
81         self.window.set_title(name)
82
83         self.gui_factory = gui.Factory(self.window, self.meter_scales, self.slider_scales)
84
85         self.vbox_top = gtk.VBox()
86         self.window.add(self.vbox_top)
87
88         self.menubar = gtk.MenuBar()
89         self.vbox_top.pack_start(self.menubar, False)
90
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)
97
98         self.window.set_default_size(120, 300)
99
100         mixer_menu = gtk.Menu()
101         mixer_menu_item.set_submenu(mixer_menu)
102
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)
106
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)
110
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 = MainMixChannel(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                 err = gtk.MessageDialog(self.window,
208                             gtk.DIALOG_MODAL,
209                             gtk.MESSAGE_ERROR,
210                             gtk.BUTTONS_OK,
211                             "Failed loading settings.")
212                 err.run()
213                 err.destroy()
214             else:
215                 self.current_filename = filename
216             finally:
217                 f.close()
218         dlg.destroy()
219
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')
224         self.save_to_xml(f)
225         f.close()
226
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()
235             self.on_save_cb()
236         dlg.destroy()
237
238     def on_quit_cb(self, *args):
239         gtk.main_quit()
240
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()
247
248     def on_add_input_channel(self, widget):
249         dialog = NewChannelDialog(app=self)
250         dialog.set_transient_for(self.window)
251         dialog.show()
252         ret = dialog.run()
253         dialog.hide()
254
255         if ret == gtk.RESPONSE_OK:
256             result = dialog.get_result()
257             channel = self.add_channel(**result)
258             self.window.show_all()
259
260     def on_add_output_channel(self, widget):
261         dialog = NewOutputChannelDialog(app=self)
262         dialog.set_transient_for(self.window)
263         dialog.show()
264         ret = dialog.run()
265         dialog.hide()
266
267         if ret == gtk.RESPONSE_OK:
268             result = dialog.get_result()
269             channel = self.add_output_channel(**result)
270             self.window.show_all()
271
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:
279                 channel.unrealize()
280                 del self.channels[i]
281                 self.hbox_inputs.remove(channel.parent)
282                 break
283         if len(self.channels) == 0:
284             self.channel_remove_menu_item.set_sensitive(False)
285
286     def on_channels_clear(self, widget):
287         for channel in self.output_channels:
288             channel.unrealize()
289             self.hbox_outputs.remove(channel.parent)
290         for channel in self.channels:
291             channel.unrealize()
292             self.hbox_inputs.remove(channel.parent)
293         self.channels = []
294         self.output_channels = []
295         self.channel_remove_menu = gtk.Menu()
296         self.channel_remove_menu_item.set_submenu(self.channel_remove_menu)
297         self.channel_remove_menu_item.set_sensitive(False)
298
299     def add_channel(self, name, stereo, volume_cc, balance_cc):
300         try:
301             channel = InputChannel(self, name, stereo)
302             self.add_channel_precreated(channel)
303         except Exception:
304             err = gtk.MessageDialog(self.window,
305                             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
306                             gtk.MESSAGE_ERROR,
307                             gtk.BUTTONS_OK,
308                             "Channel creation failed")
309             err.run()
310             err.destroy()
311             return
312         if volume_cc:
313             channel.channel.volume_midi_cc = int(volume_cc)
314         if balance_cc:
315             channel.channel.balance_midi_cc = int(balance_cc)
316         if not (volume_cc or balance_cc):
317             channel.channel.autoset_midi_cc()
318         return channel
319
320     def add_channel_precreated(self, channel):
321         frame = gtk.Frame()
322         frame.add(channel)
323         self.hbox_inputs.pack_start(frame, False)
324         channel.realize()
325         channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
326         self.channel_remove_menu.append(channel_remove_menu_item)
327         channel_remove_menu_item.connect("activate", self.on_remove_channel, channel)
328         self.channel_remove_menu_item.set_sensitive(True)
329         self.channels.append(channel)
330
331         for outputchannel in self.output_channels:
332             channel.add_control_group(outputchannel)
333
334     def read_meters(self):
335         for channel in self.channels:
336             channel.read_meter()
337         self.main_mix.read_meter()
338         for channel in self.output_channels:
339             channel.read_meter()
340         return True
341
342     def add_output_channel(self, name, stereo, volume_cc, balance_cc, display_solo_buttons):
343         try:
344             channel = OutputChannel(self, name, stereo)
345             channel.display_solo_buttons = display_solo_buttons
346             self.add_output_channel_precreated(channel)
347         except Exception:
348             err = gtk.MessageDialog(self.window,
349                             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
350                             gtk.MESSAGE_ERROR,
351                             gtk.BUTTONS_OK,
352                             "Channel creation failed")
353             err.run()
354             err.destroy()
355             return
356         if volume_cc:
357             channel.channel.volume_midi_cc = int(volume_cc)
358         if balance_cc:
359             channel.channel.balance_midi_cc = int(balance_cc)
360         return channel
361
362     def add_output_channel_precreated(self, channel):
363         frame = gtk.Frame()
364         frame.add(channel)
365         self.hbox_outputs.pack_start(frame, False)
366         channel.realize()
367         # XXX: handle deletion of output channels
368         #channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
369         #self.channel_remove_menu.append(channel_remove_menu_item)
370         #channel_remove_menu_item.connect("activate", self.on_remove_channel, channel, channel_remove_menu_item)
371         #self.channel_remove_menu_item.set_sensitive(True)
372         self.output_channels.append(channel)
373
374     _monitored_channel = None
375     def get_monitored_channel(self):
376         return self._monitored_channel
377
378     def set_monitored_channel(self, channel):
379         if self._monitored_channel:
380             if channel.channel.name == self._monitored_channel.channel.name:
381                 return
382         self._monitored_channel = channel
383         if type(channel) is InputChannel:
384             # reset all solo/mute settings
385             for in_channel in self.channels:
386                 self.monitor_channel.set_solo(in_channel.channel, False)
387                 self.monitor_channel.set_muted(in_channel.channel, False)
388             self.monitor_channel.set_solo(channel.channel, True)
389             self.monitor_channel.prefader = True
390         else:
391             self.monitor_channel.prefader = False
392         self.update_monitor(channel)
393     monitored_channel = property(get_monitored_channel, set_monitored_channel)
394
395     def update_monitor(self, channel):
396         if self.monitored_channel is not channel:
397             return
398         self.monitor_channel.volume = channel.channel.volume
399         self.monitor_channel.balance = channel.channel.balance
400         if type(self.monitored_channel) is OutputChannel:
401             # sync solo/muted channels
402             for input_channel in self.channels:
403                 self.monitor_channel.set_solo(input_channel.channel,
404                                 channel.channel.is_solo(input_channel.channel))
405                 self.monitor_channel.set_muted(input_channel.channel,
406                                 channel.channel.is_muted(input_channel.channel))
407         elif type(self.monitored_channel) is MainMixChannel:
408             # sync solo/muted channels
409             for input_channel in self.channels:
410                 self.monitor_channel.set_solo(input_channel.channel,
411                                 input_channel.channel.solo)
412                 self.monitor_channel.set_muted(input_channel.channel,
413                                 input_channel.channel.mute)
414
415     def get_input_channel_by_name(self, name):
416         for input_channel in self.channels:
417             if input_channel.channel.name == name:
418                 return input_channel
419         return None
420
421     def on_about(self, *args):
422         about = gtk.AboutDialog()
423         about.set_name('jack_mixer')
424         about.set_copyright('Copyright © 2006-2009\nNedko Arnaudov, Frederic Peters')
425         about.set_license('''\
426 jack_mixer is free software; you can redistribute it and/or modify it
427 under the terms of the GNU General Public License as published by the
428 Free Software Foundation; either version 2 of the License, or (at your
429 option) any later version.
430
431 jack_mixer is distributed in the hope that it will be useful, but
432 WITHOUT ANY WARRANTY; without even the implied warranty of
433 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
434 General Public License for more details.
435
436 You should have received a copy of the GNU General Public License along
437 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
438 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
439         about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
440                            'Frederic Peters <fpeters@0d.be>'])
441         about.set_logo_icon_name('jack_mixer')
442         about.set_website('http://home.gna.org/jackmixer/')
443
444         about.run()
445         about.destroy()
446
447     def lash_check_events(self):
448         while lash.lash_get_pending_event_count(self.lash_client):
449             event = lash.lash_get_event(self.lash_client)
450
451             #print repr(event)
452
453             event_type = lash.lash_event_get_type(event)
454             if event_type == lash.LASH_Quit:
455                 print "jack_mixer: LASH ordered quit."
456                 gtk.main_quit()
457                 return False
458             elif event_type == lash.LASH_Save_File:
459                 directory = lash.lash_event_get_string(event)
460                 print "jack_mixer: LASH ordered to save data in directory %s" % directory
461                 filename = directory + os.sep + "jack_mixer.xml"
462                 f = file(filename, "w")
463                 self.save_to_xml(f)
464                 f.close()
465                 lash.lash_send_event(self.lash_client, event) # we crash with double free
466             elif event_type == lash.LASH_Restore_File:
467                 directory = lash.lash_event_get_string(event)
468                 print "jack_mixer: LASH ordered to restore data from directory %s" % directory
469                 filename = directory + os.sep + "jack_mixer.xml"
470                 f = file(filename, "r")
471                 self.load_from_xml(f, silence_errors=True)
472                 f.close()
473                 lash.lash_send_event(self.lash_client, event)
474             else:
475                 print "jack_mixer: Got unhandled LASH event, type " + str(event_type)
476                 return True
477
478             #lash.lash_event_destroy(event)
479
480         return True
481
482     def save_to_xml(self, file):
483         #print "Saving to XML..."
484         b = XmlSerialization()
485         s = Serializator()
486         s.serialize(self, b)
487         b.save(file)
488
489     def load_from_xml(self, file, silence_errors=False):
490         #print "Loading from XML..."
491         self.on_channels_clear(None)
492         self.unserialized_channels = []
493         b = XmlSerialization()
494         try:
495             b.load(file)
496         except:
497             if silence_errors:
498                 return
499             raise
500         s = Serializator()
501         s.unserialize(self, b)
502         for channel in self.unserialized_channels:
503             if isinstance(channel, InputChannel):
504                 self.add_channel_precreated(channel)
505         for channel in self.unserialized_channels:
506             if isinstance(channel, OutputChannel):
507                 self.add_output_channel_precreated(channel)
508         del self.unserialized_channels
509         self.window.show_all()
510
511     def serialize(self, object_backend):
512         object_backend.add_property('geometry',
513                         '%sx%s' % (self.window.allocation.width, self.window.allocation.height))
514
515     def unserialize_property(self, name, value):
516         if name == 'geometry':
517             width, height = value.split('x')
518             self.window.resize(int(width), int(height))
519             return True
520
521     def unserialize_child(self, name):
522         if name == main_mix_serialization_name():
523             return self.main_mix
524
525         if name == input_channel_serialization_name():
526             channel = InputChannel(self, "", True)
527             self.unserialized_channels.append(channel)
528             return channel
529
530         if name == output_channel_serialization_name():
531             channel = OutputChannel(self, "", True)
532             self.unserialized_channels.append(channel)
533             return channel
534
535     def serialization_get_childs(self):
536         '''Get child objects tha required and support serialization'''
537         childs = self.channels[:] + self.output_channels[:]
538         childs.append(self.main_mix)
539         return childs
540
541     def serialization_name(self):
542         return "jack_mixer"
543
544     def main(self):
545         self.main_mix.realize()
546         self.main_mix.set_monitored()
547
548         if not self.mixer:
549             return
550
551         self.window.show_all()
552
553         gtk.main()
554
555         #f = file("/dev/stdout", "w")
556         #self.save_to_xml(f)
557         #f.close
558
559 def help():
560     print "Usage: %s [mixer_name]" % sys.argv[0]
561
562 def main():
563     if lash:                        # If LASH python bindings are available
564         # sys.argv is modified by this call
565         lash_client = lash.init(sys.argv, "jack_mixer", lash.LASH_Config_File)
566     else:
567         lash_client = None
568
569     parser = OptionParser()
570     parser.add_option('-c', '--config', dest='config')
571     options, args = parser.parse_args()
572
573     # Yeah , this sounds stupid, we connected earlier, but we dont want to show this if we got --help option
574     # This issue should be fixed in pylash, there is a reason for having two functions for initialization after all
575     if lash_client:
576         print "Successfully connected to LASH server at " +  lash.lash_get_server_name(lash_client)
577
578     if len(args) == 1:
579         name = args[0]
580     else:
581         name = None
582
583     if not name:
584         name = "jack_mixer-%u" % os.getpid()
585
586     gtk.gdk.threads_init()
587     try:
588         mixer = JackMixer(name, lash_client)
589     except Exception, e:
590         err = gtk.MessageDialog(None,
591                             gtk.DIALOG_MODAL,
592                             gtk.MESSAGE_ERROR,
593                             gtk.BUTTONS_OK,
594                             "Mixer creation failed (%s)" % str(e))
595         err.run()
596         err.destroy()
597         sys.exit(1)
598
599     if options.config:
600         f = file(options.config)
601         mixer.current_filename = options.config
602         try:
603             mixer.load_from_xml(f)
604         except:
605             err = gtk.MessageDialog(mixer.window,
606                             gtk.DIALOG_MODAL,
607                             gtk.MESSAGE_ERROR,
608                             gtk.BUTTONS_OK,
609                             "Failed loading settings.")
610             err.run()
611             err.destroy()
612         mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
613         f.close()
614
615     mixer.main()
616
617     mixer.cleanup()
618
619 if __name__ == "__main__":
620     main()