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/region_factory.h>
32 #include <ardour/route_group_specialized.h>
33 #include <ardour/insert.h>
34 #include <ardour/audioplaylist.h>
35 #include <ardour/panner.h>
36 #include <ardour/utils.h>
37 #include <ardour/buffer_set.h>
41 using namespace ARDOUR;
44 AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
45 : Track (sess, name, flag, mode)
47 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
49 if (_flags & Hidden) {
50 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
52 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Recordable);
55 if (mode == Destructive) {
56 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
59 boost::shared_ptr<AudioDiskstream> ds (new AudioDiskstream (_session, name, dflags));
60 _session.add_diskstream (ds);
62 set_diskstream (boost::dynamic_pointer_cast<AudioDiskstream> (ds), this);
65 AudioTrack::AudioTrack (Session& sess, const XMLNode& node)
71 AudioTrack::~AudioTrack ()
76 AudioTrack::deprecated_use_diskstream_connections ()
78 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
80 if (diskstream->deprecated_io_node == 0) {
84 const XMLProperty* prop;
85 XMLNode& node (*diskstream->deprecated_io_node);
87 /* don't do this more than once. */
89 diskstream->deprecated_io_node = 0;
91 set_input_minimum (-1);
92 set_input_maximum (-1);
93 set_output_minimum (-1);
94 set_output_maximum (-1);
96 if ((prop = node.property ("gain")) != 0) {
97 set_gain (atof (prop->value().c_str()), this);
98 _gain = _desired_gain;
101 if ((prop = node.property ("input-connection")) != 0) {
102 Connection* c = _session.connection_by_name (prop->value());
105 error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
107 if ((c = _session.connection_by_name (_("in 1"))) == 0) {
108 error << _("No input connections available as a replacement")
112 info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value())
117 use_input_connection (*c, this);
119 } else if ((prop = node.property ("inputs")) != 0) {
120 if (set_inputs (prop->value())) {
121 error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
130 AudioTrack::set_diskstream (boost::shared_ptr<AudioDiskstream> ds, void *src)
133 _diskstream->set_io (*this);
134 _diskstream->set_destructive (_mode == Destructive);
136 if (audio_diskstream()->deprecated_io_node) {
138 if (!connecting_legal) {
139 ConnectingLegal.connect (mem_fun (*this, &AudioTrack::deprecated_use_diskstream_connections));
141 deprecated_use_diskstream_connections ();
145 _diskstream->set_record_enabled (false);
146 _diskstream->monitor_input (false);
148 ic_connection.disconnect();
149 ic_connection = input_changed.connect (mem_fun (*_diskstream, &Diskstream::handle_input_change));
151 DiskstreamChanged (); /* EMIT SIGNAL */
157 AudioTrack::use_diskstream (string name)
159 boost::shared_ptr<AudioDiskstream> dstream;
161 if ((dstream = boost::dynamic_pointer_cast<AudioDiskstream>(_session.diskstream_by_name (name))) == 0) {
162 error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), name) << endmsg;
166 return set_diskstream (dstream, this);
170 AudioTrack::use_diskstream (const PBD::ID& id)
172 boost::shared_ptr<AudioDiskstream> dstream;
174 if ((dstream = boost::dynamic_pointer_cast<AudioDiskstream> (_session.diskstream_by_id (id))) == 0) {
175 error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), id) << endmsg;
179 return set_diskstream (dstream, this);
182 boost::shared_ptr<AudioDiskstream>
183 AudioTrack::audio_diskstream() const
185 return boost::dynamic_pointer_cast<AudioDiskstream>(_diskstream);
189 AudioTrack::set_state (const XMLNode& node)
191 const XMLProperty *prop;
192 XMLNodeConstIterator iter;
194 if (Route::set_state (node)) {
198 if ((prop = node.property (X_("mode"))) != 0) {
199 if (prop->value() == X_("normal")) {
201 } else if (prop->value() == X_("destructive")) {
204 warning << string_compose ("unknown audio track mode \"%1\" seen and ignored", prop->value()) << endmsg;
211 if ((prop = node.property ("diskstream-id")) == 0) {
213 /* some old sessions use the diskstream name rather than the ID */
215 if ((prop = node.property ("diskstream")) == 0) {
216 fatal << _("programming error: AudioTrack given state without diskstream!") << endmsg;
221 if (use_diskstream (prop->value())) {
227 PBD::ID id (prop->value());
229 if (use_diskstream (id)) {
236 XMLNodeConstIterator niter;
239 nlist = node.children();
240 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
243 if (child->name() == X_("remote_control")) {
244 if ((prop = child->property (X_("id"))) != 0) {
246 sscanf (prop->value().c_str(), "%d", &x);
247 set_remote_control_id (x);
252 pending_state = const_cast<XMLNode*> (&node);
254 _session.StateReady.connect (mem_fun (*this, &AudioTrack::set_state_part_two));
260 AudioTrack::state(bool full_state)
262 XMLNode& root (Route::state(full_state));
263 XMLNode* freeze_node;
266 if (_freeze_record.playlist) {
269 freeze_node = new XMLNode (X_("freeze-info"));
270 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
271 snprintf (buf, sizeof (buf), "%d", (int) _freeze_record.state);
272 freeze_node->add_property ("state", buf);
274 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
275 inode = new XMLNode (X_("insert"));
276 (*i)->id.print (buf);
277 inode->add_property (X_("id"), buf);
278 inode->add_child_copy ((*i)->state);
280 freeze_node->add_child_nocopy (*inode);
283 root.add_child_nocopy (*freeze_node);
286 /* Alignment: act as a proxy for the diskstream */
288 XMLNode* align_node = new XMLNode (X_("alignment"));
289 switch (_diskstream->alignment_style()) {
290 case ExistingMaterial:
291 snprintf (buf, sizeof (buf), X_("existing"));
294 snprintf (buf, sizeof (buf), X_("capture"));
297 align_node->add_property (X_("style"), buf);
298 root.add_child_nocopy (*align_node);
300 XMLNode* remote_control_node = new XMLNode (X_("remote_control"));
301 snprintf (buf, sizeof (buf), "%d", _remote_control_id);
302 remote_control_node->add_property (X_("id"), buf);
303 root.add_child_nocopy (*remote_control_node);
307 root.add_property (X_("mode"), X_("normal"));
310 root.add_property (X_("mode"), X_("destructive"));
314 /* we don't return diskstream state because we don't
315 own the diskstream exclusively. control of the diskstream
316 state is ceded to the Session, even if we create the
320 _diskstream->id().print (buf);
321 root.add_property ("diskstream-id", buf);
327 AudioTrack::set_state_part_two ()
331 LocaleGuard lg (X_("POSIX"));
333 /* This is called after all session state has been restored but before
334 have been made ports and connections are established.
337 if (pending_state == 0) {
341 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
344 _freeze_record.have_mementos = false;
345 _freeze_record.state = Frozen;
347 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
350 _freeze_record.insert_info.clear ();
352 if ((prop = fnode->property (X_("playlist"))) != 0) {
353 Playlist* pl = _session.playlist_by_name (prop->value());
355 _freeze_record.playlist = dynamic_cast<AudioPlaylist*> (pl);
357 _freeze_record.playlist = 0;
358 _freeze_record.state = NoFreeze;
363 if ((prop = fnode->property (X_("state"))) != 0) {
364 _freeze_record.state = (FreezeState) atoi (prop->value().c_str());
367 XMLNodeConstIterator citer;
368 XMLNodeList clist = fnode->children();
370 for (citer = clist.begin(); citer != clist.end(); ++citer) {
371 if ((*citer)->name() != X_("insert")) {
375 if ((prop = (*citer)->property (X_("id"))) == 0) {
379 FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo (*((*citer)->children().front()),
380 boost::shared_ptr<Insert>());
381 frii->id = prop->value ();
382 _freeze_record.insert_info.push_back (frii);
386 /* Alignment: act as a proxy for the diskstream */
388 if ((fnode = find_named_node (*pending_state, X_("alignment"))) != 0) {
390 if ((prop = fnode->property (X_("style"))) != 0) {
391 if (prop->value() == "existing") {
392 _diskstream->set_persistent_align_style (ExistingMaterial);
393 } else if (prop->value() == "capture") {
394 _diskstream->set_persistent_align_style (CaptureTime);
402 AudioTrack::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
403 bool session_state_changing, bool can_record, bool rec_monitors_input)
405 if (n_outputs().get_total() == 0) {
410 silence (nframes, offset);
414 if (session_state_changing) {
416 /* XXX is this safe to do against transport state changes? */
418 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
422 audio_diskstream()->check_record_status (start_frame, nframes, can_record);
426 if (_have_internal_generator) {
427 /* since the instrument has no input streams,
428 there is no reason to send any signal
434 if (_session.get_auto_input()) {
435 if (Config->get_use_sw_monitoring()) {
436 send_silence = false;
441 if (_diskstream->record_enabled()) {
442 if (Config->get_use_sw_monitoring()) {
443 send_silence = false;
453 apply_gain_automation = false;
457 /* if we're sending silence, but we want the meters to show levels for the signal,
461 if (_have_internal_generator) {
462 passthru_silence (start_frame, end_frame, nframes, offset, 0, true);
464 if (_meter_point == MeterInput) {
465 just_meter_input (start_frame, end_frame, nframes, offset);
467 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
472 /* we're sending signal, but we may still want to meter the input.
475 passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput));
482 AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, int declick,
483 bool can_record, bool rec_monitors_input)
488 jack_nframes_t transport_frame;
489 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
492 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
494 // automation snapshot can also be called from the non-rt context
495 // and it uses the redirect list, so we take the lock out here
496 automation_snapshot (start_frame);
500 if (n_outputs().get_total() == 0 && _redirects.empty()) {
505 silence (nframes, offset);
509 transport_frame = _session.transport_frame();
511 if ((nframes = check_initial_delay (nframes, offset, transport_frame)) == 0) {
512 /* need to do this so that the diskstream sets its
513 playback distance to zero, thus causing diskstream::commit
516 return diskstream->process (transport_frame, 0, 0, can_record, rec_monitors_input);
520 apply_gain_automation = false;
522 if ((dret = diskstream->process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
524 silence (nframes, offset);
529 /* special condition applies */
531 if (_meter_point == MeterInput) {
532 just_meter_input (start_frame, end_frame, nframes, offset);
535 if (diskstream->record_enabled() && !can_record && !_session.get_auto_input()) {
537 /* not actually recording, but we want to hear the input material anyway,
538 at least potentially (depending on monitoring options)
541 passthru (start_frame, end_frame, nframes, offset, 0, true);
543 } else if ((b = diskstream->playback_buffer(0)) != 0) {
546 XXX is it true that the earlier test on n_outputs()
547 means that we can avoid checking it again here? i think
548 so, because changing the i/o configuration of an IO
549 requires holding the AudioEngine lock, which we hold
550 while in the process() tree.
554 /* copy the diskstream data to all output buffers */
556 const size_t limit = n_process_buffers().get(DataType::AUDIO);
557 BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
562 for (i = 0, n = 1; i < limit; ++i, ++n) {
563 memcpy (bufs.get_audio(i).data(nframes), b, sizeof (Sample) * nframes);
564 if (n < diskstream->n_channels().get(DataType::AUDIO)) {
565 tmpb = diskstream->playback_buffer(n);
572 /* don't waste time with automation if we're recording or we've just stopped (yes it can happen) */
574 if (!diskstream->record_enabled() && _session.transport_rolling()) {
575 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
577 if (am.locked() && gain_automation_playback()) {
578 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
582 process_output_buffers (bufs, start_frame, end_frame, nframes, offset, (!_session.get_record_enabled() || !_session.get_do_not_record_plugins()), declick, (_meter_point != MeterInput));
585 /* problem with the diskstream; just be quiet for a bit */
586 silence (nframes, offset);
593 AudioTrack::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
594 bool can_record, bool rec_monitors_input)
596 if (n_outputs().get_total() == 0 && _redirects.empty()) {
601 silence (nframes, offset);
606 apply_gain_automation = false;
608 silence (nframes, offset);
610 return audio_diskstream()->process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
614 AudioTrack::export_stuff (BufferSet& buffers, jack_nframes_t start, jack_nframes_t nframes)
616 gain_t gain_automation[nframes];
617 gain_t gain_buffer[nframes];
618 float mix_buffer[nframes];
619 RedirectList::iterator i;
620 bool post_fader_work = false;
621 gain_t this_gain = _gain;
622 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
624 Glib::RWLock::ReaderLock rlock (redirect_lock);
627 AudioPlaylist* const apl = dynamic_cast<AudioPlaylist*>(diskstream->playlist());
630 if (apl->read (buffers.get_audio(nframes).data(nframes),
631 mix_buffer, gain_buffer, start, nframes) != nframes) {
635 assert(buffers.count().get(DataType::AUDIO) >= 1);
637 Sample* b = buffers.get_audio(0).data(nframes);
638 BufferSet::audio_iterator bi = buffers.audio_begin();
640 for ( ; bi != buffers.audio_end(); ++bi, ++n) {
641 if (n < diskstream->n_channels().get(DataType::AUDIO)) {
642 if (apl->read (bi->data(nframes), mix_buffer, gain_buffer, start, nframes, n) != nframes) {
645 b = bi->data(nframes);
648 /* duplicate last across remaining buffers */
649 memcpy (bi->data(nframes), b, sizeof (Sample) * nframes);
654 /* note: only run inserts during export. other layers in the machinery
655 will already have checked that there are no external port inserts.
658 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
659 boost::shared_ptr<Insert> insert;
661 if ((insert = boost::dynamic_pointer_cast<Insert>(*i)) != 0) {
662 switch (insert->placement()) {
664 insert->run (buffers, start, start+nframes, nframes, 0);
667 post_fader_work = true;
673 if (_gain_automation_curve.automation_state() == Play) {
675 _gain_automation_curve.get_vector (start, start + nframes, gain_automation, nframes);
677 for (BufferSet::audio_iterator bi = buffers.audio_begin(); bi != buffers.audio_end(); ++bi) {
678 Sample *b = bi->data(nframes);
679 for (jack_nframes_t n = 0; n < nframes; ++n) {
680 b[n] *= gain_automation[n];
686 for (BufferSet::audio_iterator bi = buffers.audio_begin(); bi != buffers.audio_end(); ++bi) {
687 Sample *b = bi->data(nframes);
688 for (jack_nframes_t n = 0; n < nframes; ++n) {
694 if (post_fader_work) {
696 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
697 boost::shared_ptr<PluginInsert> insert;
699 if ((insert = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
700 switch ((*i)->placement()) {
704 insert->run (buffers, start, start+nframes, nframes, 0);
715 AudioTrack::bounce (InterThreadInfo& itt)
717 vector<boost::shared_ptr<Source> > srcs;
718 _session.write_one_audio_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
723 AudioTrack::bounce_range (jack_nframes_t start, jack_nframes_t end, InterThreadInfo& itt)
725 vector<boost::shared_ptr<Source> > srcs;
726 _session.write_one_audio_track (*this, start, end, false, srcs, itt);
730 AudioTrack::freeze (InterThreadInfo& itt)
732 vector<boost::shared_ptr<Source> > srcs;
733 string new_playlist_name;
734 Playlist* new_playlist;
737 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
739 if ((_freeze_record.playlist = dynamic_cast<AudioPlaylist*>(diskstream->playlist())) == 0) {
745 while (n < (UINT_MAX-1)) {
749 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
751 if (_session.playlist_by_name (candidate) == 0) {
752 new_playlist_name = candidate;
760 if (n == (UINT_MAX-1)) {
761 error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
762 " to create another one"), _freeze_record.playlist->name())
767 if (_session.write_one_audio_track (*this, 0, _session.current_end_frame(), true, srcs, itt)) {
771 _freeze_record.insert_info.clear ();
772 _freeze_record.have_mementos = true;
775 Glib::RWLock::ReaderLock lm (redirect_lock);
777 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); ++r) {
779 boost::shared_ptr<Insert> insert;
781 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
783 FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo ((*r)->get_state(), insert);
785 frii->id = insert->id();
786 frii->memento = (*r)->get_memento();
788 _freeze_record.insert_info.push_back (frii);
790 /* now deactivate the insert */
792 insert->set_active (false, this);
797 new_playlist = new AudioPlaylist (_session, new_playlist_name, false);
798 region_name = new_playlist_name;
800 /* create a new region from all filesources, keep it private */
802 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, srcs[0]->length(),
804 (Region::Flag) (Region::WholeFile|Region::DefaultFlags),
807 new_playlist->set_orig_diskstream_id (diskstream->id());
808 new_playlist->add_region (region, 0);
809 new_playlist->set_frozen (true);
810 region->set_locked (true);
812 diskstream->use_playlist (dynamic_cast<AudioPlaylist*>(new_playlist));
813 diskstream->set_record_enabled (false);
815 _freeze_record.state = Frozen;
816 FreezeChange(); /* EMIT SIGNAL */
820 AudioTrack::unfreeze ()
822 if (_freeze_record.playlist) {
823 audio_diskstream()->use_playlist (_freeze_record.playlist);
825 if (_freeze_record.have_mementos) {
827 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
833 Glib::RWLock::ReaderLock lm (redirect_lock); // should this be a write lock? jlc
834 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
835 for (vector<FreezeRecordInsertInfo*>::iterator ii = _freeze_record.insert_info.begin(); ii != _freeze_record.insert_info.end(); ++ii) {
836 if ((*ii)->id == (*i)->id()) {
837 (*i)->set_state (((*ii)->state));
844 _freeze_record.playlist = 0;
847 _freeze_record.state = UnFrozen;
848 FreezeChange (); /* EMIT SIGNAL */