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