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