]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.py
Don't create empty project file on new NSM session (#71)
[jack_mixer.git] / jack_mixer.py
1 #!/usr/bin/env python3
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 import logging
23 import os
24 import re
25 import signal
26 import sys
27 from argparse import ArgumentParser
28
29 import gi
30 gi.require_version('Gtk', '3.0')
31 from gi.repository import Gtk
32 from gi.repository import GObject
33 from gi.repository import GLib
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
42 import gui
43 import scale
44 from channel import *
45 from nsmclient import NSMClient
46 from serialization_xml import XmlSerialization
47 from serialization import SerializedObject, Serializator
48 from preferences import PreferencesDialog
49 from version import __version__
50
51
52 # restore Python modules lookup path
53 sys.path = old_path
54 log = logging.getLogger("jack_mixer")
55
56
57 def add_number_suffix(s):
58     def inc(match):
59         return str(int(match.group(0)) + 1)
60
61     new_s = re.sub('(\d+)\s*$', inc, s)
62     if new_s == s:
63         new_s = s + ' 1'
64
65     return new_s
66
67
68 class JackMixer(SerializedObject):
69
70     # scales suitable as meter scales
71     meter_scales = [
72         scale.K20(),
73         scale.K14(),
74         scale.IEC268(),
75         scale.Linear70dB(),
76         scale.IEC268Minimalistic()
77     ]
78
79     # scales suitable as volume slider scales
80     slider_scales = [
81         scale.Linear30dB(),
82         scale.Linear70dB()
83     ]
84
85     # name of settings file that is currently open
86     current_filename = None
87
88     _init_solo_channels = None
89
90     def __init__(self, client_name='jack_mixer'):
91         self.visible = False
92         self.nsm_client = None
93
94         if os.environ.get('NSM_URL'):
95             self.nsm_client = NSMClient(
96                 prettyName="jack_mixer",
97                 saveCallback=self.nsm_save_cb,
98                 openOrNewCallback=self.nsm_open_cb,
99                 supportsSaveStatus=False,
100                 hideGUICallback=self.nsm_hide_cb,
101                 showGUICallback=self.nsm_show_cb,
102                 exitProgramCallback=self.nsm_exit_cb,
103                 loggingLevel="error",
104             )
105             self.nsm_client.announceGuiVisibility(self.visible)
106         else:
107             self.visible = True
108             self.create_mixer(client_name, with_nsm=False)
109
110     def create_mixer(self, client_name, with_nsm=True):
111         self.mixer = jack_mixer_c.Mixer(client_name)
112         if not self.mixer:
113             raise RuntimeError("Failed to create Mixer instance.")
114
115         self.create_ui(with_nsm)
116         self.window.set_title(client_name)
117
118         self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
119         self.save = False
120
121         GLib.timeout_add(33, self.read_meters)
122         GLib.timeout_add(50, self.midi_events_check)
123
124         if with_nsm:
125             GLib.timeout_add(200, self.nsm_react)
126
127     def new_menu_item(self, title, callback=None, accel=None, enabled=True):
128         menuitem = Gtk.MenuItem.new_with_mnemonic(title)
129         menuitem.set_sensitive(enabled)
130         if callback:
131             menuitem.connect("activate", callback)
132         if accel:
133             key, mod = Gtk.accelerator_parse(accel)
134             menuitem.add_accelerator("activate", self.menu_accelgroup, key, mod,
135                                      Gtk.AccelFlags.VISIBLE)
136         return menuitem
137
138     def create_ui(self, with_nsm):
139         self.channels = []
140         self.output_channels = []
141         self.window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
142         self.window.set_icon_name('jack_mixer')
143         self.gui_factory = gui.Factory(self.window, self.meter_scales, self.slider_scales)
144         self.gui_factory.connect('midi-behavior-mode-changed', self.on_midi_behavior_mode_changed)
145         self.gui_factory.emit_midi_behavior_mode()
146
147         self.vbox_top = Gtk.VBox()
148         self.window.add(self.vbox_top)
149
150         self.menu_accelgroup = Gtk.AccelGroup()
151         self.window.add_accel_group(self.menu_accelgroup)
152
153         self.menubar = Gtk.MenuBar()
154         self.vbox_top.pack_start(self.menubar, False, True, 0)
155
156         mixer_menu_item = Gtk.MenuItem.new_with_mnemonic("_Mixer")
157         self.menubar.append(mixer_menu_item)
158         edit_menu_item = Gtk.MenuItem.new_with_mnemonic('_Edit')
159         self.menubar.append(edit_menu_item)
160         help_menu_item = Gtk.MenuItem.new_with_mnemonic('_Help')
161         self.menubar.append(help_menu_item)
162
163         self.width = 420
164         self.height = 420
165         self.paned_position = 210
166         self.window.set_default_size(self.width, self.height)
167
168         self.mixer_menu = Gtk.Menu()
169         mixer_menu_item.set_submenu(self.mixer_menu)
170
171         self.mixer_menu.append(self.new_menu_item('New _Input Channel',
172                                                   self.on_add_input_channel, "<Control>N"))
173         self.mixer_menu.append(self.new_menu_item('New Output _Channel',
174                                                   self.on_add_output_channel, "<Shift><Control>N"))
175
176         self.mixer_menu.append(Gtk.SeparatorMenuItem())
177         if not with_nsm:
178             self.mixer_menu.append(self.new_menu_item('_Open...', self.on_open_cb, "<Control>O"))
179
180         self.mixer_menu.append(self.new_menu_item('_Save', self.on_save_cb, "<Control>S"))
181
182         if not with_nsm:
183             self.mixer_menu.append(self.new_menu_item('Save _As...', self.on_save_as_cb,
184                                                       "<Shift><Control>S"))
185
186         self.mixer_menu.append(Gtk.SeparatorMenuItem())
187         if with_nsm:
188             self.mixer_menu.append(self.new_menu_item('_Hide', self.nsm_hide_cb, "<Control>W"))
189         else:
190             self.mixer_menu.append(self.new_menu_item('_Quit', self.on_quit_cb, "<Control>Q"))
191
192         edit_menu = Gtk.Menu()
193         edit_menu_item.set_submenu(edit_menu)
194
195         self.channel_edit_input_menu_item = self.new_menu_item('_Edit Input Channel',
196                                                                enabled=False)
197         edit_menu.append(self.channel_edit_input_menu_item)
198         self.channel_edit_input_menu = Gtk.Menu()
199         self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
200
201         self.channel_edit_output_menu_item = self.new_menu_item('E_dit Output Channel',
202                                                                 enabled=False)
203         edit_menu.append(self.channel_edit_output_menu_item)
204         self.channel_edit_output_menu = Gtk.Menu()
205         self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
206
207         self.channel_remove_input_menu_item = self.new_menu_item('_Remove Input Channel',
208                                                                  enabled=False)
209         edit_menu.append(self.channel_remove_input_menu_item)
210         self.channel_remove_input_menu = Gtk.Menu()
211         self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
212
213         self.channel_remove_output_menu_item = self.new_menu_item('Re_move Output Channel',
214                                                                   enabled=False)
215         edit_menu.append(self.channel_remove_output_menu_item)
216         self.channel_remove_output_menu = Gtk.Menu()
217         self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
218
219         edit_menu.append(Gtk.SeparatorMenuItem())
220         edit_menu.append(self.new_menu_item('Shrink Input Channels', self.on_narrow_input_channels_cb, "<Control>minus"))
221         edit_menu.append(self.new_menu_item('Expand Input Channels', self.on_widen_input_channels_cb, "<Control>plus"))
222         edit_menu.append(Gtk.SeparatorMenuItem())
223
224         edit_menu.append(self.new_menu_item('_Clear', self.on_channels_clear, "<Control>X"))
225         edit_menu.append(Gtk.SeparatorMenuItem())
226         edit_menu.append(self.new_menu_item('_Preferences', self.on_preferences_cb, "<Control>P"))
227
228         help_menu = Gtk.Menu()
229         help_menu_item.set_submenu(help_menu)
230
231         help_menu.append(self.new_menu_item('_About', self.on_about, "F1"))
232
233         self.hbox_top = Gtk.HBox()
234         self.vbox_top.pack_start(self.hbox_top, True, True, 0)
235
236         self.scrolled_window = Gtk.ScrolledWindow()
237         self.scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
238
239         self.hbox_inputs = Gtk.Box()
240         self.hbox_inputs.set_spacing(0)
241         self.hbox_inputs.set_border_width(0)
242         self.hbox_top.set_spacing(0)
243         self.hbox_top.set_border_width(0)
244         self.scrolled_window.add(self.hbox_inputs)
245         self.hbox_outputs = Gtk.Box()
246         self.hbox_outputs.set_spacing(0)
247         self.hbox_outputs.set_border_width(0)
248         self.scrolled_output = Gtk.ScrolledWindow()
249         self.scrolled_output.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
250         self.scrolled_output.add(self.hbox_outputs)
251         self.paned = Gtk.HPaned()
252         self.paned.set_wide_handle(True)
253         self.hbox_top.pack_start(self.paned, True, True, 0)
254         self.paned.pack1(self.scrolled_window, True, False)
255         self.paned.pack2(self.scrolled_output, True, False)
256         self.window.connect("destroy", Gtk.main_quit)
257         self.window.connect('delete-event', self.on_delete_event)
258
259     def nsm_react(self):
260         self.nsm_client.reactToMessage()
261         return True
262
263     def nsm_hide_cb(self, *args):
264         self.window.hide()
265         self.visible = False
266         self.nsm_client.announceGuiVisibility(False)
267
268     def nsm_show_cb(self):
269         width, height = self.window.get_size()
270         self.window.show_all()
271         self.paned.set_position(self.paned_position/self.width*width)
272
273         self.visible = True
274         self.nsm_client.announceGuiVisibility(True)
275
276     def nsm_open_cb(self, path, session_name, client_name):
277         self.create_mixer(client_name, with_nsm=True)
278         self.current_filename = path + '.xml'
279         if os.path.isfile(self.current_filename):
280             try:
281                 with open(self.current_filename, 'r') as fp:
282                     self.load_from_xml(fp, from_nsm=True)
283             except Exception as exc:
284                 # Re-raise with more meaningful error message
285                 raise IOError("Error loading settings file '{}': {}".format(
286                               self.current_filename, exc))
287
288     def nsm_save_cb(self, path, session_name, client_name):
289         self.current_filename = path + '.xml'
290         f = open(self.current_filename, 'w')
291         self.save_to_xml(f)
292         f.close()
293
294     def nsm_exit_cb(self, path, session_name, client_name):
295         Gtk.main_quit()
296
297     def on_midi_behavior_mode_changed(self, gui_factory, value):
298         self.mixer.midi_behavior_mode = value
299
300     def on_delete_event(self, widget, event):
301         if self.nsm_client:
302             self.nsm_hide_cb()
303             return True
304
305         return self.on_quit_cb()
306
307     def sighandler(self, signum, frame):
308         log.debug("Signal %d received.", signum)
309         if signum == signal.SIGUSR1:
310             self.save = True
311         elif signum == signal.SIGTERM:
312             self.on_quit_cb()
313         elif signum == signal.SIGINT:
314             self.on_quit_cb()
315         else:
316             log.warning("Unknown signal %d received.", signum)
317
318     def cleanup(self):
319         log.debug("Cleaning jack_mixer.")
320         if not self.mixer:
321             return
322
323         for channel in self.channels:
324             channel.unrealize()
325
326         self.mixer.destroy()
327
328     def on_open_cb(self, *args):
329         dlg = Gtk.FileChooserDialog(title='Open', parent=self.window,
330                         action=Gtk.FileChooserAction.OPEN)
331         dlg.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
332                         Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
333         dlg.set_default_response(Gtk.ResponseType.OK)
334         if dlg.run() == Gtk.ResponseType.OK:
335             filename = dlg.get_filename()
336             try:
337                 with open(filename, 'r') as fp:
338                     self.load_from_xml(fp)
339             except Exception as exc:
340                 error_dialog(self.window, "Error loading settings file '%s': %s", filename, exc)
341             else:
342                 self.current_filename = filename
343         dlg.destroy()
344
345     def on_save_cb(self, *args):
346         if not self.current_filename:
347             return self.on_save_as_cb()
348         f = open(self.current_filename, 'w')
349         self.save_to_xml(f)
350         f.close()
351
352     def on_save_as_cb(self, *args):
353         dlg = Gtk.FileChooserDialog(title='Save', parent=self.window,
354                         action=Gtk.FileChooserAction.SAVE)
355         dlg.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
356                         Gtk.STOCK_SAVE, Gtk.ResponseType.OK)
357         dlg.set_default_response(Gtk.ResponseType.OK)
358         if dlg.run() == Gtk.ResponseType.OK:
359             self.current_filename = dlg.get_filename()
360             self.on_save_cb()
361         dlg.destroy()
362
363     def on_quit_cb(self, *args):
364         if not self.nsm_client and self.gui_factory.get_confirm_quit():
365             dlg = Gtk.MessageDialog(parent=self.window,
366                                     message_type=Gtk.MessageType.QUESTION,
367                                     buttons=Gtk.ButtonsType.NONE)
368             dlg.set_markup("<b>Quit application?</b>")
369             dlg.format_secondary_markup("All jack_mixer ports will be closed and connections lost,"
370                                         "\nstopping all sound going through jack_mixer.\n\n"
371                                         "Are you sure?")
372             dlg.add_buttons(
373                 Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
374                 Gtk.STOCK_QUIT, Gtk.ResponseType.OK
375             )
376             response = dlg.run()
377             dlg.destroy()
378             if response != Gtk.ResponseType.OK:
379                 return True
380
381         Gtk.main_quit()
382
383     def on_narrow_input_channels_cb(self, widget):
384         for channel in self.channels:
385             channel.narrow()
386
387     def on_widen_input_channels_cb(self, widget):
388         for channel in self.channels:
389             channel.widen()
390
391     preferences_dialog = None
392     def on_preferences_cb(self, widget):
393         if not self.preferences_dialog:
394             self.preferences_dialog = PreferencesDialog(self)
395         self.preferences_dialog.show()
396         self.preferences_dialog.present()
397
398     def on_add_channel(self, inout="input", default_name="Input"):
399         dialog = getattr(self, '_add_{}_dialog'.format(inout), None)
400         values = getattr(self, '_add_{}_values'.format(inout), {})
401
402         if dialog == None:
403             cls = NewInputChannelDialog if inout == 'input' else NewOutputChannelDialog
404             dialog = cls(app=self)
405             setattr(self, '_add_{}_dialog'.format(inout), dialog)
406
407         names = {ch.channel_name
408                  for ch in (self.channels if inout == 'input' else self.output_channels)}
409         values.setdefault('name', default_name)
410         while True:
411             if values['name'] in names:
412                 values['name'] = add_number_suffix(values['name'])
413             else:
414                 break
415
416         dialog.fill_ui(**values)
417         dialog.set_transient_for(self.window)
418         dialog.show()
419         ret = dialog.run()
420         dialog.hide()
421
422         if ret == Gtk.ResponseType.OK:
423             result = dialog.get_result()
424             setattr(self, '_add_{}_values'.format(inout), result)
425             method = getattr(self, 'add_channel' if inout == 'input' else 'add_output_channel')
426             channel = method(**result)
427             if self.visible or self.nsm_client == None:
428                 self.window.show_all()
429
430     def on_add_input_channel(self, widget):
431         return self.on_add_channel("input", "Input")
432
433     def on_add_output_channel(self, widget):
434         return self.on_add_channel("output", "Output")
435
436     def on_edit_input_channel(self, widget, channel):
437         log.debug('Editing input channel "%s".', channel.channel_name)
438         channel.on_channel_properties()
439
440     def remove_channel_edit_input_menuitem_by_label(self, widget, label):
441         if (widget.get_label() == label):
442             self.channel_edit_input_menu.remove(widget)
443
444     def on_remove_input_channel(self, widget, channel):
445         log.debug('Removing input channel "%s".', channel.channel_name)
446         self.channel_remove_input_menu.remove(widget)
447         self.channel_edit_input_menu.foreach(
448             self.remove_channel_edit_input_menuitem_by_label,
449             channel.channel_name);
450         if self.monitored_channel is channel:
451             channel.monitor_button.set_active(False)
452         for i in range(len(self.channels)):
453             if self.channels[i] is channel:
454                 channel.unrealize()
455                 del self.channels[i]
456                 self.hbox_inputs.remove(channel.get_parent())
457                 break
458         if not self.channels:
459             self.channel_edit_input_menu_item.set_sensitive(False)
460             self.channel_remove_input_menu_item.set_sensitive(False)
461
462     def on_edit_output_channel(self, widget, channel):
463         log.debug('Editing output channel "%s".', channel.channel_name)
464         channel.on_channel_properties()
465
466     def remove_channel_edit_output_menuitem_by_label(self, widget, label):
467         if (widget.get_label() == label):
468             self.channel_edit_output_menu.remove(widget)
469
470     def on_remove_output_channel(self, widget, channel):
471         log.debug('Removing output channel "%s".', channel.channel_name)
472         self.channel_remove_output_menu.remove(widget)
473         self.channel_edit_output_menu.foreach(
474             self.remove_channel_edit_output_menuitem_by_label,
475             channel.channel_name);
476         if self.monitored_channel is channel:
477             channel.monitor_button.set_active(False)
478         for i in range(len(self.channels)):
479             if self.output_channels[i] is channel:
480                 channel.unrealize()
481                 del self.output_channels[i]
482                 self.hbox_outputs.remove(channel.get_parent())
483                 break
484         if not self.output_channels:
485             self.channel_edit_output_menu_item.set_sensitive(False)
486             self.channel_remove_output_menu_item.set_sensitive(False)
487
488     def rename_channels(self, container, parameters):
489         if (container.get_label() == parameters['oldname']):
490             container.set_label(parameters['newname'])
491
492     def on_channel_rename(self, oldname, newname):
493         rename_parameters = { 'oldname' : oldname, 'newname' : newname }
494         self.channel_edit_input_menu.foreach(self.rename_channels,
495             rename_parameters)
496         self.channel_edit_output_menu.foreach(self.rename_channels,
497             rename_parameters)
498         self.channel_remove_input_menu.foreach(self.rename_channels,
499             rename_parameters)
500         self.channel_remove_output_menu.foreach(self.rename_channels,
501             rename_parameters)
502         log.debug('Renaming channel from "%s" to "%s".', oldname, newname)
503
504     def on_channels_clear(self, widget):
505         dlg = Gtk.MessageDialog(parent = self.window,
506                 modal = True,
507                 message_type = Gtk.MessageType.WARNING,
508                 text = "Are you sure you want to clear all channels?",
509                 buttons = Gtk.ButtonsType.OK_CANCEL)
510         if not widget or dlg.run() == Gtk.ResponseType.OK:
511             for channel in self.output_channels:
512                 channel.unrealize()
513                 self.hbox_outputs.remove(channel.get_parent())
514             for channel in self.channels:
515                 channel.unrealize()
516                 self.hbox_inputs.remove(channel.get_parent())
517             self.channels = []
518             self.output_channels = []
519             self.channel_edit_input_menu = Gtk.Menu()
520             self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
521             self.channel_edit_input_menu_item.set_sensitive(False)
522             self.channel_remove_input_menu = Gtk.Menu()
523             self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
524             self.channel_remove_input_menu_item.set_sensitive(False)
525             self.channel_edit_output_menu = Gtk.Menu()
526             self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
527             self.channel_edit_output_menu_item.set_sensitive(False)
528             self.channel_remove_output_menu = Gtk.Menu()
529             self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
530             self.channel_remove_output_menu_item.set_sensitive(False)
531         dlg.destroy()
532
533     def add_channel(self, name, stereo, volume_cc, balance_cc, mute_cc, solo_cc, value):
534         try:
535             channel = InputChannel(self, name, stereo, value)
536             self.add_channel_precreated(channel)
537         except Exception:
538             error_dialog(self.window, "Channel creation failed.")
539             return
540         if volume_cc != -1:
541             channel.channel.volume_midi_cc = volume_cc
542         else:
543             channel.channel.autoset_volume_midi_cc()
544         if balance_cc != -1:
545             channel.channel.balance_midi_cc = balance_cc
546         else:
547             channel.channel.autoset_balance_midi_cc()
548         if mute_cc != -1:
549             channel.channel.mute_midi_cc = mute_cc
550         else:
551             channel.channel.autoset_mute_midi_cc()
552         if solo_cc != -1:
553             channel.channel.solo_midi_cc = solo_cc
554         else:
555             channel.channel.autoset_solo_midi_cc()
556
557         return channel
558
559     def add_channel_precreated(self, channel):
560         frame = Gtk.Frame()
561         frame.add(channel)
562         self.hbox_inputs.pack_start(frame, False, True, 0)
563         channel.realize()
564
565         channel_edit_menu_item = Gtk.MenuItem(label=channel.channel_name)
566         self.channel_edit_input_menu.append(channel_edit_menu_item)
567         channel_edit_menu_item.connect("activate", self.on_edit_input_channel, channel)
568         self.channel_edit_input_menu_item.set_sensitive(True)
569
570         channel_remove_menu_item = Gtk.MenuItem(label=channel.channel_name)
571         self.channel_remove_input_menu.append(channel_remove_menu_item)
572         channel_remove_menu_item.connect("activate", self.on_remove_input_channel, channel)
573         self.channel_remove_input_menu_item.set_sensitive(True)
574
575         self.channels.append(channel)
576
577         for outputchannel in self.output_channels:
578             channel.add_control_group(outputchannel)
579
580         # create post fader output channel matching the input channel
581         channel.post_fader_output_channel = self.mixer.add_output_channel(
582                         channel.channel.name + ' Out', channel.channel.is_stereo, True)
583         channel.post_fader_output_channel.volume = 0
584         channel.post_fader_output_channel.set_solo(channel.channel, True)
585
586         channel.connect('input-channel-order-changed', self.on_input_channel_order_changed)
587
588     def on_input_channel_order_changed(self, widget, source_name, dest_name):
589         self.channels.clear()
590
591         channel_box = self.hbox_inputs
592         frames = channel_box.get_children()
593
594         for f in frames:
595             c = f.get_child()
596             if source_name == c._channel_name:
597                 source_frame = f
598                 break
599
600         for f in frames:
601             c = f.get_child()
602             if (dest_name == c._channel_name):
603                 pos = frames.index(f)
604                 channel_box.reorder_child(source_frame, pos)
605                 break
606
607         for frame in self.hbox_inputs.get_children():
608             c = frame.get_child()
609             self.channels.append(c)
610
611     def read_meters(self):
612         for channel in self.channels:
613             channel.read_meter()
614         for channel in self.output_channels:
615             channel.read_meter()
616         return True
617
618     def midi_events_check(self):
619         for channel in self.channels + self.output_channels:
620             channel.midi_events_check()
621         return True
622
623     def add_output_channel(self, name, stereo, volume_cc, balance_cc, mute_cc,
624             display_solo_buttons, color, value):
625         try:
626             channel = OutputChannel(self, name, stereo, value)
627             channel.display_solo_buttons = display_solo_buttons
628             channel.color = color
629             self.add_output_channel_precreated(channel)
630         except Exception:
631             error_dialog(self.window, "Channel creation failed")
632             return
633
634         if volume_cc != -1:
635             channel.channel.volume_midi_cc = volume_cc
636         else:
637             channel.channel.autoset_volume_midi_cc()
638         if balance_cc != -1:
639             channel.channel.balance_midi_cc = balance_cc
640         else:
641             channel.channel.autoset_balance_midi_cc()
642         if mute_cc != -1:
643             channel.channel.mute_midi_cc = mute_cc
644         else:
645             channel.channel.autoset_mute_midi_cc()
646
647         return channel
648
649     def add_output_channel_precreated(self, channel):
650         frame = Gtk.Frame()
651         frame.add(channel)
652         self.hbox_outputs.pack_end(frame, False, True, 0)
653         self.hbox_outputs.reorder_child(frame, 0)
654         channel.realize()
655
656         channel_edit_menu_item = Gtk.MenuItem(label=channel.channel_name)
657         self.channel_edit_output_menu.append(channel_edit_menu_item)
658         channel_edit_menu_item.connect("activate", self.on_edit_output_channel, channel)
659         self.channel_edit_output_menu_item.set_sensitive(True)
660
661         channel_remove_menu_item = Gtk.MenuItem(label=channel.channel_name)
662         self.channel_remove_output_menu.append(channel_remove_menu_item)
663         channel_remove_menu_item.connect("activate", self.on_remove_output_channel, channel)
664         self.channel_remove_output_menu_item.set_sensitive(True)
665
666         self.output_channels.append(channel)
667         channel.connect('output-channel-order-changed', self.on_output_channel_order_changed)
668
669     def on_output_channel_order_changed(self, widget, source_name, dest_name):
670         self.output_channels.clear()
671         channel_box = self.hbox_outputs
672
673         frames = channel_box.get_children()
674
675         for f in frames:
676             c = f.get_child()
677             if source_name == c._channel_name:
678                  source_frame = f
679                  break
680
681         for f in frames:
682             c = f.get_child()
683             if (dest_name == c._channel_name):
684                 pos = len(frames) - 1 - frames.index(f)
685                 channel_box.reorder_child(source_frame, pos)
686                 break
687
688         for frame in self.hbox_outputs.get_children():
689             c = frame.get_child()
690             self.output_channels.append(c)
691
692     _monitored_channel = None
693     def get_monitored_channel(self):
694         return self._monitored_channel
695
696     def set_monitored_channel(self, channel):
697         if self._monitored_channel:
698             if channel.channel.name == self._monitored_channel.channel.name:
699                 return
700         self._monitored_channel = channel
701         if type(channel) is InputChannel:
702             # reset all solo/mute settings
703             for in_channel in self.channels:
704                 self.monitor_channel.set_solo(in_channel.channel, False)
705                 self.monitor_channel.set_muted(in_channel.channel, False)
706             self.monitor_channel.set_solo(channel.channel, True)
707             self.monitor_channel.prefader = True
708         else:
709             self.monitor_channel.prefader = False
710         self.update_monitor(channel)
711     monitored_channel = property(get_monitored_channel, set_monitored_channel)
712
713     def update_monitor(self, channel):
714         if self._monitored_channel is not channel:
715             return
716         self.monitor_channel.volume = channel.channel.volume
717         self.monitor_channel.balance = channel.channel.balance
718         if type(self.monitored_channel) is OutputChannel:
719             # sync solo/muted channels
720             for input_channel in self.channels:
721                 self.monitor_channel.set_solo(input_channel.channel,
722                                 channel.channel.is_solo(input_channel.channel))
723                 self.monitor_channel.set_muted(input_channel.channel,
724                                 channel.channel.is_muted(input_channel.channel))
725
726     def get_input_channel_by_name(self, name):
727         for input_channel in self.channels:
728             if input_channel.channel.name == name:
729                 return input_channel
730         return None
731
732     def on_about(self, *args):
733         about = Gtk.AboutDialog()
734         about.set_name('jack_mixer')
735         about.set_program_name('jack_mixer')
736         about.set_copyright('Copyright © 2006-2020\nNedko Arnaudov, Frédéric Péters, Arnout Engelen, Daniel Sheeler')
737         about.set_license("""\
738 jack_mixer is free software; you can redistribute it and/or modify it
739 under the terms of the GNU General Public License as published by the
740 Free Software Foundation; either version 2 of the License, or (at your
741 option) any later version.
742
743 jack_mixer is distributed in the hope that it will be useful, but
744 WITHOUT ANY WARRANTY; without even the implied warranty of
745 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
746 General Public License for more details.
747
748 You should have received a copy of the GNU General Public License along
749 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
750 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA""")
751         about.set_authors([
752             'Nedko Arnaudov <nedko@arnaudov.name>',
753             'Christopher Arndt <chris@chrisarndt.de>',
754             'Arnout Engelen <arnouten@bzzt.net>',
755             'John Hedges <john@drystone.co.uk>',
756             'Olivier Humbert <trebmuh@tuxfamily.org>',
757             'Sarah Mischke <sarah@spooky-online.de>',
758             'Frédéric Péters <fpeters@0d.be>',
759             'Daniel Sheeler <dsheeler@pobox.com>',
760             'Athanasios Silis <athanasios.silis@gmail.com>',
761         ])
762         about.set_logo_icon_name('jack_mixer')
763         about.set_version(__version__)
764         about.set_website('https://rdio.space/jackmixer/')
765
766         about.run()
767         about.destroy()
768
769     def save_to_xml(self, file):
770         log.debug("Saving to XML...")
771         b = XmlSerialization()
772         s = Serializator()
773         s.serialize(self, b)
774         b.save(file)
775
776     def load_from_xml(self, file, silence_errors=False, from_nsm=False):
777         log.debug("Loading from XML...")
778         self.unserialized_channels = []
779         b = XmlSerialization()
780         try:
781             b.load(file)
782         except:
783             if silence_errors:
784                 return
785             raise
786         self.on_channels_clear(None)
787         s = Serializator()
788         s.unserialize(self, b)
789         for channel in self.unserialized_channels:
790             if isinstance(channel, InputChannel):
791                 if self._init_solo_channels and channel.channel_name in self._init_solo_channels:
792                     channel.solo = True
793                 self.add_channel_precreated(channel)
794         self._init_solo_channels = None
795         for channel in self.unserialized_channels:
796             if isinstance(channel, OutputChannel):
797                 self.add_output_channel_precreated(channel)
798         del self.unserialized_channels
799         width, height = self.window.get_size()
800         if self.visible or not from_nsm:
801             self.window.show_all()
802         self.paned.set_position(self.paned_position/self.width*width)
803         self.window.resize(self.width, self.height)
804
805     def serialize(self, object_backend):
806         width, height = self.window.get_size()
807         object_backend.add_property('geometry',
808                         '%sx%s' % (width, height))
809         pos = self.paned.get_position()
810         object_backend.add_property('paned_position', '%s' % pos)
811         solo_channels = []
812         for input_channel in self.channels:
813             if input_channel.channel.solo:
814                 solo_channels.append(input_channel)
815         if solo_channels:
816             object_backend.add_property('solo_channels', '|'.join([x.channel.name for x in solo_channels]))
817         object_backend.add_property('visible', '%s' % str(self.visible))
818
819     def unserialize_property(self, name, value):
820         if name == 'geometry':
821             width, height = value.split('x')
822             self.width = int(width)
823             self.height = int(height)
824             return True
825         if name == 'solo_channels':
826             self._init_solo_channels = value.split('|')
827             return True
828         if name == 'visible':
829             self.visible = value == 'True'
830             return True
831         if name == 'paned_position':
832             self.paned_position = int(value)
833             return True
834         return False
835
836     def unserialize_child(self, name):
837         if name == InputChannel.serialization_name():
838             channel = InputChannel(self, "", True)
839             self.unserialized_channels.append(channel)
840             return channel
841
842         if name == OutputChannel.serialization_name():
843             channel = OutputChannel(self, "", True)
844             self.unserialized_channels.append(channel)
845             return channel
846
847         if name == gui.Factory.serialization_name():
848             return self.gui_factory
849
850     def serialization_get_childs(self):
851         '''Get child objects that required and support serialization'''
852         childs = self.channels[:] + self.output_channels[:] + [self.gui_factory]
853         return childs
854
855     def serialization_name(self):
856         return "jack_mixer"
857
858     def main(self):
859         if not self.mixer:
860             return
861
862         if self.visible or self.nsm_client == None:
863             width, height = self.window.get_size()
864             self.window.show_all()
865             if hasattr(self, 'paned_position'):
866                 self.paned.set_position(self.paned_position/self.width*width)
867
868         signal.signal(signal.SIGUSR1, self.sighandler)
869         signal.signal(signal.SIGTERM, self.sighandler)
870         signal.signal(signal.SIGINT, self.sighandler)
871         signal.signal(signal.SIGHUP, signal.SIG_IGN)
872
873         Gtk.main()
874
875 def error_dialog(parent, msg, *args):
876     log.exception(msg, *args)
877     err = Gtk.MessageDialog(parent=parent, modal=True, destroy_with_parent=True,
878         message_type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK, text=msg % args)
879     err.run()
880     err.destroy()
881
882 def main():
883     parser = ArgumentParser()
884     parser.add_argument('-c', '--config', metavar="FILE", help='load mixer project configuration from FILE')
885     parser.add_argument('-d', '--debug', action="store_true", help='enable debug logging messages')
886     parser.add_argument('client_name', metavar='NAME', nargs='?', default='jack_mixer',
887                         help='set JACK client name')
888     args = parser.parse_args()
889
890     logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO,
891                         format="%(levelname)s: %(message)s")
892
893     try:
894         mixer = JackMixer(args.client_name)
895     except Exception as e:
896         error_dialog(None, "Mixer creation failed:\n\n%s", e)
897         sys.exit(1)
898
899     if not mixer.nsm_client and args.config:
900         try:
901             with open(args.config) as fp:
902                 mixer.load_from_xml(fp)
903         except Exception as exc:
904             error_dialog(mixer.window, "Error loading settings file '%s': %s", args.config, exc)
905         else:
906             mixer.current_filename = args.config
907
908         mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
909
910     mixer.main()
911
912     mixer.cleanup()
913
914 if __name__ == "__main__":
915     main()