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 <boost/scoped_array.hpp>
22 #include "pbd/error.h"
23 #include "pbd/enumwriter.h"
24 #include "pbd/boost_debug.h"
26 #include "evoral/Curve.hpp"
28 #include "ardour/amp.h"
29 #include "ardour/audio_buffer.h"
30 #include "ardour/audio_diskstream.h"
31 #include "ardour/audio_track.h"
32 #include "ardour/audioplaylist.h"
33 #include "ardour/audioregion.h"
34 #include "ardour/audiosource.h"
35 #include "ardour/buffer_set.h"
36 #include "ardour/io_processor.h"
37 #include "ardour/panner.h"
38 #include "ardour/meter.h"
39 #include "ardour/playlist_factory.h"
40 #include "ardour/plugin_insert.h"
41 #include "ardour/processor.h"
42 #include "ardour/region_factory.h"
43 #include "ardour/route_group_specialized.h"
44 #include "ardour/session.h"
45 #include "ardour/utils.h"
46 #include "ardour/session_playlists.h"
50 using namespace ARDOUR;
53 AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
54 : Track (sess, name, flag, mode)
58 AudioTrack::~AudioTrack ()
63 AudioTrack::use_new_diskstream ()
65 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
67 if (_flags & Hidden) {
68 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
70 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Recordable);
73 if (_mode == Destructive) {
74 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
75 } else if (_mode == NonLayered){
76 dflags = AudioDiskstream::Flag(dflags | AudioDiskstream::NonLayered);
79 AudioDiskstream* dsp (new AudioDiskstream (_session, name(), dflags));
80 boost::shared_ptr<AudioDiskstream> ds (dsp);
82 _session.add_diskstream (ds);
88 AudioTrack::set_diskstream (boost::shared_ptr<Diskstream> ds)
91 _diskstream->set_route (*this);
92 _diskstream->set_destructive (_mode == Destructive);
93 _diskstream->set_non_layered (_mode == NonLayered);
95 if (audio_diskstream()->deprecated_io_node) {
97 if (!IO::connecting_legal) {
98 IO::ConnectingLegal.connect_same_thread (*this, boost::bind (&AudioTrack::deprecated_use_diskstream_connections, this));
100 deprecated_use_diskstream_connections ();
104 _diskstream->set_record_enabled (false);
105 _diskstream->monitor_input (false);
107 DiskstreamChanged (); /* EMIT SIGNAL */
110 boost::shared_ptr<AudioDiskstream>
111 AudioTrack::audio_diskstream() const
113 return boost::dynamic_pointer_cast<AudioDiskstream>(_diskstream);
117 AudioTrack::set_mode (TrackMode m)
121 if (_diskstream->set_destructive (m == Destructive)) {
125 _diskstream->set_non_layered (m == NonLayered);
128 TrackModeChanged (); /* EMIT SIGNAL */
135 AudioTrack::can_use_mode (TrackMode m, bool& bounce_required)
140 bounce_required = false;
145 return _diskstream->can_become_destructive (bounce_required);
150 AudioTrack::deprecated_use_diskstream_connections ()
152 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
154 if (diskstream->deprecated_io_node == 0) {
158 const XMLProperty* prop;
159 XMLNode& node (*diskstream->deprecated_io_node);
161 /* don't do this more than once. */
163 diskstream->deprecated_io_node = 0;
165 if ((prop = node.property ("gain")) != 0) {
166 _amp->set_gain (atof (prop->value().c_str()), this);
169 if ((prop = node.property ("input-connection")) != 0) {
170 boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
173 error << string_compose(_("Unknown bundle \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
175 if ((c = _session.bundle_by_name (_("in 1"))) == 0) {
176 error << _("No input bundles available as a replacement")
180 info << string_compose (_("Bundle %1 was not available - \"in 1\" used instead"), prop->value())
185 _input->connect_ports_to_bundle (c, this);
187 } else if ((prop = node.property ("inputs")) != 0) {
188 if (_input->set_ports (prop->value())) {
189 error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
198 AudioTrack::set_state (const XMLNode& node, int version)
200 return _set_state (node, version, true);
204 AudioTrack::_set_state (const XMLNode& node, int version, bool call_base)
206 const XMLProperty *prop;
207 XMLNodeConstIterator iter;
210 if (Route::_set_state (node, version, call_base)) {
215 if ((prop = node.property (X_("mode"))) != 0) {
216 _mode = TrackMode (string_2_enum (prop->value(), _mode));
222 XMLNodeConstIterator niter;
225 nlist = node.children();
226 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
229 if (child->name() == X_("recenable")) {
230 _rec_enable_control->set_state (*child, version);
231 _session.add_controllable (_rec_enable_control);
235 pending_state = const_cast<XMLNode*> (&node);
237 if (_session.state_of_the_state() & Session::Loading) {
238 _session.StateReady.connect_same_thread (*this, boost::bind (&AudioTrack::set_state_part_two, this));
240 set_state_part_two ();
247 AudioTrack::state(bool full_state)
249 XMLNode& root (Route::state(full_state));
250 XMLNode* freeze_node;
253 if (_freeze_record.playlist) {
256 freeze_node = new XMLNode (X_("freeze-info"));
257 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
258 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
260 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
261 inode = new XMLNode (X_("processor"));
262 (*i)->id.print (buf, sizeof (buf));
263 inode->add_property (X_("id"), buf);
264 inode->add_child_copy ((*i)->state);
266 freeze_node->add_child_nocopy (*inode);
269 root.add_child_nocopy (*freeze_node);
272 /* Alignment: act as a proxy for the diskstream */
274 XMLNode* align_node = new XMLNode (X_("Alignment"));
275 AlignStyle as = _diskstream->alignment_style ();
276 align_node->add_property (X_("style"), enum_2_string (as));
277 root.add_child_nocopy (*align_node);
279 root.add_property (X_("mode"), enum_2_string (_mode));
281 /* we don't return diskstream state because we don't
282 own the diskstream exclusively. control of the diskstream
283 state is ceded to the Session, even if we create the
287 _diskstream->id().print (buf, sizeof (buf));
288 root.add_property ("diskstream-id", buf);
290 root.add_child_nocopy (_rec_enable_control->get_state());
296 AudioTrack::set_state_part_two ()
300 LocaleGuard lg (X_("POSIX"));
302 /* This is called after all session state has been restored but before
303 have been made ports and connections are established.
306 if (pending_state == 0) {
310 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
312 _freeze_record.state = Frozen;
314 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
317 _freeze_record.processor_info.clear ();
319 if ((prop = fnode->property (X_("playlist"))) != 0) {
320 boost::shared_ptr<Playlist> pl = _session.playlists->by_name (prop->value());
322 _freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
324 _freeze_record.playlist.reset ();
325 _freeze_record.state = NoFreeze;
330 if ((prop = fnode->property (X_("state"))) != 0) {
331 _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
334 XMLNodeConstIterator citer;
335 XMLNodeList clist = fnode->children();
337 for (citer = clist.begin(); citer != clist.end(); ++citer) {
338 if ((*citer)->name() != X_("processor")) {
342 if ((prop = (*citer)->property (X_("id"))) == 0) {
346 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo (*((*citer)->children().front()),
347 boost::shared_ptr<Processor>());
348 frii->id = prop->value ();
349 _freeze_record.processor_info.push_back (frii);
353 /* Alignment: act as a proxy for the diskstream */
355 if ((fnode = find_named_node (*pending_state, X_("Alignment"))) != 0) {
357 if ((prop = fnode->property (X_("style"))) != 0) {
359 /* fix for older sessions from before EnumWriter */
363 if (prop->value() == "capture") {
364 pstr = "CaptureTime";
365 } else if (prop->value() == "existing") {
366 pstr = "ExistingMaterial";
368 pstr = prop->value();
371 AlignStyle as = AlignStyle (string_2_enum (pstr, as));
372 _diskstream->set_persistent_align_style (as);
379 AudioTrack::roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick,
380 bool can_record, bool rec_monitors_input, bool& need_butler)
385 nframes_t transport_frame;
386 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
389 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
391 // automation snapshot can also be called from the non-rt context
392 // and it uses the redirect list, so we take the lock out here
393 automation_snapshot (start_frame, false);
397 if (n_outputs().n_total() == 0 && _processors.empty()) {
406 transport_frame = _session.transport_frame();
408 if ((nframes = check_initial_delay (nframes, transport_frame)) == 0) {
410 /* need to do this so that the diskstream sets its
411 playback distance to zero, thus causing diskstream::commit
414 return diskstream->process (transport_frame, 0, can_record, rec_monitors_input, need_butler);
418 _amp->apply_gain_automation(false);
420 if ((dret = diskstream->process (transport_frame, nframes, can_record, rec_monitors_input, need_butler)) != 0) {
425 /* special condition applies */
427 if (_meter_point == MeterInput) {
428 _input->process_input (_meter, start_frame, end_frame, nframes);
431 if (diskstream->record_enabled() && !can_record && !_session.config.get_auto_input()) {
433 /* not actually recording, but we want to hear the input material anyway,
434 at least potentially (depending on monitoring options)
437 passthru (start_frame, end_frame, nframes, false);
439 } else if ((b = diskstream->playback_buffer(0)) != 0) {
442 XXX is it true that the earlier test on n_outputs()
443 means that we can avoid checking it again here? i think
444 so, because changing the i/o configuration of an IO
445 requires holding the AudioEngine lock, which we hold
446 while in the process() tree.
450 /* copy the diskstream data to all output buffers */
452 size_t limit = input_streams ().n_audio();
453 BufferSet& bufs = _session.get_scratch_buffers ();
454 const size_t blimit = bufs.count().n_audio();
459 if (limit > blimit) {
461 /* example case: auditioner configured for stereo output,
462 but loaded with an 8 channel file. there are only
463 2 passthrough buffers, but n_process_buffers() will
466 arbitrary decision: map all channels in the diskstream
467 to the outputs available.
470 float scaling = limit/blimit;
472 for (i = 0, n = 1; i < blimit; ++i, ++n) {
474 /* first time through just copy a channel into
478 Sample* bb = bufs.get_audio (i).data();
480 for (nframes_t xx = 0; xx < nframes; ++xx) {
481 bb[xx] = b[xx] * scaling;
484 if (n < diskstream->n_channels().n_audio()) {
485 tmpb = diskstream->playback_buffer(n);
492 for (;i < limit; ++i, ++n) {
494 /* for all remaining channels, sum with existing
495 data in the output buffers
498 bufs.get_audio (i%blimit).accumulate_with_gain_from (b, nframes, 0, scaling);
500 if (n < diskstream->n_channels().n_audio()) {
501 tmpb = diskstream->playback_buffer(n);
512 for (i = 0, n = 1; i < limit; ++i, ++n) {
513 memcpy (bufs.get_audio (i).data(), b, sizeof (Sample) * nframes);
514 if (n < diskstream->n_channels().n_audio()) {
515 tmpb = diskstream->playback_buffer(n);
522 /* try to leave any MIDI buffers alone */
525 chn.set_audio (limit);
526 chn.set_midi (_input->n_ports().n_midi());
527 bufs.set_count (chn);
530 /* don't waste time with automation if we're recording or we've just stopped (yes it can happen) */
532 if (!diskstream->record_enabled() && _session.transport_rolling()) {
533 #ifdef XXX_MOVE_THIS_TO_AMP
534 Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
536 if (am.locked() && gain_control()->automation_playback()) {
537 _amp->apply_gain_automation(
538 gain_control()->list()->curve().rt_safe_get_vector (
539 start_frame, end_frame, _session.gain_automation_buffer(), nframes));
544 process_output_buffers (bufs, start_frame, end_frame, nframes, (!_session.get_record_enabled() || !Config->get_do_not_record_plugins()), declick);
547 /* problem with the diskstream; just be quiet for a bit */
555 AudioTrack::export_stuff (BufferSet& buffers, sframes_t start, nframes_t nframes, bool enable_processing)
557 boost::scoped_array<gain_t> gain_buffer (new gain_t[nframes]);
558 boost::scoped_array<float> mix_buffer (new float[nframes]);
559 ProcessorList::iterator i;
560 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
562 Glib::RWLock::ReaderLock rlock (_processor_lock);
564 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist());
567 assert(buffers.get_audio(0).capacity() >= nframes);
569 if (apl->read (buffers.get_audio(0).data(), mix_buffer.get(), gain_buffer.get(), start, nframes) != nframes) {
573 assert(buffers.count().n_audio() >= 1);
575 Sample* b = buffers.get_audio(0).data();
576 BufferSet::audio_iterator bi = buffers.audio_begin();
578 for ( ; bi != buffers.audio_end(); ++bi, ++n) {
579 if (n < diskstream->n_channels().n_audio()) {
580 if (apl->read (bi->data(), mix_buffer.get(), gain_buffer.get(), start, nframes, n) != nframes) {
586 /* duplicate last across remaining buffers */
587 memcpy (bi->data(), b, sizeof (Sample) * nframes);
591 // If no processing is required, there's no need to go any further.
592 if (!enable_processing) {
596 /* note: only run processors during export. other layers in the machinery
597 will already have checked that there are no external port processors.
600 for (i = _processors.begin(); i != _processors.end(); ++i) {
601 boost::shared_ptr<Processor> processor;
602 if ((processor = boost::dynamic_pointer_cast<Processor>(*i)) != 0) {
603 processor->run (buffers, start, start+nframes, nframes, true);
610 boost::shared_ptr<Region>
611 AudioTrack::bounce (InterThreadInfo& itt)
613 vector<boost::shared_ptr<Source> > srcs;
614 return _session.write_one_track (*this, _session.current_start_frame(), _session.current_end_frame(), false, srcs, itt);
617 boost::shared_ptr<Region>
618 AudioTrack::bounce_range (nframes_t start, nframes_t end, InterThreadInfo& itt, bool enable_processing)
620 vector<boost::shared_ptr<Source> > srcs;
621 return _session.write_one_track (*this, start, end, false, srcs, itt, enable_processing);
625 AudioTrack::freeze_me (InterThreadInfo& itt)
627 vector<boost::shared_ptr<Source> > srcs;
628 string new_playlist_name;
629 boost::shared_ptr<Playlist> new_playlist;
632 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
634 if ((_freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist())) == 0) {
640 while (n < (UINT_MAX-1)) {
644 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
646 if (_session.playlists->by_name (candidate) == 0) {
647 new_playlist_name = candidate;
655 if (n == (UINT_MAX-1)) {
656 error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
657 " to create another one"), _freeze_record.playlist->name())
662 boost::shared_ptr<Region> res;
664 if ((res = _session.write_one_track (*this, _session.current_start_frame(), _session.current_end_frame(), true, srcs, itt)) == 0) {
668 _freeze_record.processor_info.clear ();
671 Glib::RWLock::ReaderLock lm (_processor_lock);
673 for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); ++r) {
675 boost::shared_ptr<Processor> processor;
677 if ((processor = boost::dynamic_pointer_cast<Processor>(*r)) != 0) {
679 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo ((*r)->get_state(), processor);
681 frii->id = processor->id();
683 _freeze_record.processor_info.push_back (frii);
685 /* now deactivate the processor */
687 processor->deactivate ();
688 _session.set_dirty ();
693 new_playlist = PlaylistFactory::create (DataType::AUDIO, _session, new_playlist_name, false);
695 _freeze_record.gain = _amp->gain();
696 _freeze_record.gain_automation_state = _amp->gain_control()->automation_state();
697 /* XXX need main outs automation state _freeze_record.pan_automation_state = _mainpanner->automation_state(); */
699 region_name = new_playlist_name;
701 /* create a new region from all filesources, keep it private */
705 plist.add (Properties::start, 0);
706 plist.add (Properties::length, srcs[0]->length(srcs[0]->timeline_position()));
707 plist.add (Properties::name, region_name);
708 plist.add (Properties::whole_file, true);
710 boost::shared_ptr<Region> region (RegionFactory::create (srcs, plist, false));
712 new_playlist->set_orig_diskstream_id (diskstream->id());
713 new_playlist->add_region (region, _session.current_start_frame());
714 new_playlist->set_frozen (true);
715 region->set_locked (true);
717 diskstream->use_playlist (boost::dynamic_pointer_cast<AudioPlaylist>(new_playlist));
718 diskstream->set_record_enabled (false);
720 /* reset stuff that has already been accounted for in the freeze process */
722 set_gain (1.0, this);
723 _amp->gain_control()->set_automation_state (Off);
724 /* XXX need to use _main_outs _panner->set_automation_state (Off); */
726 _freeze_record.state = Frozen;
727 FreezeChange(); /* EMIT SIGNAL */
731 AudioTrack::unfreeze ()
733 if (_freeze_record.playlist) {
734 audio_diskstream()->use_playlist (_freeze_record.playlist);
737 Glib::RWLock::ReaderLock lm (_processor_lock); // should this be a write lock? jlc
738 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
739 for (vector<FreezeRecordProcessorInfo*>::iterator ii = _freeze_record.processor_info.begin(); ii != _freeze_record.processor_info.end(); ++ii) {
740 if ((*ii)->id == (*i)->id()) {
741 (*i)->set_state (((*ii)->state), Stateful::current_state_version);
748 _freeze_record.playlist.reset ();
749 set_gain (_freeze_record.gain, this);
750 _amp->gain_control()->set_automation_state (_freeze_record.gain_automation_state);
751 /* XXX need to use _main_outs _panner->set_automation_state (_freeze_record.pan_automation_state); */
754 _freeze_record.state = UnFrozen;
755 FreezeChange (); /* EMIT SIGNAL */