add IsSkip enum to enums.cc
[ardour.git] / libs / ardour / diskstream.cc
1 /*
2     Copyright (C) 2000-2006 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 <fstream>
21 #include <cassert>
22 #include <cstdio>
23 #include <unistd.h>
24 #include <cmath>
25 #include <cerrno>
26 #include <string>
27 #include <climits>
28 #include <fcntl.h>
29 #include <cstdlib>
30 #include <ctime>
31 #include <sys/stat.h>
32
33 #include <glibmm/threads.h>
34
35 #include "pbd/error.h"
36 #include "pbd/basename.h"
37 #include "pbd/memento_command.h"
38 #include "pbd/xml++.h"
39 #include "pbd/stacktrace.h"
40
41 #include "ardour/debug.h"
42 #include "ardour/diskstream.h"
43 #include "ardour/io.h"
44 #include "ardour/pannable.h"
45 #include "ardour/playlist.h"
46 #include "ardour/session.h"
47 #include "ardour/track.h"
48
49 #include "i18n.h"
50 #include <locale.h>
51
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55
56 /* XXX This goes uninitialized when there is no ~/.config/ardour3 directory.
57  * I can't figure out why, so this will do for now (just stole the
58  * default from configuration_vars.h).  0 is not a good value for
59  * allocating buffer sizes..
60  */
61 ARDOUR::framecnt_t Diskstream::disk_io_chunk_frames = 1024 * 256 / sizeof (Sample);
62
63 PBD::Signal0<void>                Diskstream::DiskOverrun;
64 PBD::Signal0<void>                Diskstream::DiskUnderrun;
65
66 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
67         : SessionObject(sess, name)
68         , i_am_the_modifier (0)
69         , _track (0)
70         , _record_enabled (0)
71         , _visible_speed (1.0f)
72         , _actual_speed (1.0f)
73         , _buffer_reallocation_required (false)
74         , _seek_required (false)
75         , capture_start_frame (0)
76         , capture_captured (0)
77         , was_recording (false)
78         , adjust_capture_position (0)
79         , _capture_offset (0)
80         , _roll_delay (0)
81         , first_recordable_frame (max_framepos)
82         , last_recordable_frame (max_framepos)
83         , last_possibly_recording (0)
84         , _alignment_style (ExistingMaterial)
85         , _alignment_choice (Automatic)
86         , _slaved (false)
87         , loop_location (0)
88         , overwrite_frame (0)
89         , overwrite_offset (0)
90         , _pending_overwrite (false)
91         , overwrite_queued (false)
92         , wrap_buffer_size (0)
93         , speed_buffer_size (0)
94         , _speed (1.0)
95         , _target_speed (_speed)
96         , file_frame (0)
97         , playback_sample (0)
98         , in_set_state (false)
99         , _flags (flag)
100         , deprecated_io_node (0)
101 {
102 }
103
104 Diskstream::Diskstream (Session& sess, const XMLNode& /*node*/)
105         : SessionObject(sess, "unnamed diskstream")
106         , i_am_the_modifier (0)
107         , _track (0)
108         , _record_enabled (0)
109         , _visible_speed (1.0f)
110         , _actual_speed (1.0f)
111         , _buffer_reallocation_required (false)
112         , _seek_required (false)
113         , capture_start_frame (0)
114         , capture_captured (0)
115         , was_recording (false)
116         , adjust_capture_position (0)
117         , _capture_offset (0)
118         , _roll_delay (0)
119         , first_recordable_frame (max_framepos)
120         , last_recordable_frame (max_framepos)
121         , last_possibly_recording (0)
122         , _alignment_style (ExistingMaterial)
123         , _alignment_choice (Automatic)
124         , _slaved (false)
125         , loop_location (0)
126         , overwrite_frame (0)
127         , overwrite_offset (0)
128         , _pending_overwrite (false)
129         , overwrite_queued (false)
130         , wrap_buffer_size (0)
131         , speed_buffer_size (0)
132         , _speed (1.0)
133         , _target_speed (_speed)
134         , file_frame (0)
135         , playback_sample (0)
136         , in_set_state (false)
137         , _flags (Recordable)
138         , deprecated_io_node (0)
139 {
140 }
141
142 Diskstream::~Diskstream ()
143 {
144         DEBUG_TRACE (DEBUG::Destruction, string_compose ("Diskstream %1 deleted\n", _name));
145
146         if (_playlist) {
147                 _playlist->release ();
148         }
149
150         delete deprecated_io_node;
151 }
152
153 void
154 Diskstream::set_track (Track* t)
155 {
156         _track = t;
157         _io = _track->input();
158
159         ic_connection.disconnect();
160         _io->changed.connect_same_thread (ic_connection, boost::bind (&Diskstream::handle_input_change, this, _1, _2));
161
162         if (_io->n_ports() != ChanCount::ZERO) {
163                 input_change_pending.type = IOChange::Type (IOChange::ConfigurationChanged|IOChange::ConnectionsChanged);
164                 non_realtime_input_change ();
165         }
166
167         _track->Destroyed.connect_same_thread (*this, boost::bind (&Diskstream::route_going_away, this));
168 }
169
170 void
171 Diskstream::handle_input_change (IOChange change, void * /*src*/)
172 {
173         Glib::Threads::Mutex::Lock lm (state_lock);
174
175         if (change.type & (IOChange::ConfigurationChanged|IOChange::ConnectionsChanged)) {
176
177                 /* rather than handle this here on a DS-by-DS basis we defer to the
178                    session transport/butler thread, and let it tackle
179                    as many diskstreams as need it in one shot. this avoids many repeated
180                    takings of the audioengine process lock.
181                 */
182
183                 if (!(input_change_pending.type & change.type)) {
184                         input_change_pending.type = IOChange::Type (input_change_pending.type | change.type);
185                         _session.request_input_change_handling ();
186                 }
187         }
188 }
189
190 void
191 Diskstream::non_realtime_set_speed ()
192 {
193         if (_buffer_reallocation_required)
194         {
195                 Glib::Threads::Mutex::Lock lm (state_lock);
196                 allocate_temporary_buffers ();
197
198                 _buffer_reallocation_required = false;
199         }
200
201         if (_seek_required) {
202                 if (speed() != 1.0f || speed() != -1.0f) {
203                         seek ((framepos_t) (_session.transport_frame() * (double) speed()), true);
204                 }
205                 else {
206                         seek (_session.transport_frame(), true);
207                 }
208
209                 _seek_required = false;
210         }
211 }
212
213 bool
214 Diskstream::realtime_set_speed (double sp, bool global)
215 {
216         bool changed = false;
217         double new_speed = sp * _session.transport_speed();
218
219         if (_visible_speed != sp) {
220                 _visible_speed = sp;
221                 changed = true;
222         }
223
224         if (new_speed != _actual_speed) {
225
226                 framecnt_t required_wrap_size = (framecnt_t) ceil (_session.get_block_size() *
227                                                                   fabs (new_speed)) + 2;
228
229                 if (required_wrap_size > wrap_buffer_size) {
230                         _buffer_reallocation_required = true;
231                 }
232
233                 _actual_speed = new_speed;
234                 _target_speed = fabs(_actual_speed);
235         }
236
237         if (changed) {
238                 if (!global) {
239                         _seek_required = true;
240                 }
241                 SpeedChanged (); /* EMIT SIGNAL */
242         }
243
244         return _buffer_reallocation_required || _seek_required;
245 }
246
247 void
248 Diskstream::set_capture_offset ()
249 {
250         if (_io == 0) {
251                 /* can't capture, so forget it */
252                 return;
253         }
254
255         _capture_offset = _io->latency();
256         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: using IO latency, capture offset set to %2\n", name(), _capture_offset));
257 }
258
259
260 void
261 Diskstream::set_align_style (AlignStyle a, bool force)
262 {
263         if (record_enabled() && _session.actively_recording()) {
264                 return;
265         }
266
267         if ((a != _alignment_style) || force) {
268                 _alignment_style = a;
269                 AlignmentStyleChanged ();
270         }
271 }
272
273 void
274 Diskstream::set_align_choice (AlignChoice a, bool force)
275 {
276         if (record_enabled() && _session.actively_recording()) {
277                 return;
278         }
279
280         if ((a != _alignment_choice) || force) {
281                 _alignment_choice = a;
282
283                 switch (_alignment_choice) {
284                         case Automatic:
285                                 set_align_style_from_io ();
286                                 break;
287                         case UseExistingMaterial:
288                                 set_align_style (ExistingMaterial);
289                                 break;
290                         case UseCaptureTime:
291                                 set_align_style (CaptureTime);
292                                 break;
293                 }
294         }
295 }
296
297 int
298 Diskstream::set_loop (Location *location)
299 {
300         if (location) {
301                 if (location->start() >= location->end()) {
302                         error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
303                         return -1;
304                 }
305         }
306
307         loop_location = location;
308
309         LoopSet (location); /* EMIT SIGNAL */
310         return 0;
311 }
312
313 /** Get the start position (in session frames) of the nth capture in the current pass */
314 ARDOUR::framepos_t
315 Diskstream::get_capture_start_frame (uint32_t n) const
316 {
317         Glib::Threads::Mutex::Lock lm (capture_info_lock);
318
319         if (capture_info.size() > n) {
320                 /* this is a completed capture */
321                 return capture_info[n]->start;
322         } else {
323                 /* this is the currently in-progress capture */
324                 return capture_start_frame;
325         }
326 }
327
328 ARDOUR::framecnt_t
329 Diskstream::get_captured_frames (uint32_t n) const
330 {
331         Glib::Threads::Mutex::Lock lm (capture_info_lock);
332
333         if (capture_info.size() > n) {
334                 /* this is a completed capture */
335                 return capture_info[n]->frames;
336         } else {
337                 /* this is the currently in-progress capture */
338                 return capture_captured;
339         }
340 }
341
342 void
343 Diskstream::set_roll_delay (ARDOUR::framecnt_t nframes)
344 {
345         _roll_delay = nframes;
346 }
347
348 int
349 Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
350 {
351         if (!playlist) {
352                 return 0;
353         }
354
355         bool prior_playlist = false;
356
357         {
358                 Glib::Threads::Mutex::Lock lm (state_lock);
359
360                 if (playlist == _playlist) {
361                         return 0;
362                 }
363
364                 playlist_connections.drop_connections ();
365
366                 if (_playlist) {
367                         _playlist->release();
368                         prior_playlist = true;
369                 }
370
371                 _playlist = playlist;
372                 _playlist->use();
373
374                 if (!in_set_state && recordable()) {
375                         reset_write_sources (false);
376                 }
377
378                 _playlist->ContentsChanged.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_modified, this));
379                 _playlist->DropReferences.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_deleted, this, boost::weak_ptr<Playlist>(_playlist)));
380                 _playlist->RangesMoved.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_ranges_moved, this, _1, _2));
381         }
382
383         /* don't do this if we've already asked for it *or* if we are setting up
384            the diskstream for the very first time - the input changed handling will
385            take care of the buffer refill.
386         */
387
388         if (!overwrite_queued && prior_playlist) {
389                 _session.request_overwrite_buffer (_track);
390                 overwrite_queued = true;
391         }
392
393         PlaylistChanged (); /* EMIT SIGNAL */
394         _session.set_dirty ();
395
396         return 0;
397 }
398
399 void
400 Diskstream::playlist_changed (const PropertyChange&)
401 {
402         playlist_modified ();
403 }
404
405 void
406 Diskstream::playlist_modified ()
407 {
408         if (!i_am_the_modifier && !overwrite_queued) {
409                 _session.request_overwrite_buffer (_track);
410                 overwrite_queued = true;
411         }
412 }
413
414 void
415 Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
416 {
417         boost::shared_ptr<Playlist> pl (wpl.lock());
418
419         if (pl == _playlist) {
420
421                 /* this catches an ordering issue with session destruction. playlists
422                    are destroyed before diskstreams. we have to invalidate any handles
423                    we have to the playlist.
424                 */
425
426                 if (_playlist) {
427                         _playlist.reset ();
428                 }
429         }
430 }
431
432 bool
433 Diskstream::set_name (const string& str)
434 {
435         if (_name != str) {
436                 assert(playlist());
437                 playlist()->set_name (str);
438                 SessionObject::set_name(str);
439         }
440         return true;
441 }
442
443 bool
444 Diskstream::set_write_source_name (const std::string& str) {
445         _write_source_name = str;
446         return true;
447 }
448
449 XMLNode&
450 Diskstream::get_state ()
451 {
452         XMLNode* node = new XMLNode ("Diskstream");
453         char buf[64];
454         LocaleGuard lg (X_("POSIX"));
455
456         node->add_property ("flags", enum_2_string (_flags));
457         node->add_property ("playlist", _playlist->name());
458         node->add_property("name", _name);
459         id().print (buf, sizeof (buf));
460         node->add_property("id", buf);
461         snprintf (buf, sizeof(buf), "%f", _visible_speed);
462         node->add_property ("speed", buf);
463         node->add_property ("capture-alignment", enum_2_string (_alignment_choice));
464
465         if (_extra_xml) {
466                 node->add_child_copy (*_extra_xml);
467         }
468
469         return *node;
470 }
471
472 int
473 Diskstream::set_state (const XMLNode& node, int /*version*/)
474 {
475         const XMLProperty* prop;
476
477         if ((prop = node.property ("name")) != 0) {
478                 _name = prop->value();
479         }
480
481         if (deprecated_io_node) {
482                 set_id (*deprecated_io_node);
483         } else {
484                 set_id (node);
485         }
486
487         if ((prop = node.property ("flags")) != 0) {
488                 _flags = Flag (string_2_enum (prop->value(), _flags));
489         }
490
491         if ((prop = node.property (X_("capture-alignment"))) != 0) {
492                 set_align_choice (AlignChoice (string_2_enum (prop->value(), _alignment_choice)), true);
493         } else {
494                 set_align_choice (Automatic, true);
495         }
496
497         if ((prop = node.property ("playlist")) == 0) {
498                 return -1;
499         }
500
501         if (find_and_use_playlist (prop->value())) {
502                 return -1;
503         }
504
505         if ((prop = node.property ("speed")) != 0) {
506                 double sp = atof (prop->value().c_str());
507
508                 if (realtime_set_speed (sp, false)) {
509                         non_realtime_set_speed ();
510                 }
511         }
512
513         return 0;
514 }
515
516 void
517 Diskstream::playlist_ranges_moved (list< Evoral::RangeMove<framepos_t> > const & movements_frames, bool from_undo)
518 {
519         /* If we're coming from an undo, it will have handled
520            automation undo (it must, since automation-follows-regions
521            can lose automation data).  Hence we can do nothing here.
522         */
523
524         if (from_undo) {
525                 return;
526         }
527
528         if (!_track || Config->get_automation_follows_regions () == false) {
529                 return;
530         }
531
532         list< Evoral::RangeMove<double> > movements;
533
534         for (list< Evoral::RangeMove<framepos_t> >::const_iterator i = movements_frames.begin();
535              i != movements_frames.end();
536              ++i) {
537
538                 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
539         }
540
541         /* move panner automation */
542         boost::shared_ptr<Pannable> pannable = _track->pannable();
543         Evoral::ControlSet::Controls& c (pannable->controls());
544
545         for (Evoral::ControlSet::Controls::iterator ci = c.begin(); ci != c.end(); ++ci) {
546                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
547                 if (!ac) {
548                         continue;
549                 }
550                 boost::shared_ptr<AutomationList> alist = ac->alist();
551
552                 XMLNode & before = alist->get_state ();
553                 bool const things_moved = alist->move_ranges (movements);
554                 if (things_moved) {
555                         _session.add_command (new MementoCommand<AutomationList> (
556                                                       *alist.get(), &before, &alist->get_state ()));
557                 }
558         }
559
560         /* move processor automation */
561         _track->foreach_processor (boost::bind (&Diskstream::move_processor_automation, this, _1, movements_frames));
562 }
563
564 void
565 Diskstream::move_processor_automation (boost::weak_ptr<Processor> p, list< Evoral::RangeMove<framepos_t> > const & movements_frames)
566 {
567         boost::shared_ptr<Processor> processor (p.lock ());
568         if (!processor) {
569                 return;
570         }
571
572         list< Evoral::RangeMove<double> > movements;
573         for (list< Evoral::RangeMove<framepos_t> >::const_iterator i = movements_frames.begin(); i != movements_frames.end(); ++i) {
574                 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
575         }
576
577         set<Evoral::Parameter> const a = processor->what_can_be_automated ();
578
579         for (set<Evoral::Parameter>::const_iterator i = a.begin (); i != a.end (); ++i) {
580                 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
581                 XMLNode & before = al->get_state ();
582                 bool const things_moved = al->move_ranges (movements);
583                 if (things_moved) {
584                         _session.add_command (
585                                 new MementoCommand<AutomationList> (
586                                         *al.get(), &before, &al->get_state ()
587                                         )
588                                 );
589                 }
590         }
591 }
592
593 void
594 Diskstream::check_record_status (framepos_t transport_frame, bool can_record)
595 {
596         int possibly_recording;
597         int rolling;
598         int change;
599         const int transport_rolling = 0x4;
600         const int track_rec_enabled = 0x2;
601         const int global_rec_enabled = 0x1;
602         const int fully_rec_enabled = (transport_rolling|track_rec_enabled|global_rec_enabled);
603
604         /* merge together the 3 factors that affect record status, and compute
605          * what has changed.
606          */
607
608         rolling = _session.transport_speed() != 0.0f;
609         possibly_recording = (rolling << 2) | ((int)record_enabled() << 1) | (int)can_record;
610         change = possibly_recording ^ last_possibly_recording;
611
612         if (possibly_recording == last_possibly_recording) {
613                 return;
614         }
615
616         framecnt_t existing_material_offset = _session.worst_playback_latency();
617
618         if (possibly_recording == fully_rec_enabled) {
619
620                 if (last_possibly_recording == fully_rec_enabled) {
621                         return;
622                 }
623
624                 capture_start_frame = _session.transport_frame();
625                 first_recordable_frame = capture_start_frame + _capture_offset;
626                 last_recordable_frame = max_framepos;
627
628                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: @ %7 (%9) FRF = %2 CSF = %4 CO = %5, EMO = %6 RD = %8 WOL %10 WTL %11\n",
629                                                                       name(), first_recordable_frame, last_recordable_frame, capture_start_frame,
630                                                                       _capture_offset,
631                                                                       existing_material_offset,
632                                                                       transport_frame,
633                                                                       _roll_delay,
634                                                                       _session.transport_frame(),
635                                                                       _session.worst_output_latency(),
636                                                                       _session.worst_track_latency()));
637
638
639                 if (_alignment_style == ExistingMaterial) {
640                         first_recordable_frame += existing_material_offset;
641                         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("\tshift FRF by EMO %1\n",
642                                                                               first_recordable_frame));
643                 }
644
645                 prepare_record_status (capture_start_frame);
646
647         } else {
648
649                 if (last_possibly_recording == fully_rec_enabled) {
650
651                         /* we were recording last time */
652
653                         if (change & transport_rolling) {
654
655                                 /* transport-change (stopped rolling): last_recordable_frame was set in ::prepare_to_stop(). We
656                                  * had to set it there because we likely rolled past the stopping point to declick out,
657                                  * and then backed up.
658                                  */
659
660                         } else {
661                                 /* punch out */
662
663                                 last_recordable_frame = _session.transport_frame() + _capture_offset;
664
665                                 if (_alignment_style == ExistingMaterial) {
666                                         last_recordable_frame += existing_material_offset;
667                                 }
668                         }
669                 }
670         }
671
672         last_possibly_recording = possibly_recording;
673 }
674
675 void
676 Diskstream::route_going_away ()
677 {
678         _io.reset ();
679 }
680
681 void
682 Diskstream::calculate_record_range (Evoral::OverlapType ot, framepos_t transport_frame, framecnt_t nframes,
683                                     framecnt_t & rec_nframes, framecnt_t & rec_offset)
684 {
685         switch (ot) {
686         case Evoral::OverlapNone:
687                 rec_nframes = 0;
688                 break;
689
690         case Evoral::OverlapInternal:
691                 /*     ----------    recrange
692                          |---|       transrange
693                 */
694                 rec_nframes = nframes;
695                 rec_offset = 0;
696                 break;
697
698         case Evoral::OverlapStart:
699                 /*    |--------|    recrange
700                 -----|          transrange
701                 */
702                 rec_nframes = transport_frame + nframes - first_recordable_frame;
703                 if (rec_nframes) {
704                         rec_offset = first_recordable_frame - transport_frame;
705                 }
706                 break;
707
708         case Evoral::OverlapEnd:
709                 /*    |--------|    recrange
710                          |--------  transrange
711                 */
712                 rec_nframes = last_recordable_frame - transport_frame;
713                 rec_offset = 0;
714                 break;
715
716         case Evoral::OverlapExternal:
717                 /*    |--------|    recrange
718                     --------------  transrange
719                 */
720                 rec_nframes = last_recordable_frame - first_recordable_frame;
721                 rec_offset = first_recordable_frame - transport_frame;
722                 break;
723         }
724
725         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 rec? %2 @ %3 (for %4) FRF %5 LRF %6 : rf %7 @ %8\n",
726                                                               _name, enum_2_string (ot), transport_frame, nframes,
727                                                               first_recordable_frame, last_recordable_frame, rec_nframes, rec_offset));
728 }
729
730 void
731 Diskstream::prepare_to_stop (framepos_t pos)
732 {
733         last_recordable_frame = pos + _capture_offset;
734 }
735
736 void
737 Diskstream::engage_record_enable ()
738 {
739         g_atomic_int_set (&_record_enabled, 1);
740 }
741
742 void
743 Diskstream::disengage_record_enable ()
744 {
745         g_atomic_int_set (&_record_enabled, 0);
746 }