2 Copyright (C) 2000 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <sigc++/bind.h>
28 #include <pbd/lockmonitor.h>
29 #include <pbd/xml++.h>
31 #include <ardour/audioengine.h>
32 #include <ardour/io.h>
33 #include <ardour/port.h>
34 #include <ardour/connection.h>
35 #include <ardour/session.h>
36 #include <ardour/cycle_timer.h>
37 #include <ardour/panner.h>
38 #include <ardour/dB.h>
45 A bug in OS X's cmath that causes isnan() and isinf() to be
46 "undeclared". the following works around that
49 #if defined(__APPLE__) && defined(__MACH__)
50 extern "C" int isnan (double);
51 extern "C" int isinf (double);
56 using namespace ARDOUR;
57 //using namespace sigc;
59 static float current_automation_version_number = 1.0;
61 jack_nframes_t IO::_automation_interval = 0;
62 const string IO::state_node_name = "IO";
63 bool IO::connecting_legal = false;
64 bool IO::ports_legal = false;
65 bool IO::panners_legal = false;
66 sigc::signal<void> IO::GrabPeakPower;
67 sigc::signal<int> IO::ConnectingLegal;
68 sigc::signal<int> IO::PortsLegal;
69 sigc::signal<int> IO::PannersLegal;
70 sigc::signal<void,uint32_t> IO::MoreOutputs;
71 sigc::signal<int> IO::PortsCreated;
73 /* this is a default mapper of MIDI control values to a gain coefficient.
74 others can be imagined. see IO::set_midi_to_gain_function().
77 static gain_t direct_midi_to_gain (double fract) {
78 /* XXX Marcus writes: this doesn't seem right to me. but i don't have a better answer ... */
79 /* this maxes at +6dB */
80 return pow (2.0,(sqrt(sqrt(sqrt(fract)))*198.0-192.0)/6.0);
83 static double direct_gain_to_midi (gain_t gain) {
84 /* XXX Marcus writes: this doesn't seem right to me. but i don't have a better answer ... */
85 if (gain == 0) return 0.0;
87 return pow((6.0*log(gain)/log(2.0)+192.0)/198.0, 8.0);
90 static bool sort_ports_by_name (Port* a, Port* b)
92 return a->name() < b->name();
96 IO::IO (Session& s, string name,
98 int input_min, int input_max, int output_min, int output_max)
101 _midi_gain_control (*this, _session.midi_port()),
102 _gain_automation_curve (0.0, 2.0, 1.0),
103 _input_minimum (input_min),
104 _input_maximum (input_max),
105 _output_minimum (output_min),
106 _output_maximum (output_max)
109 _panner = new Panner (name, _session);
112 _input_connection = 0;
113 _output_connection = 0;
114 pending_state_node = 0;
117 no_panner_reset = false;
120 _midi_gain_control.midi_to_gain = direct_midi_to_gain;
121 _midi_gain_control.gain_to_midi = direct_gain_to_midi;
123 apply_gain_automation = false;
125 last_automation_snapshot = 0;
127 _gain_automation_state = Off;
128 _gain_automation_style = Absolute;
130 GrabPeakPower.connect (mem_fun (*this, &IO::grab_peak_power));
135 LockMonitor lm (io_lock, __LINE__, __FILE__);
136 vector<Port *>::iterator i;
138 for (i = _inputs.begin(); i != _inputs.end(); ++i) {
139 _session.engine().unregister_port (*i);
142 for (i = _outputs.begin(); i != _outputs.end(); ++i) {
143 _session.engine().unregister_port (*i);
148 IO::silence (jack_nframes_t nframes, jack_nframes_t offset)
150 /* io_lock, not taken: function must be called from Session::process() calltree */
152 for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
153 (*i)->silence (nframes, offset);
158 IO::apply_declick (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, gain_t initial, gain_t target, bool invert_polarity)
160 jack_nframes_t declick = min ((jack_nframes_t)4096, nframes);
163 double fractional_shift;
164 double fractional_pos;
166 fractional_shift = -1.0/declick;
168 if (target < initial) {
169 /* fade out: remove more and more of delta from initial */
170 delta = -(initial - target);
172 /* fade in: add more and more of delta from initial */
173 delta = target - initial;
176 for (uint32_t n = 0; n < nbufs; ++n) {
179 fractional_pos = 1.0;
181 if (invert_polarity) {
182 for (jack_nframes_t nx = 0; nx < declick; ++nx) {
183 buffer[nx] *= -(initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
184 fractional_pos += fractional_shift;
187 for (jack_nframes_t nx = 0; nx < declick; ++nx) {
188 buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
189 fractional_pos += fractional_shift;
193 /* now ensure the rest of the buffer has the target value
194 applied, if necessary.
197 if (declick != nframes) {
199 if (invert_polarity) {
204 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
205 } else if (target != 1.0) {
206 for (jack_nframes_t nx = declick; nx < nframes; ++nx) {
207 buffer[nx] *= target;
215 IO::pan_automated (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes, jack_nframes_t offset)
219 /* io_lock, not taken: function must be called from Session::process() calltree */
221 if (_noutputs == 0) {
225 if (_noutputs == 1) {
227 dst = output(0)->get_buffer (nframes) + offset;
229 for (uint32_t n = 0; n < nbufs; ++n) {
230 if (bufs[n] != dst) {
231 memcpy (dst, bufs[n], sizeof (Sample) * nframes);
235 output(0)->mark_silence (false);
241 vector<Port *>::iterator out;
242 vector<Sample *>::iterator in;
243 Panner::iterator pan;
244 Sample* obufs[_noutputs];
246 /* the terrible silence ... */
248 for (out = _outputs.begin(), o = 0; out != _outputs.end(); ++out, ++o) {
249 obufs[o] = (*out)->get_buffer (nframes) + offset;
250 memset (obufs[o], 0, sizeof (Sample) * nframes);
251 (*out)->mark_silence (false);
256 for (pan = _panner->begin(), n = 0; n < nbufs; ++n, ++pan) {
257 (*pan)->distribute_automated (bufs[n], obufs, start, end, nframes, _session.pan_automation_buffer());
262 IO::pan (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset, gain_t gain_coeff)
267 /* io_lock, not taken: function must be called from Session::process() calltree */
269 if (_noutputs == 0) {
273 /* the panner can be empty if there are no inputs to the
274 route, but still outputs
277 if (_panner->bypassed() || _panner->empty()) {
278 deliver_output_no_pan (bufs, nbufs, nframes, offset);
282 if (_noutputs == 1) {
284 dst = output(0)->get_buffer (nframes) + offset;
286 if (gain_coeff == 0.0f) {
288 /* only one output, and gain was zero, so make it silent */
290 memset (dst, 0, sizeof (Sample) * nframes);
292 } else if (gain_coeff == 1.0f){
294 /* mix all buffers into the output */
298 memcpy (dst, bufs[0], sizeof (Sample) * nframes);
300 for (n = 1; n < nbufs; ++n) {
303 for (jack_nframes_t n = 0; n < nframes; ++n) {
308 output(0)->mark_silence (false);
312 /* mix all buffers into the output, scaling them all by the gain */
318 for (jack_nframes_t n = 0; n < nframes; ++n) {
319 dst[n] = src[n] * gain_coeff;
322 for (n = 1; n < nbufs; ++n) {
325 for (jack_nframes_t n = 0; n < nframes; ++n) {
326 dst[n] += src[n] * gain_coeff;
330 output(0)->mark_silence (false);
337 vector<Port *>::iterator out;
338 vector<Sample *>::iterator in;
339 Panner::iterator pan;
340 Sample* obufs[_noutputs];
342 /* the terrible silence ... */
344 /* XXX this is wasteful but i see no way to avoid it */
346 for (out = _outputs.begin(), o = 0; out != _outputs.end(); ++out, ++o) {
347 obufs[o] = (*out)->get_buffer (nframes) + offset;
348 memset (obufs[o], 0, sizeof (Sample) * nframes);
349 (*out)->mark_silence (false);
354 for (pan = _panner->begin(), n = 0; n < nbufs; ++n) {
355 Panner::iterator tmp;
360 (*pan)->distribute (bufs[n], obufs, gain_coeff, nframes);
362 if (tmp != _panner->end()) {
369 IO::deliver_output (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset)
371 /* io_lock, not taken: function must be called from Session::process() calltree */
373 if (_noutputs == 0) {
377 if (_panner->bypassed()) {
378 deliver_output_no_pan (bufs, nbufs, nframes, offset);
386 TentativeLockMonitor dm (declick_lock, __LINE__, __FILE__);
396 apply_declick (bufs, nbufs, nframes, _gain, dg, false);
400 /* simple, non-automation panning to outputs */
402 if (_session.transport_speed() > 1.5f || _session.transport_speed() < -1.5f) {
403 pan (bufs, nbufs, nframes, offset, _gain * speed_quietning);
405 pan (bufs, nbufs, nframes, offset, _gain);
410 IO::deliver_output_no_pan (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset)
412 /* io_lock, not taken: function must be called from Session::process() calltree */
414 if (_noutputs == 0) {
421 if (apply_gain_automation) {
423 /* gain has already been applied by automation code. do nothing here except
433 TentativeLockMonitor dm (declick_lock, __LINE__, __FILE__);
445 vector<Port*>::iterator o;
446 vector<Sample*> outs;
450 /* unlikely condition */
451 for (o = _outputs.begin(), i = 0; o != _outputs.end(); ++o, ++i) {
452 outs.push_back ((*o)->get_buffer (nframes) + offset);
456 /* reduce nbufs to the index of the last input buffer */
460 if (_session.transport_speed() > 1.5f || _session.transport_speed() < -1.5f) {
461 actual_gain = _gain * speed_quietning;
466 for (o = _outputs.begin(), i = 0; o != _outputs.end(); ++o, ++i) {
468 dst = (*o)->get_buffer (nframes) + offset;
469 src = bufs[min(nbufs,i)];
471 if (dg != _gain || actual_gain == 1.0f) {
472 memcpy (dst, src, sizeof (Sample) * nframes);
473 } else if (actual_gain == 0.0f) {
474 memset (dst, 0, sizeof (Sample) * nframes);
476 for (jack_nframes_t x = 0; x < nframes; ++x) {
477 dst[x] = src[x] * actual_gain;
481 (*o)->mark_silence (false);
485 apply_declick (outs, outs.size(), nframes, _gain, dg, false);
489 if (apply_gain_automation) {
495 IO::collect_input (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset)
497 /* io_lock, not taken: function must be called from Session::process() calltree */
499 vector<Port *>::iterator i;
503 /* we require that bufs.size() >= 1 */
505 for (n = 0, i = _inputs.begin(); n < nbufs; ++i, ++n) {
506 if (i == _inputs.end()) {
510 /* XXX always read the full extent of the port buffer that
511 we need. One day, we may use jack_port_get_buffer_at_offset()
512 or something similar. For now, this simple hack will
515 Hack? Why yes .. we only need to read nframes-worth of
516 data, but the data we want is at `offset' within the
520 last = (*i)->get_buffer (nframes+offset) + offset;
521 // the dest buffer's offset has already been applied
522 memcpy (bufs[n], last, sizeof (Sample) * nframes);
525 /* fill any excess outputs with the last input */
527 while (n < nbufs && last) {
528 // the dest buffer's offset has already been applied
529 memcpy (bufs[n], last, sizeof (Sample) * nframes);
535 IO::just_meter_input (jack_nframes_t start_frame, jack_nframes_t end_frame,
536 jack_nframes_t nframes, jack_nframes_t offset)
538 vector<Sample*>& bufs = _session.get_passthru_buffers ();
539 uint32_t nbufs = n_process_buffers ();
541 collect_input (bufs, nbufs, nframes, offset);
543 for (uint32_t n = 0; n < nbufs; ++n) {
544 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
549 IO::drop_input_connection ()
551 _input_connection = 0;
552 input_connection_configuration_connection.disconnect();
553 input_connection_connection_connection.disconnect();
554 _session.set_dirty ();
558 IO::drop_output_connection ()
560 _output_connection = 0;
561 output_connection_configuration_connection.disconnect();
562 output_connection_connection_connection.disconnect();
563 _session.set_dirty ();
567 IO::disconnect_input (Port* our_port, string other_port, void* src)
569 if (other_port.length() == 0 || our_port == 0) {
574 LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
577 LockMonitor lm (io_lock, __LINE__, __FILE__);
579 /* check that our_port is really one of ours */
581 if (find (_inputs.begin(), _inputs.end(), our_port) == _inputs.end()) {
585 /* disconnect it from the source */
587 if (_session.engine().disconnect (other_port, our_port->name())) {
588 error << compose(_("IO: cannot disconnect input port %1 from %2"), our_port->name(), other_port) << endmsg;
592 drop_input_connection();
596 input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */
597 _session.set_dirty ();
603 IO::connect_input (Port* our_port, string other_port, void* src)
605 if (other_port.length() == 0 || our_port == 0) {
610 LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
613 LockMonitor lm (io_lock, __LINE__, __FILE__);
615 /* check that our_port is really one of ours */
617 if (find (_inputs.begin(), _inputs.end(), our_port) == _inputs.end()) {
621 /* connect it to the source */
623 if (_session.engine().connect (other_port, our_port->name())) {
627 drop_input_connection ();
631 input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */
632 _session.set_dirty ();
637 IO::disconnect_output (Port* our_port, string other_port, void* src)
639 if (other_port.length() == 0 || our_port == 0) {
644 LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
647 LockMonitor lm (io_lock, __LINE__, __FILE__);
649 if (find (_outputs.begin(), _outputs.end(), our_port) == _outputs.end()) {
653 /* disconnect it from the destination */
655 if (_session.engine().disconnect (our_port->name(), other_port)) {
656 error << compose(_("IO: cannot disconnect output port %1 from %2"), our_port->name(), other_port) << endmsg;
660 drop_output_connection ();
664 output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */
665 _session.set_dirty ();
670 IO::connect_output (Port* our_port, string other_port, void* src)
672 if (other_port.length() == 0 || our_port == 0) {
677 LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
680 LockMonitor lm (io_lock, __LINE__, __FILE__);
682 /* check that our_port is really one of ours */
684 if (find (_outputs.begin(), _outputs.end(), our_port) == _outputs.end()) {
688 /* connect it to the destination */
690 if (_session.engine().connect (our_port->name(), other_port)) {
694 drop_output_connection ();
698 output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */
699 _session.set_dirty ();
704 IO::set_input (Port* other_port, void* src)
706 /* this removes all but one ports, and connects that one port
707 to the specified source.
710 if (_input_minimum > 1 || _input_minimum == 0) {
711 /* sorry, you can't do this */
715 if (other_port == 0) {
716 if (_input_minimum < 0) {
717 return ensure_inputs (0, false, true, src);
723 if (ensure_inputs (1, true, true, src)) {
727 return connect_input (_inputs.front(), other_port->name(), src);
731 IO::remove_output_port (Port* port, void* src)
733 IOChange change (NoChange);
736 LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
739 LockMonitor lm (io_lock, __LINE__, __FILE__);
741 if (_noutputs - 1 == (uint32_t) _output_minimum) {
742 /* sorry, you can't do this */
746 for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
748 change = IOChange (change|ConfigurationChanged);
749 if (port->connected()) {
750 change = IOChange (change|ConnectionsChanged);
753 _session.engine().unregister_port (*i);
756 drop_output_connection ();
762 if (change != NoChange) {
763 setup_peak_meters ();
769 if (change != NoChange) {
770 output_changed (change, src); /* EMIT SIGNAL */
771 _session.set_dirty ();
779 IO::add_output_port (string destination, void* src)
785 LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
788 LockMonitor lm (io_lock, __LINE__, __FILE__);
790 if (_output_maximum >= 0 && (int) _noutputs == _output_maximum) {
794 /* Create a new output port */
796 if (_output_maximum == 1) {
797 snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
799 snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
802 if ((our_port = _session.engine().register_audio_output_port (buf)) == 0) {
803 error << compose(_("IO: cannot register output port %1"), buf) << endmsg;
807 _outputs.push_back (our_port);
808 sort (_outputs.begin(), _outputs.end(), sort_ports_by_name);
810 drop_output_connection ();
811 setup_peak_meters ();
815 MoreOutputs (_noutputs); /* EMIT SIGNAL */
818 if (destination.length()) {
819 if (_session.engine().connect (our_port->name(), destination)) {
824 // pan_changed (src); /* EMIT SIGNAL */
825 output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
826 _session.set_dirty ();
831 IO::remove_input_port (Port* port, void* src)
833 IOChange change (NoChange);
836 LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
839 LockMonitor lm (io_lock, __LINE__, __FILE__);
841 if (((int)_ninputs - 1) < _input_minimum) {
842 /* sorry, you can't do this */
845 for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
848 change = IOChange (change|ConfigurationChanged);
850 if (port->connected()) {
851 change = IOChange (change|ConnectionsChanged);
854 _session.engine().unregister_port (*i);
857 drop_input_connection ();
863 if (change != NoChange) {
864 setup_peak_meters ();
870 if (change != NoChange) {
871 input_changed (change, src);
872 _session.set_dirty ();
880 IO::add_input_port (string source, void* src)
886 LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
889 LockMonitor lm (io_lock, __LINE__, __FILE__);
891 if (_input_maximum >= 0 && (int) _ninputs == _input_maximum) {
895 /* Create a new input port */
897 if (_input_maximum == 1) {
898 snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
900 snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
903 if ((our_port = _session.engine().register_audio_input_port (buf)) == 0) {
904 error << compose(_("IO: cannot register input port %1"), buf) << endmsg;
908 _inputs.push_back (our_port);
909 sort (_inputs.begin(), _inputs.end(), sort_ports_by_name);
911 drop_input_connection ();
912 setup_peak_meters ();
916 MoreOutputs (_ninputs); /* EMIT SIGNAL */
919 if (source.length()) {
921 if (_session.engine().connect (source, our_port->name())) {
926 // pan_changed (src); /* EMIT SIGNAL */
927 input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
928 _session.set_dirty ();
934 IO::disconnect_inputs (void* src)
937 LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
940 LockMonitor lm (io_lock, __LINE__, __FILE__);
942 for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
943 _session.engine().disconnect (*i);
946 drop_input_connection ();
949 input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */
954 IO::disconnect_outputs (void* src)
957 LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
960 LockMonitor lm (io_lock, __LINE__, __FILE__);
962 for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
963 _session.engine().disconnect (*i);
966 drop_output_connection ();
970 output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */
971 _session.set_dirty ();
976 IO::ensure_inputs_locked (uint32_t n, bool clear, void* src)
979 bool changed = false;
980 bool reduced = false;
982 /* remove unused ports */
984 while (_ninputs > n) {
985 _session.engine().unregister_port (_inputs.back());
992 /* create any necessary new ports */
994 while (_ninputs < n) {
998 /* Create a new input port */
1000 if (_input_maximum == 1) {
1001 snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
1004 snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
1009 if ((input_port = _session.engine().register_audio_input_port (buf)) == 0) {
1010 error << compose(_("IO: cannot register input port %1"), buf) << endmsg;
1015 catch (AudioEngine::PortRegistrationFailure& err) {
1016 setup_peak_meters ();
1022 _inputs.push_back (input_port);
1023 sort (_inputs.begin(), _inputs.end(), sort_ports_by_name);
1029 drop_input_connection ();
1030 setup_peak_meters ();
1032 MoreOutputs (_ninputs); /* EMIT SIGNAL */
1033 _session.set_dirty ();
1037 /* disconnect all existing ports so that we get a fresh start */
1039 for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
1040 _session.engine().disconnect (*i);
1048 IO::ensure_io (uint32_t nin, uint32_t nout, bool clear, void* src)
1050 bool in_changed = false;
1051 bool out_changed = false;
1052 bool in_reduced = false;
1053 bool out_reduced = false;
1054 bool need_pan_reset;
1056 if (_input_maximum >= 0) {
1057 nin = min (_input_maximum, (int) nin);
1060 if (_output_maximum >= 0) {
1061 nout = min (_output_maximum, (int) nout);
1064 if (nin == _ninputs && nout == _noutputs && !clear) {
1069 LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
1070 LockMonitor lm (io_lock, __LINE__, __FILE__);
1074 if (_noutputs == nout) {
1075 need_pan_reset = false;
1077 need_pan_reset = true;
1080 /* remove unused ports */
1082 while (_ninputs > nin) {
1083 _session.engine().unregister_port (_inputs.back());
1090 while (_noutputs > nout) {
1091 _session.engine().unregister_port (_outputs.back());
1092 _outputs.pop_back();
1098 /* create any necessary new ports */
1100 while (_ninputs < nin) {
1104 /* Create a new input port */
1106 if (_input_maximum == 1) {
1107 snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
1110 snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
1114 if ((port = _session.engine().register_audio_input_port (buf)) == 0) {
1115 error << compose(_("IO: cannot register input port %1"), buf) << endmsg;
1120 catch (AudioEngine::PortRegistrationFailure& err) {
1121 setup_peak_meters ();
1127 _inputs.push_back (port);
1132 /* create any necessary new ports */
1134 while (_noutputs < nout) {
1138 /* Create a new output port */
1140 if (_output_maximum == 1) {
1141 snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
1143 snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
1147 if ((port = _session.engine().register_audio_output_port (buf)) == 0) {
1148 error << compose(_("IO: cannot register output port %1"), buf) << endmsg;
1153 catch (AudioEngine::PortRegistrationFailure& err) {
1154 setup_peak_meters ();
1160 _outputs.push_back (port);
1167 /* disconnect all existing ports so that we get a fresh start */
1169 for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
1170 _session.engine().disconnect (*i);
1173 for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
1174 _session.engine().disconnect (*i);
1179 if (in_changed || out_changed) {
1180 setup_peak_meters ();
1185 sort (_outputs.begin(), _outputs.end(), sort_ports_by_name);
1186 drop_output_connection ();
1187 output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
1191 sort (_inputs.begin(), _inputs.end(), sort_ports_by_name);
1192 drop_input_connection ();
1193 input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
1196 if (in_changed || out_changed) {
1197 MoreOutputs (max (_noutputs, _ninputs)); /* EMIT SIGNAL */
1198 _session.set_dirty ();
1205 IO::ensure_inputs (uint32_t n, bool clear, bool lockit, void* src)
1207 bool changed = false;
1209 if (_input_maximum >= 0) {
1210 n = min (_input_maximum, (int) n);
1212 if (n == _ninputs && !clear) {
1218 LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
1219 changed = ensure_inputs_locked (n, clear, src);
1221 changed = ensure_inputs_locked (n, clear, src);
1225 input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
1226 _session.set_dirty ();
1233 IO::ensure_outputs_locked (uint32_t n, bool clear, void* src)
1236 bool changed = false;
1237 bool reduced = false;
1238 bool need_pan_reset;
1240 if (_noutputs == n) {
1241 need_pan_reset = false;
1243 need_pan_reset = true;
1246 /* remove unused ports */
1248 while (_noutputs > n) {
1250 _session.engine().unregister_port (_outputs.back());
1251 _outputs.pop_back();
1257 /* create any necessary new ports */
1259 while (_noutputs < n) {
1263 /* Create a new output port */
1265 if (_output_maximum == 1) {
1266 snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
1268 snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
1271 if ((output_port = _session.engine().register_audio_output_port (buf)) == 0) {
1272 error << compose(_("IO: cannot register output port %1"), buf) << endmsg;
1276 _outputs.push_back (output_port);
1277 sort (_outputs.begin(), _outputs.end(), sort_ports_by_name);
1280 setup_peak_meters ();
1282 if (need_pan_reset) {
1288 drop_output_connection ();
1289 MoreOutputs (_noutputs); /* EMIT SIGNAL */
1290 _session.set_dirty ();
1294 /* disconnect all existing ports so that we get a fresh start */
1296 for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
1297 _session.engine().disconnect (*i);
1305 IO::ensure_outputs (uint32_t n, bool clear, bool lockit, void* src)
1307 bool changed = false;
1309 if (_output_maximum >= 0) {
1310 n = min (_output_maximum, (int) n);
1311 if (n == _noutputs && !clear) {
1316 /* XXX caller should hold io_lock, but generally doesn't */
1319 LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
1320 changed = ensure_outputs_locked (n, clear, src);
1322 changed = ensure_outputs_locked (n, clear, src);
1326 output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
1333 IO::effective_gain () const
1335 if (gain_automation_playback()) {
1336 return _effective_gain;
1338 return _desired_gain;
1345 if (panners_legal) {
1346 if (!no_panner_reset) {
1347 _panner->reset (_noutputs, pans_required());
1350 panner_legal_c.disconnect ();
1351 panner_legal_c = PannersLegal.connect (mem_fun (*this, &IO::panners_became_legal));
1356 IO::panners_became_legal ()
1358 _panner->reset (_noutputs, pans_required());
1359 _panner->load (); // automation
1360 panner_legal_c.disconnect ();
1365 IO::defer_pan_reset ()
1367 no_panner_reset = true;
1371 IO::allow_pan_reset ()
1373 no_panner_reset = false;
1379 IO::get_state (void)
1381 return state (true);
1385 IO::state (bool full_state)
1387 XMLNode* node = new XMLNode (state_node_name);
1390 bool need_ins = true;
1391 bool need_outs = true;
1392 LocaleGuard lg (X_("POSIX"));
1393 LockMonitor lm (io_lock, __LINE__, __FILE__);
1395 node->add_property("name", _name);
1396 snprintf (buf, sizeof(buf), "%" PRIu64, id());
1397 node->add_property("id", buf);
1401 if (_input_connection) {
1402 node->add_property ("input-connection", _input_connection->name());
1406 if (_output_connection) {
1407 node->add_property ("output-connection", _output_connection->name());
1412 for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
1414 const char **connections = (*i)->get_connections();
1416 if (connections && connections[0]) {
1419 for (int n = 0; connections && connections[n]; ++n) {
1424 /* if its a connection to our own port,
1425 return only the port name, not the
1426 whole thing. this allows connections
1427 to be re-established even when our
1428 client name is different.
1431 str += _session.engine().make_port_name_relative (connections[n]);
1443 node->add_property ("inputs", str);
1449 for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
1451 const char **connections = (*i)->get_connections();
1453 if (connections && connections[0]) {
1457 for (int n = 0; connections[n]; ++n) {
1462 str += _session.engine().make_port_name_relative (connections[n]);
1474 node->add_property ("outputs", str);
1477 node->add_child_nocopy (_panner->state (full_state));
1479 snprintf (buf, sizeof(buf), "%2.12f", gain());
1480 node->add_property ("gain", buf);
1482 snprintf (buf, sizeof(buf)-1, "%d,%d,%d,%d",
1488 node->add_property ("iolimits", buf);
1492 MIDI::channel_t chn;
1494 MIDI::byte additional;
1495 XMLNode* midi_node = 0;
1498 if (_midi_gain_control.get_control_info (chn, ev, additional)) {
1500 midi_node = node->add_child ("MIDI");
1502 child = midi_node->add_child ("gain");
1503 set_midi_node_info (child, ev, chn, additional);
1509 snprintf (buf, sizeof (buf), "0x%x", (int) _gain_automation_curve.automation_state());
1511 /* never store anything except Off for automation state in a template */
1512 snprintf (buf, sizeof (buf), "0x%x", ARDOUR::Off);
1514 node->add_property ("automation-state", buf);
1515 snprintf (buf, sizeof (buf), "0x%x", (int) _gain_automation_curve.automation_style());
1516 node->add_property ("automation-style", buf);
1518 /* XXX same for pan etc. */
1524 IO::connecting_became_legal ()
1528 if (pending_state_node == 0) {
1529 fatal << _("IO::connecting_became_legal() called without a pending state node") << endmsg;
1534 connection_legal_c.disconnect ();
1536 ret = make_connections (*pending_state_node);
1539 delete pending_state_node;
1540 pending_state_node = 0;
1547 IO::ports_became_legal ()
1551 if (pending_state_node == 0) {
1552 fatal << _("IO::ports_became_legal() called without a pending state node") << endmsg;
1557 port_legal_c.disconnect ();
1559 ret = create_ports (*pending_state_node);
1561 if (connecting_legal) {
1562 delete pending_state_node;
1563 pending_state_node = 0;
1570 IO::set_state (const XMLNode& node)
1572 const XMLProperty* prop;
1573 XMLNodeConstIterator iter;
1574 XMLNodeList midi_kids;
1575 LocaleGuard lg (X_("POSIX"));
1577 /* force use of non-localized representation of decimal point,
1578 since we use it a lot in XML files and so forth.
1581 if (node.name() != state_node_name) {
1582 error << compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
1586 if ((prop = node.property ("name")) != 0) {
1587 _name = prop->value();
1588 _panner->set_name (_name);
1591 if ((prop = node.property ("id")) != 0) {
1592 sscanf (prop->value().c_str(), "%llu", &_id);
1595 if ((prop = node.property ("iolimits")) != 0) {
1596 sscanf (prop->value().c_str(), "%d,%d,%d,%d",
1603 if ((prop = node.property ("gain")) != 0) {
1604 set_gain (atof (prop->value().c_str()), this);
1605 _gain = _desired_gain;
1608 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
1609 if ((*iter)->name() == "Panner") {
1610 _panner->set_state (**iter);
1614 midi_kids = node.children ("MIDI");
1616 for (iter = midi_kids.begin(); iter != midi_kids.end(); ++iter) {
1619 XMLNodeConstIterator miter;
1622 kids = (*iter)->children ();
1624 for (miter = kids.begin(); miter != kids.end(); ++miter) {
1628 if (child->name() == "gain") {
1630 MIDI::eventType ev = MIDI::on; /* initialize to keep gcc happy */
1631 MIDI::byte additional = 0; /* ditto */
1632 MIDI::channel_t chn = 0; /* ditto */
1634 if (get_midi_node_info (child, ev, chn, additional)) {
1635 _midi_gain_control.set_control_type (chn, ev, additional);
1637 error << compose(_("MIDI gain control specification for %1 is incomplete, so it has been ignored"), _name) << endmsg;
1643 if ((prop = node.property ("automation-state")) != 0) {
1646 x = strtol (prop->value().c_str(), 0, 16);
1647 set_gain_automation_state (AutoState (x));
1650 if ((prop = node.property ("automation-style")) != 0) {
1653 x = strtol (prop->value().c_str(), 0, 16);
1654 set_gain_automation_style (AutoStyle (x));
1659 if (create_ports (node)) {
1665 port_legal_c = PortsLegal.connect (mem_fun (*this, &IO::ports_became_legal));
1668 if (panners_legal) {
1671 panner_legal_c = PannersLegal.connect (mem_fun (*this, &IO::panners_became_legal));
1674 if (connecting_legal) {
1676 if (make_connections (node)) {
1682 connection_legal_c = ConnectingLegal.connect (mem_fun (*this, &IO::connecting_became_legal));
1685 if (!ports_legal || !connecting_legal) {
1686 pending_state_node = new XMLNode (node);
1693 IO::create_ports (const XMLNode& node)
1695 const XMLProperty* prop;
1697 int num_outputs = 0;
1699 if ((prop = node.property ("input-connection")) != 0) {
1701 Connection* c = _session.connection_by_name (prop->value());
1704 error << compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
1706 if ((c = _session.connection_by_name (_("in 1"))) == 0) {
1707 error << _("No input connections available as a replacement")
1711 info << compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value())
1716 num_inputs = c->nports();
1718 } else if ((prop = node.property ("inputs")) != 0) {
1720 num_inputs = count (prop->value().begin(), prop->value().end(), '{');
1723 if ((prop = node.property ("output-connection")) != 0) {
1724 Connection* c = _session.connection_by_name (prop->value());
1727 error << compose(_("Unknown connection \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
1729 if ((c = _session.connection_by_name (_("out 1"))) == 0) {
1730 error << _("No output connections available as a replacement")
1734 info << compose (_("Connection %1 was not available - \"out 1\" used instead"), prop->value())
1739 num_outputs = c->nports ();
1741 } else if ((prop = node.property ("outputs")) != 0) {
1742 num_outputs = count (prop->value().begin(), prop->value().end(), '{');
1745 no_panner_reset = true;
1747 if (ensure_io (num_inputs, num_outputs, true, this)) {
1748 error << compose(_("%1: cannot create I/O ports"), _name) << endmsg;
1752 no_panner_reset = false;
1754 set_deferred_state ();
1761 IO::get_midi_node_info (XMLNode * node, MIDI::eventType & ev, MIDI::channel_t & chan, MIDI::byte & additional)
1764 const XMLProperty* prop;
1767 if ((prop = node->property ("event")) != 0) {
1768 sscanf (prop->value().c_str(), "0x%x", &xx);
1769 ev = (MIDI::eventType) xx;
1774 if (ok && ((prop = node->property ("channel")) != 0)) {
1775 sscanf (prop->value().c_str(), "%d", &xx);
1776 chan = (MIDI::channel_t) xx;
1781 if (ok && ((prop = node->property ("additional")) != 0)) {
1782 sscanf (prop->value().c_str(), "0x%x", &xx);
1783 additional = (MIDI::byte) xx;
1790 IO::set_midi_node_info (XMLNode * node, MIDI::eventType ev, MIDI::channel_t chan, MIDI::byte additional)
1794 snprintf (buf, sizeof(buf), "0x%x", ev);
1795 node->add_property ("event", buf);
1796 snprintf (buf, sizeof(buf), "%d", chan);
1797 node->add_property ("channel", buf);
1798 snprintf (buf, sizeof(buf), "0x%x", additional);
1799 node->add_property ("additional", buf);
1806 IO::make_connections (const XMLNode& node)
1808 const XMLProperty* prop;
1810 if ((prop = node.property ("input-connection")) != 0) {
1811 Connection* c = _session.connection_by_name (prop->value());
1814 error << compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
1816 if ((c = _session.connection_by_name (_("in 1"))) == 0) {
1817 error << _("No input connections available as a replacement")
1821 info << compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value())
1826 use_input_connection (*c, this);
1828 } else if ((prop = node.property ("inputs")) != 0) {
1829 if (set_inputs (prop->value())) {
1830 error << compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
1835 if ((prop = node.property ("output-connection")) != 0) {
1836 Connection* c = _session.connection_by_name (prop->value());
1839 error << compose(_("Unknown connection \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
1841 if ((c = _session.connection_by_name (_("out 1"))) == 0) {
1842 error << _("No output connections available as a replacement")
1846 info << compose (_("Connection %1 was not available - \"out 1\" used instead"), prop->value())
1851 use_output_connection (*c, this);
1853 } else if ((prop = node.property ("outputs")) != 0) {
1854 if (set_outputs (prop->value())) {
1855 error << compose(_("improper output channel list in XML node (%1)"), prop->value()) << endmsg;
1864 IO::set_inputs (const string& str)
1866 vector<string> ports;
1871 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1875 if (ensure_inputs (nports, true, true, this)) {
1879 string::size_type start, end, ostart;
1886 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1889 if ((end = str.find_first_of ('}', start)) == string::npos) {
1890 error << compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1894 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1895 error << compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1901 for (int x = 0; x < n; ++x) {
1902 connect_input (input (i), ports[x], this);
1914 IO::set_outputs (const string& str)
1916 vector<string> ports;
1921 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1925 if (ensure_outputs (nports, true, true, this)) {
1929 string::size_type start, end, ostart;
1936 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1939 if ((end = str.find_first_of ('}', start)) == string::npos) {
1940 error << compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1944 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1945 error << compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1951 for (int x = 0; x < n; ++x) {
1952 connect_output (output (i), ports[x], this);
1964 IO::parse_io_string (const string& str, vector<string>& ports)
1966 string::size_type pos, opos;
1968 if (str.length() == 0) {
1977 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1978 ports.push_back (str.substr (opos, pos - opos));
1982 if (opos < str.length()) {
1983 ports.push_back (str.substr(opos));
1986 return ports.size();
1990 IO::parse_gain_string (const string& str, vector<string>& ports)
1992 string::size_type pos, opos;
1998 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1999 ports.push_back (str.substr (opos, pos - opos));
2003 if (opos < str.length()) {
2004 ports.push_back (str.substr(opos));
2007 return ports.size();
2011 IO::set_name (string name, void* src)
2013 if (name == _name) {
2017 for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2018 string current_name = (*i)->short_name();
2019 current_name.replace (current_name.find (_name), _name.length(), name);
2020 (*i)->set_name (current_name);
2023 for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
2024 string current_name = (*i)->short_name();
2025 current_name.replace (current_name.find (_name), _name.length(), name);
2026 (*i)->set_name (current_name);
2030 name_changed (src); /* EMIT SIGNAL */
2036 IO::set_input_minimum (int n)
2042 IO::set_input_maximum (int n)
2048 IO::set_output_minimum (int n)
2050 _output_minimum = n;
2054 IO::set_output_maximum (int n)
2056 _output_maximum = n;
2060 IO::set_port_latency (jack_nframes_t nframes)
2062 LockMonitor lm (io_lock, __LINE__, __FILE__);
2064 for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
2065 (*i)->set_latency (nframes);
2070 IO::output_latency () const
2072 jack_nframes_t max_latency;
2073 jack_nframes_t latency;
2077 /* io lock not taken - must be protected by other means */
2079 for (vector<Port *>::const_iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
2080 if ((latency = _session.engine().get_port_total_latency (*(*i))) > max_latency) {
2081 max_latency = latency;
2089 IO::input_latency () const
2091 jack_nframes_t max_latency;
2092 jack_nframes_t latency;
2096 /* io lock not taken - must be protected by other means */
2098 for (vector<Port *>::const_iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2099 if ((latency = _session.engine().get_port_total_latency (*(*i))) > max_latency) {
2100 max_latency = latency;
2108 IO::use_input_connection (Connection& c, void* src)
2113 LockMonitor lm (_session.engine().process_lock(), __LINE__, __FILE__);
2114 LockMonitor lm2 (io_lock, __LINE__, __FILE__);
2118 drop_input_connection ();
2120 if (ensure_inputs (limit, false, false, src)) {
2124 /* first pass: check the current state to see what's correctly
2125 connected, and drop anything that we don't want.
2128 for (uint32_t n = 0; n < limit; ++n) {
2129 const Connection::PortList& pl = c.port_connections (n);
2131 for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
2133 if (!_inputs[n]->connected_to ((*i))) {
2135 /* clear any existing connections */
2137 _session.engine().disconnect (_inputs[n]);
2139 } else if (_inputs[n]->connected() > 1) {
2141 /* OK, it is connected to the port we want,
2142 but its also connected to other ports.
2143 Change that situation.
2146 /* XXX could be optimized to not drop
2150 _session.engine().disconnect (_inputs[n]);
2156 /* second pass: connect all requested ports where necessary */
2158 for (uint32_t n = 0; n < limit; ++n) {
2159 const Connection::PortList& pl = c.port_connections (n);
2161 for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
2163 if (!_inputs[n]->connected_to ((*i))) {
2165 if (_session.engine().connect (*i, _inputs[n]->name())) {
2173 _input_connection = &c;
2175 input_connection_configuration_connection = c.ConfigurationChanged.connect
2176 (mem_fun (*this, &IO::input_connection_configuration_changed));
2177 input_connection_connection_connection = c.ConnectionsChanged.connect
2178 (mem_fun (*this, &IO::input_connection_connection_changed));
2181 input_changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */
2186 IO::use_output_connection (Connection& c, void* src)
2191 LockMonitor lm (_session.engine().process_lock(), __LINE__, __FILE__);
2192 LockMonitor lm2 (io_lock, __LINE__, __FILE__);
2196 drop_output_connection ();
2198 if (ensure_outputs (limit, false, false, src)) {
2202 /* first pass: check the current state to see what's correctly
2203 connected, and drop anything that we don't want.
2206 for (uint32_t n = 0; n < limit; ++n) {
2208 const Connection::PortList& pl = c.port_connections (n);
2210 for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
2212 if (!_outputs[n]->connected_to ((*i))) {
2214 /* clear any existing connections */
2216 _session.engine().disconnect (_outputs[n]);
2218 } else if (_outputs[n]->connected() > 1) {
2220 /* OK, it is connected to the port we want,
2221 but its also connected to other ports.
2222 Change that situation.
2225 /* XXX could be optimized to not drop
2229 _session.engine().disconnect (_outputs[n]);
2234 /* second pass: connect all requested ports where necessary */
2236 for (uint32_t n = 0; n < limit; ++n) {
2238 const Connection::PortList& pl = c.port_connections (n);
2240 for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
2242 if (!_outputs[n]->connected_to ((*i))) {
2244 if (_session.engine().connect (_outputs[n]->name(), *i)) {
2251 _output_connection = &c;
2253 output_connection_configuration_connection = c.ConfigurationChanged.connect
2254 (mem_fun (*this, &IO::output_connection_configuration_changed));
2255 output_connection_connection_connection = c.ConnectionsChanged.connect
2256 (mem_fun (*this, &IO::output_connection_connection_changed));
2259 output_changed (IOChange (ConnectionsChanged|ConfigurationChanged), src); /* EMIT SIGNAL */
2265 IO::disable_connecting ()
2267 connecting_legal = false;
2272 IO::enable_connecting ()
2274 connecting_legal = true;
2275 return ConnectingLegal ();
2279 IO::disable_ports ()
2281 ports_legal = false;
2289 return PortsLegal ();
2293 IO::disable_panners (void)
2295 panners_legal = false;
2300 IO::reset_panners ()
2302 panners_legal = true;
2303 return PannersLegal ();
2307 IO::input_connection_connection_changed (int ignored)
2309 use_input_connection (*_input_connection, this);
2313 IO::input_connection_configuration_changed ()
2315 use_input_connection (*_input_connection, this);
2319 IO::output_connection_connection_changed (int ignored)
2321 use_output_connection (*_output_connection, this);
2325 IO::output_connection_configuration_changed ()
2327 use_output_connection (*_output_connection, this);
2330 IO::MIDIGainControl::MIDIGainControl (IO& i, MIDI::Port* port)
2331 : MIDI::Controllable (port, 0), io (i), setting(false)
2336 last_written = 0; /* XXX need a good out-of-bound-value */
2340 IO::MIDIGainControl::set_value (float val)
2342 if (midi_to_gain == 0) return;
2345 io.set_gain (midi_to_gain (val), this);
2350 IO::MIDIGainControl::send_feedback (gain_t gain)
2352 if (!setting && get_midi_feedback() && gain_to_midi) {
2353 MIDI::byte val = (MIDI::byte) (gain_to_midi (gain) * 127.0);
2354 MIDI::channel_t ch = 0;
2355 MIDI::eventType ev = MIDI::none;
2356 MIDI::byte additional = 0;
2357 MIDI::EventTwoBytes data;
2359 if (get_control_info (ch, ev, additional)) {
2360 data.controller_number = additional;
2363 io._session.send_midi_message (get_port(), ev, ch, data);
2365 //send_midi_feedback (gain_to_midi (gain));
2370 IO::MIDIGainControl::write_feedback (MIDI::byte* buf, int32_t& bufsize, gain_t val, bool force)
2372 if (get_midi_feedback() && gain_to_midi && bufsize > 2) {
2373 MIDI::channel_t ch = 0;
2374 MIDI::eventType ev = MIDI::none;
2375 MIDI::byte additional = 0;
2378 if (get_control_info (ch, ev, additional)) {
2379 gm = (MIDI::byte) (gain_to_midi (val) * 127.0);
2381 if (gm != last_written) {
2382 *buf++ = (0xF0 & ev) | (0xF & ch);
2383 *buf++ = additional; /* controller number */
2395 IO::reset_peak_meters ()
2397 uint32_t limit = max (_ninputs, _noutputs);
2399 for (uint32_t i = 0; i < limit; ++i) {
2405 IO::setup_peak_meters ()
2407 uint32_t limit = max (_ninputs, _noutputs);
2409 while (_peak_power.size() < limit) {
2410 _peak_power.push_back (0);
2411 _stored_peak_power.push_back (0);
2416 IO::get_memento() const
2418 return sigc::bind (mem_fun (*(const_cast<IO *>(this)), &StateManager::use_state), _current_state_id);
2422 IO::restore_state (StateManager::State& state)
2427 StateManager::State*
2428 IO::state_factory (std::string why) const
2430 StateManager::State* state = new StateManager::State (why);
2435 IO::send_state_changed ()
2441 IO::grab_peak_power ()
2443 LockMonitor lm (io_lock, __LINE__, __FILE__);
2445 uint32_t limit = max (_ninputs, _noutputs);
2447 for (uint32_t n = 0; n < limit; ++n) {
2448 /* XXX should we use atomic exchange here ? */
2449 _stored_peak_power[n] = _peak_power[n];
2455 IO::reset_midi_control (MIDI::Port* port, bool on)
2457 MIDI::channel_t chn;
2461 _midi_gain_control.get_control_info (chn, ev, extra);
2465 _midi_gain_control.midi_rebind (port, chn);
2467 _panner->reset_midi_control (port, on);
2472 IO::save_automation (const string& path)
2477 fullpath = _session.automation_dir();
2480 out.open (fullpath.c_str());
2483 error << compose(_("%1: could not open automation event file \"%2\""), _name, fullpath) << endmsg;
2487 out << X_("version ") << current_automation_version_number << endl;
2489 /* XXX use apply_to_points to get thread safety */
2491 for (AutomationList::iterator i = _gain_automation_curve.begin(); i != _gain_automation_curve.end(); ++i) {
2492 out << "g " << (jack_nframes_t) floor ((*i)->when) << ' ' << (*i)->value << endl;
2501 IO::load_automation (const string& path)
2506 uint32_t linecnt = 0;
2508 LocaleGuard lg (X_("POSIX"));
2510 fullpath = _session.automation_dir();
2513 in.open (fullpath.c_str());
2516 fullpath = _session.automation_dir();
2517 fullpath += _session.snap_name();
2520 in.open (fullpath.c_str());
2522 error << compose(_("%1: cannot open automation event file \"%2\""), _name, fullpath) << endmsg;
2527 clear_automation ();
2529 while (in.getline (line, sizeof(line), '\n')) {
2531 jack_nframes_t when;
2534 if (++linecnt == 1) {
2535 if (memcmp (line, "version", 7) == 0) {
2536 if (sscanf (line, "version %f", &version) != 1) {
2537 error << compose(_("badly formed version number in automation event file \"%1\""), path) << endmsg;
2541 error << compose(_("no version information in automation event file \"%1\""), path) << endmsg;
2545 if (version != current_automation_version_number) {
2546 error << compose(_("mismatched automation event file version (%1)"), version) << endmsg;
2553 if (sscanf (line, "%c %" PRIu32 " %lf", &type, &when, &value) != 3) {
2554 warning << compose(_("badly formatted automation event record at line %1 of %2 (ignored)"), linecnt, path) << endmsg;
2560 _gain_automation_curve.add (when, value, true);
2570 /* older (pre-1.0) versions of ardour used this */
2574 warning << _("dubious automation event found (and ignored)") << endmsg;
2578 _gain_automation_curve.save_state (_("loaded from disk"));
2584 IO::clear_automation ()
2586 LockMonitor lm (automation_lock, __LINE__, __FILE__);
2587 _gain_automation_curve.clear ();
2588 _panner->clear_automation ();
2592 IO::set_gain_automation_state (AutoState state)
2594 bool changed = false;
2597 LockMonitor lm (automation_lock, __LINE__, __FILE__);
2599 if (state != _gain_automation_curve.automation_state()) {
2601 last_automation_snapshot = 0;
2602 _gain_automation_curve.set_automation_state (state);
2605 set_gain (_gain_automation_curve.eval (_session.transport_frame()), this);
2611 _session.set_dirty ();
2612 gain_automation_state_changed (); /* EMIT SIGNAL */
2617 IO::set_gain_automation_style (AutoStyle style)
2619 bool changed = false;
2622 LockMonitor lm (automation_lock, __LINE__, __FILE__);
2624 if (style != _gain_automation_curve.automation_style()) {
2626 _gain_automation_curve.set_automation_style (style);
2631 gain_automation_style_changed (); /* EMIT SIGNAL */
2635 IO::inc_gain (gain_t factor, void *src)
2637 if (_desired_gain == 0.0f)
2638 set_gain (0.000001f + (0.000001f * factor), src);
2640 set_gain (_desired_gain + (_desired_gain * factor), src);
2644 IO::set_gain (gain_t val, void *src)
2646 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
2647 if (val>1.99526231f) val=1.99526231f;
2650 LockMonitor dm (declick_lock, __LINE__, __FILE__);
2651 _desired_gain = val;
2654 if (_session.transport_stopped()) {
2655 _effective_gain = val;
2661 if (_session.get_midi_feedback()) {
2662 _midi_gain_control.send_feedback (_desired_gain);
2665 if (_session.transport_stopped() && src != 0 && src != this && gain_automation_recording()) {
2666 _gain_automation_curve.add (_session.transport_frame(), val);
2670 _session.set_dirty();
2674 IO::send_all_midi_feedback ()
2676 if (_session.get_midi_feedback()) {
2677 _midi_gain_control.send_feedback (_effective_gain);
2680 _panner->send_all_midi_feedback();
2685 IO::write_midi_feedback (MIDI::byte* buf, int32_t& bufsize)
2687 if (_session.get_midi_feedback()) {
2688 if (gain_automation_playback ()) {
2689 buf = _midi_gain_control.write_feedback (buf, bufsize, _effective_gain);
2691 buf = _panner->write_midi_feedback (buf, bufsize);
2698 IO::start_gain_touch ()
2700 _gain_automation_curve.start_touch ();
2704 IO::end_gain_touch ()
2706 _gain_automation_curve.stop_touch ();
2710 IO::start_pan_touch (uint32_t which)
2712 if (which < _panner->size()) {
2713 (*_panner)[which]->automation().start_touch();
2718 IO::end_pan_touch (uint32_t which)
2720 if (which < _panner->size()) {
2721 (*_panner)[which]->automation().stop_touch();
2727 IO::automation_snapshot (jack_nframes_t now)
2729 if (last_automation_snapshot > now || (now - last_automation_snapshot) > _automation_interval) {
2731 if (gain_automation_recording()) {
2732 _gain_automation_curve.rt_add (now, gain());
2735 _panner->snapshot (now);
2737 last_automation_snapshot = now;
2742 IO::transport_stopped (jack_nframes_t frame)
2744 _gain_automation_curve.reposition_for_rt_add (frame);
2746 if (_gain_automation_curve.automation_state() != Off) {
2748 if (gain_automation_recording()) {
2749 _gain_automation_curve.save_state (_("automation write/touch"));
2752 /* the src=0 condition is a special signal to not propagate
2753 automation gain changes into the mix group when locating.
2756 set_gain (_gain_automation_curve.eval (frame), 0);
2759 _panner->transport_stopped (frame);
2763 IO::find_input_port_hole ()
2765 /* CALLER MUST HOLD IO LOCK */
2769 if (_inputs.empty()) {
2773 for (n = 1; n < UINT_MAX; ++n) {
2774 char buf[jack_port_name_size()];
2775 vector<Port*>::iterator i;
2777 snprintf (buf, jack_port_name_size(), _("%s/in %u"), _name.c_str(), n);
2779 for (i = _inputs.begin(); i != _inputs.end(); ++i) {
2780 if ((*i)->short_name() == buf) {
2785 if (i == _inputs.end()) {
2793 IO::find_output_port_hole ()
2795 /* CALLER MUST HOLD IO LOCK */
2799 if (_outputs.empty()) {
2803 for (n = 1; n < UINT_MAX; ++n) {
2804 char buf[jack_port_name_size()];
2805 vector<Port*>::iterator i;
2807 snprintf (buf, jack_port_name_size(), _("%s/out %u"), _name.c_str(), n);
2809 for (i = _outputs.begin(); i != _outputs.end(); ++i) {
2810 if ((*i)->short_name() == buf) {
2815 if (i == _outputs.end()) {