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