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