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/region.h"
39 #include "ardour/region_factory.h"
40 #include "ardour/session.h"
41 #include "ardour/session_playlists.h"
42 #include "ardour/source.h"
43 #include "ardour/utils.h"
48 using namespace ARDOUR;
51 AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
52 : Track (sess, name, flag, mode)
56 AudioTrack::~AudioTrack ()
60 boost::shared_ptr<Diskstream>
61 AudioTrack::create_diskstream ()
63 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (AudioDiskstream::Recordable);
65 if (_mode == Destructive) {
66 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
67 } else if (_mode == NonLayered){
68 dflags = AudioDiskstream::Flag(dflags | AudioDiskstream::NonLayered);
71 return boost::shared_ptr<AudioDiskstream> (new AudioDiskstream (_session, name(), dflags));
75 AudioTrack::set_diskstream (boost::shared_ptr<Diskstream> ds)
77 Track::set_diskstream (ds);
79 _diskstream->set_track (this);
80 _diskstream->set_destructive (_mode == Destructive);
81 _diskstream->set_non_layered (_mode == NonLayered);
83 if (audio_diskstream()->deprecated_io_node) {
85 if (!IO::connecting_legal) {
86 IO::ConnectingLegal.connect_same_thread (*this, boost::bind (&AudioTrack::deprecated_use_diskstream_connections, this));
88 deprecated_use_diskstream_connections ();
92 _diskstream->set_record_enabled (false);
93 _diskstream->request_input_monitoring (false);
95 DiskstreamChanged (); /* EMIT SIGNAL */
98 boost::shared_ptr<AudioDiskstream>
99 AudioTrack::audio_diskstream() const
101 return boost::dynamic_pointer_cast<AudioDiskstream>(_diskstream);
105 AudioTrack::set_mode (TrackMode m)
109 if (_diskstream->set_destructive (m == Destructive)) {
113 _diskstream->set_non_layered (m == NonLayered);
116 TrackModeChanged (); /* EMIT SIGNAL */
123 AudioTrack::can_use_mode (TrackMode m, bool& bounce_required)
128 bounce_required = false;
133 return _diskstream->can_become_destructive (bounce_required);
138 AudioTrack::deprecated_use_diskstream_connections ()
140 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
142 if (diskstream->deprecated_io_node == 0) {
146 const XMLProperty* prop;
147 XMLNode& node (*diskstream->deprecated_io_node);
149 /* don't do this more than once. */
151 diskstream->deprecated_io_node = 0;
153 if ((prop = node.property ("gain")) != 0) {
154 _amp->set_gain (atof (prop->value().c_str()), this);
157 if ((prop = node.property ("input-connection")) != 0) {
158 boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
161 error << string_compose(_("Unknown bundle \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
163 if ((c = _session.bundle_by_name (_("in 1"))) == 0) {
164 error << _("No input bundles available as a replacement")
168 info << string_compose (_("Bundle %1 was not available - \"in 1\" used instead"), prop->value())
173 _input->connect_ports_to_bundle (c, true, this);
175 } else if ((prop = node.property ("inputs")) != 0) {
176 if (_input->set_ports (prop->value())) {
177 error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
186 AudioTrack::set_state (const XMLNode& node, int version)
188 const XMLProperty *prop;
190 if (Track::set_state (node, version)) {
194 if ((prop = node.property (X_("mode"))) != 0) {
195 _mode = TrackMode (string_2_enum (prop->value(), _mode));
200 pending_state = const_cast<XMLNode*> (&node);
202 if (_session.state_of_the_state() & Session::Loading) {
203 _session.StateReady.connect_same_thread (*this, boost::bind (&AudioTrack::set_state_part_two, this));
205 set_state_part_two ();
212 AudioTrack::state (bool full_state)
214 XMLNode& root (Track::state(full_state));
215 XMLNode* freeze_node;
218 if (_freeze_record.playlist) {
221 freeze_node = new XMLNode (X_("freeze-info"));
222 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
223 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
225 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
226 inode = new XMLNode (X_("processor"));
227 (*i)->id.print (buf, sizeof (buf));
228 inode->add_property (X_("id"), buf);
229 inode->add_child_copy ((*i)->state);
231 freeze_node->add_child_nocopy (*inode);
234 root.add_child_nocopy (*freeze_node);
237 root.add_property (X_("mode"), enum_2_string (_mode));
243 AudioTrack::set_state_part_two ()
247 LocaleGuard lg (X_("POSIX"));
249 /* This is called after all session state has been restored but before
250 have been made ports and connections are established.
253 if (pending_state == 0) {
257 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
259 _freeze_record.state = Frozen;
261 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
264 _freeze_record.processor_info.clear ();
266 if ((prop = fnode->property (X_("playlist"))) != 0) {
267 boost::shared_ptr<Playlist> pl = _session.playlists->by_name (prop->value());
269 _freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
271 _freeze_record.playlist.reset ();
272 _freeze_record.state = NoFreeze;
277 if ((prop = fnode->property (X_("state"))) != 0) {
278 _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
281 XMLNodeConstIterator citer;
282 XMLNodeList clist = fnode->children();
284 for (citer = clist.begin(); citer != clist.end(); ++citer) {
285 if ((*citer)->name() != X_("processor")) {
289 if ((prop = (*citer)->property (X_("id"))) == 0) {
293 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo (*((*citer)->children().front()),
294 boost::shared_ptr<Processor>());
295 frii->id = prop->value ();
296 _freeze_record.processor_info.push_back (frii);
301 /** @param need_butler to be set to true if this track now needs the butler, otherwise it can be left alone
305 AudioTrack::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
307 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
310 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
311 framecnt_t playback_distance = diskstream->calculate_playback_distance(nframes);
312 if (can_internal_playback_seek(llabs(playback_distance))) {
313 /* TODO should declick */
314 internal_playback_seek(playback_distance);
319 framepos_t transport_frame;
320 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
322 if (n_outputs().n_total() == 0 && _processors.empty()) {
328 if (_meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled())) {
334 transport_frame = _session.transport_frame();
337 framecnt_t playback_distance;
339 if ((nframes = check_initial_delay (nframes, transport_frame)) == 0) {
341 /* need to do this so that the diskstream sets its
342 playback distance to zero, thus causing diskstream::commit
346 BufferSet bufs; /* empty set, no matter - nothing will happen */
348 dret = diskstream->process (bufs, transport_frame, 0, playback_distance, false);
349 need_butler = diskstream->commit (playback_distance);
353 if (_mute_control->list() && _mute_control->automation_playback()) {
355 const float mute = _mute_control->list()->rt_safe_eval(transport_frame, valid);
356 if (mute >= 0.5 && !muted()) {
357 _mute_control->set_value(1.0); // mute
358 } else if (mute < 0.5 && muted()) {
359 _mute_control->set_value(0.0); // unmute
364 _amp->apply_gain_automation(false);
366 BufferSet& bufs = _session.get_route_buffers (n_process_buffers ());
368 fill_buffers_with_input (bufs, _input, nframes);
370 if (_meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled())) {
371 _meter->run (bufs, start_frame, end_frame, nframes, true);
374 if ((dret = diskstream->process (bufs, transport_frame, nframes, playback_distance, (monitoring_state() == MonitoringDisk))) != 0) {
375 need_butler = diskstream->commit (playback_distance);
380 process_output_buffers (bufs, start_frame, end_frame, nframes, declick, (!diskstream->record_enabled() && _session.transport_rolling()));
382 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
383 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
385 d->flush_buffers (nframes);
389 need_butler = diskstream->commit (playback_distance);
395 AudioTrack::export_stuff (BufferSet& buffers, framepos_t start, framecnt_t nframes,
396 boost::shared_ptr<Processor> endpoint, bool include_endpoint, bool for_export, bool for_freeze)
398 boost::scoped_array<gain_t> gain_buffer (new gain_t[nframes]);
399 boost::scoped_array<Sample> mix_buffer (new Sample[nframes]);
400 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
402 Glib::Threads::RWLock::ReaderLock rlock (_processor_lock);
404 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist());
407 assert(buffers.count().n_audio() >= 1);
408 assert ((framecnt_t) buffers.get_audio(0).capacity() >= nframes);
410 if (apl->read (buffers.get_audio(0).data(), mix_buffer.get(), gain_buffer.get(), start, nframes) != nframes) {
415 Sample* b = buffers.get_audio(0).data();
416 BufferSet::audio_iterator bi = buffers.audio_begin();
418 for ( ; bi != buffers.audio_end(); ++bi, ++n) {
419 if (n < diskstream->n_channels().n_audio()) {
420 if (apl->read (bi->data(), mix_buffer.get(), gain_buffer.get(), start, nframes, n) != nframes) {
425 /* duplicate last across remaining buffers */
426 memcpy (bi->data(), b, sizeof (Sample) * nframes);
430 bounce_process (buffers, start, nframes, endpoint, include_endpoint, for_export, for_freeze);
436 AudioTrack::bounceable (boost::shared_ptr<Processor> endpoint, bool include_endpoint) const
438 if (!endpoint && !include_endpoint) {
439 /* no processing - just read from the playlist and create new
440 files: always possible.
445 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
446 uint32_t naudio = n_inputs().n_audio();
448 for (ProcessorList::const_iterator r = _processors.begin(); r != _processors.end(); ++r) {
450 /* if we're not including the endpoint, potentially stop
451 right here before we test matching i/o valences.
454 if (!include_endpoint && (*r) == endpoint) {
458 /* ignore any processors that do routing, because we will not
459 * use them during a bounce/freeze/export operation.
462 if ((*r)->does_routing()) {
466 /* does the output from the last considered processor match the
470 if (naudio != (*r)->input_streams().n_audio()) {
474 /* we're including the endpoint - if we just hit it,
478 if ((*r) == endpoint) {
482 /* save outputs of this processor to test against inputs
486 naudio = (*r)->output_streams().n_audio();
492 boost::shared_ptr<Region>
493 AudioTrack::bounce (InterThreadInfo& itt)
495 return bounce_range (_session.current_start_frame(), _session.current_end_frame(), itt, main_outs(), false);
498 boost::shared_ptr<Region>
499 AudioTrack::bounce_range (framepos_t start, framepos_t end, InterThreadInfo& itt,
500 boost::shared_ptr<Processor> endpoint, bool include_endpoint)
502 vector<boost::shared_ptr<Source> > srcs;
503 return _session.write_one_track (*this, start, end, false, srcs, itt, endpoint, include_endpoint, false, false);
507 AudioTrack::freeze_me (InterThreadInfo& itt)
509 vector<boost::shared_ptr<Source> > srcs;
510 string new_playlist_name;
511 boost::shared_ptr<Playlist> new_playlist;
514 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
516 if ((_freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist())) == 0) {
522 while (n < (UINT_MAX-1)) {
526 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
528 if (_session.playlists->by_name (candidate) == 0) {
529 new_playlist_name = candidate;
537 if (n == (UINT_MAX-1)) {
538 error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
539 " to create another one"), _freeze_record.playlist->name())
544 boost::shared_ptr<Region> res;
546 if ((res = _session.write_one_track (*this, _session.current_start_frame(), _session.current_end_frame(),
547 true, srcs, itt, main_outs(), false, false, true)) == 0) {
551 _freeze_record.processor_info.clear ();
554 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
556 for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); ++r) {
558 if ((*r)->does_routing() && (*r)->active()) {
561 if (!boost::dynamic_pointer_cast<PeakMeter>(*r)) {
563 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo ((*r)->get_state(), (*r));
565 frii->id = (*r)->id();
567 _freeze_record.processor_info.push_back (frii);
569 /* now deactivate the processor, */
570 if (!boost::dynamic_pointer_cast<Amp>(*r)) {
575 _session.set_dirty ();
579 new_playlist = PlaylistFactory::create (DataType::AUDIO, _session, new_playlist_name, false);
581 /* XXX need main outs automation state _freeze_record.pan_automation_state = _mainpanner->automation_state(); */
583 region_name = new_playlist_name;
585 /* create a new region from all filesources, keep it private */
589 plist.add (Properties::start, 0);
590 plist.add (Properties::length, srcs[0]->length(srcs[0]->timeline_position()));
591 plist.add (Properties::name, region_name);
592 plist.add (Properties::whole_file, true);
594 boost::shared_ptr<Region> region (RegionFactory::create (srcs, plist, false));
596 new_playlist->set_orig_track_id (id());
597 new_playlist->add_region (region, _session.current_start_frame());
598 new_playlist->set_frozen (true);
599 region->set_locked (true);
601 diskstream->use_playlist (boost::dynamic_pointer_cast<AudioPlaylist>(new_playlist));
602 diskstream->set_record_enabled (false);
604 /* reset stuff that has already been accounted for in the freeze process */
606 set_gain (1.0, this);
607 _amp->gain_control()->set_automation_state (Off);
608 /* XXX need to use _main_outs _panner->set_automation_state (Off); */
610 _freeze_record.state = Frozen;
611 FreezeChange(); /* EMIT SIGNAL */
615 AudioTrack::unfreeze ()
617 if (_freeze_record.playlist) {
618 audio_diskstream()->use_playlist (_freeze_record.playlist);
621 Glib::Threads::RWLock::ReaderLock lm (_processor_lock); // should this be a write lock? jlc
622 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
623 for (vector<FreezeRecordProcessorInfo*>::iterator ii = _freeze_record.processor_info.begin(); ii != _freeze_record.processor_info.end(); ++ii) {
624 if ((*ii)->id == (*i)->id()) {
625 (*i)->set_state (((*ii)->state), Stateful::current_state_version);
632 _freeze_record.playlist.reset ();
633 /* XXX need to use _main_outs _panner->set_automation_state (_freeze_record.pan_automation_state); */
636 _freeze_record.state = UnFrozen;
637 FreezeChange (); /* EMIT SIGNAL */
640 boost::shared_ptr<AudioFileSource>
641 AudioTrack::write_source (uint32_t n)
643 boost::shared_ptr<AudioDiskstream> ds = boost::dynamic_pointer_cast<AudioDiskstream> (_diskstream);
645 return ds->write_source (n);
648 boost::shared_ptr<Diskstream>
649 AudioTrack::diskstream_factory (XMLNode const & node)
651 return boost::shared_ptr<Diskstream> (new AudioDiskstream (_session, node));