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 : Track (sess, name, flag, mode)
46 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
48 if (_flags & Hidden) {
49 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
51 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Recordable);
54 if (mode == Destructive) {
55 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
58 AudioDiskstream* ds = new AudioDiskstream (_session, name, dflags);
60 set_diskstream (*ds, this);
63 AudioTrack::AudioTrack (Session& sess, const XMLNode& node)
69 AudioTrack::~AudioTrack ()
74 AudioTrack::deprecated_use_diskstream_connections ()
76 AudioDiskstream& diskstream = audio_diskstream();
78 if (diskstream.deprecated_io_node == 0) {
82 const XMLProperty* prop;
83 XMLNode& node (*diskstream.deprecated_io_node);
85 /* don't do this more than once. */
87 diskstream.deprecated_io_node = 0;
89 set_input_minimum (-1);
90 set_input_maximum (-1);
91 set_output_minimum (-1);
92 set_output_maximum (-1);
94 if ((prop = node.property ("gain")) != 0) {
95 set_gain (atof (prop->value().c_str()), this);
96 _gain = _desired_gain;
99 if ((prop = node.property ("input-connection")) != 0) {
100 Connection* c = _session.connection_by_name (prop->value());
103 error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
105 if ((c = _session.connection_by_name (_("in 1"))) == 0) {
106 error << _("No input connections available as a replacement")
110 info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value())
115 use_input_connection (*c, this);
117 } else if ((prop = node.property ("inputs")) != 0) {
118 if (set_inputs (prop->value())) {
119 error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
128 AudioTrack::set_diskstream (AudioDiskstream& ds, void *src)
131 _diskstream->unref();
134 _diskstream = &ds.ref();
135 _diskstream->set_io (*this);
136 _diskstream->set_destructive (_mode == Destructive);
138 if (audio_diskstream().deprecated_io_node) {
140 if (!connecting_legal) {
141 ConnectingLegal.connect (mem_fun (*this, &AudioTrack::deprecated_use_diskstream_connections));
143 deprecated_use_diskstream_connections ();
147 _diskstream->set_record_enabled (false);
148 _diskstream->monitor_input (false);
150 ic_connection.disconnect();
151 ic_connection = input_changed.connect (mem_fun (*_diskstream, &Diskstream::handle_input_change));
153 DiskstreamChanged (); /* EMIT SIGNAL */
159 AudioTrack::use_diskstream (string name)
161 AudioDiskstream *dstream;
163 if ((dstream = dynamic_cast<AudioDiskstream*>(_session.diskstream_by_name (name))) == 0) {
164 error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), name) << endmsg;
168 return set_diskstream (*dstream, this);
172 AudioTrack::use_diskstream (const PBD::ID& id)
174 AudioDiskstream *dstream;
176 if ((dstream = dynamic_cast<AudioDiskstream*>(_session.diskstream_by_id (id))) == 0) {
177 error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), id) << endmsg;
181 return set_diskstream (*dstream, this);
185 AudioTrack::audio_diskstream() const
187 return *dynamic_cast<AudioDiskstream*>(_diskstream);
191 AudioTrack::set_state (const XMLNode& node)
193 const XMLProperty *prop;
194 XMLNodeConstIterator iter;
196 if (Route::set_state (node)) {
200 if ((prop = node.property (X_("mode"))) != 0) {
201 if (prop->value() == X_("normal")) {
203 } else if (prop->value() == X_("destructive")) {
206 warning << string_compose ("unknown audio track mode \"%1\" seen and ignored", prop->value()) << endmsg;
213 if ((prop = node.property ("diskstream-id")) == 0) {
215 /* some old sessions use the diskstream name rather than the ID */
217 if ((prop = node.property ("diskstream")) == 0) {
218 fatal << _("programming error: AudioTrack given state without diskstream!") << endmsg;
223 if (use_diskstream (prop->value())) {
229 PBD::ID id (prop->value());
231 if (use_diskstream (id)) {
238 XMLNodeConstIterator niter;
241 nlist = node.children();
242 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
245 if (child->name() == X_("remote_control")) {
246 if ((prop = child->property (X_("id"))) != 0) {
248 sscanf (prop->value().c_str(), "%d", &x);
249 set_remote_control_id (x);
254 pending_state = const_cast<XMLNode*> (&node);
256 _session.StateReady.connect (mem_fun (*this, &AudioTrack::set_state_part_two));
262 AudioTrack::state(bool full_state)
264 XMLNode& root (Route::state(full_state));
265 XMLNode* freeze_node;
268 if (_freeze_record.playlist) {
271 freeze_node = new XMLNode (X_("freeze-info"));
272 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
273 snprintf (buf, sizeof (buf), "%d", (int) _freeze_record.state);
274 freeze_node->add_property ("state", buf);
276 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
277 inode = new XMLNode (X_("insert"));
278 (*i)->id.print (buf);
279 inode->add_property (X_("id"), buf);
280 inode->add_child_copy ((*i)->state);
282 freeze_node->add_child_nocopy (*inode);
285 root.add_child_nocopy (*freeze_node);
288 /* Alignment: act as a proxy for the diskstream */
290 XMLNode* align_node = new XMLNode (X_("alignment"));
291 switch (_diskstream->alignment_style()) {
292 case ExistingMaterial:
293 snprintf (buf, sizeof (buf), X_("existing"));
296 snprintf (buf, sizeof (buf), X_("capture"));
299 align_node->add_property (X_("style"), buf);
300 root.add_child_nocopy (*align_node);
302 XMLNode* remote_control_node = new XMLNode (X_("remote_control"));
303 snprintf (buf, sizeof (buf), "%d", _remote_control_id);
304 remote_control_node->add_property (X_("id"), buf);
305 root.add_child_nocopy (*remote_control_node);
309 root.add_property (X_("mode"), X_("normal"));
312 root.add_property (X_("mode"), X_("destructive"));
316 /* we don't return diskstream state because we don't
317 own the diskstream exclusively. control of the diskstream
318 state is ceded to the Session, even if we create the
322 _diskstream->id().print (buf);
323 root.add_property ("diskstream-id", buf);
329 AudioTrack::set_state_part_two ()
333 LocaleGuard lg (X_("POSIX"));
335 /* This is called after all session state has been restored but before
336 have been made ports and connections are established.
339 if (pending_state == 0) {
343 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
346 _freeze_record.have_mementos = false;
347 _freeze_record.state = Frozen;
349 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
352 _freeze_record.insert_info.clear ();
354 if ((prop = fnode->property (X_("playlist"))) != 0) {
355 Playlist* pl = _session.playlist_by_name (prop->value());
357 _freeze_record.playlist = dynamic_cast<AudioPlaylist*> (pl);
359 _freeze_record.playlist = 0;
360 _freeze_record.state = NoFreeze;
365 if ((prop = fnode->property (X_("state"))) != 0) {
366 _freeze_record.state = (FreezeState) atoi (prop->value().c_str());
369 XMLNodeConstIterator citer;
370 XMLNodeList clist = fnode->children();
372 for (citer = clist.begin(); citer != clist.end(); ++citer) {
373 if ((*citer)->name() != X_("insert")) {
377 if ((prop = (*citer)->property (X_("id"))) == 0) {
381 FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo (*((*citer)->children().front()),
382 boost::shared_ptr<Insert>());
383 frii->id = prop->value ();
384 _freeze_record.insert_info.push_back (frii);
388 /* Alignment: act as a proxy for the diskstream */
390 if ((fnode = find_named_node (*pending_state, X_("alignment"))) != 0) {
392 if ((prop = fnode->property (X_("style"))) != 0) {
393 if (prop->value() == "existing") {
394 _diskstream->set_persistent_align_style (ExistingMaterial);
395 } else if (prop->value() == "capture") {
396 _diskstream->set_persistent_align_style (CaptureTime);
404 AudioTrack::n_process_buffers ()
406 return max ((uint32_t) _diskstream->n_channels(), redirect_max_outs);
410 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)
412 uint32_t nbufs = n_process_buffers ();
413 process_output_buffers (_session.get_silent_buffers (nbufs), nbufs, start_frame, end_frame, nframes, offset, true, declick, meter);
417 AudioTrack::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
418 bool session_state_changing, bool can_record, bool rec_monitors_input)
420 if (n_outputs() == 0) {
425 silence (nframes, offset);
429 if (session_state_changing) {
431 /* XXX is this safe to do against transport state changes? */
433 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
437 audio_diskstream().check_record_status (start_frame, nframes, can_record);
441 if (_have_internal_generator) {
442 /* since the instrument has no input streams,
443 there is no reason to send any signal
449 if (_session.get_auto_input()) {
450 if (Config->get_use_sw_monitoring()) {
451 send_silence = false;
456 if (_diskstream->record_enabled()) {
457 if (Config->get_use_sw_monitoring()) {
458 send_silence = false;
468 apply_gain_automation = false;
472 /* if we're sending silence, but we want the meters to show levels for the signal,
476 if (_have_internal_generator) {
477 passthru_silence (start_frame, end_frame, nframes, offset, 0, true);
479 if (_meter_point == MeterInput) {
480 just_meter_input (start_frame, end_frame, nframes, offset);
482 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
487 /* we're sending signal, but we may still want to meter the input.
490 passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput));
497 AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, int declick,
498 bool can_record, bool rec_monitors_input)
503 jack_nframes_t transport_frame;
504 AudioDiskstream& diskstream = audio_diskstream();
507 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
509 // automation snapshot can also be called from the non-rt context
510 // and it uses the redirect list, so we take the lock out here
511 automation_snapshot (start_frame);
515 if (n_outputs() == 0 && _redirects.empty()) {
520 silence (nframes, offset);
524 transport_frame = _session.transport_frame();
526 if ((nframes = check_initial_delay (nframes, offset, transport_frame)) == 0) {
527 /* need to do this so that the diskstream sets its
528 playback distance to zero, thus causing diskstream::commit
531 return diskstream.process (transport_frame, 0, 0, can_record, rec_monitors_input);
535 apply_gain_automation = false;
537 if ((dret = diskstream.process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
539 silence (nframes, offset);
544 /* special condition applies */
546 if (_meter_point == MeterInput) {
547 just_meter_input (start_frame, end_frame, nframes, offset);
550 if (diskstream.record_enabled() && !can_record && !_session.get_auto_input()) {
552 /* not actually recording, but we want to hear the input material anyway,
553 at least potentially (depending on monitoring options)
556 passthru (start_frame, end_frame, nframes, offset, 0, true);
558 } else if ((b = diskstream.playback_buffer(0)) != 0) {
561 XXX is it true that the earlier test on n_outputs()
562 means that we can avoid checking it again here? i think
563 so, because changing the i/o configuration of an IO
564 requires holding the AudioEngine lock, which we hold
565 while in the process() tree.
569 /* copy the diskstream data to all output buffers */
571 vector<Sample*>& bufs = _session.get_passthru_buffers ();
572 uint32_t limit = n_process_buffers ();
578 for (i = 0, n = 1; i < limit; ++i, ++n) {
579 memcpy (bufs[i], b, sizeof (Sample) * nframes);
580 if (n < diskstream.n_channels()) {
581 tmpb = diskstream.playback_buffer(n);
588 /* don't waste time with automation if we're recording or we've just stopped (yes it can happen) */
590 if (!diskstream.record_enabled() && _session.transport_rolling()) {
591 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
593 if (am.locked() && gain_automation_playback()) {
594 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
598 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));
601 /* problem with the diskstream; just be quiet for a bit */
602 silence (nframes, offset);
609 AudioTrack::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
610 bool can_record, bool rec_monitors_input)
612 if (n_outputs() == 0 && _redirects.empty()) {
617 silence (nframes, offset);
622 apply_gain_automation = false;
624 silence (nframes, offset);
626 return audio_diskstream().process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
630 AudioTrack::export_stuff (vector<Sample*>& buffers, uint32_t nbufs, jack_nframes_t start, jack_nframes_t nframes)
632 gain_t gain_automation[nframes];
633 gain_t gain_buffer[nframes];
634 float mix_buffer[nframes];
635 RedirectList::iterator i;
636 bool post_fader_work = false;
637 gain_t this_gain = _gain;
638 vector<Sample*>::iterator bi;
640 AudioDiskstream& diskstream = audio_diskstream();
642 Glib::RWLock::ReaderLock rlock (redirect_lock);
645 AudioPlaylist* const apl = dynamic_cast<AudioPlaylist*>(diskstream.playlist());
648 if (apl->read (buffers[0], mix_buffer, gain_buffer, start, nframes) != nframes) {
653 bi = buffers.begin();
656 for (; bi != buffers.end(); ++bi, ++n) {
657 if (n < diskstream.n_channels()) {
658 if (apl->read ((*bi), mix_buffer, gain_buffer, start, nframes, n) != nframes) {
664 /* duplicate last across remaining buffers */
665 memcpy ((*bi), b, sizeof (Sample) * nframes);
670 /* note: only run inserts during export. other layers in the machinery
671 will already have checked that there are no external port inserts.
674 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
675 boost::shared_ptr<Insert> insert;
677 if ((insert = boost::dynamic_pointer_cast<Insert>(*i)) != 0) {
678 switch (insert->placement()) {
680 insert->run (buffers, nbufs, nframes, 0);
683 post_fader_work = true;
689 if (_gain_automation_curve.automation_state() == Play) {
691 _gain_automation_curve.get_vector (start, start + nframes, gain_automation, nframes);
693 for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
695 for (jack_nframes_t n = 0; n < nframes; ++n) {
696 b[n] *= gain_automation[n];
702 for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
704 for (jack_nframes_t n = 0; n < nframes; ++n) {
710 if (post_fader_work) {
712 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
713 boost::shared_ptr<PluginInsert> insert;
715 if ((insert = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
716 switch ((*i)->placement()) {
720 insert->run (buffers, nbufs, nframes, 0);
731 AudioTrack::bounce (InterThreadInfo& itt)
733 vector<AudioSource*> srcs;
734 _session.write_one_audio_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
739 AudioTrack::bounce_range (jack_nframes_t start, jack_nframes_t end, InterThreadInfo& itt)
741 vector<AudioSource*> srcs;
742 _session.write_one_audio_track (*this, start, end, false, srcs, itt);
746 AudioTrack::freeze (InterThreadInfo& itt)
748 vector<AudioSource*> srcs;
749 string new_playlist_name;
750 Playlist* new_playlist;
754 AudioDiskstream& diskstream = audio_diskstream();
756 if ((_freeze_record.playlist = dynamic_cast<AudioPlaylist*>(diskstream.playlist())) == 0) {
762 while (n < (UINT_MAX-1)) {
766 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
768 if (_session.playlist_by_name (candidate) == 0) {
769 new_playlist_name = candidate;
777 if (n == (UINT_MAX-1)) {
778 error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
779 " to create another one"), _freeze_record.playlist->name())
784 if (_session.write_one_audio_track (*this, 0, _session.current_end_frame(), true, srcs, itt)) {
788 _freeze_record.insert_info.clear ();
789 _freeze_record.have_mementos = true;
792 Glib::RWLock::ReaderLock lm (redirect_lock);
794 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); ++r) {
796 boost::shared_ptr<Insert> insert;
798 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
800 FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo ((*r)->get_state(), insert);
802 frii->id = insert->id();
803 frii->memento = (*r)->get_memento();
805 _freeze_record.insert_info.push_back (frii);
807 /* now deactivate the insert */
809 insert->set_active (false, this);
814 new_playlist = new AudioPlaylist (_session, new_playlist_name, false);
815 region_name = new_playlist_name;
817 /* create a new region from all filesources, keep it private */
819 region = new AudioRegion (srcs, 0, srcs[0]->length(),
821 (AudioRegion::Flag) (AudioRegion::WholeFile|AudioRegion::DefaultFlags),
824 new_playlist->set_orig_diskstream_id (diskstream.id());
825 new_playlist->add_region (*region, 0);
826 new_playlist->set_frozen (true);
827 region->set_locked (true);
829 diskstream.use_playlist (dynamic_cast<AudioPlaylist*>(new_playlist));
830 diskstream.set_record_enabled (false);
832 _freeze_record.state = Frozen;
833 FreezeChange(); /* EMIT SIGNAL */
837 AudioTrack::unfreeze ()
839 if (_freeze_record.playlist) {
840 audio_diskstream().use_playlist (_freeze_record.playlist);
842 if (_freeze_record.have_mementos) {
844 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
850 Glib::RWLock::ReaderLock lm (redirect_lock); // should this be a write lock? jlc
851 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
852 for (vector<FreezeRecordInsertInfo*>::iterator ii = _freeze_record.insert_info.begin(); ii != _freeze_record.insert_info.end(); ++ii) {
853 if ((*ii)->id == (*i)->id()) {
854 (*i)->set_state (((*ii)->state));
861 _freeze_record.playlist = 0;
864 _freeze_record.state = UnFrozen;
865 FreezeChange (); /* EMIT SIGNAL */