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