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