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.
21 #include <sigc++/retype.h>
22 #include <sigc++/retype_return.h>
23 #include <sigc++/bind.h>
25 #include <pbd/error.h>
26 #include <pbd/enumwriter.h>
28 #include <ardour/audio_track.h>
29 #include <ardour/audio_diskstream.h>
30 #include <ardour/session.h>
31 #include <ardour/redirect.h>
32 #include <ardour/audioregion.h>
33 #include <ardour/audiosource.h>
34 #include <ardour/region_factory.h>
35 #include <ardour/route_group_specialized.h>
36 #include <ardour/insert.h>
37 #include <ardour/audioplaylist.h>
38 #include <ardour/playlist_factory.h>
39 #include <ardour/panner.h>
40 #include <ardour/utils.h>
45 using namespace ARDOUR;
48 AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
49 : Track (sess, name, flag, mode)
51 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
53 if (_flags & Hidden) {
54 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
56 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Recordable);
59 if (mode == Destructive) {
60 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
63 boost::shared_ptr<AudioDiskstream> ds (new AudioDiskstream (_session, name, dflags));
64 _session.add_diskstream (ds);
66 set_diskstream (boost::dynamic_pointer_cast<AudioDiskstream> (ds), this);
69 AudioTrack::AudioTrack (Session& sess, const XMLNode& node)
72 _set_state (node, false);
75 AudioTrack::~AudioTrack ()
80 AudioTrack::set_mode (TrackMode m)
84 if (_diskstream->set_destructive (m == Destructive)) {
90 TrackModeChanged (); /* EMIT SIGNAL */
97 AudioTrack::can_use_mode (TrackMode m, bool& bounce_required)
101 bounce_required = false;
106 return _diskstream->can_become_destructive (bounce_required);
111 AudioTrack::deprecated_use_diskstream_connections ()
113 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
115 if (diskstream->deprecated_io_node == 0) {
119 const XMLProperty* prop;
120 XMLNode& node (*diskstream->deprecated_io_node);
122 /* don't do this more than once. */
124 diskstream->deprecated_io_node = 0;
126 set_input_minimum (-1);
127 set_input_maximum (-1);
128 set_output_minimum (-1);
129 set_output_maximum (-1);
131 if ((prop = node.property ("gain")) != 0) {
132 set_gain (atof (prop->value().c_str()), this);
133 _gain = _desired_gain;
136 if ((prop = node.property ("input-connection")) != 0) {
137 Connection* c = _session.connection_by_name (prop->value());
140 error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
142 if ((c = _session.connection_by_name (_("in 1"))) == 0) {
143 error << _("No input connections available as a replacement")
147 info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value())
152 use_input_connection (*c, this);
154 } else if ((prop = node.property ("inputs")) != 0) {
155 if (set_inputs (prop->value())) {
156 error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
165 AudioTrack::set_diskstream (boost::shared_ptr<AudioDiskstream> ds, void *src)
168 _diskstream->set_io (*this);
169 _diskstream->set_destructive (_mode == Destructive);
171 if (audio_diskstream()->deprecated_io_node) {
173 if (!connecting_legal) {
174 ConnectingLegal.connect (mem_fun (*this, &AudioTrack::deprecated_use_diskstream_connections));
176 deprecated_use_diskstream_connections ();
180 _diskstream->set_record_enabled (false);
181 _diskstream->monitor_input (false);
183 ic_connection.disconnect();
184 ic_connection = input_changed.connect (mem_fun (*_diskstream, &Diskstream::handle_input_change));
186 DiskstreamChanged (); /* EMIT SIGNAL */
192 AudioTrack::use_diskstream (string name)
194 boost::shared_ptr<AudioDiskstream> dstream;
196 if ((dstream = boost::dynamic_pointer_cast<AudioDiskstream>(_session.diskstream_by_name (name))) == 0) {
197 error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), name) << endmsg;
201 return set_diskstream (dstream, this);
205 AudioTrack::use_diskstream (const PBD::ID& id)
207 boost::shared_ptr<AudioDiskstream> dstream;
209 if ((dstream = boost::dynamic_pointer_cast<AudioDiskstream> (_session.diskstream_by_id (id))) == 0) {
210 error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), id) << endmsg;
214 return set_diskstream (dstream, this);
217 boost::shared_ptr<AudioDiskstream>
218 AudioTrack::audio_diskstream() const
220 return boost::dynamic_pointer_cast<AudioDiskstream>(_diskstream);
224 AudioTrack::set_state (const XMLNode& node)
226 return _set_state (node, true);
230 AudioTrack::_set_state (const XMLNode& node, bool call_base)
232 const XMLProperty *prop;
233 XMLNodeConstIterator iter;
236 if (Route::_set_state (node, call_base)) {
241 if ((prop = node.property (X_("mode"))) != 0) {
242 _mode = TrackMode (string_2_enum (prop->value(), _mode));
247 if ((prop = node.property ("diskstream-id")) == 0) {
249 /* some old sessions use the diskstream name rather than the ID */
251 if ((prop = node.property ("diskstream")) == 0) {
252 fatal << _("programming error: AudioTrack given state without diskstream!") << endmsg;
257 if (use_diskstream (prop->value())) {
263 PBD::ID id (prop->value());
265 if (use_diskstream (id)) {
272 XMLNodeConstIterator niter;
275 nlist = node.children();
276 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
279 if (child->name() == X_("remote_control")) {
280 if ((prop = child->property (X_("id"))) != 0) {
282 sscanf (prop->value().c_str(), "%d", &x);
283 set_remote_control_id (x);
286 } else if (child->name() == X_("recenable")) {
287 _rec_enable_control.set_state (*child);
288 _session.add_controllable (&_rec_enable_control);
292 pending_state = const_cast<XMLNode*> (&node);
294 _session.StateReady.connect (mem_fun (*this, &AudioTrack::set_state_part_two));
300 AudioTrack::state(bool full_state)
302 XMLNode& root (Route::state(full_state));
303 XMLNode* freeze_node;
306 if (_freeze_record.playlist) {
309 freeze_node = new XMLNode (X_("freeze-info"));
310 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
311 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
313 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
314 inode = new XMLNode (X_("insert"));
315 (*i)->id.print (buf, sizeof (buf));
316 inode->add_property (X_("id"), buf);
317 inode->add_child_copy ((*i)->state);
319 freeze_node->add_child_nocopy (*inode);
322 root.add_child_nocopy (*freeze_node);
325 /* Alignment: act as a proxy for the diskstream */
327 XMLNode* align_node = new XMLNode (X_("alignment"));
328 AlignStyle as = _diskstream->alignment_style ();
329 align_node->add_property (X_("style"), enum_2_string (as));
330 root.add_child_nocopy (*align_node);
332 XMLNode* remote_control_node = new XMLNode (X_("remote_control"));
333 snprintf (buf, sizeof (buf), "%d", _remote_control_id);
334 remote_control_node->add_property (X_("id"), buf);
335 root.add_child_nocopy (*remote_control_node);
337 root.add_property (X_("mode"), enum_2_string (_mode));
339 /* we don't return diskstream state because we don't
340 own the diskstream exclusively. control of the diskstream
341 state is ceded to the Session, even if we create the
345 _diskstream->id().print (buf, sizeof (buf));
346 root.add_property ("diskstream-id", buf);
348 root.add_child_nocopy (_rec_enable_control.get_state());
354 AudioTrack::set_state_part_two ()
358 LocaleGuard lg (X_("POSIX"));
360 /* This is called after all session state has been restored but before
361 have been made ports and connections are established.
364 if (pending_state == 0) {
368 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
371 _freeze_record.have_mementos = false;
372 _freeze_record.state = Frozen;
374 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
377 _freeze_record.insert_info.clear ();
379 if ((prop = fnode->property (X_("playlist"))) != 0) {
380 boost::shared_ptr<Playlist> pl = _session.playlist_by_name (prop->value());
382 _freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
384 _freeze_record.playlist.reset ();
385 _freeze_record.state = NoFreeze;
390 if ((prop = fnode->property (X_("state"))) != 0) {
391 _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
394 XMLNodeConstIterator citer;
395 XMLNodeList clist = fnode->children();
397 for (citer = clist.begin(); citer != clist.end(); ++citer) {
398 if ((*citer)->name() != X_("insert")) {
402 if ((prop = (*citer)->property (X_("id"))) == 0) {
406 FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo (*((*citer)->children().front()),
407 boost::shared_ptr<Insert>());
408 frii->id = prop->value ();
409 _freeze_record.insert_info.push_back (frii);
413 /* Alignment: act as a proxy for the diskstream */
415 if ((fnode = find_named_node (*pending_state, X_("alignment"))) != 0) {
417 if ((prop = fnode->property (X_("style"))) != 0) {
419 /* fix for older sessions from before EnumWriter */
423 if (prop->value() == "capture") {
424 pstr = "CaptureTime";
425 } else if (prop->value() == "existing") {
426 pstr = "ExistingMaterial";
428 pstr = prop->value();
431 AlignStyle as = AlignStyle (string_2_enum (pstr, as));
432 _diskstream->set_persistent_align_style (as);
439 AudioTrack::n_process_buffers ()
441 return max ((uint32_t) _diskstream->n_channels(), redirect_max_outs);
445 AudioTrack::passthru_silence (nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset, int declick, bool meter)
447 uint32_t nbufs = n_process_buffers ();
448 process_output_buffers (_session.get_silent_buffers (nbufs), nbufs, start_frame, end_frame, nframes, offset, true, declick, meter);
452 AudioTrack::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
453 bool session_state_changing, bool can_record, bool rec_monitors_input)
455 if (n_outputs() == 0) {
460 silence (nframes, offset);
464 if (session_state_changing) {
466 /* XXX is this safe to do against transport state changes? */
468 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
472 audio_diskstream()->check_record_status (start_frame, nframes, can_record);
476 if (_have_internal_generator) {
477 /* since the instrument has no input streams,
478 there is no reason to send any signal
484 if (Config->get_auto_input()) {
485 if (Config->get_monitoring_model() == SoftwareMonitoring) {
486 send_silence = false;
491 if (_diskstream->record_enabled()) {
492 if (Config->get_monitoring_model() == SoftwareMonitoring) {
493 send_silence = false;
503 apply_gain_automation = false;
507 /* if we're sending silence, but we want the meters to show levels for the signal,
511 if (_have_internal_generator) {
512 passthru_silence (start_frame, end_frame, nframes, offset, 0, true);
514 if (_meter_point == MeterInput) {
515 just_meter_input (start_frame, end_frame, nframes, offset);
517 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
522 /* we're sending signal, but we may still want to meter the input.
525 passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput));
532 AudioTrack::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, int declick,
533 bool can_record, bool rec_monitors_input)
538 nframes_t transport_frame;
539 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
542 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
544 // automation snapshot can also be called from the non-rt context
545 // and it uses the redirect list, so we take the lock out here
546 automation_snapshot (start_frame);
551 if (n_outputs() == 0 && _redirects.empty()) {
556 silence (nframes, offset);
560 transport_frame = _session.transport_frame();
562 if ((nframes = check_initial_delay (nframes, offset, transport_frame)) == 0) {
563 /* need to do this so that the diskstream sets its
564 playback distance to zero, thus causing diskstream::commit
567 return diskstream->process (transport_frame, 0, 0, can_record, rec_monitors_input);
571 apply_gain_automation = false;
573 if ((dret = diskstream->process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
575 silence (nframes, offset);
580 /* special condition applies */
582 if (_meter_point == MeterInput) {
583 just_meter_input (start_frame, end_frame, nframes, offset);
586 if (diskstream->record_enabled() && !can_record && !Config->get_auto_input()) {
588 /* not actually recording, but we want to hear the input material anyway,
589 at least potentially (depending on monitoring options)
592 passthru (start_frame, end_frame, nframes, offset, 0, true);
594 } else if ((b = diskstream->playback_buffer(0)) != 0) {
597 XXX is it true that the earlier test on n_outputs()
598 means that we can avoid checking it again here? i think
599 so, because changing the i/o configuration of an IO
600 requires holding the AudioEngine lock, which we hold
601 while in the process() tree.
605 /* copy the diskstream data to all output buffers */
607 vector<Sample*>& bufs = _session.get_passthru_buffers ();
608 uint32_t limit = n_process_buffers ();
614 for (i = 0, n = 1; i < limit; ++i, ++n) {
615 memcpy (bufs[i], b, sizeof (Sample) * nframes);
616 if (n < diskstream->n_channels()) {
617 tmpb = diskstream->playback_buffer(n);
624 /* don't waste time with automation if we're recording or we've just stopped (yes it can happen) */
626 if (!diskstream->record_enabled() && _session.transport_rolling()) {
627 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
629 if (am.locked() && gain_automation_playback()) {
630 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
634 process_output_buffers (bufs, limit, start_frame, end_frame, nframes, offset, (!_session.get_record_enabled() || !Config->get_do_not_record_plugins()), declick, (_meter_point != MeterInput));
637 /* problem with the diskstream; just be quiet for a bit */
638 silence (nframes, offset);
645 AudioTrack::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
646 bool can_record, bool rec_monitors_input)
648 if (n_outputs() == 0 && _redirects.empty()) {
653 silence (nframes, offset);
658 apply_gain_automation = false;
660 silence (nframes, offset);
662 return audio_diskstream()->process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
666 AudioTrack::export_stuff (vector<Sample*>& buffers, uint32_t nbufs, nframes_t start, nframes_t nframes)
668 gain_t gain_automation[nframes];
669 gain_t gain_buffer[nframes];
670 float mix_buffer[nframes];
671 RedirectList::iterator i;
672 bool post_fader_work = false;
673 gain_t this_gain = _gain;
674 vector<Sample*>::iterator bi;
676 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
678 Glib::RWLock::ReaderLock rlock (redirect_lock);
680 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist());
683 if (apl->read (buffers[0], mix_buffer, gain_buffer, start, nframes) != nframes) {
688 bi = buffers.begin();
691 for (; bi != buffers.end(); ++bi, ++n) {
692 if (n < diskstream->n_channels()) {
693 if (apl->read ((*bi), mix_buffer, gain_buffer, start, nframes, n) != nframes) {
699 /* duplicate last across remaining buffers */
700 memcpy ((*bi), b, sizeof (Sample) * nframes);
705 /* note: only run inserts during export. other layers in the machinery
706 will already have checked that there are no external port inserts.
709 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
710 boost::shared_ptr<Insert> insert;
712 if ((insert = boost::dynamic_pointer_cast<Insert>(*i)) != 0) {
713 switch (insert->placement()) {
715 insert->run (buffers, nbufs, nframes, 0);
718 post_fader_work = true;
724 if (_gain_automation_curve.automation_state() == Play) {
726 _gain_automation_curve.get_vector (start, start + nframes, gain_automation, nframes);
728 for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
730 for (nframes_t n = 0; n < nframes; ++n) {
731 b[n] *= gain_automation[n];
737 for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
739 for (nframes_t n = 0; n < nframes; ++n) {
745 if (post_fader_work) {
747 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
748 boost::shared_ptr<PluginInsert> insert;
750 if ((insert = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
751 switch ((*i)->placement()) {
755 insert->run (buffers, nbufs, nframes, 0);
766 AudioTrack::bounce (InterThreadInfo& itt)
768 vector<boost::shared_ptr<AudioSource> > srcs;
769 _session.write_one_audio_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
774 AudioTrack::bounce_range (nframes_t start, nframes_t end, InterThreadInfo& itt)
776 vector<boost::shared_ptr<AudioSource> > srcs;
777 _session.write_one_audio_track (*this, start, end, false, srcs, itt);
781 AudioTrack::freeze (InterThreadInfo& itt)
783 vector<boost::shared_ptr<AudioSource> > srcs;
784 string new_playlist_name;
785 boost::shared_ptr<Playlist> new_playlist;
788 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
790 if ((_freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist())) == 0) {
796 while (n < (UINT_MAX-1)) {
800 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
802 if (_session.playlist_by_name (candidate) == 0) {
803 new_playlist_name = candidate;
811 if (n == (UINT_MAX-1)) {
812 error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
813 " to create another one"), _freeze_record.playlist->name())
818 if (_session.write_one_audio_track (*this, _session.current_start_frame(), _session.current_end_frame(), true, srcs, itt)) {
822 _freeze_record.insert_info.clear ();
823 _freeze_record.have_mementos = true;
826 Glib::RWLock::ReaderLock lm (redirect_lock);
828 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); ++r) {
830 boost::shared_ptr<Insert> insert;
832 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
834 FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo ((*r)->get_state(), insert);
836 frii->id = insert->id();
838 _freeze_record.insert_info.push_back (frii);
840 /* now deactivate the insert */
842 insert->set_active (false, this);
847 new_playlist = PlaylistFactory::create (_session, new_playlist_name, false);
848 region_name = new_playlist_name;
850 /* create a new region from all filesources, keep it private */
852 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, srcs[0]->length(),
854 (AudioRegion::Flag) (AudioRegion::WholeFile|AudioRegion::DefaultFlags),
857 new_playlist->set_orig_diskstream_id (diskstream->id());
858 new_playlist->add_region (region, _session.current_start_frame());
859 new_playlist->set_frozen (true);
860 region->set_locked (true);
862 diskstream->use_playlist (boost::dynamic_pointer_cast<AudioPlaylist>(new_playlist));
863 diskstream->set_record_enabled (false);
865 _freeze_record.state = Frozen;
866 FreezeChange(); /* EMIT SIGNAL */
870 AudioTrack::unfreeze ()
872 if (_freeze_record.playlist) {
873 audio_diskstream()->use_playlist (_freeze_record.playlist);
875 if (_freeze_record.have_mementos) {
877 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
883 Glib::RWLock::ReaderLock lm (redirect_lock); // should this be a write lock? jlc
884 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
885 for (vector<FreezeRecordInsertInfo*>::iterator ii = _freeze_record.insert_info.begin(); ii != _freeze_record.insert_info.end(); ++ii) {
886 if ((*ii)->id == (*i)->id()) {
887 (*i)->set_state (((*ii)->state));
894 _freeze_record.playlist.reset ();
897 _freeze_record.state = UnFrozen;
898 FreezeChange (); /* EMIT SIGNAL */