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 <sigc++/retype.h>
21 #include <sigc++/retype_return.h>
22 #include <sigc++/bind.h>
24 #include "pbd/error.h"
25 #include "pbd/enumwriter.h"
27 #include "evoral/Curve.hpp"
29 #include "ardour/amp.h"
30 #include "ardour/audio_buffer.h"
31 #include "ardour/audio_diskstream.h"
32 #include "ardour/audio_track.h"
33 #include "ardour/audioplaylist.h"
34 #include "ardour/audioregion.h"
35 #include "ardour/audiosource.h"
36 #include "ardour/buffer_set.h"
37 #include "ardour/io_processor.h"
38 #include "ardour/panner.h"
39 #include "ardour/meter.h"
40 #include "ardour/playlist_factory.h"
41 #include "ardour/plugin_insert.h"
42 #include "ardour/processor.h"
43 #include "ardour/region_factory.h"
44 #include "ardour/route_group_specialized.h"
45 #include "ardour/session.h"
46 #include "ardour/utils.h"
50 using namespace ARDOUR;
53 AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
54 : Track (sess, name, flag, mode)
56 use_new_diskstream ();
59 AudioTrack::AudioTrack (Session& sess, const XMLNode& node, int /*version*/)
62 _set_state (node, Stateful::loading_state_version, false);
65 AudioTrack::~AudioTrack ()
70 AudioTrack::use_new_diskstream ()
72 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
74 if (_flags & Hidden) {
75 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
77 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Recordable);
80 if (_mode == Destructive) {
81 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
82 } else if (_mode == NonLayered){
83 dflags = AudioDiskstream::Flag(dflags | AudioDiskstream::NonLayered);
87 boost::shared_ptr<AudioDiskstream> ds (new AudioDiskstream (_session, name(), dflags));
89 _session.add_diskstream (ds);
91 set_diskstream (boost::dynamic_pointer_cast<AudioDiskstream> (ds), this);
95 AudioTrack::set_mode (TrackMode m)
99 if (_diskstream->set_destructive (m == Destructive)) {
103 _diskstream->set_non_layered (m == NonLayered);
106 TrackModeChanged (); /* EMIT SIGNAL */
113 AudioTrack::can_use_mode (TrackMode m, bool& bounce_required)
118 bounce_required = false;
123 return _diskstream->can_become_destructive (bounce_required);
128 AudioTrack::deprecated_use_diskstream_connections ()
130 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
132 if (diskstream->deprecated_io_node == 0) {
136 const XMLProperty* prop;
137 XMLNode& node (*diskstream->deprecated_io_node);
139 /* don't do this more than once. */
141 diskstream->deprecated_io_node = 0;
143 if ((prop = node.property ("gain")) != 0) {
144 _amp->set_gain (atof (prop->value().c_str()), this);
147 if ((prop = node.property ("input-connection")) != 0) {
148 boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
151 error << string_compose(_("Unknown bundle \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
153 if ((c = _session.bundle_by_name (_("in 1"))) == 0) {
154 error << _("No input bundles available as a replacement")
158 info << string_compose (_("Bundle %1 was not available - \"in 1\" used instead"), prop->value())
163 _input->connect_ports_to_bundle (c, this);
165 } else if ((prop = node.property ("inputs")) != 0) {
166 if (_input->set_ports (prop->value())) {
167 error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
176 AudioTrack::set_diskstream (boost::shared_ptr<AudioDiskstream> ds, void * /*src*/)
179 _diskstream->set_route (*this);
180 _diskstream->set_destructive (_mode == Destructive);
181 _diskstream->set_non_layered (_mode == NonLayered);
183 if (audio_diskstream()->deprecated_io_node) {
185 if (!IO::connecting_legal) {
186 IO::ConnectingLegal.connect (mem_fun (*this, &AudioTrack::deprecated_use_diskstream_connections));
188 deprecated_use_diskstream_connections ();
192 _diskstream->set_record_enabled (false);
193 _diskstream->monitor_input (false);
195 ic_connection.disconnect();
196 ic_connection = _input->changed.connect (mem_fun (*_diskstream, &Diskstream::handle_input_change));
198 DiskstreamChanged (); /* EMIT SIGNAL */
204 AudioTrack::use_diskstream (string name)
206 boost::shared_ptr<AudioDiskstream> dstream;
208 if ((dstream = boost::dynamic_pointer_cast<AudioDiskstream>(_session.diskstream_by_name (name))) == 0) {
209 error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), name) << endmsg;
213 return set_diskstream (dstream, this);
217 AudioTrack::use_diskstream (const PBD::ID& id)
219 boost::shared_ptr<AudioDiskstream> dstream;
221 if ((dstream = boost::dynamic_pointer_cast<AudioDiskstream> (_session.diskstream_by_id (id))) == 0) {
222 error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), id) << endmsg;
226 return set_diskstream (dstream, this);
229 boost::shared_ptr<AudioDiskstream>
230 AudioTrack::audio_diskstream() const
232 return boost::dynamic_pointer_cast<AudioDiskstream>(_diskstream);
236 AudioTrack::set_state (const XMLNode& node, int version)
238 return _set_state (node, version, true);
242 AudioTrack::_set_state (const XMLNode& node, int version, bool call_base)
244 const XMLProperty *prop;
245 XMLNodeConstIterator iter;
248 if (Route::_set_state (node, version, call_base)) {
253 if ((prop = node.property (X_("mode"))) != 0) {
254 _mode = TrackMode (string_2_enum (prop->value(), _mode));
259 if ((prop = node.property ("diskstream-id")) == 0) {
261 /* some old sessions use the diskstream name rather than the ID */
263 if ((prop = node.property ("diskstream")) == 0) {
264 fatal << _("programming error: AudioTrack given state without diskstream!") << endmsg;
269 if (use_diskstream (prop->value())) {
275 PBD::ID id (prop->value());
278 /* this wierd hack is used when creating tracks from a template. there isn't
279 a particularly good time to interpose between setting the first part of
280 the track state (notably Route::set_state() and the track mode), and the
281 second part (diskstream stuff). So, we have a special ID for the diskstream
282 that means "you should create a new diskstream here, not look for
287 use_new_diskstream ();
288 } else if (use_diskstream (id)) {
295 XMLNodeConstIterator niter;
298 nlist = node.children();
299 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
302 if (child->name() == X_("recenable")) {
303 _rec_enable_control->set_state (*child, version);
304 _session.add_controllable (_rec_enable_control);
308 pending_state = const_cast<XMLNode*> (&node);
310 if (_session.state_of_the_state() & Session::Loading) {
311 _session.StateReady.connect (mem_fun (*this, &AudioTrack::set_state_part_two));
313 set_state_part_two ();
320 AudioTrack::state(bool full_state)
322 XMLNode& root (Route::state(full_state));
323 XMLNode* freeze_node;
326 if (_freeze_record.playlist) {
329 freeze_node = new XMLNode (X_("freeze-info"));
330 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
331 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
333 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
334 inode = new XMLNode (X_("processor"));
335 (*i)->id.print (buf, sizeof (buf));
336 inode->add_property (X_("id"), buf);
337 inode->add_child_copy ((*i)->state);
339 freeze_node->add_child_nocopy (*inode);
342 root.add_child_nocopy (*freeze_node);
345 /* Alignment: act as a proxy for the diskstream */
347 XMLNode* align_node = new XMLNode (X_("Alignment"));
348 AlignStyle as = _diskstream->alignment_style ();
349 align_node->add_property (X_("style"), enum_2_string (as));
350 root.add_child_nocopy (*align_node);
352 root.add_property (X_("mode"), enum_2_string (_mode));
354 /* we don't return diskstream state because we don't
355 own the diskstream exclusively. control of the diskstream
356 state is ceded to the Session, even if we create the
360 _diskstream->id().print (buf, sizeof (buf));
361 root.add_property ("diskstream-id", buf);
363 root.add_child_nocopy (_rec_enable_control->get_state());
369 AudioTrack::set_state_part_two ()
373 LocaleGuard lg (X_("POSIX"));
375 /* This is called after all session state has been restored but before
376 have been made ports and connections are established.
379 if (pending_state == 0) {
383 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
385 _freeze_record.state = Frozen;
387 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
390 _freeze_record.processor_info.clear ();
392 if ((prop = fnode->property (X_("playlist"))) != 0) {
393 boost::shared_ptr<Playlist> pl = _session.playlist_by_name (prop->value());
395 _freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
397 _freeze_record.playlist.reset ();
398 _freeze_record.state = NoFreeze;
403 if ((prop = fnode->property (X_("state"))) != 0) {
404 _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
407 XMLNodeConstIterator citer;
408 XMLNodeList clist = fnode->children();
410 for (citer = clist.begin(); citer != clist.end(); ++citer) {
411 if ((*citer)->name() != X_("processor")) {
415 if ((prop = (*citer)->property (X_("id"))) == 0) {
419 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo (*((*citer)->children().front()),
420 boost::shared_ptr<Processor>());
421 frii->id = prop->value ();
422 _freeze_record.processor_info.push_back (frii);
426 /* Alignment: act as a proxy for the diskstream */
428 if ((fnode = find_named_node (*pending_state, X_("Alignment"))) != 0) {
430 if ((prop = fnode->property (X_("style"))) != 0) {
432 /* fix for older sessions from before EnumWriter */
436 if (prop->value() == "capture") {
437 pstr = "CaptureTime";
438 } else if (prop->value() == "existing") {
439 pstr = "ExistingMaterial";
441 pstr = prop->value();
444 AlignStyle as = AlignStyle (string_2_enum (pstr, as));
445 _diskstream->set_persistent_align_style (as);
452 AudioTrack::roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick,
453 bool can_record, bool rec_monitors_input)
458 nframes_t transport_frame;
459 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
462 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
464 // automation snapshot can also be called from the non-rt context
465 // and it uses the redirect list, so we take the lock out here
466 automation_snapshot (start_frame, false);
471 if (n_outputs().n_total() == 0 && _processors.empty()) {
480 transport_frame = _session.transport_frame();
482 if ((nframes = check_initial_delay (nframes, transport_frame)) == 0) {
484 /* need to do this so that the diskstream sets its
485 playback distance to zero, thus causing diskstream::commit
488 return diskstream->process (transport_frame, 0, can_record, rec_monitors_input);
492 _amp->apply_gain_automation(false);
494 if ((dret = diskstream->process (transport_frame, nframes, can_record, rec_monitors_input)) != 0) {
499 /* special condition applies */
501 if (_meter_point == MeterInput) {
502 _input->process_input (_meter, start_frame, end_frame, nframes);
505 if (diskstream->record_enabled() && !can_record && !_session.config.get_auto_input()) {
507 /* not actually recording, but we want to hear the input material anyway,
508 at least potentially (depending on monitoring options)
511 passthru (start_frame, end_frame, nframes, false);
513 } else if ((b = diskstream->playback_buffer(0)) != 0) {
516 XXX is it true that the earlier test on n_outputs()
517 means that we can avoid checking it again here? i think
518 so, because changing the i/o configuration of an IO
519 requires holding the AudioEngine lock, which we hold
520 while in the process() tree.
524 /* copy the diskstream data to all output buffers */
526 size_t limit = _input->n_ports().n_audio();
527 BufferSet& bufs = _session.get_scratch_buffers ();
528 const size_t blimit = bufs.count().n_audio();
533 if (limit > blimit) {
535 /* example case: auditioner configured for stereo output,
536 but loaded with an 8 channel file. there are only
537 2 passthrough buffers, but n_process_buffers() will
540 arbitrary decision: map all channels in the diskstream
541 to the outputs available.
544 float scaling = limit/blimit;
546 for (i = 0, n = 1; i < blimit; ++i, ++n) {
548 /* first time through just copy a channel into
552 Sample* bb = bufs.get_audio (i).data();
554 for (nframes_t xx = 0; xx < nframes; ++xx) {
555 bb[xx] = b[xx] * scaling;
558 if (n < diskstream->n_channels().n_audio()) {
559 tmpb = diskstream->playback_buffer(n);
566 for (;i < limit; ++i, ++n) {
568 /* for all remaining channels, sum with existing
569 data in the output buffers
572 bufs.get_audio (i%blimit).accumulate_with_gain_from (b, nframes, 0, scaling);
574 if (n < diskstream->n_channels().n_audio()) {
575 tmpb = diskstream->playback_buffer(n);
586 for (i = 0, n = 1; i < limit; ++i, ++n) {
587 memcpy (bufs.get_audio (i).data(), b, sizeof (Sample) * nframes);
588 if (n < diskstream->n_channels().n_audio()) {
589 tmpb = diskstream->playback_buffer(n);
596 /* try to leave any MIDI buffers alone */
599 chn.set_audio (limit);
600 chn.set_midi (_input->n_ports().n_midi());
601 bufs.set_count (chn);
604 /* don't waste time with automation if we're recording or we've just stopped (yes it can happen) */
606 if (!diskstream->record_enabled() && _session.transport_rolling()) {
607 #ifdef XXX_MOVE_THIS_TO_AMP
608 Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
610 if (am.locked() && gain_control()->automation_playback()) {
611 _amp->apply_gain_automation(
612 gain_control()->list()->curve().rt_safe_get_vector (
613 start_frame, end_frame, _session.gain_automation_buffer(), nframes));
618 process_output_buffers (bufs, start_frame, end_frame, nframes, (!_session.get_record_enabled() || !Config->get_do_not_record_plugins()), declick);
621 /* problem with the diskstream; just be quiet for a bit */
629 AudioTrack::export_stuff (BufferSet& buffers, sframes_t start, nframes_t nframes, bool enable_processing)
631 gain_t gain_buffer[nframes];
632 float mix_buffer[nframes];
633 ProcessorList::iterator i;
634 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
636 Glib::RWLock::ReaderLock rlock (_processor_lock);
638 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist());
641 assert(buffers.get_audio(0).capacity() >= nframes);
643 if (apl->read (buffers.get_audio(0).data(), mix_buffer, gain_buffer, start, nframes) != nframes) {
647 assert(buffers.count().n_audio() >= 1);
649 Sample* b = buffers.get_audio(0).data();
650 BufferSet::audio_iterator bi = buffers.audio_begin();
652 for ( ; bi != buffers.audio_end(); ++bi, ++n) {
653 if (n < diskstream->n_channels().n_audio()) {
654 if (apl->read (bi->data(), mix_buffer, gain_buffer, start, nframes, n) != nframes) {
660 /* duplicate last across remaining buffers */
661 memcpy (bi->data(), b, sizeof (Sample) * nframes);
665 // If no processing is required, there's no need to go any further.
666 if (!enable_processing) {
670 /* note: only run processors during export. other layers in the machinery
671 will already have checked that there are no external port processors.
674 for (i = _processors.begin(); i != _processors.end(); ++i) {
675 boost::shared_ptr<Processor> processor;
676 if ((processor = boost::dynamic_pointer_cast<Processor>(*i)) != 0) {
677 processor->run (buffers, start, start+nframes, nframes);
684 boost::shared_ptr<Region>
685 AudioTrack::bounce (InterThreadInfo& itt)
687 vector<boost::shared_ptr<Source> > srcs;
688 return _session.write_one_track (*this, _session.current_start_frame(), _session.current_end_frame(), false, srcs, itt);
691 boost::shared_ptr<Region>
692 AudioTrack::bounce_range (nframes_t start, nframes_t end, InterThreadInfo& itt, bool enable_processing)
694 vector<boost::shared_ptr<Source> > srcs;
695 return _session.write_one_track (*this, start, end, false, srcs, itt, enable_processing);
699 AudioTrack::freeze (InterThreadInfo& itt)
701 vector<boost::shared_ptr<Source> > srcs;
702 string new_playlist_name;
703 boost::shared_ptr<Playlist> new_playlist;
706 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
708 if ((_freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist())) == 0) {
714 while (n < (UINT_MAX-1)) {
718 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
720 if (_session.playlist_by_name (candidate) == 0) {
721 new_playlist_name = candidate;
729 if (n == (UINT_MAX-1)) {
730 error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
731 " to create another one"), _freeze_record.playlist->name())
736 boost::shared_ptr<Region> res;
738 if ((res = _session.write_one_track (*this, _session.current_start_frame(), _session.current_end_frame(), true, srcs, itt)) == 0) {
742 _freeze_record.processor_info.clear ();
745 Glib::RWLock::ReaderLock lm (_processor_lock);
747 for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); ++r) {
749 boost::shared_ptr<Processor> processor;
751 if ((processor = boost::dynamic_pointer_cast<Processor>(*r)) != 0) {
753 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo ((*r)->get_state(), processor);
755 frii->id = processor->id();
757 _freeze_record.processor_info.push_back (frii);
759 /* now deactivate the processor */
761 processor->deactivate ();
762 _session.set_dirty ();
767 new_playlist = PlaylistFactory::create (DataType::AUDIO, _session, new_playlist_name, false);
769 _freeze_record.gain = _amp->gain();
770 _freeze_record.gain_automation_state = _amp->gain_control()->automation_state();
771 /* XXX need main outs automation state _freeze_record.pan_automation_state = _mainpanner->automation_state(); */
773 region_name = new_playlist_name;
775 /* create a new region from all filesources, keep it private */
777 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0,
778 srcs[0]->length(srcs[0]->timeline_position()),
780 (Region::Flag) (Region::WholeFile|Region::DefaultFlags),
783 new_playlist->set_orig_diskstream_id (diskstream->id());
784 new_playlist->add_region (region, _session.current_start_frame());
785 new_playlist->set_frozen (true);
786 region->set_locked (true);
788 diskstream->use_playlist (boost::dynamic_pointer_cast<AudioPlaylist>(new_playlist));
789 diskstream->set_record_enabled (false);
791 /* reset stuff that has already been accounted for in the freeze process */
793 set_gain (1.0, this);
794 _amp->gain_control()->set_automation_state (Off);
795 /* XXX need to use _main_outs _panner->set_automation_state (Off); */
797 _freeze_record.state = Frozen;
798 FreezeChange(); /* EMIT SIGNAL */
802 AudioTrack::unfreeze ()
804 if (_freeze_record.playlist) {
805 audio_diskstream()->use_playlist (_freeze_record.playlist);
808 Glib::RWLock::ReaderLock lm (_processor_lock); // should this be a write lock? jlc
809 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
810 for (vector<FreezeRecordProcessorInfo*>::iterator ii = _freeze_record.processor_info.begin(); ii != _freeze_record.processor_info.end(); ++ii) {
811 if ((*ii)->id == (*i)->id()) {
812 (*i)->set_state (((*ii)->state), Stateful::current_state_version);
819 _freeze_record.playlist.reset ();
820 set_gain (_freeze_record.gain, this);
821 _amp->gain_control()->set_automation_state (_freeze_record.gain_automation_state);
822 /* XXX need to use _main_outs _panner->set_automation_state (_freeze_record.pan_automation_state); */
825 _freeze_record.state = UnFrozen;
826 FreezeChange (); /* EMIT SIGNAL */