Add missed parent class call to Sequence::control_list_marked_dirty. Fixes #4335.
[ardour.git] / libs / ardour / audiosource.cc
1 /*
2     Copyright (C) 2000 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 <sys/stat.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <float.h>
25 #include <utime.h>
26 #include <cerrno>
27 #include <ctime>
28 #include <cmath>
29 #include <iomanip>
30 #include <fstream>
31 #include <algorithm>
32 #include <vector>
33
34 #include <glibmm/fileutils.h>
35 #include <glibmm/miscutils.h>
36
37 #include "pbd/xml++.h"
38 #include "pbd/pthread_utils.h"
39
40 #include "ardour/audiosource.h"
41 #include "ardour/audio_diskstream.h"
42 #include "ardour/cycle_timer.h"
43 #include "ardour/session.h"
44 #include "ardour/transient_detector.h"
45 #include "ardour/runtime_functions.h"
46
47 #include "i18n.h"
48
49 using namespace std;
50 using namespace ARDOUR;
51 using namespace PBD;
52
53 Glib::StaticMutex AudioSource::_level_buffer_lock = GLIBMM_STATIC_MUTEX_INIT;
54 vector<boost::shared_ptr<Sample> > AudioSource::_mixdown_buffers;
55 vector<boost::shared_ptr<gain_t> > AudioSource::_gain_buffers;
56 size_t AudioSource::_working_buffers_size = 0;
57 bool AudioSource::_build_missing_peakfiles = false;
58
59 /** true if we want peakfiles (e.g. if we are displaying a GUI) */
60 bool AudioSource::_build_peakfiles = false;
61
62 #define _FPP 256
63
64 AudioSource::AudioSource (Session& s, string name)
65         : Source (s, DataType::AUDIO, name)
66         , _length (0)
67 {
68         _peaks_built = false;
69         _peak_byte_max = 0;
70         _peakfile_descriptor = 0;
71         _read_data_count = 0;
72         _write_data_count = 0;
73         peak_leftover_cnt = 0;
74         peak_leftover_size = 0;
75         peak_leftovers = 0;
76 }
77
78 AudioSource::AudioSource (Session& s, const XMLNode& node)
79         : Source (s, node)
80         , _length (0)
81 {
82
83         _peaks_built = false;
84         _peak_byte_max = 0;
85         _peakfile_descriptor = 0;
86         _read_data_count = 0;
87         _write_data_count = 0;
88         peak_leftover_cnt = 0;
89         peak_leftover_size = 0;
90         peak_leftovers = 0;
91
92         if (set_state (node, Stateful::loading_state_version)) {
93                 throw failed_constructor();
94         }
95 }
96
97 AudioSource::~AudioSource ()
98 {
99         /* shouldn't happen but make sure we don't leak file descriptors anyway */
100
101         if (peak_leftover_cnt) {
102                 cerr << "AudioSource destroyed with leftover peak data pending" << endl;
103         }
104
105         delete _peakfile_descriptor;
106         delete [] peak_leftovers;
107 }
108
109 XMLNode&
110 AudioSource::get_state ()
111 {
112         XMLNode& node (Source::get_state());
113
114         if (_captured_for.length()) {
115                 node.add_property ("captured-for", _captured_for);
116         }
117
118         return node;
119 }
120
121 int
122 AudioSource::set_state (const XMLNode& node, int /*version*/)
123 {
124         const XMLProperty* prop;
125
126         if ((prop = node.property ("captured-for")) != 0) {
127                 _captured_for = prop->value();
128         }
129
130         return 0;
131 }
132
133 bool
134 AudioSource::empty () const
135 {
136         return _length == 0;
137 }
138
139 framecnt_t
140 AudioSource::length (framepos_t /*pos*/) const
141 {
142         return _length;
143 }
144
145 void
146 AudioSource::update_length (framepos_t pos, framecnt_t cnt)
147 {
148         if (pos + cnt > _length) {
149                 _length = pos + cnt;
150         }
151 }
152
153
154 /***********************************************************************
155   PEAK FILE STUFF
156  ***********************************************************************/
157
158 /** Checks to see if peaks are ready.  If so, we return true.  If not, we return false, and
159  *  things are set up so that doThisWhenReady is called when the peaks are ready.
160  *  A new PBD::ScopedConnection is created for the associated connection and written to
161  *  *connect_here_if_not.
162  *
163  *  @param doThisWhenReady Function to call when peaks are ready (if they are not already).
164  *  @param connect_here_if_not Address to write new ScopedConnection to.
165  *  @param event_loop Event loop for doThisWhenReady to be called in.
166  */
167 bool
168 AudioSource::peaks_ready (boost::function<void()> doThisWhenReady, ScopedConnection** connect_here_if_not, EventLoop* event_loop) const
169 {
170         bool ret;
171         Glib::Mutex::Lock lm (_peaks_ready_lock);
172
173         if (!(ret = _peaks_built)) {
174                 *connect_here_if_not = new ScopedConnection;
175                 PeaksReady.connect (**connect_here_if_not, MISSING_INVALIDATOR, doThisWhenReady, event_loop);
176         }
177
178         return ret;
179 }
180
181 void
182 AudioSource::touch_peakfile ()
183 {
184         struct stat statbuf;
185
186         if (stat (peakpath.c_str(), &statbuf) != 0 || statbuf.st_size == 0) {
187                 return;
188         }
189
190         struct utimbuf tbuf;
191
192         tbuf.actime = statbuf.st_atime;
193         tbuf.modtime = time ((time_t) 0);
194
195         utime (peakpath.c_str(), &tbuf);
196 }
197
198 int
199 AudioSource::rename_peakfile (string newpath)
200 {
201         /* caller must hold _lock */
202
203         string oldpath = peakpath;
204
205         if (Glib::file_test (oldpath, Glib::FILE_TEST_EXISTS)) {
206                 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
207                         error << string_compose (_("cannot rename peakfile for %1 from %2 to %3 (%4)"), _name, oldpath, newpath, strerror (errno)) << endmsg;
208                         return -1;
209                 }
210         }
211
212         peakpath = newpath;
213
214         return 0;
215 }
216
217 int
218 AudioSource::initialize_peakfile (bool newfile, string audio_path)
219 {
220         struct stat statbuf;
221
222         peakpath = peak_path (audio_path);
223
224         /* if the peak file should be there, but isn't .... */
225
226         if (!newfile && !Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) {
227                 peakpath = find_broken_peakfile (peakpath, audio_path);
228         }
229
230         if (stat (peakpath.c_str(), &statbuf)) {
231                 if (errno != ENOENT) {
232                         /* it exists in the peaks dir, but there is some kind of error */
233
234                         error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg;
235                         return -1;
236                 }
237
238                 /* peakfile does not exist */
239
240                 _peaks_built = false;
241
242         } else {
243
244                 /* we found it in the peaks dir, so check it out */
245
246                 if (statbuf.st_size == 0 || (statbuf.st_size < (off_t) ((length(_timeline_position) / _FPP) * sizeof (PeakData)))) {
247                         // empty
248                         _peaks_built = false;
249                 } else {
250                         // Check if the audio file has changed since the peakfile was built.
251                         struct stat stat_file;
252                         int err = stat (audio_path.c_str(), &stat_file);
253
254                         if (err) {
255
256                                 /* no audio path - nested source or we can't
257                                    read it or ... whatever, use the peakfile as-is.
258                                 */
259
260                                 _peaks_built = true;
261                                 _peak_byte_max = statbuf.st_size;
262
263                         } else {
264
265                                 /* allow 6 seconds slop on checking peak vs. file times because of various
266                                    disk action "races"
267                                 */
268
269                                 if (stat_file.st_mtime > statbuf.st_mtime && (stat_file.st_mtime - statbuf.st_mtime > 6)) {
270                                         _peaks_built = false;
271                                         _peak_byte_max = 0;
272                                 } else {
273                                         _peaks_built = true;
274                                         _peak_byte_max = statbuf.st_size;
275                                 }
276                         }
277                 }
278         }
279
280         if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) {
281                 build_peaks_from_scratch ();
282         }
283
284         return 0;
285 }
286
287 framecnt_t
288 AudioSource::read (Sample *dst, framepos_t start, framecnt_t cnt, int /*channel*/) const
289 {
290         assert (cnt >= 0);
291         
292         Glib::Mutex::Lock lm (_lock);
293         return read_unlocked (dst, start, cnt);
294 }
295
296 framecnt_t
297 AudioSource::write (Sample *dst, framecnt_t cnt)
298 {
299         Glib::Mutex::Lock lm (_lock);
300         /* any write makes the fill not removable */
301         _flags = Flag (_flags & ~Removable);
302         return write_unlocked (dst, cnt);
303 }
304
305 int
306 AudioSource::read_peaks (PeakData *peaks, framecnt_t npeaks, framepos_t start, framecnt_t cnt, double samples_per_visual_peak) const
307 {
308         return read_peaks_with_fpp (peaks, npeaks, start, cnt, samples_per_visual_peak, _FPP);
309 }
310
311 /** @param peaks Buffer to write peak data.
312  *  @param npeaks Number of peaks to write.
313  */
314
315 int
316 AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t start, framecnt_t cnt,
317                                   double samples_per_visual_peak, framecnt_t samples_per_file_peak) const
318 {
319         Glib::Mutex::Lock lm (_lock);
320         double scale;
321         double expected_peaks;
322         PeakData::PeakDatum xmax;
323         PeakData::PeakDatum xmin;
324         int32_t to_read;
325         uint32_t nread;
326         framecnt_t zero_fill = 0;
327         int ret = -1;
328         PeakData* staging = 0;
329         Sample* raw_staging = 0;
330
331         FdFileDescriptor* peakfile_descriptor = new FdFileDescriptor (peakpath, false, 0664);
332         int peakfile_fd = -1;
333
334         expected_peaks = (cnt / (double) samples_per_file_peak);
335         scale = npeaks/expected_peaks;
336
337 #undef DEBUG_READ_PEAKS
338 #ifdef DEBUG_READ_PEAKS
339         cerr << "======>RP: npeaks = " << npeaks
340              << " start = " << start
341              << " cnt = " << cnt
342              << " len = " << _length
343              << "   samples_per_visual_peak =" << samples_per_visual_peak
344              << " expected was " << expected_peaks << " ... scale = " << scale
345              << " PD ptr = " << peaks
346              <<endl;
347
348 #endif
349
350         /* fix for near-end-of-file conditions */
351
352         if (cnt > _length - start) {
353                 // cerr << "too close to end @ " << _length << " given " << start << " + " << cnt << endl;
354                 cnt = _length - start;
355                 framecnt_t old = npeaks;
356                 npeaks = min ((framecnt_t) floor (cnt / samples_per_visual_peak), npeaks);
357                 zero_fill = old - npeaks;
358         }
359
360         // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
361
362         if (npeaks == cnt) {
363
364 #ifdef DEBUG_READ_PEAKS
365                 cerr << "RAW DATA\n";
366 #endif
367                 /* no scaling at all, just get the sample data and duplicate it for
368                    both max and min peak values.
369                 */
370
371                 Sample* raw_staging = new Sample[cnt];
372
373                 if (read_unlocked (raw_staging, start, cnt) != cnt) {
374                         error << _("cannot read sample data for unscaled peak computation") << endmsg;
375                         return -1;
376                 }
377
378                 for (framecnt_t i = 0; i < npeaks; ++i) {
379                         peaks[i].max = raw_staging[i];
380                         peaks[i].min = raw_staging[i];
381                 }
382
383                 delete peakfile_descriptor;
384                 delete [] raw_staging;
385                 return 0;
386         }
387
388         if (scale == 1.0) {
389
390                 off_t first_peak_byte = (start / samples_per_file_peak) * sizeof (PeakData);
391
392                 /* open, read, close */
393
394                 if ((peakfile_fd = peakfile_descriptor->allocate ()) < 0) {
395                         error << string_compose(_("AudioSource: cannot open peakpath (a) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
396                         delete peakfile_descriptor;
397                         return -1;
398                 }
399
400 #ifdef DEBUG_READ_PEAKS
401                 cerr << "DIRECT PEAKS\n";
402 #endif
403
404                 nread = ::pread (peakfile_fd, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
405                 delete peakfile_descriptor;
406
407                 if (nread != sizeof (PeakData) * npeaks) {
408                         cerr << "AudioSource["
409                              << _name
410                              << "]: cannot read peaks from peakfile! (read only "
411                              << nread
412                              << " not "
413                              << npeaks
414                               << "at sample "
415                              << start
416                              << " = byte "
417                              << first_peak_byte
418                              << ')'
419                              << endl;
420                         delete peakfile_descriptor;
421                         return -1;
422                 }
423
424                 if (zero_fill) {
425                         memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
426                 }
427
428                 delete peakfile_descriptor;
429                 return 0;
430         }
431
432
433         framecnt_t tnp;
434
435         if (scale < 1.0) {
436
437 #ifdef DEBUG_READ_PEAKS
438                 cerr << "DOWNSAMPLE\n";
439 #endif
440                 /* the caller wants:
441
442                     - more frames-per-peak (lower resolution) than the peakfile, or to put it another way,
443                     - less peaks than the peakfile holds for the same range
444
445                     So, read a block into a staging area, and then downsample from there.
446
447                     to avoid confusion, I'll refer to the requested peaks as visual_peaks and the peakfile peaks as stored_peaks
448                 */
449
450                 const framecnt_t chunksize = (framecnt_t) min (expected_peaks, 65536.0);
451
452                 staging = new PeakData[chunksize];
453
454                 /* compute the rounded up frame position  */
455
456                 framepos_t current_frame = start;
457                 framepos_t current_stored_peak = (framepos_t) ceil (current_frame / (double) samples_per_file_peak);
458                 framepos_t next_visual_peak  = (framepos_t) ceil (current_frame / samples_per_visual_peak);
459                 double     next_visual_peak_frame = next_visual_peak * samples_per_visual_peak;
460                 framepos_t stored_peak_before_next_visual_peak = (framepos_t) next_visual_peak_frame / samples_per_file_peak;
461                 framecnt_t nvisual_peaks = 0;
462                 framecnt_t stored_peaks_read = 0;
463                 framecnt_t i = 0;
464
465                 /* handle the case where the initial visual peak is on a pixel boundary */
466
467                 current_stored_peak = min (current_stored_peak, stored_peak_before_next_visual_peak);
468
469                 /* open ... close during out: handling */
470
471                 if ((peakfile_fd = peakfile_descriptor->allocate ()) < 0) {
472                         error << string_compose(_("AudioSource: cannot open peakpath (b) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
473                         delete peakfile_descriptor;
474                         delete [] staging;
475                         return 0;
476                 }
477
478                 while (nvisual_peaks < npeaks) {
479
480                         if (i == stored_peaks_read) {
481
482                                 uint32_t       start_byte = current_stored_peak * sizeof(PeakData);
483                                 tnp = min ((framecnt_t)(_length/samples_per_file_peak - current_stored_peak), (framecnt_t) expected_peaks);
484                                 to_read = min (chunksize, tnp);
485
486 #ifdef DEBUG_READ_PEAKS
487                                 cerr << "read " << sizeof (PeakData) * to_read << " from peakfile @ " << start_byte << endl;
488 #endif
489
490                                 if ((nread = ::pread (peakfile_fd, staging, sizeof (PeakData) * to_read, start_byte))
491                                     != sizeof (PeakData) * to_read) {
492
493                                         off_t fend = lseek (peakfile_fd, 0, SEEK_END);
494
495                                         cerr << "AudioSource["
496                                              << _name
497                                              << "]: cannot read peak data from peakfile ("
498                                              << (nread / sizeof(PeakData))
499                                              << " peaks instead of "
500                                              << to_read
501                                              << ") ("
502                                              << strerror (errno)
503                                              << ')'
504                                              << " at start_byte = " << start_byte
505                                              << " _length = " << _length << " versus len = " << fend
506                                              << " expected maxpeaks = " << (_length - current_frame)/samples_per_file_peak
507                                              << " npeaks was " << npeaks
508                                              << endl;
509                                         goto out;
510                                 }
511
512                                 i = 0;
513                                 stored_peaks_read = nread / sizeof(PeakData);
514                         }
515
516                         xmax = -1.0;
517                         xmin = 1.0;
518
519                         while ((i < stored_peaks_read) && (current_stored_peak <= stored_peak_before_next_visual_peak)) {
520
521                                 xmax = max (xmax, staging[i].max);
522                                 xmin = min (xmin, staging[i].min);
523                                 ++i;
524                                 ++current_stored_peak;
525                                 --expected_peaks;
526                         }
527
528                         peaks[nvisual_peaks].max = xmax;
529                         peaks[nvisual_peaks].min = xmin;
530                         ++nvisual_peaks;
531                         ++next_visual_peak;
532
533                         //next_visual_peak_frame = min ((next_visual_peak * samples_per_visual_peak), (next_visual_peak_frame+samples_per_visual_peak) );
534                         next_visual_peak_frame =  min ((double) start+cnt, (next_visual_peak_frame+samples_per_visual_peak) );
535                         stored_peak_before_next_visual_peak = (uint32_t) next_visual_peak_frame / samples_per_file_peak;
536                 }
537
538                 if (zero_fill) {
539                         memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
540                 }
541
542                 ret = 0;
543
544         } else {
545
546 #ifdef DEBUG_READ_PEAKS
547                 cerr << "UPSAMPLE\n";
548 #endif
549                 /* the caller wants
550
551                      - less frames-per-peak (more resolution)
552                      - more peaks than stored in the Peakfile
553
554                    So, fetch data from the raw source, and generate peak
555                    data on the fly.
556                 */
557
558                 framecnt_t frames_read = 0;
559                 framepos_t current_frame = start;
560                 framecnt_t i = 0;
561                 framecnt_t nvisual_peaks = 0;
562                 framecnt_t chunksize = (framecnt_t) min (cnt, (framecnt_t) 4096);
563                 raw_staging = new Sample[chunksize];
564
565                 framepos_t frame_pos = start;
566                 double pixel_pos = floor (frame_pos / samples_per_visual_peak);
567                 double next_pixel_pos = ceil (frame_pos / samples_per_visual_peak);
568                 double pixels_per_frame = 1.0 / samples_per_visual_peak;
569
570                 xmin = 1.0;
571                 xmax = -1.0;
572
573                 while (nvisual_peaks < npeaks) {
574
575                         if (i == frames_read) {
576
577                                 to_read = min (chunksize, (framecnt_t)(_length - current_frame));
578
579                                 if (current_frame >= _length) {
580
581                                         /* hmm, error condition - we've reached the end of the file
582                                            without generating all the peak data. cook up a zero-filled
583                                            data buffer and then use it. this is simpler than
584                                            adjusting zero_fill and npeaks and then breaking out of
585                                            this loop early
586                                         */
587
588                                         memset (raw_staging, 0, sizeof (Sample) * chunksize);
589
590                                 } else {
591
592                                         to_read = min (chunksize, (_length - current_frame));
593
594
595                                         if ((frames_read = read_unlocked (raw_staging, current_frame, to_read)) == 0) {
596                                                 error << string_compose(_("AudioSource[%1]: peak read - cannot read %2 samples at offset %3 of %4 (%5)"),
597                                                                         _name, to_read, current_frame, _length, strerror (errno))
598                                                       << endmsg;
599                                                 goto out;
600                                         }
601                                 }
602
603                                 i = 0;
604                         }
605
606                         xmax = max (xmax, raw_staging[i]);
607                         xmin = min (xmin, raw_staging[i]);
608                         ++i;
609                         ++current_frame;
610                         pixel_pos += pixels_per_frame;
611
612                         if (pixel_pos >= next_pixel_pos) {
613
614                                 peaks[nvisual_peaks].max = xmax;
615                                 peaks[nvisual_peaks].min = xmin;
616                                 ++nvisual_peaks;
617                                 xmin = 1.0;
618                                 xmax = -1.0;
619
620                                 next_pixel_pos = ceil (pixel_pos + 0.5);
621                         }
622                 }
623
624                 if (zero_fill) {
625                         memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
626                 }
627
628                 ret = 0;
629         }
630
631   out:
632         delete peakfile_descriptor;
633
634         delete [] staging;
635         delete [] raw_staging;
636
637 #ifdef DEBUG_READ_PEAKS
638         cerr << "RP DONE\n";
639 #endif
640
641         return ret;
642 }
643
644 #undef DEBUG_PEAK_BUILD
645
646 int
647 AudioSource::build_peaks_from_scratch ()
648 {
649         Sample* buf = 0;
650
651         const framecnt_t bufsize = 65536; // 256kB per disk read for mono data is about ideal
652
653         int ret = -1;
654
655         {
656                 /* hold lock while building peaks */
657
658                 Glib::Mutex::Lock lp (_lock);
659
660                 if (prepare_for_peakfile_writes ()) {
661                         goto out;
662                 }
663
664                 framecnt_t current_frame = 0;
665                 framecnt_t cnt = _length;
666
667                 _peaks_built = false;
668                 buf = new Sample[bufsize];
669
670                 while (cnt) {
671
672                         framecnt_t frames_to_read = min (bufsize, cnt);
673                         framecnt_t frames_read;
674                         
675                         if ((frames_read = read_unlocked (buf, current_frame, frames_to_read)) != frames_to_read) {
676                                 error << string_compose(_("%1: could not write read raw data for peak computation (%2)"), _name, strerror (errno)) << endmsg;
677                                 done_with_peakfile_writes (false);
678                                 goto out;
679                         }
680
681                         if (compute_and_write_peaks (buf, current_frame, frames_read, true, false, _FPP)) {
682                                 break;
683                         }
684
685                         current_frame += frames_read;
686                         cnt -= frames_read;
687                 }
688
689                 if (cnt == 0) {
690                         /* success */
691                         truncate_peakfile();
692                 }
693
694                 done_with_peakfile_writes ((cnt == 0));
695                 if (cnt == 0) {
696                         ret = 0;
697                 }
698         }
699
700   out:
701         if (ret) {
702                 unlink (peakpath.c_str());
703         }
704
705         delete [] buf;
706
707         return ret;
708 }
709
710 int
711 AudioSource::prepare_for_peakfile_writes ()
712 {
713         _peakfile_descriptor = new FdFileDescriptor (peakpath, true, 0664);
714         if ((_peakfile_fd = _peakfile_descriptor->allocate()) < 0) {
715                 error << string_compose(_("AudioSource: cannot open peakpath (c) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
716                 return -1;
717         }
718         return 0;
719 }
720
721 void
722 AudioSource::done_with_peakfile_writes (bool done)
723 {
724         if (peak_leftover_cnt) {
725                 compute_and_write_peaks (0, 0, 0, true, false, _FPP);
726         }
727
728         if (done) {
729                 Glib::Mutex::Lock lm (_peaks_ready_lock);
730                 _peaks_built = true;
731                 PeaksReady (); /* EMIT SIGNAL */
732         }
733
734         delete _peakfile_descriptor;
735         _peakfile_descriptor = 0;
736 }
737
738 /** @param first_frame Offset from the source start of the first frame to process */
739 int
740 AudioSource::compute_and_write_peaks (Sample* buf, framecnt_t first_frame, framecnt_t cnt,
741                                       bool force, bool intermediate_peaks_ready)
742 {
743         return compute_and_write_peaks (buf, first_frame, cnt, force, intermediate_peaks_ready, _FPP);
744 }
745
746 int
747 AudioSource::compute_and_write_peaks (Sample* buf, framecnt_t first_frame, framecnt_t cnt,
748                                       bool force, bool intermediate_peaks_ready, framecnt_t fpp)
749 {
750         Sample* buf2 = 0;
751         framecnt_t to_do;
752         uint32_t  peaks_computed;
753         PeakData* peakbuf = 0;
754         int ret = -1;
755         framepos_t current_frame;
756         framecnt_t frames_done;
757         const size_t blocksize = (128 * 1024);
758         off_t first_peak_byte;
759
760         if (_peakfile_descriptor == 0) {
761                 prepare_for_peakfile_writes ();
762         }
763
764   restart:
765         if (peak_leftover_cnt) {
766
767                 if (first_frame != peak_leftover_frame + peak_leftover_cnt) {
768
769                         /* uh-oh, ::seek() since the last ::compute_and_write_peaks(),
770                            and we have leftovers. flush a single peak (since the leftovers
771                            never represent more than that, and restart.
772                         */
773
774                         PeakData x;
775
776                         x.min = peak_leftovers[0];
777                         x.max = peak_leftovers[0];
778
779                         off_t byte = (peak_leftover_frame / fpp) * sizeof (PeakData);
780
781                         if (::pwrite (_peakfile_fd, &x, sizeof (PeakData), byte) != sizeof (PeakData)) {
782                                 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
783                                 goto out;
784                         }
785
786                         _peak_byte_max = max (_peak_byte_max, (off_t) (byte + sizeof(PeakData)));
787
788                         {
789                                 Glib::Mutex::Lock lm (_peaks_ready_lock);
790                                 PeakRangeReady (peak_leftover_frame, peak_leftover_cnt); /* EMIT SIGNAL */
791                                 if (intermediate_peaks_ready) {
792                                         PeaksReady (); /* EMIT SIGNAL */
793                                 }
794                         }
795
796                         /* left overs are done */
797
798                         peak_leftover_cnt = 0;
799                         goto restart;
800                 }
801
802                 /* else ... had leftovers, but they immediately preceed the new data, so just
803                    merge them and compute.
804                 */
805
806                 /* make a new contiguous buffer containing leftovers and the new stuff */
807
808                 to_do = cnt + peak_leftover_cnt;
809                 buf2 = new Sample[to_do];
810
811                 /* the remnants */
812                 memcpy (buf2, peak_leftovers, peak_leftover_cnt * sizeof (Sample));
813
814                 /* the new stuff */
815                 memcpy (buf2+peak_leftover_cnt, buf, cnt * sizeof (Sample));
816
817                 /* no more leftovers */
818                 peak_leftover_cnt = 0;
819
820                 /* use the temporary buffer */
821                 buf = buf2;
822
823                 /* make sure that when we write into the peakfile, we startup where we left off */
824
825                 first_frame = peak_leftover_frame;
826
827         } else {
828                 to_do = cnt;
829         }
830
831         peakbuf = new PeakData[(to_do/fpp)+1];
832         peaks_computed = 0;
833         current_frame = first_frame;
834         frames_done = 0;
835
836         while (to_do) {
837
838                 /* if some frames were passed in (i.e. we're not flushing leftovers)
839                    and there are less than fpp to do, save them till
840                    next time
841                 */
842
843                 if (force && (to_do < fpp)) {
844                         /* keep the left overs around for next time */
845
846                         if (peak_leftover_size < to_do) {
847                                 delete [] peak_leftovers;
848                                 peak_leftovers = new Sample[to_do];
849                                 peak_leftover_size = to_do;
850                         }
851                         memcpy (peak_leftovers, buf, to_do * sizeof (Sample));
852                         peak_leftover_cnt = to_do;
853                         peak_leftover_frame = current_frame;
854
855                         /* done for now */
856
857                         break;
858                 }
859
860                 framecnt_t this_time = min (fpp, to_do);
861
862                 peakbuf[peaks_computed].max = buf[0];
863                 peakbuf[peaks_computed].min = buf[0];
864
865                 ARDOUR::find_peaks (buf+1, this_time-1, &peakbuf[peaks_computed].min, &peakbuf[peaks_computed].max);
866
867                 peaks_computed++;
868                 buf += this_time;
869                 to_do -= this_time;
870                 frames_done += this_time;
871                 current_frame += this_time;
872         }
873
874         first_peak_byte = (first_frame / fpp) * sizeof (PeakData);
875
876         if (can_truncate_peaks()) {
877
878                 /* on some filesystems (ext3, at least) this helps to reduce fragmentation of
879                    the peakfiles. its not guaranteed to do so, and even on ext3 (as of december 2006)
880                    it does not cause single-extent allocation even for peakfiles of
881                    less than BLOCKSIZE bytes.  only call ftruncate if we'll make the file larger.
882                 */
883
884                 off_t endpos = lseek (_peakfile_fd, 0, SEEK_END);
885                 off_t target_length = blocksize * ((first_peak_byte + blocksize + 1) / blocksize);
886
887                 if (endpos < target_length) {
888                         (void) ftruncate (_peakfile_fd, target_length);
889                         /* error doesn't actually matter though, so continue on without testing */
890                 }
891         }
892
893         if (::pwrite (_peakfile_fd, peakbuf, sizeof (PeakData) * peaks_computed, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaks_computed)) {
894                 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
895                 goto out;
896         }
897
898         _peak_byte_max = max (_peak_byte_max, (off_t) (first_peak_byte + sizeof(PeakData)*peaks_computed));
899
900         if (frames_done) {
901                 Glib::Mutex::Lock lm (_peaks_ready_lock);
902                 PeakRangeReady (first_frame, frames_done); /* EMIT SIGNAL */
903                 if (intermediate_peaks_ready) {
904                         PeaksReady (); /* EMIT SIGNAL */
905                 }
906         }
907
908         ret = 0;
909
910   out:
911         delete [] peakbuf;
912         delete [] buf2;
913
914         return ret;
915 }
916
917 void
918 AudioSource::truncate_peakfile ()
919 {
920         if (_peakfile_descriptor == 0) {
921                 error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor")
922                       << endmsg;
923                 return;
924         }
925
926         /* truncate the peakfile down to its natural length if necessary */
927
928         off_t end = lseek (_peakfile_fd, 0, SEEK_END);
929
930         if (end > _peak_byte_max) {
931                 (void) ftruncate (_peakfile_fd, _peak_byte_max);
932         }
933 }
934
935 framecnt_t
936 AudioSource::available_peaks (double zoom_factor) const
937 {
938         if (zoom_factor < _FPP) {
939                 return length(_timeline_position); // peak data will come from the audio file
940         }
941
942         /* peak data comes from peakfile, but the filesize might not represent
943            the valid data due to ftruncate optimizations, so use _peak_byte_max state.
944            XXX - there might be some atomicity issues here, we should probably add a lock,
945            but _peak_byte_max only monotonically increases after initialization.
946         */
947
948         off_t end = _peak_byte_max;
949
950         return (end/sizeof(PeakData)) * _FPP;
951 }
952
953 void
954 AudioSource::dec_read_data_count (framecnt_t cnt)
955 {
956         uint32_t val = cnt * sizeof (Sample);
957
958         if (val < _read_data_count) {
959                 _read_data_count -= val;
960         } else {
961                 _read_data_count = 0;
962         }
963 }
964
965 void
966 AudioSource::mark_streaming_write_completed ()
967 {
968         Glib::Mutex::Lock lm (_peaks_ready_lock);
969
970         if (_peaks_built) {
971                 PeaksReady (); /* EMIT SIGNAL */
972         }
973 }
974
975 void
976 AudioSource::allocate_working_buffers (framecnt_t framerate)
977 {
978         Glib::Mutex::Lock lm (_level_buffer_lock);
979
980
981         /* Note: we don't need any buffers allocated until
982            a level 1 audiosource is created, at which
983            time we'll call ::ensure_buffers_for_level()
984            with the right value and do the right thing.
985         */
986
987         if (!_mixdown_buffers.empty()) {
988                 ensure_buffers_for_level_locked ( _mixdown_buffers.size(), framerate);
989         }
990 }
991
992 void
993 AudioSource::ensure_buffers_for_level (uint32_t level, framecnt_t frame_rate)
994 {
995         Glib::Mutex::Lock lm (_level_buffer_lock);
996         ensure_buffers_for_level_locked (level, frame_rate);
997 }
998
999 void
1000 AudioSource::ensure_buffers_for_level_locked (uint32_t level, framecnt_t frame_rate)
1001 {
1002         framecnt_t nframes = (framecnt_t) floor (Config->get_audio_playback_buffer_seconds() * frame_rate);
1003
1004         _mixdown_buffers.clear ();
1005         _gain_buffers.clear ();
1006
1007         while (_mixdown_buffers.size() < level) {
1008                 _mixdown_buffers.push_back (boost::shared_ptr<Sample> (new Sample[nframes]));
1009                 _gain_buffers.push_back (boost::shared_ptr<gain_t> (new gain_t[nframes]));
1010         }
1011 }