2 Copyright (C) 2002 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.
20 #include <pbd/error.h>
21 #include <sigc++/retype.h>
22 #include <sigc++/retype_return.h>
23 #include <sigc++/bind.h>
25 #include <ardour/audio_track.h>
26 #include <ardour/audio_diskstream.h>
27 #include <ardour/session.h>
28 #include <ardour/redirect.h>
29 #include <ardour/audioregion.h>
30 #include <ardour/audiosource.h>
31 #include <ardour/route_group_specialized.h>
32 #include <ardour/insert.h>
33 #include <ardour/audioplaylist.h>
34 #include <ardour/panner.h>
35 #include <ardour/utils.h>
40 using namespace ARDOUR;
43 AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
44 : Route (sess, name, 1, -1, -1, -1, flag),
46 _rec_enable_control (*this)
48 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
50 if (_flags & Hidden) {
51 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
53 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Recordable);
56 if (mode == Destructive) {
57 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
60 AudioDiskstream* ds = new AudioDiskstream (_session, name, dflags);
63 _freeze_record.state = NoFreeze;
64 _saved_meter_point = _meter_point;
67 set_diskstream (*ds, this);
70 AudioTrack::AudioTrack (Session& sess, const XMLNode& node)
71 : Route (sess, "to be renamed", 0, 0, -1, -1),
73 _rec_enable_control (*this)
75 _freeze_record.state = NoFreeze;
78 _saved_meter_point = _meter_point;
81 AudioTrack::~AudioTrack ()
89 AudioTrack::deprecated_use_diskstream_connections ()
91 if (diskstream->deprecated_io_node == 0) {
95 const XMLProperty* prop;
96 XMLNode& node (*diskstream->deprecated_io_node);
98 /* don't do this more than once. */
100 diskstream->deprecated_io_node = 0;
102 set_input_minimum (-1);
103 set_input_maximum (-1);
104 set_output_minimum (-1);
105 set_output_maximum (-1);
107 if ((prop = node.property ("gain")) != 0) {
108 set_gain (atof (prop->value().c_str()), this);
109 _gain = _desired_gain;
112 if ((prop = node.property ("input-connection")) != 0) {
113 Connection* c = _session.connection_by_name (prop->value());
116 error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
118 if ((c = _session.connection_by_name (_("in 1"))) == 0) {
119 error << _("No input connections available as a replacement")
123 info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value())
128 use_input_connection (*c, this);
130 } else if ((prop = node.property ("inputs")) != 0) {
131 if (set_inputs (prop->value())) {
132 error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
141 AudioTrack::set_diskstream (AudioDiskstream& ds, void *src)
147 diskstream = &ds.ref();
148 diskstream->set_io (*this);
149 diskstream->set_destructive (_mode == Destructive);
151 if (diskstream->deprecated_io_node) {
153 if (!connecting_legal) {
154 ConnectingLegal.connect (mem_fun (*this, &AudioTrack::deprecated_use_diskstream_connections));
156 deprecated_use_diskstream_connections ();
160 diskstream->set_record_enabled (false, this);
161 diskstream->monitor_input (false);
163 ic_connection.disconnect();
164 ic_connection = input_changed.connect (mem_fun (*diskstream, &AudioDiskstream::handle_input_change));
166 diskstream_changed (src); /* EMIT SIGNAL */
172 AudioTrack::use_diskstream (string name)
174 AudioDiskstream *dstream;
176 if ((dstream = _session.diskstream_by_name (name)) == 0) {
177 error << string_compose(_("AudioTrack: diskstream \"%1\" not known by session"), name) << endmsg;
181 return set_diskstream (*dstream, this);
185 AudioTrack::use_diskstream (const PBD::ID& id)
187 AudioDiskstream *dstream;
189 if ((dstream = _session.diskstream_by_id (id)) == 0) {
190 error << string_compose(_("AudioTrack: diskstream \"%1\" not known by session"), id) << endmsg;
194 return set_diskstream (*dstream, this);
198 AudioTrack::record_enabled () const
200 return diskstream->record_enabled ();
204 AudioTrack::set_record_enable (bool yn, void *src)
206 if (_freeze_record.state == Frozen) {
210 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
211 _mix_group->apply (&AudioTrack::set_record_enable, yn, _mix_group);
215 /* keep track of the meter point as it was before we rec-enabled */
217 if (!diskstream->record_enabled()) {
218 _saved_meter_point = _meter_point;
221 diskstream->set_record_enabled (yn, src);
223 if (diskstream->record_enabled()) {
224 set_meter_point (MeterInput, this);
226 set_meter_point (_saved_meter_point, this);
229 _rec_enable_control.Changed ();
233 AudioTrack::set_meter_point (MeterPoint p, void *src)
235 Route::set_meter_point (p, src);
239 AudioTrack::set_state (const XMLNode& node)
241 const XMLProperty *prop;
242 XMLNodeConstIterator iter;
244 if (Route::set_state (node)) {
248 if ((prop = node.property (X_("mode"))) != 0) {
249 if (prop->value() == X_("normal")) {
251 } else if (prop->value() == X_("destructive")) {
254 warning << string_compose ("unknown audio track mode \"%1\" seen and ignored", prop->value()) << endmsg;
261 if ((prop = node.property ("diskstream-id")) == 0) {
263 /* some old sessions use the diskstream name rather than the ID */
265 if ((prop = node.property ("diskstream")) == 0) {
266 fatal << _("programming error: AudioTrack given state without diskstream!") << endmsg;
271 if (use_diskstream (prop->value())) {
277 PBD::ID id (prop->value());
279 if (use_diskstream (id)) {
286 XMLNodeConstIterator niter;
289 nlist = node.children();
290 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
293 if (child->name() == X_("remote_control")) {
294 if ((prop = child->property (X_("id"))) != 0) {
296 sscanf (prop->value().c_str(), "%d", &x);
297 set_remote_control_id (x);
302 pending_state = const_cast<XMLNode*> (&node);
304 _session.StateReady.connect (mem_fun (*this, &AudioTrack::set_state_part_two));
310 AudioTrack::get_template ()
312 return state (false);
316 AudioTrack::get_state ()
322 AudioTrack::state(bool full_state)
324 XMLNode& root (Route::state(full_state));
325 XMLNode* freeze_node;
328 if (_freeze_record.playlist) {
331 freeze_node = new XMLNode (X_("freeze-info"));
332 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
333 snprintf (buf, sizeof (buf), "%d", (int) _freeze_record.state);
334 freeze_node->add_property ("state", buf);
336 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
337 inode = new XMLNode (X_("insert"));
338 (*i)->id.print (buf);
339 inode->add_property (X_("id"), buf);
340 inode->add_child_copy ((*i)->state);
342 freeze_node->add_child_nocopy (*inode);
345 root.add_child_nocopy (*freeze_node);
348 /* Alignment: act as a proxy for the diskstream */
350 XMLNode* align_node = new XMLNode (X_("alignment"));
351 switch (diskstream->alignment_style()) {
352 case ExistingMaterial:
353 snprintf (buf, sizeof (buf), X_("existing"));
356 snprintf (buf, sizeof (buf), X_("capture"));
359 align_node->add_property (X_("style"), buf);
360 root.add_child_nocopy (*align_node);
362 XMLNode* remote_control_node = new XMLNode (X_("remote_control"));
363 snprintf (buf, sizeof (buf), "%d", _remote_control_id);
364 remote_control_node->add_property (X_("id"), buf);
365 root.add_child_nocopy (*remote_control_node);
369 root.add_property (X_("mode"), X_("normal"));
372 root.add_property (X_("mode"), X_("destructive"));
376 /* we don't return diskstream state because we don't
377 own the diskstream exclusively. control of the diskstream
378 state is ceded to the Session, even if we create the
382 diskstream->id().print (buf);
383 root.add_property ("diskstream-id", buf);
389 AudioTrack::set_state_part_two ()
393 LocaleGuard lg (X_("POSIX"));
395 /* This is called after all session state has been restored but before
396 have been made ports and connections are established.
399 if (pending_state == 0) {
403 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
406 _freeze_record.have_mementos = false;
407 _freeze_record.state = Frozen;
409 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
412 _freeze_record.insert_info.clear ();
414 if ((prop = fnode->property (X_("playlist"))) != 0) {
415 Playlist* pl = _session.playlist_by_name (prop->value());
417 _freeze_record.playlist = dynamic_cast<AudioPlaylist*> (pl);
419 _freeze_record.playlist = 0;
420 _freeze_record.state = NoFreeze;
425 if ((prop = fnode->property (X_("state"))) != 0) {
426 _freeze_record.state = (FreezeState) atoi (prop->value().c_str());
429 XMLNodeConstIterator citer;
430 XMLNodeList clist = fnode->children();
432 for (citer = clist.begin(); citer != clist.end(); ++citer) {
433 if ((*citer)->name() != X_("insert")) {
437 if ((prop = (*citer)->property (X_("id"))) == 0) {
441 FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo (*((*citer)->children().front()),
442 boost::shared_ptr<Insert>());
443 frii->id = prop->value ();
444 _freeze_record.insert_info.push_back (frii);
448 /* Alignment: act as a proxy for the diskstream */
450 if ((fnode = find_named_node (*pending_state, X_("alignment"))) != 0) {
452 if ((prop = fnode->property (X_("style"))) != 0) {
453 if (prop->value() == "existing") {
454 diskstream->set_persistent_align_style (ExistingMaterial);
455 } else if (prop->value() == "capture") {
456 diskstream->set_persistent_align_style (CaptureTime);
464 AudioTrack::n_process_buffers ()
466 return max ((uint32_t) diskstream->n_channels(), redirect_max_outs);
470 AudioTrack::passthru_silence (jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t nframes, jack_nframes_t offset, int declick, bool meter)
472 uint32_t nbufs = n_process_buffers ();
473 process_output_buffers (_session.get_silent_buffers (nbufs), nbufs, start_frame, end_frame, nframes, offset, true, declick, meter);
477 AudioTrack::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
478 bool session_state_changing, bool can_record, bool rec_monitors_input)
480 if (n_outputs() == 0) {
485 silence (nframes, offset);
489 if (session_state_changing) {
491 /* XXX is this safe to do against transport state changes? */
493 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
497 diskstream->check_record_status (start_frame, nframes, can_record);
501 if (_have_internal_generator) {
502 /* since the instrument has no input streams,
503 there is no reason to send any signal
509 if (_session.get_auto_input()) {
510 if (Config->get_use_sw_monitoring()) {
511 send_silence = false;
516 if (diskstream->record_enabled()) {
517 if (Config->get_use_sw_monitoring()) {
518 send_silence = false;
528 apply_gain_automation = false;
532 /* if we're sending silence, but we want the meters to show levels for the signal,
536 if (_have_internal_generator) {
537 passthru_silence (start_frame, end_frame, nframes, offset, 0, true);
539 if (_meter_point == MeterInput) {
540 just_meter_input (start_frame, end_frame, nframes, offset);
542 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
547 /* we're sending signal, but we may still want to meter the input.
550 passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput));
557 AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, int declick,
558 bool can_record, bool rec_monitors_input)
563 jack_nframes_t transport_frame;
566 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
568 // automation snapshot can also be called from the non-rt context
569 // and it uses the redirect list, so we take the lock out here
570 automation_snapshot (start_frame);
574 if (n_outputs() == 0 && _redirects.empty()) {
579 silence (nframes, offset);
583 transport_frame = _session.transport_frame();
585 if ((nframes = check_initial_delay (nframes, offset, transport_frame)) == 0) {
586 /* need to do this so that the diskstream sets its
587 playback distance to zero, thus causing diskstream::commit
590 return diskstream->process (transport_frame, 0, 0, can_record, rec_monitors_input);
594 apply_gain_automation = false;
596 if ((dret = diskstream->process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
598 silence (nframes, offset);
603 /* special condition applies */
605 if (_meter_point == MeterInput) {
606 just_meter_input (start_frame, end_frame, nframes, offset);
609 if (diskstream->record_enabled() && !can_record && !_session.get_auto_input()) {
611 /* not actually recording, but we want to hear the input material anyway,
612 at least potentially (depending on monitoring options)
615 passthru (start_frame, end_frame, nframes, offset, 0, true);
617 } else if ((b = diskstream->playback_buffer(0)) != 0) {
620 XXX is it true that the earlier test on n_outputs()
621 means that we can avoid checking it again here? i think
622 so, because changing the i/o configuration of an IO
623 requires holding the AudioEngine lock, which we hold
624 while in the process() tree.
628 /* copy the diskstream data to all output buffers */
630 vector<Sample*>& bufs = _session.get_passthru_buffers ();
631 uint32_t limit = n_process_buffers ();
637 for (i = 0, n = 1; i < limit; ++i, ++n) {
638 memcpy (bufs[i], b, sizeof (Sample) * nframes);
639 if (n < diskstream->n_channels()) {
640 tmpb = diskstream->playback_buffer(n);
647 /* don't waste time with automation if we're recording or we've just stopped (yes it can happen) */
649 if (!diskstream->record_enabled() && _session.transport_rolling()) {
650 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
652 if (am.locked() && gain_automation_playback()) {
653 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
657 process_output_buffers (bufs, limit, start_frame, end_frame, nframes, offset, (!_session.get_record_enabled() || !_session.get_do_not_record_plugins()), declick, (_meter_point != MeterInput));
660 /* problem with the diskstream; just be quiet for a bit */
661 silence (nframes, offset);
668 AudioTrack::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
669 bool can_record, bool rec_monitors_input)
671 if (n_outputs() == 0 && _redirects.empty()) {
676 silence (nframes, offset);
681 apply_gain_automation = false;
683 silence (nframes, offset);
685 return diskstream->process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
689 AudioTrack::toggle_monitor_input ()
691 for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
692 (*i)->request_monitor_input(!(*i)->monitoring_input());
697 AudioTrack::set_name (string str, void *src)
701 if (record_enabled() && _session.actively_recording()) {
702 /* this messes things up if done while recording */
706 if (diskstream->set_name (str, src)) {
710 /* save state so that the statefile fully reflects any filename changes */
712 if ((ret = IO::set_name (str, src)) == 0) {
713 _session.save_state ("");
719 AudioTrack::export_stuff (vector<Sample*>& buffers, char * workbuf, uint32_t nbufs, jack_nframes_t start, jack_nframes_t nframes)
721 gain_t gain_automation[nframes];
722 gain_t gain_buffer[nframes];
723 float mix_buffer[nframes];
724 RedirectList::iterator i;
725 bool post_fader_work = false;
726 gain_t this_gain = _gain;
727 vector<Sample*>::iterator bi;
730 Glib::RWLock::ReaderLock rlock (redirect_lock);
732 if (diskstream->playlist()->read (buffers[0], mix_buffer, gain_buffer, workbuf, start, nframes) != nframes) {
737 bi = buffers.begin();
740 for (; bi != buffers.end(); ++bi, ++n) {
741 if (n < diskstream->n_channels()) {
742 if (diskstream->playlist()->read ((*bi), mix_buffer, gain_buffer, workbuf, start, nframes, n) != nframes) {
748 /* duplicate last across remaining buffers */
749 memcpy ((*bi), b, sizeof (Sample) * nframes);
754 /* note: only run inserts during export. other layers in the machinery
755 will already have checked that there are no external port inserts.
758 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
759 boost::shared_ptr<Insert> insert;
761 if ((insert = boost::dynamic_pointer_cast<Insert>(*i)) != 0) {
762 switch (insert->placement()) {
764 insert->run (buffers, nbufs, nframes, 0);
767 post_fader_work = true;
773 if (_gain_automation_curve.automation_state() == Play) {
775 _gain_automation_curve.get_vector (start, start + nframes, gain_automation, nframes);
777 for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
779 for (jack_nframes_t n = 0; n < nframes; ++n) {
780 b[n] *= gain_automation[n];
786 for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
788 for (jack_nframes_t n = 0; n < nframes; ++n) {
794 if (post_fader_work) {
796 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
797 boost::shared_ptr<PluginInsert> insert;
799 if ((insert = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
800 switch ((*i)->placement()) {
804 insert->run (buffers, nbufs, nframes, 0);
815 AudioTrack::set_latency_delay (jack_nframes_t longest_session_latency)
817 Route::set_latency_delay (longest_session_latency);
818 diskstream->set_roll_delay (_roll_delay);
822 AudioTrack::update_total_latency ()
826 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
827 if ((*i)->active ()) {
828 _own_latency += (*i)->latency ();
832 set_port_latency (_own_latency);
838 AudioTrack::bounce (InterThreadInfo& itt)
840 vector<AudioSource*> srcs;
841 _session.write_one_audio_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
846 AudioTrack::bounce_range (jack_nframes_t start, jack_nframes_t end, InterThreadInfo& itt)
848 vector<AudioSource*> srcs;
849 _session.write_one_audio_track (*this, start, end, false, srcs, itt);
853 AudioTrack::freeze (InterThreadInfo& itt)
855 vector<AudioSource*> srcs;
856 string new_playlist_name;
857 Playlist* new_playlist;
862 if ((_freeze_record.playlist = diskstream->playlist()) == 0) {
868 while (n < (UINT_MAX-1)) {
872 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
874 if (_session.playlist_by_name (candidate) == 0) {
875 new_playlist_name = candidate;
883 if (n == (UINT_MAX-1)) {
884 error << string_compose (X_("There Are too many frozen versions of playlist \"%1\""
885 " to create another one"), _freeze_record.playlist->name())
890 if (_session.write_one_audio_track (*this, 0, _session.current_end_frame(), true, srcs, itt)) {
894 _freeze_record.insert_info.clear ();
895 _freeze_record.have_mementos = true;
898 Glib::RWLock::ReaderLock lm (redirect_lock);
900 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); ++r) {
902 boost::shared_ptr<Insert> insert;
904 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
906 FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo ((*r)->get_state(), insert);
908 frii->id = insert->id();
909 frii->memento = (*r)->get_memento();
911 _freeze_record.insert_info.push_back (frii);
913 /* now deactivate the insert */
915 insert->set_active (false, this);
920 new_playlist = new AudioPlaylist (_session, new_playlist_name, false);
921 region_name = new_playlist_name;
923 /* create a new region from all filesources, keep it private */
925 region = new AudioRegion (srcs, 0, srcs[0]->length(),
927 (AudioRegion::Flag) (AudioRegion::WholeFile|AudioRegion::DefaultFlags),
930 new_playlist->set_orig_diskstream_id (diskstream->id());
931 new_playlist->add_region (*region, 0);
932 new_playlist->set_frozen (true);
933 region->set_locked (true);
935 diskstream->use_playlist (dynamic_cast<AudioPlaylist*>(new_playlist));
936 diskstream->set_record_enabled (false, this);
938 _freeze_record.state = Frozen;
939 FreezeChange(); /* EMIT SIGNAL */
943 AudioTrack::unfreeze ()
945 if (_freeze_record.playlist) {
946 diskstream->use_playlist (_freeze_record.playlist);
948 if (_freeze_record.have_mementos) {
950 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
956 Glib::RWLock::ReaderLock lm (redirect_lock); // should this be a write lock? jlc
957 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
958 for (vector<FreezeRecordInsertInfo*>::iterator ii = _freeze_record.insert_info.begin(); ii != _freeze_record.insert_info.end(); ++ii) {
959 if ((*ii)->id == (*i)->id()) {
960 (*i)->set_state (((*ii)->state));
967 _freeze_record.playlist = 0;
970 _freeze_record.state = UnFrozen;
971 FreezeChange (); /* EMIT SIGNAL */
974 AudioTrack::FreezeRecord::~FreezeRecord ()
976 for (vector<FreezeRecordInsertInfo*>::iterator i = insert_info.begin(); i != insert_info.end(); ++i) {
981 AudioTrack::FreezeState
982 AudioTrack::freeze_state() const
984 return _freeze_record.state;
987 AudioTrack::RecEnableControllable::RecEnableControllable (AudioTrack& s)
993 AudioTrack::RecEnableControllable::set_value (float val)
995 bool bval = ((val >= 0.5f) ? true: false);
996 track.set_record_enable (bval, this);
1000 AudioTrack::RecEnableControllable::get_value (void) const
1002 if (track.record_enabled()) { return 1.0f; }
1007 AudioTrack::set_mode (TrackMode m)
1012 diskstream->set_destructive (m == Destructive);