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/boost_debug.h"
23 #include "pbd/enumwriter.h"
24 #include "pbd/error.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/buffer_set.h"
34 #include "ardour/delivery.h"
35 #include "ardour/meter.h"
36 #include "ardour/playlist_factory.h"
37 #include "ardour/processor.h"
38 #include "ardour/profile.h"
39 #include "ardour/region.h"
40 #include "ardour/region_factory.h"
41 #include "ardour/session.h"
42 #include "ardour/session_playlists.h"
43 #include "ardour/source.h"
44 #include "ardour/utils.h"
49 using namespace ARDOUR;
52 AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
53 : Track (sess, name, flag, mode)
57 AudioTrack::~AudioTrack ()
61 boost::shared_ptr<Diskstream>
62 AudioTrack::create_diskstream ()
64 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (AudioDiskstream::Recordable);
66 if (_mode == Destructive && !Profile->get_trx()) {
67 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
68 } else if (_mode == NonLayered){
69 dflags = AudioDiskstream::Flag(dflags | AudioDiskstream::NonLayered);
72 return boost::shared_ptr<AudioDiskstream> (new AudioDiskstream (_session, name(), dflags));
76 AudioTrack::set_diskstream (boost::shared_ptr<Diskstream> ds)
78 Track::set_diskstream (ds);
80 _diskstream->set_track (this);
81 if (Profile->get_trx()) {
82 _diskstream->set_destructive (false);
84 _diskstream->set_destructive (_mode == Destructive);
86 _diskstream->set_non_layered (_mode == NonLayered);
88 if (audio_diskstream()->deprecated_io_node) {
90 if (!IO::connecting_legal) {
91 IO::ConnectingLegal.connect_same_thread (*this, boost::bind (&AudioTrack::deprecated_use_diskstream_connections, this));
93 deprecated_use_diskstream_connections ();
97 _diskstream->set_record_enabled (false);
98 _diskstream->request_input_monitoring (false);
100 DiskstreamChanged (); /* EMIT SIGNAL */
103 boost::shared_ptr<AudioDiskstream>
104 AudioTrack::audio_diskstream() const
106 return boost::dynamic_pointer_cast<AudioDiskstream>(_diskstream);
110 AudioTrack::set_mode (TrackMode m)
114 if (!Profile->get_trx() && _diskstream->set_destructive (m == Destructive)) {
118 _diskstream->set_non_layered (m == NonLayered);
121 TrackModeChanged (); /* EMIT SIGNAL */
128 AudioTrack::can_use_mode (TrackMode m, bool& bounce_required)
133 bounce_required = false;
137 if (Profile->get_trx()) {
140 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, true, 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 const XMLProperty *prop;
202 if ((prop = node.property (X_("mode"))) != 0) {
203 _mode = TrackMode (string_2_enum (prop->value(), _mode));
208 if (Profile->get_trx() && _mode == Destructive) {
209 /* Tracks does not support destructive tracks and trying to
210 handle it as a normal track would be wrong.
212 error << string_compose (_("%1: this session uses destructive tracks, which are not supported"), PROGRAM_NAME) << endmsg;
216 if (Track::set_state (node, version)) {
220 pending_state = const_cast<XMLNode*> (&node);
222 if (_session.state_of_the_state() & Session::Loading) {
223 _session.StateReady.connect_same_thread (*this, boost::bind (&AudioTrack::set_state_part_two, this));
225 set_state_part_two ();
232 AudioTrack::state (bool full_state)
234 XMLNode& root (Track::state(full_state));
235 XMLNode* freeze_node;
238 if (_freeze_record.playlist) {
241 freeze_node = new XMLNode (X_("freeze-info"));
242 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
243 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
245 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
246 inode = new XMLNode (X_("processor"));
247 (*i)->id.print (buf, sizeof (buf));
248 inode->add_property (X_("id"), buf);
249 inode->add_child_copy ((*i)->state);
251 freeze_node->add_child_nocopy (*inode);
254 root.add_child_nocopy (*freeze_node);
257 root.add_property (X_("mode"), enum_2_string (_mode));
263 AudioTrack::set_state_part_two ()
267 LocaleGuard lg (X_("C"));
269 /* This is called after all session state has been restored but before
270 have been made ports and connections are established.
273 if (pending_state == 0) {
277 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
279 _freeze_record.state = Frozen;
281 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
284 _freeze_record.processor_info.clear ();
286 if ((prop = fnode->property (X_("playlist"))) != 0) {
287 boost::shared_ptr<Playlist> pl = _session.playlists->by_name (prop->value());
289 _freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
291 _freeze_record.playlist.reset ();
292 _freeze_record.state = NoFreeze;
297 if ((prop = fnode->property (X_("state"))) != 0) {
298 _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
301 XMLNodeConstIterator citer;
302 XMLNodeList clist = fnode->children();
304 for (citer = clist.begin(); citer != clist.end(); ++citer) {
305 if ((*citer)->name() != X_("processor")) {
309 if ((prop = (*citer)->property (X_("id"))) == 0) {
313 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo (*((*citer)->children().front()),
314 boost::shared_ptr<Processor>());
315 frii->id = prop->value ();
316 _freeze_record.processor_info.push_back (frii);
321 /** @param need_butler to be set to true if this track now needs the butler, otherwise it can be left alone
325 AudioTrack::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
327 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
330 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
331 framecnt_t playback_distance = diskstream->calculate_playback_distance(nframes);
332 if (can_internal_playback_seek(::llabs(playback_distance))) {
333 /* TODO should declick */
334 internal_playback_seek(playback_distance);
339 framepos_t transport_frame;
340 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
342 if (n_outputs().n_total() == 0 && _processors.empty()) {
348 if (_meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled())) {
354 transport_frame = _session.transport_frame();
357 framecnt_t playback_distance;
359 if ((nframes = check_initial_delay (nframes, transport_frame)) == 0) {
361 /* need to do this so that the diskstream sets its
362 playback distance to zero, thus causing diskstream::commit
366 BufferSet bufs; /* empty set, no matter - nothing will happen */
368 dret = diskstream->process (bufs, transport_frame, 0, playback_distance, false);
369 need_butler = diskstream->commit (playback_distance);
373 if (_mute_control->list() && _mute_control->automation_playback()) {
375 const float mute = _mute_control->list()->rt_safe_eval(transport_frame, valid);
376 if (mute >= 0.5 && !muted()) {
377 _mute_control->set_value(1.0); // mute
378 } else if (mute < 0.5 && muted()) {
379 _mute_control->set_value(0.0); // unmute
384 _amp->apply_gain_automation(false);
386 BufferSet& bufs = _session.get_route_buffers (n_process_buffers ());
388 fill_buffers_with_input (bufs, _input, nframes);
390 if (_meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled())) {
391 _meter->run (bufs, start_frame, end_frame, nframes, true);
394 if ((dret = diskstream->process (bufs, transport_frame, nframes, playback_distance, (monitoring_state() == MonitoringDisk))) != 0) {
395 need_butler = diskstream->commit (playback_distance);
400 process_output_buffers (bufs, start_frame, end_frame, nframes, declick, (!diskstream->record_enabled() && _session.transport_rolling()));
402 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
403 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
405 d->flush_buffers (nframes);
409 need_butler = diskstream->commit (playback_distance);
415 AudioTrack::export_stuff (BufferSet& buffers, framepos_t start, framecnt_t nframes,
416 boost::shared_ptr<Processor> endpoint, bool include_endpoint, bool for_export, bool for_freeze)
418 boost::scoped_array<gain_t> gain_buffer (new gain_t[nframes]);
419 boost::scoped_array<Sample> mix_buffer (new Sample[nframes]);
420 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
422 Glib::Threads::RWLock::ReaderLock rlock (_processor_lock);
424 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist());
427 assert(buffers.count().n_audio() >= 1);
428 assert ((framecnt_t) buffers.get_audio(0).capacity() >= nframes);
430 if (apl->read (buffers.get_audio(0).data(), mix_buffer.get(), gain_buffer.get(), start, nframes) != nframes) {
435 Sample* b = buffers.get_audio(0).data();
436 BufferSet::audio_iterator bi = buffers.audio_begin();
438 for ( ; bi != buffers.audio_end(); ++bi, ++n) {
439 if (n < diskstream->n_channels().n_audio()) {
440 if (apl->read (bi->data(), mix_buffer.get(), gain_buffer.get(), start, nframes, n) != nframes) {
445 /* duplicate last across remaining buffers */
446 memcpy (bi->data(), b, sizeof (Sample) * nframes);
450 bounce_process (buffers, start, nframes, endpoint, include_endpoint, for_export, for_freeze);
456 AudioTrack::bounceable (boost::shared_ptr<Processor> endpoint, bool include_endpoint) const
458 if (!endpoint && !include_endpoint) {
459 /* no processing - just read from the playlist and create new
460 files: always possible.
465 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
466 uint32_t naudio = n_inputs().n_audio();
468 for (ProcessorList::const_iterator r = _processors.begin(); r != _processors.end(); ++r) {
470 /* if we're not including the endpoint, potentially stop
471 right here before we test matching i/o valences.
474 if (!include_endpoint && (*r) == endpoint) {
478 /* ignore any processors that do routing, because we will not
479 * use them during a bounce/freeze/export operation.
482 if ((*r)->does_routing()) {
486 /* does the output from the last considered processor match the
490 if (naudio != (*r)->input_streams().n_audio()) {
494 /* we're including the endpoint - if we just hit it,
498 if ((*r) == endpoint) {
502 /* save outputs of this processor to test against inputs
506 naudio = (*r)->output_streams().n_audio();
512 boost::shared_ptr<Region>
513 AudioTrack::bounce (InterThreadInfo& itt)
515 return bounce_range (_session.current_start_frame(), _session.current_end_frame(), itt, main_outs(), false);
518 boost::shared_ptr<Region>
519 AudioTrack::bounce_range (framepos_t start, framepos_t end, InterThreadInfo& itt,
520 boost::shared_ptr<Processor> endpoint, bool include_endpoint)
522 vector<boost::shared_ptr<Source> > srcs;
523 return _session.write_one_track (*this, start, end, false, srcs, itt, endpoint, include_endpoint, false, false);
527 AudioTrack::freeze_me (InterThreadInfo& itt)
529 vector<boost::shared_ptr<Source> > srcs;
530 string new_playlist_name;
531 boost::shared_ptr<Playlist> new_playlist;
534 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
536 if ((_freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist())) == 0) {
542 while (n < (UINT_MAX-1)) {
546 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
548 if (_session.playlists->by_name (candidate) == 0) {
549 new_playlist_name = candidate;
557 if (n == (UINT_MAX-1)) {
558 error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
559 " to create another one"), _freeze_record.playlist->name())
564 boost::shared_ptr<Region> res;
566 if ((res = _session.write_one_track (*this, _session.current_start_frame(), _session.current_end_frame(),
567 true, srcs, itt, main_outs(), false, false, true)) == 0) {
571 _freeze_record.processor_info.clear ();
574 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
576 for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); ++r) {
578 if ((*r)->does_routing() && (*r)->active()) {
581 if (!boost::dynamic_pointer_cast<PeakMeter>(*r)) {
583 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo ((*r)->get_state(), (*r));
585 frii->id = (*r)->id();
587 _freeze_record.processor_info.push_back (frii);
589 /* now deactivate the processor, */
590 if (!boost::dynamic_pointer_cast<Amp>(*r)) {
595 _session.set_dirty ();
599 new_playlist = PlaylistFactory::create (DataType::AUDIO, _session, new_playlist_name, false);
601 /* XXX need main outs automation state _freeze_record.pan_automation_state = _mainpanner->automation_state(); */
603 region_name = new_playlist_name;
605 /* create a new region from all filesources, keep it private */
609 plist.add (Properties::start, 0);
610 plist.add (Properties::length, srcs[0]->length(srcs[0]->timeline_position()));
611 plist.add (Properties::name, region_name);
612 plist.add (Properties::whole_file, true);
614 boost::shared_ptr<Region> region (RegionFactory::create (srcs, plist, false));
616 new_playlist->set_orig_track_id (id());
617 new_playlist->add_region (region, _session.current_start_frame());
618 new_playlist->set_frozen (true);
619 region->set_locked (true);
621 diskstream->use_playlist (boost::dynamic_pointer_cast<AudioPlaylist>(new_playlist));
622 diskstream->set_record_enabled (false);
624 /* reset stuff that has already been accounted for in the freeze process */
626 set_gain (GAIN_COEFF_UNITY, this);
627 _amp->gain_control()->set_automation_state (Off);
628 /* XXX need to use _main_outs _panner->set_automation_state (Off); */
630 _freeze_record.state = Frozen;
631 FreezeChange(); /* EMIT SIGNAL */
635 AudioTrack::unfreeze ()
637 if (_freeze_record.playlist) {
638 audio_diskstream()->use_playlist (_freeze_record.playlist);
641 Glib::Threads::RWLock::ReaderLock lm (_processor_lock); // should this be a write lock? jlc
642 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
643 for (vector<FreezeRecordProcessorInfo*>::iterator ii = _freeze_record.processor_info.begin(); ii != _freeze_record.processor_info.end(); ++ii) {
644 if ((*ii)->id == (*i)->id()) {
645 (*i)->set_state (((*ii)->state), Stateful::current_state_version);
652 _freeze_record.playlist.reset ();
653 /* XXX need to use _main_outs _panner->set_automation_state (_freeze_record.pan_automation_state); */
656 _freeze_record.state = UnFrozen;
657 FreezeChange (); /* EMIT SIGNAL */
660 boost::shared_ptr<AudioFileSource>
661 AudioTrack::write_source (uint32_t n)
663 boost::shared_ptr<AudioDiskstream> ds = boost::dynamic_pointer_cast<AudioDiskstream> (_diskstream);
665 return ds->write_source (n);
668 boost::shared_ptr<Diskstream>
669 AudioTrack::diskstream_factory (XMLNode const & node)
671 return boost::shared_ptr<Diskstream> (new AudioDiskstream (_session, node));