remove Diskstream from Track and derivatives; get ardour to actually startup
[ardour.git] / libs / ardour / audio_track.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <boost/scoped_array.hpp>
21
22 #include "pbd/enumwriter.h"
23 #include "pbd/error.h"
24
25 #include "evoral/Curve.hpp"
26
27 #include "ardour/amp.h"
28 #include "ardour/audio_buffer.h"
29 #include "ardour/audio_diskstream.h"
30 #include "ardour/audio_track.h"
31 #include "ardour/audioplaylist.h"
32 #include "ardour/boost_debug.h"
33 #include "ardour/buffer_set.h"
34 #include "ardour/delivery.h"
35 #include "ardour/disk_reader.h"
36 #include "ardour/disk_writer.h"
37 #include "ardour/meter.h"
38 #include "ardour/monitor_control.h"
39 #include "ardour/playlist_factory.h"
40 #include "ardour/processor.h"
41 #include "ardour/profile.h"
42 #include "ardour/region.h"
43 #include "ardour/region_factory.h"
44 #include "ardour/session.h"
45 #include "ardour/session_playlists.h"
46 #include "ardour/source.h"
47 #include "ardour/types_convert.h"
48 #include "ardour/utils.h"
49
50 #include "pbd/i18n.h"
51
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55
56 AudioTrack::AudioTrack (Session& sess, string name, TrackMode mode)
57         : Track (sess, name, PresentationInfo::AudioTrack, mode)
58 {
59 }
60
61 AudioTrack::~AudioTrack ()
62 {
63         if (_freeze_record.playlist && !_session.deletion_in_progress()) {
64                 _freeze_record.playlist->release();
65         }
66 }
67
68 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
69 int
70 AudioTrack::set_mode (TrackMode m)
71 {
72         if (m != _mode) {
73
74                 if (!Profile->get_trx() && _diskstream->set_destructive (m == Destructive)) {
75                         return -1;
76                 }
77
78                 _diskstream->set_non_layered (m == NonLayered);
79                 _mode = m;
80
81                 TrackModeChanged (); /* EMIT SIGNAL */
82         }
83
84         return 0;
85 }
86
87 bool
88 AudioTrack::can_use_mode (TrackMode m, bool& bounce_required)
89 {
90         switch (m) {
91         case NonLayered:
92         case Normal:
93                 bounce_required = false;
94                 return true;
95
96         case Destructive:
97                 if (Profile->get_trx()) {
98                         return false;
99                 } else {
100                         return _diskstream->can_become_destructive (bounce_required);
101                 }
102                 break;
103
104         default:
105                 return false;
106         }
107 }
108 #endif
109
110 int
111 AudioTrack::set_state (const XMLNode& node, int version)
112 {
113         if (!node.get_property (X_("mode"), _mode)) {
114                 _mode = Normal;
115         }
116
117         if (Profile->get_trx() && _mode == Destructive) {
118                 /* Tracks does not support destructive tracks and trying to
119                    handle it as a normal track would be wrong.
120                 */
121                 error << string_compose (_("%1: this session uses destructive tracks, which are not supported"), PROGRAM_NAME) << endmsg;
122                 return -1;
123         }
124
125         if (Track::set_state (node, version)) {
126                 return -1;
127         }
128
129         pending_state = const_cast<XMLNode*> (&node);
130
131         if (_session.state_of_the_state() & Session::Loading) {
132                 _session.StateReady.connect_same_thread (*this, boost::bind (&AudioTrack::set_state_part_two, this));
133         } else {
134                 set_state_part_two ();
135         }
136
137         return 0;
138 }
139
140 XMLNode&
141 AudioTrack::state (bool full_state)
142 {
143         XMLNode& root (Track::state(full_state));
144         XMLNode* freeze_node;
145
146         if (_freeze_record.playlist) {
147                 XMLNode* inode;
148
149                 freeze_node = new XMLNode (X_("freeze-info"));
150                 freeze_node->set_property ("playlist", _freeze_record.playlist->name());
151                 freeze_node->set_property ("state", _freeze_record.state);
152
153                 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
154                         inode = new XMLNode (X_("processor"));
155                         inode->set_property (X_ ("id"), (*i)->id.to_s ());
156                         inode->add_child_copy ((*i)->state);
157
158                         freeze_node->add_child_nocopy (*inode);
159                 }
160
161                 root.add_child_nocopy (*freeze_node);
162         }
163
164         root.set_property (X_("mode"), _mode);
165
166         return root;
167 }
168
169 void
170 AudioTrack::set_state_part_two ()
171 {
172         XMLNode* fnode;
173         XMLProperty const * prop;
174
175         /* This is called after all session state has been restored but before
176            have been made ports and connections are established.
177         */
178
179         if (pending_state == 0) {
180                 return;
181         }
182
183         if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
184
185                 _freeze_record.state = Frozen;
186
187                 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
188                         delete *i;
189                 }
190                 _freeze_record.processor_info.clear ();
191
192                 if ((prop = fnode->property (X_("playlist"))) != 0) {
193                         boost::shared_ptr<Playlist> pl = _session.playlists->by_name (prop->value());
194                         if (pl) {
195                                 _freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
196                                 _freeze_record.playlist->use();
197                         } else {
198                                 _freeze_record.playlist.reset ();
199                                 _freeze_record.state = NoFreeze;
200                         return;
201                         }
202                 }
203
204                 fnode->get_property (X_("state"), _freeze_record.state);
205
206                 XMLNodeConstIterator citer;
207                 XMLNodeList clist = fnode->children();
208
209                 for (citer = clist.begin(); citer != clist.end(); ++citer) {
210                         if ((*citer)->name() != X_("processor")) {
211                                 continue;
212                         }
213
214                         if ((prop = (*citer)->property (X_("id"))) == 0) {
215                                 continue;
216                         }
217
218                         FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo (*((*citer)->children().front()),
219                                                                                    boost::shared_ptr<Processor>());
220                         frii->id = prop->value ();
221                         _freeze_record.processor_info.push_back (frii);
222                 }
223         }
224 }
225
226 /** @param need_butler to be set to true if this track now needs the butler, otherwise it can be left alone
227  *  or set to false.
228  */
229 int
230 AudioTrack::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
231 {
232         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
233
234         if (!lm.locked()) {
235                 return 0;
236         }
237
238         if (n_outputs().n_total() == 0 && _processors.empty()) {
239                 return 0;
240         }
241
242         if (!_active) {
243                 silence (nframes);
244                 if (_meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _disk_writer->record_enabled())) {
245                         _meter->reset();
246                 }
247                 return 0;
248         }
249
250         _silent = false;
251         _amp->apply_gain_automation(false);
252
253         BufferSet& bufs = _session.get_route_buffers (n_process_buffers ());
254
255         fill_buffers_with_input (bufs, _input, nframes);
256
257         if (_meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _disk_writer->record_enabled())) {
258                 _meter->run (bufs, start_frame, end_frame, 1.0 /*speed()*/, nframes, true);
259         }
260
261         process_output_buffers (bufs, start_frame, end_frame, nframes, declick, (!_disk_writer->record_enabled() && _session.transport_rolling()));
262
263         flush_processor_buffers_locked (nframes);
264
265         return 0;
266 }
267
268 int
269 AudioTrack::export_stuff (BufferSet& buffers, framepos_t start, framecnt_t nframes,
270                           boost::shared_ptr<Processor> endpoint, bool include_endpoint, bool for_export, bool for_freeze)
271 {
272         boost::scoped_array<gain_t> gain_buffer (new gain_t[nframes]);
273         boost::scoped_array<Sample> mix_buffer (new Sample[nframes]);
274
275         Glib::Threads::RWLock::ReaderLock rlock (_processor_lock);
276
277         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(playlist());
278
279         assert(apl);
280         assert(buffers.count().n_audio() >= 1);
281         assert ((framecnt_t) buffers.get_audio(0).capacity() >= nframes);
282
283         if (apl->read (buffers.get_audio(0).data(), mix_buffer.get(), gain_buffer.get(), start, nframes) != nframes) {
284                 return -1;
285         }
286
287         uint32_t n=1;
288         Sample* b = buffers.get_audio(0).data();
289         BufferSet::audio_iterator bi = buffers.audio_begin();
290         ++bi;
291         for ( ; bi != buffers.audio_end(); ++bi, ++n) {
292                 if (n < _disk_reader->output_streams().n_audio()) {
293                         if (apl->read (bi->data(), mix_buffer.get(), gain_buffer.get(), start, nframes, n) != nframes) {
294                                 return -1;
295                         }
296                         b = bi->data();
297                 } else {
298                         /* duplicate last across remaining buffers */
299                         memcpy (bi->data(), b, sizeof (Sample) * nframes);
300                 }
301         }
302
303         bounce_process (buffers, start, nframes, endpoint, include_endpoint, for_export, for_freeze);
304
305         return 0;
306 }
307
308 bool
309 AudioTrack::bounceable (boost::shared_ptr<Processor> endpoint, bool include_endpoint) const
310 {
311         if (!endpoint && !include_endpoint) {
312                 /* no processing - just read from the playlist and create new
313                    files: always possible.
314                 */
315                 return true;
316         }
317
318         Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
319         uint32_t naudio = n_inputs().n_audio();
320
321         for (ProcessorList::const_iterator r = _processors.begin(); r != _processors.end(); ++r) {
322
323                 /* if we're not including the endpoint, potentially stop
324                    right here before we test matching i/o valences.
325                 */
326
327                 if (!include_endpoint && (*r) == endpoint) {
328                         return true;
329                 }
330
331                 /* ignore any processors that do routing, because we will not
332                  * use them during a bounce/freeze/export operation.
333                  */
334
335                 if ((*r)->does_routing()) {
336                         continue;
337                 }
338
339                 /* does the output from the last considered processor match the
340                  * input to this one?
341                  */
342
343                 if (naudio != (*r)->input_streams().n_audio()) {
344                         return false;
345                 }
346
347                 /* we're including the endpoint - if we just hit it,
348                    then stop.
349                 */
350
351                 if ((*r) == endpoint) {
352                         return true;
353                 }
354
355                 /* save outputs of this processor to test against inputs
356                    of the next one.
357                 */
358
359                 naudio = (*r)->output_streams().n_audio();
360         }
361
362         return true;
363 }
364
365 boost::shared_ptr<Region>
366 AudioTrack::bounce (InterThreadInfo& itt)
367 {
368         return bounce_range (_session.current_start_frame(), _session.current_end_frame(), itt, main_outs(), false);
369 }
370
371 boost::shared_ptr<Region>
372 AudioTrack::bounce_range (framepos_t start, framepos_t end, InterThreadInfo& itt,
373                           boost::shared_ptr<Processor> endpoint, bool include_endpoint)
374 {
375         vector<boost::shared_ptr<Source> > srcs;
376         return _session.write_one_track (*this, start, end, false, srcs, itt, endpoint, include_endpoint, false, false);
377 }
378
379 void
380 AudioTrack::freeze_me (InterThreadInfo& itt)
381 {
382         vector<boost::shared_ptr<Source> > srcs;
383         string new_playlist_name;
384         boost::shared_ptr<Playlist> new_playlist;
385         string dir;
386         string region_name;
387
388         if ((_freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist>(playlist())) == 0) {
389                 return;
390         }
391
392         uint32_t n = 1;
393
394         while (n < (UINT_MAX-1)) {
395
396                 string candidate;
397
398                 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
399
400                 if (_session.playlists->by_name (candidate) == 0) {
401                         new_playlist_name = candidate;
402                         break;
403                 }
404
405                 ++n;
406
407         }
408
409         if (n == (UINT_MAX-1)) {
410           error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
411                             " to create another one"), _freeze_record.playlist->name())
412                << endmsg;
413                 return;
414         }
415
416         boost::shared_ptr<Region> res;
417
418         if ((res = _session.write_one_track (*this, _session.current_start_frame(), _session.current_end_frame(),
419                                         true, srcs, itt, main_outs(), false, false, true)) == 0) {
420                 return;
421         }
422
423         _freeze_record.processor_info.clear ();
424
425         {
426                 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
427
428                 for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); ++r) {
429
430                         if ((*r)->does_routing() && (*r)->active()) {
431                                 break;
432                         }
433                         if (!boost::dynamic_pointer_cast<PeakMeter>(*r)) {
434
435                                 FreezeRecordProcessorInfo* frii  = new FreezeRecordProcessorInfo ((*r)->get_state(), (*r));
436
437                                 frii->id = (*r)->id();
438
439                                 _freeze_record.processor_info.push_back (frii);
440
441                                 /* now deactivate the processor, */
442                                 if (!boost::dynamic_pointer_cast<Amp>(*r)) {
443                                         (*r)->deactivate ();
444                                 }
445                         }
446
447                         _session.set_dirty ();
448                 }
449         }
450
451         new_playlist = PlaylistFactory::create (DataType::AUDIO, _session, new_playlist_name, false);
452
453         /* XXX need main outs automation state _freeze_record.pan_automation_state = _mainpanner->automation_state(); */
454
455         region_name = new_playlist_name;
456
457         /* create a new region from all filesources, keep it private */
458
459         PropertyList plist;
460
461         plist.add (Properties::start, 0);
462         plist.add (Properties::length, srcs[0]->length(srcs[0]->timeline_position()));
463         plist.add (Properties::name, region_name);
464         plist.add (Properties::whole_file, true);
465
466         boost::shared_ptr<Region> region (RegionFactory::create (srcs, plist, false));
467
468         new_playlist->set_orig_track_id (id());
469         new_playlist->add_region (region, _session.current_start_frame());
470         new_playlist->set_frozen (true);
471         region->set_locked (true);
472
473         use_playlist (DataType::AUDIO, boost::dynamic_pointer_cast<AudioPlaylist>(new_playlist));
474         _disk_writer->set_record_enabled (false);
475
476         _freeze_record.playlist->use(); // prevent deletion
477
478         /* reset stuff that has already been accounted for in the freeze process */
479
480         gain_control()->set_value (GAIN_COEFF_UNITY, Controllable::NoGroup);
481         gain_control()->set_automation_state (Off);
482
483         /* XXX need to use _main_outs _panner->set_automation_state (Off); */
484
485         _freeze_record.state = Frozen;
486         FreezeChange(); /* EMIT SIGNAL */
487 }
488
489 void
490 AudioTrack::unfreeze ()
491 {
492         if (_freeze_record.playlist) {
493                 _freeze_record.playlist->release();
494                 use_playlist (DataType::AUDIO, _freeze_record.playlist);
495
496                 {
497                         Glib::Threads::RWLock::ReaderLock lm (_processor_lock); // should this be a write lock? jlc
498                         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
499                                 for (vector<FreezeRecordProcessorInfo*>::iterator ii = _freeze_record.processor_info.begin(); ii != _freeze_record.processor_info.end(); ++ii) {
500                                         if ((*ii)->id == (*i)->id()) {
501                                                 (*i)->set_state (((*ii)->state), Stateful::current_state_version);
502                                                 break;
503                                         }
504                                 }
505                         }
506                 }
507
508                 _freeze_record.playlist.reset ();
509                 /* XXX need to use _main_outs _panner->set_automation_state (_freeze_record.pan_automation_state); */
510         }
511
512         _freeze_record.state = UnFrozen;
513         FreezeChange (); /* EMIT SIGNAL */
514 }
515
516 boost::shared_ptr<AudioFileSource>
517 AudioTrack::write_source (uint32_t n)
518 {
519         assert (_disk_writer);
520         return _disk_writer->audio_write_source (n);
521 }