Implement #2425: option for automation to follow region moves.
[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 #include <sigc++/bind.h>
35
36 #include <pbd/error.h>
37 #include <pbd/basename.h>
38 #include <glibmm/thread.h>
39 #include <pbd/xml++.h>
40 #include <pbd/memento_command.h>
41
42 #include <ardour/ardour.h>
43 #include <ardour/audioengine.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/playlist.h>
50 #include <ardour/cycle_timer.h>
51 #include <ardour/region.h>
52 #include <ardour/panner.h>
53
54 #include "i18n.h"
55 #include <locale.h>
56
57 using namespace std;
58 using namespace ARDOUR;
59 using namespace PBD;
60
61 /* XXX This goes uninitialized when there is no ~/.ardour3 directory.
62  * I can't figure out why, so this will do for now (just stole the
63  * default from configuration_vars.h).  0 is not a good value for
64  * allocating buffer sizes..
65  */
66 ARDOUR::nframes_t Diskstream::disk_io_chunk_frames = 1024 * 256;
67
68 sigc::signal<void>                Diskstream::DiskOverrun;
69 sigc::signal<void>                Diskstream::DiskUnderrun;
70
71 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
72         : SessionObject(sess, name)
73 {
74         init (flag);
75 }
76         
77 Diskstream::Diskstream (Session& sess, const XMLNode& node)
78         : SessionObject(sess, "unnamed diskstream")
79 {
80         init (Recordable);
81 }
82
83 void
84 Diskstream::init (Flag f)
85 {
86         _flags = f;
87         _io = 0;
88         _alignment_style = ExistingMaterial;
89         _persistent_alignment_style = ExistingMaterial;
90         first_input_change = true;
91         i_am_the_modifier = 0;
92         g_atomic_int_set (&_record_enabled, 0);
93         was_recording = false;
94         capture_start_frame = 0;
95         capture_captured = 0;
96         _visible_speed = 1.0f;
97         _actual_speed = 1.0f;
98         _buffer_reallocation_required = false;
99         _seek_required = false;
100         first_recordable_frame = max_frames;
101         last_recordable_frame = max_frames;
102         _roll_delay = 0;
103         _capture_offset = 0;
104         _processed = false;
105         _slaved = false;
106         adjust_capture_position = 0;
107         last_possibly_recording = 0;
108         loop_location = 0;
109         wrap_buffer_size = 0;
110         speed_buffer_size = 0;
111         last_phase = 0;
112         phi = (uint64_t) (0x1000000);
113         target_phi = phi;
114         file_frame = 0;
115         playback_sample = 0;
116         playback_distance = 0;
117         _read_data_count = 0;
118         _write_data_count = 0;
119         commit_should_unlock = false;
120
121         pending_overwrite = false;
122         overwrite_frame = 0;
123         overwrite_queued = false;
124         input_change_pending = NoChange;
125 }
126
127 Diskstream::~Diskstream ()
128 {
129         if (_playlist)
130                 _playlist->release ();
131 }
132
133 void
134 Diskstream::set_io (IO& io)
135 {
136         _io = &io;
137         set_align_style_from_io ();
138 }
139
140 void
141 Diskstream::handle_input_change (IOChange change, void *src)
142 {
143         Glib::Mutex::Lock lm (state_lock);
144
145         if (!(input_change_pending & change)) {
146                 input_change_pending = IOChange (input_change_pending|change);
147                 _session.request_input_change_handling ();
148         }
149 }
150
151 void
152 Diskstream::non_realtime_set_speed ()
153 {
154         if (_buffer_reallocation_required)
155         {
156                 Glib::Mutex::Lock lm (state_lock);
157                 allocate_temporary_buffers ();
158
159                 _buffer_reallocation_required = false;
160         }
161
162         if (_seek_required) {
163                 if (speed() != 1.0f || speed() != -1.0f) {
164                         seek ((nframes_t) (_session.transport_frame() * (double) speed()), true);
165                 }
166                 else {
167                         seek (_session.transport_frame(), true);
168                 }
169
170                 _seek_required = false;
171         }
172 }
173
174 bool
175 Diskstream::realtime_set_speed (double sp, bool global)
176 {
177         bool changed = false;
178         double new_speed = sp * _session.transport_speed();
179         
180         if (_visible_speed != sp) {
181                 _visible_speed = sp;
182                 changed = true;
183         }
184         
185         if (new_speed != _actual_speed) {
186                 
187                 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * 
188                                                                             fabs (new_speed)) + 1;
189                 
190                 if (required_wrap_size > wrap_buffer_size) {
191                         _buffer_reallocation_required = true;
192                 }
193                 
194                 _actual_speed = new_speed;
195                 target_phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
196         }
197
198         if (changed) {
199                 if (!global) {
200                         _seek_required = true;
201                 }
202                 SpeedChanged (); /* EMIT SIGNAL */
203         }
204
205         return _buffer_reallocation_required || _seek_required;
206 }
207
208 void
209 Diskstream::prepare ()
210 {
211         _processed = false;
212         playback_distance = 0;
213 }
214
215 void
216 Diskstream::recover ()
217 {
218         if (commit_should_unlock) {
219                 state_lock.unlock();
220         }
221         _processed = false;
222 }
223
224 void
225 Diskstream::set_capture_offset ()
226 {
227         if (_io == 0) {
228                 /* can't capture, so forget it */
229                 return;
230         }
231
232         _capture_offset = _io->input_latency();
233 }
234
235 void
236 Diskstream::set_align_style (AlignStyle a)
237 {
238         if (record_enabled() && _session.actively_recording()) {
239                 return;
240         }
241
242         if (a != _alignment_style) {
243                 _alignment_style = a;
244                 AlignmentStyleChanged ();
245         }
246 }
247
248 int
249 Diskstream::set_loop (Location *location)
250 {
251         if (location) {
252                 if (location->start() >= location->end()) {
253                         error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
254                         return -1;
255                 }
256         }
257
258         loop_location = location;
259
260          LoopSet (location); /* EMIT SIGNAL */
261         return 0;
262 }
263
264 ARDOUR::nframes_t
265 Diskstream::get_capture_start_frame (uint32_t n)
266 {
267         Glib::Mutex::Lock lm (capture_info_lock);
268
269         if (capture_info.size() > n) {
270                 return capture_info[n]->start;
271         }
272         else {
273                 return capture_start_frame;
274         }
275 }
276
277 ARDOUR::nframes_t
278 Diskstream::get_captured_frames (uint32_t n)
279 {
280         Glib::Mutex::Lock lm (capture_info_lock);
281
282         if (capture_info.size() > n) {
283                 return capture_info[n]->frames;
284         }
285         else {
286                 return capture_captured;
287         }
288 }
289
290 void
291 Diskstream::set_roll_delay (ARDOUR::nframes_t nframes)
292 {
293         _roll_delay = nframes;
294 }
295
296 void
297 Diskstream::set_speed (double sp)
298 {
299         _session.request_diskstream_speed (*this, sp);
300
301         /* to force a rebuffering at the right place */
302         playlist_modified();
303 }
304
305 int
306 Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
307 {
308         {
309                 Glib::Mutex::Lock lm (state_lock);
310
311                 if (playlist == _playlist) {
312                         return 0;
313                 }
314
315                 plmod_connection.disconnect ();
316                 plgone_connection.disconnect ();
317                 plregion_connection.disconnect ();
318
319                 if (_playlist) {
320                         _playlist->release();
321                 }
322                         
323                 _playlist = playlist;
324                 _playlist->use();
325
326                 if (!in_set_state && recordable()) {
327                         reset_write_sources (false);
328                 }
329                 
330                 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &Diskstream::playlist_modified));
331                 plgone_connection = _playlist->GoingAway.connect (bind (mem_fun (*this, &Diskstream::playlist_deleted), boost::weak_ptr<Playlist>(_playlist)));
332                 plregion_connection = _playlist->RangesMoved.connect (mem_fun (*this, &Diskstream::playlist_ranges_moved));
333         }
334
335         /* don't do this if we've already asked for it *or* if we are setting up
336            the diskstream for the very first time - the input changed handling will
337            take care of the buffer refill.
338         */
339
340         if (!overwrite_queued && !(_session.state_of_the_state() & Session::CannotSave)) {
341                 _session.request_overwrite_buffer (this);
342                 overwrite_queued = true;
343         }
344         
345         PlaylistChanged (); /* EMIT SIGNAL */
346         _session.set_dirty ();
347
348         return 0;
349 }
350
351 void
352 Diskstream::playlist_changed (Change ignored)
353 {
354         playlist_modified ();
355 }
356
357 void
358 Diskstream::playlist_modified ()
359 {
360         if (!i_am_the_modifier && !overwrite_queued) {
361                 _session.request_overwrite_buffer (this);
362                 overwrite_queued = true;
363         } 
364 }
365
366 void
367 Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
368 {
369         boost::shared_ptr<Playlist> pl (wpl.lock());
370
371         if (pl == _playlist) {
372
373                 /* this catches an ordering issue with session destruction. playlists 
374                    are destroyed before diskstreams. we have to invalidate any handles
375                    we have to the playlist.
376                 */
377                 
378                 if (_playlist) {
379                         _playlist.reset ();
380                 } 
381         }
382 }
383
384 bool
385 Diskstream::set_name (const string& str)
386 {
387         if (str != _name) {
388                 assert(playlist());
389                 playlist()->set_name (str);
390                 
391                 SessionObject::set_name(str);
392                 
393                 if (!in_set_state && recordable()) {
394                         /* rename existing capture files so that they have the correct name */
395                         return rename_write_sources ();
396                 } else {
397                         return false;
398                 }
399         }
400
401         return true;
402 }
403
404 void
405 Diskstream::remove_region_from_last_capture (boost::weak_ptr<Region> wregion)
406 {
407         boost::shared_ptr<Region> region (wregion.lock());
408
409         if (!region) {
410                 return;
411         }
412         
413         _last_capture_regions.remove (region);
414 }
415
416 void
417 Diskstream::playlist_ranges_moved (Evoral::RangeMoveList const & movements)
418 {
419         if (Config->get_automation_follows_regions () == false) {
420                 return;
421         }
422         
423         /* move gain automation */
424         boost::shared_ptr<AutomationList> gain_alist = _io->gain_control()->alist();
425         XMLNode & before = gain_alist->get_state ();
426         gain_alist->move_ranges (movements);
427         _session.add_command (
428                 new MementoCommand<AutomationList> (
429                         *gain_alist.get(), &before, &gain_alist->get_state ()
430                         )
431                 );
432         
433         /* move panner automation */
434         Panner & p = _io->panner ();
435         for (uint32_t i = 0; i < p.npanners (); ++i) {
436
437                 boost::shared_ptr<AutomationList> pan_alist = p.streampanner(i).pan_control()->alist();
438                 XMLNode & before = pan_alist->get_state ();
439                 pan_alist->move_ranges (movements);
440                 _session.add_command (
441                         new MementoCommand<AutomationList> (
442                                 *pan_alist.get(), &before, &pan_alist->get_state ()
443                                 )
444                         );
445         }
446
447         /* move processor automation */
448         /* XXX: ewww */
449         Route * route = dynamic_cast<Route*> (_io);
450         if (route) {
451                 route->foreach_processor (sigc::bind (sigc::mem_fun (*this, &Diskstream::move_processor_automation), movements));
452         }
453 }
454
455 void
456 Diskstream::move_processor_automation (boost::weak_ptr<Processor> p, Evoral::RangeMoveList const & movements)
457 {
458         boost::shared_ptr<Processor> processor (p.lock ());
459         if (!processor) {
460                 return;
461         }
462         
463         set<Evoral::Parameter> const a = processor->what_can_be_automated ();
464
465         for (set<Evoral::Parameter>::iterator i = a.begin (); i != a.end (); ++i) {
466                 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
467                 XMLNode & before = al->get_state ();
468                 al->move_ranges (movements);
469                 _session.add_command (
470                         new MementoCommand<AutomationList> (
471                                 *al.get(), &before, &al->get_state ()
472                                 )
473                         );
474         }
475 }
476