Merge branch 'master' into windows
[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 XMLNode&
444 Diskstream::get_state ()
445 {
446         XMLNode* node = new XMLNode ("Diskstream");
447         char buf[64];
448         LocaleGuard lg (X_("POSIX"));
449
450         node->add_property ("flags", enum_2_string (_flags));
451         node->add_property ("playlist", _playlist->name());
452         node->add_property("name", _name);
453         id().print (buf, sizeof (buf));
454         node->add_property("id", buf);
455         snprintf (buf, sizeof(buf), "%f", _visible_speed);
456         node->add_property ("speed", buf);
457         node->add_property ("capture-alignment", enum_2_string (_alignment_choice));
458
459         if (_extra_xml) {
460                 node->add_child_copy (*_extra_xml);
461         }
462
463         return *node;
464 }
465
466 int
467 Diskstream::set_state (const XMLNode& node, int /*version*/)
468 {
469         const XMLProperty* prop;
470
471         if ((prop = node.property ("name")) != 0) {
472                 _name = prop->value();
473         }
474
475         if (deprecated_io_node) {
476                 set_id (*deprecated_io_node);
477         } else {
478                 set_id (node);
479         }
480
481         if ((prop = node.property ("flags")) != 0) {
482                 _flags = Flag (string_2_enum (prop->value(), _flags));
483         }
484
485         if ((prop = node.property (X_("capture-alignment"))) != 0) {
486                 set_align_choice (AlignChoice (string_2_enum (prop->value(), _alignment_choice)), true);
487         } else {
488                 set_align_choice (Automatic, true);
489         }
490
491         if ((prop = node.property ("playlist")) == 0) {
492                 return -1;
493         }
494
495         if (find_and_use_playlist (prop->value())) {
496                 return -1;
497         }
498
499         if ((prop = node.property ("speed")) != 0) {
500                 double sp = atof (prop->value().c_str());
501
502                 if (realtime_set_speed (sp, false)) {
503                         non_realtime_set_speed ();
504                 }
505         }
506
507         return 0;
508 }
509
510 void
511 Diskstream::playlist_ranges_moved (list< Evoral::RangeMove<framepos_t> > const & movements_frames, bool from_undo)
512 {
513         /* If we're coming from an undo, it will have handled
514            automation undo (it must, since automation-follows-regions
515            can lose automation data).  Hence we can do nothing here.
516         */
517
518         if (from_undo) {
519                 return;
520         }
521
522         if (!_track || Config->get_automation_follows_regions () == false) {
523                 return;
524         }
525
526         list< Evoral::RangeMove<double> > movements;
527
528         for (list< Evoral::RangeMove<framepos_t> >::const_iterator i = movements_frames.begin();
529              i != movements_frames.end();
530              ++i) {
531
532                 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
533         }
534
535         /* move panner automation */
536         boost::shared_ptr<Pannable> pannable = _track->pannable();
537         Evoral::ControlSet::Controls& c (pannable->controls());
538
539         for (Evoral::ControlSet::Controls::iterator ci = c.begin(); ci != c.end(); ++ci) {
540                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
541                 if (!ac) {
542                         continue;
543                 }
544                 boost::shared_ptr<AutomationList> alist = ac->alist();
545
546                 XMLNode & before = alist->get_state ();
547                 bool const things_moved = alist->move_ranges (movements);
548                 if (things_moved) {
549                         _session.add_command (new MementoCommand<AutomationList> (
550                                                       *alist.get(), &before, &alist->get_state ()));
551                 }
552         }
553
554         /* move processor automation */
555         _track->foreach_processor (boost::bind (&Diskstream::move_processor_automation, this, _1, movements_frames));
556 }
557
558 void
559 Diskstream::move_processor_automation (boost::weak_ptr<Processor> p, list< Evoral::RangeMove<framepos_t> > const & movements_frames)
560 {
561         boost::shared_ptr<Processor> processor (p.lock ());
562         if (!processor) {
563                 return;
564         }
565
566         list< Evoral::RangeMove<double> > movements;
567         for (list< Evoral::RangeMove<framepos_t> >::const_iterator i = movements_frames.begin(); i != movements_frames.end(); ++i) {
568                 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
569         }
570
571         set<Evoral::Parameter> const a = processor->what_can_be_automated ();
572
573         for (set<Evoral::Parameter>::const_iterator i = a.begin (); i != a.end (); ++i) {
574                 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
575                 XMLNode & before = al->get_state ();
576                 bool const things_moved = al->move_ranges (movements);
577                 if (things_moved) {
578                         _session.add_command (
579                                 new MementoCommand<AutomationList> (
580                                         *al.get(), &before, &al->get_state ()
581                                         )
582                                 );
583                 }
584         }
585 }
586
587 void
588 Diskstream::check_record_status (framepos_t transport_frame, bool can_record)
589 {
590         int possibly_recording;
591         int rolling;
592         int change;
593         const int transport_rolling = 0x4;
594         const int track_rec_enabled = 0x2;
595         const int global_rec_enabled = 0x1;
596         const int fully_rec_enabled = (transport_rolling|track_rec_enabled|global_rec_enabled);
597
598         /* merge together the 3 factors that affect record status, and compute
599            what has changed.
600         */
601
602         rolling = _session.transport_speed() != 0.0f;
603         possibly_recording = (rolling << 2) | ((int)record_enabled() << 1) | (int)can_record;
604         change = possibly_recording ^ last_possibly_recording;
605
606         if (possibly_recording == last_possibly_recording) {
607                 return;
608         }
609
610         framecnt_t existing_material_offset = _session.worst_playback_latency();
611
612         if (possibly_recording == fully_rec_enabled) {
613
614                 if (last_possibly_recording == fully_rec_enabled) {
615                         return;
616                 }
617
618                 capture_start_frame = _session.transport_frame();
619                 first_recordable_frame = capture_start_frame + _capture_offset;
620                 last_recordable_frame = max_framepos;
621
622                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: @ %7 (%9) FRF = %2 CSF = %4 CO = %5, EMO = %6 RD = %8 WOL %10 WTL %11\n",
623                                                                       name(), first_recordable_frame, last_recordable_frame, capture_start_frame,
624                                                                       _capture_offset,
625                                                                       existing_material_offset,
626                                                                       transport_frame,
627                                                                       _roll_delay,
628                                                                       _session.transport_frame(),
629                                                                       _session.worst_output_latency(),
630                                                                       _session.worst_track_latency()));
631
632
633                 if (_alignment_style == ExistingMaterial) {
634                         first_recordable_frame += existing_material_offset;
635                         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("\tshift FRF by EMO %1\n",
636                                                                               first_recordable_frame));
637                 }
638
639                 prepare_record_status (capture_start_frame);
640
641         } else {
642
643                 if (last_possibly_recording == fully_rec_enabled) {
644
645                         /* we were recording last time */
646
647                         if (change & transport_rolling) {
648
649                                 /* transport-change (stopped rolling): last_recordable_frame was set in ::prepare_to_stop(). We
650                                    had to set it there because we likely rolled past the stopping point to declick out,
651                                    and then backed up.
652                                  */
653
654                         } else {
655                                 /* punch out */
656
657                                 last_recordable_frame = _session.transport_frame() + _capture_offset;
658
659                                 if (_alignment_style == ExistingMaterial) {
660                                         last_recordable_frame += existing_material_offset;
661                                 }
662                         }
663                 }
664         }
665
666         last_possibly_recording = possibly_recording;
667 }
668
669 void
670 Diskstream::route_going_away ()
671 {
672         _io.reset ();
673 }
674
675 void
676 Diskstream::calculate_record_range (Evoral::OverlapType ot, framepos_t transport_frame, framecnt_t nframes,
677                                     framecnt_t & rec_nframes, framecnt_t & rec_offset)
678 {
679         switch (ot) {
680         case Evoral::OverlapNone:
681                 rec_nframes = 0;
682                 break;
683
684         case Evoral::OverlapInternal:
685                 /*     ----------    recrange
686                          |---|       transrange
687                 */
688                 rec_nframes = nframes;
689                 rec_offset = 0;
690                 break;
691
692         case Evoral::OverlapStart:
693                 /*    |--------|    recrange
694                 -----|          transrange
695                 */
696                 rec_nframes = transport_frame + nframes - first_recordable_frame;
697                 if (rec_nframes) {
698                         rec_offset = first_recordable_frame - transport_frame;
699                 }
700                 break;
701
702         case Evoral::OverlapEnd:
703                 /*    |--------|    recrange
704                          |--------  transrange
705                 */
706                 rec_nframes = last_recordable_frame - transport_frame;
707                 rec_offset = 0;
708                 break;
709
710         case Evoral::OverlapExternal:
711                 /*    |--------|    recrange
712                     --------------  transrange
713                 */
714                 rec_nframes = last_recordable_frame - first_recordable_frame;
715                 rec_offset = first_recordable_frame - transport_frame;
716                 break;
717         }
718
719         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 rec? %2 @ %3 (for %4) FRF %5 LRF %6 : rf %7 @ %8\n",
720                                                               _name, enum_2_string (ot), transport_frame, nframes,
721                                                               first_recordable_frame, last_recordable_frame, rec_nframes, rec_offset));
722 }
723
724 void
725 Diskstream::prepare_to_stop (framepos_t pos)
726 {
727         last_recordable_frame = pos + _capture_offset;
728 }
729
730 void
731 Diskstream::engage_record_enable ()
732 {
733         g_atomic_int_set (&_record_enabled, 1);
734 }
735
736 void
737 Diskstream::disengage_record_enable ()
738 {
739         g_atomic_int_set (&_record_enabled, 0);
740 }
741