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