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