2 Copyright (C) 2000 Paul Davis
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.
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.
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.
34 #include <glibmm/fileutils.h>
35 #include <glibmm/miscutils.h>
37 #include "pbd/xml++.h"
38 #include "pbd/pthread_utils.h"
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"
49 using namespace ARDOUR;
53 bool AudioSource::_build_missing_peakfiles = false;
55 /** true if we want peakfiles (e.g. if we are displaying a GUI) */
56 bool AudioSource::_build_peakfiles = false;
60 AudioSource::AudioSource (Session& s, ustring name)
61 : Source (s, DataType::AUDIO, name)
68 _write_data_count = 0;
69 peak_leftover_cnt = 0;
70 peak_leftover_size = 0;
74 AudioSource::AudioSource (Session& s, const XMLNode& node)
83 _write_data_count = 0;
84 peak_leftover_cnt = 0;
85 peak_leftover_size = 0;
88 if (set_state (node, Stateful::loading_state_version)) {
89 throw failed_constructor();
93 AudioSource::~AudioSource ()
95 /* shouldn't happen but make sure we don't leak file descriptors anyway */
97 if (peak_leftover_cnt) {
98 cerr << "AudioSource destroyed with leftover peak data pending" << endl;
105 delete [] peak_leftovers;
109 AudioSource::get_state ()
111 XMLNode& node (Source::get_state());
113 if (_captured_for.length()) {
114 node.add_property ("captured-for", _captured_for);
121 AudioSource::set_state (const XMLNode& node, int /*version*/)
123 const XMLProperty* prop;
125 if ((prop = node.property ("captured-for")) != 0) {
126 _captured_for = prop->value();
133 AudioSource::length (sframes_t /*pos*/) const
139 AudioSource::update_length (sframes_t pos, sframes_t cnt)
141 if (pos + cnt > _length) {
147 /***********************************************************************
149 ***********************************************************************/
152 AudioSource::peaks_ready (boost::function<void()> doThisWhenReady, ScopedConnection& connect_here_if_not, EventLoop* event_loop) const
155 Glib::Mutex::Lock lm (_peaks_ready_lock);
157 /* check to see if the peak data is ready. if not
158 connect the slot while still holding the lock.
161 if (!(ret = _peaks_built)) {
162 PeaksReady.connect (connect_here_if_not, MISSING_INVALIDATOR, doThisWhenReady, event_loop);
169 AudioSource::touch_peakfile ()
173 if (stat (peakpath.c_str(), &statbuf) != 0 || statbuf.st_size == 0) {
179 tbuf.actime = statbuf.st_atime;
180 tbuf.modtime = time ((time_t) 0);
182 utime (peakpath.c_str(), &tbuf);
186 AudioSource::rename_peakfile (ustring newpath)
188 /* caller must hold _lock */
190 ustring oldpath = peakpath;
192 if (access (oldpath.c_str(), F_OK) == 0) {
193 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
194 error << string_compose (_("cannot rename peakfile for %1 from %2 to %3 (%4)"), _name, oldpath, newpath, strerror (errno)) << endmsg;
205 AudioSource::initialize_peakfile (bool newfile, ustring audio_path)
209 peakpath = peak_path (audio_path);
211 /* if the peak file should be there, but isn't .... */
213 if (!newfile && !Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) {
214 peakpath = find_broken_peakfile (peakpath, audio_path);
217 if (stat (peakpath.c_str(), &statbuf)) {
218 if (errno != ENOENT) {
219 /* it exists in the peaks dir, but there is some kind of error */
221 error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg;
225 /* peakfile does not exist */
227 _peaks_built = false;
231 /* we found it in the peaks dir, so check it out */
233 if (statbuf.st_size == 0 || ((nframes_t) statbuf.st_size < ((length(_timeline_position) / _FPP) * sizeof (PeakData)))) {
235 _peaks_built = false;
237 // Check if the audio file has changed since the peakfile was built.
238 struct stat stat_file;
239 int err = stat (audio_path.c_str(), &stat_file);
242 _peaks_built = false;
246 /* allow 6 seconds slop on checking peak vs. file times because of various
250 if (stat_file.st_mtime > statbuf.st_mtime && (stat_file.st_mtime - statbuf.st_mtime > 6)) {
251 _peaks_built = false;
255 _peak_byte_max = statbuf.st_size;
261 if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) {
262 build_peaks_from_scratch ();
269 AudioSource::read (Sample *dst, framepos_t start, framecnt_t cnt, int /*channel*/) const
271 Glib::Mutex::Lock lm (_lock);
272 return read_unlocked (dst, start, cnt);
276 AudioSource::write (Sample *dst, framecnt_t cnt)
278 Glib::Mutex::Lock lm (_lock);
279 /* any write makes the fill not removable */
280 _flags = Flag (_flags & ~Removable);
281 return write_unlocked (dst, cnt);
285 AudioSource::read_peaks (PeakData *peaks, framecnt_t npeaks, framepos_t start, framecnt_t cnt, double samples_per_visual_peak) const
287 return read_peaks_with_fpp (peaks, npeaks, start, cnt, samples_per_visual_peak, _FPP);
290 /** @param peaks Buffer to write peak data.
291 * @param npeaks Number of peaks to write.
295 AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t start, framecnt_t cnt,
296 double samples_per_visual_peak, framecnt_t samples_per_file_peak) const
298 Glib::Mutex::Lock lm (_lock);
300 double expected_peaks;
301 PeakData::PeakDatum xmax;
302 PeakData::PeakDatum xmin;
305 framecnt_t zero_fill = 0;
307 PeakData* staging = 0;
308 Sample* raw_staging = 0;
311 expected_peaks = (cnt / (double) samples_per_file_peak);
312 scale = npeaks/expected_peaks;
314 #undef DEBUG_READ_PEAKS
315 #ifdef DEBUG_READ_PEAKS
316 cerr << "======>RP: npeaks = " << npeaks
317 << " start = " << start
319 << " len = " << _length
320 << " samples_per_visual_peak =" << samples_per_visual_peak
321 << " expected was " << expected_peaks << " ... scale = " << scale
322 << " PD ptr = " << peaks
327 /* fix for near-end-of-file conditions */
329 if (cnt > _length - start) {
330 // cerr << "too close to end @ " << _length << " given " << start << " + " << cnt << endl;
331 cnt = _length - start;
332 framecnt_t old = npeaks;
333 npeaks = min ((framecnt_t) floor (cnt / samples_per_visual_peak), npeaks);
334 zero_fill = old - npeaks;
337 // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
341 #ifdef DEBUG_READ_PEAKS
342 cerr << "RAW DATA\n";
344 /* no scaling at all, just get the sample data and duplicate it for
345 both max and min peak values.
348 Sample* raw_staging = new Sample[cnt];
350 if (read_unlocked (raw_staging, start, cnt) != cnt) {
351 error << _("cannot read sample data for unscaled peak computation") << endmsg;
355 for (framecnt_t i = 0; i < npeaks; ++i) {
356 peaks[i].max = raw_staging[i];
357 peaks[i].min = raw_staging[i];
360 delete [] raw_staging;
366 off_t first_peak_byte = (start / samples_per_file_peak) * sizeof (PeakData);
368 /* open, read, close */
370 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
371 error << string_compose(_("AudioSource: cannot open peakpath (a) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
375 #ifdef DEBUG_READ_PEAKS
376 cerr << "DIRECT PEAKS\n";
379 nread = ::pread (_peakfile, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
382 if (nread != sizeof (PeakData) * npeaks) {
383 cerr << "AudioSource["
385 << "]: cannot read peaks from peakfile! (read only "
399 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
410 #ifdef DEBUG_READ_PEAKS
411 cerr << "DOWNSAMPLE\n";
415 - more frames-per-peak (lower resolution) than the peakfile, or to put it another way,
416 - less peaks than the peakfile holds for the same range
418 So, read a block into a staging area, and then downsample from there.
420 to avoid confusion, I'll refer to the requested peaks as visual_peaks and the peakfile peaks as stored_peaks
423 const framecnt_t chunksize = (framecnt_t) min (expected_peaks, 65536.0);
425 staging = new PeakData[chunksize];
427 /* compute the rounded up frame position */
429 framepos_t current_frame = start;
430 framepos_t current_stored_peak = (framepos_t) ceil (current_frame / (double) samples_per_file_peak);
431 framepos_t next_visual_peak = (framepos_t) ceil (current_frame / samples_per_visual_peak);
432 double next_visual_peak_frame = next_visual_peak * samples_per_visual_peak;
433 framepos_t stored_peak_before_next_visual_peak = (framepos_t) next_visual_peak_frame / samples_per_file_peak;
434 framecnt_t nvisual_peaks = 0;
435 framecnt_t stored_peaks_read = 0;
438 /* handle the case where the initial visual peak is on a pixel boundary */
440 current_stored_peak = min (current_stored_peak, stored_peak_before_next_visual_peak);
442 /* open ... close during out: handling */
444 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
445 error << string_compose(_("AudioSource: cannot open peakpath (b) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
450 while (nvisual_peaks < npeaks) {
452 if (i == stored_peaks_read) {
454 uint32_t start_byte = current_stored_peak * sizeof(PeakData);
455 tnp = min ((framecnt_t)(_length/samples_per_file_peak - current_stored_peak), (framecnt_t) expected_peaks);
456 to_read = min (chunksize, tnp);
458 #ifdef DEBUG_READ_PEAKS
459 cerr << "read " << sizeof (PeakData) * to_read << " from peakfile @ " << start_byte << endl;
462 if ((nread = ::pread (_peakfile, staging, sizeof (PeakData) * to_read, start_byte))
463 != sizeof (PeakData) * to_read) {
465 off_t fend = lseek (_peakfile, 0, SEEK_END);
467 cerr << "AudioSource["
469 << "]: cannot read peak data from peakfile ("
470 << (nread / sizeof(PeakData))
471 << " peaks instead of "
476 << " at start_byte = " << start_byte
477 << " _length = " << _length << " versus len = " << fend
478 << " expected maxpeaks = " << (_length - current_frame)/samples_per_file_peak
479 << " npeaks was " << npeaks
485 stored_peaks_read = nread / sizeof(PeakData);
491 while ((i < stored_peaks_read) && (current_stored_peak <= stored_peak_before_next_visual_peak)) {
493 xmax = max (xmax, staging[i].max);
494 xmin = min (xmin, staging[i].min);
496 ++current_stored_peak;
500 peaks[nvisual_peaks].max = xmax;
501 peaks[nvisual_peaks].min = xmin;
505 //next_visual_peak_frame = min ((next_visual_peak * samples_per_visual_peak), (next_visual_peak_frame+samples_per_visual_peak) );
506 next_visual_peak_frame = min ((double) start+cnt, (next_visual_peak_frame+samples_per_visual_peak) );
507 stored_peak_before_next_visual_peak = (uint32_t) next_visual_peak_frame / samples_per_file_peak;
511 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
518 #ifdef DEBUG_READ_PEAKS
519 cerr << "UPSAMPLE\n";
523 - less frames-per-peak (more resolution)
524 - more peaks than stored in the Peakfile
526 So, fetch data from the raw source, and generate peak
530 framecnt_t frames_read = 0;
531 framepos_t current_frame = start;
533 framecnt_t nvisual_peaks = 0;
534 framecnt_t chunksize = (framecnt_t) min (cnt, (framecnt_t) 4096);
535 raw_staging = new Sample[chunksize];
537 framepos_t frame_pos = start;
538 double pixel_pos = floor (frame_pos / samples_per_visual_peak);
539 double next_pixel_pos = ceil (frame_pos / samples_per_visual_peak);
540 double pixels_per_frame = 1.0 / samples_per_visual_peak;
545 while (nvisual_peaks < npeaks) {
547 if (i == frames_read) {
549 to_read = min (chunksize, (framecnt_t)(_length - current_frame));
551 if (current_frame >= _length) {
553 /* hmm, error condition - we've reached the end of the file
554 without generating all the peak data. cook up a zero-filled
555 data buffer and then use it. this is simpler than
556 adjusting zero_fill and npeaks and then breaking out of
560 memset (raw_staging, 0, sizeof (Sample) * chunksize);
564 to_read = min (chunksize, (_length - current_frame));
567 if ((frames_read = read_unlocked (raw_staging, current_frame, to_read)) == 0) {
568 error << string_compose(_("AudioSource[%1]: peak read - cannot read %2 samples at offset %3 of %4 (%5)"),
569 _name, to_read, current_frame, _length, strerror (errno))
578 xmax = max (xmax, raw_staging[i]);
579 xmin = min (xmin, raw_staging[i]);
582 pixel_pos += pixels_per_frame;
584 if (pixel_pos >= next_pixel_pos) {
586 peaks[nvisual_peaks].max = xmax;
587 peaks[nvisual_peaks].min = xmin;
592 next_pixel_pos = ceil (pixel_pos + 0.5);
597 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
604 if (_peakfile >= 0) {
609 delete [] raw_staging;
611 #ifdef DEBUG_READ_PEAKS
618 #undef DEBUG_PEAK_BUILD
621 AudioSource::build_peaks_from_scratch ()
625 const framecnt_t bufsize = 65536; // 256kB per disk read for mono data is about ideal
630 /* hold lock while building peaks */
632 Glib::Mutex::Lock lp (_lock);
634 if (prepare_for_peakfile_writes ()) {
638 framepos_t current_frame = 0;
639 framecnt_t cnt = _length;
641 _peaks_built = false;
642 buf = new Sample[bufsize];
646 framecnt_t frames_to_read = min (bufsize, cnt);
647 framecnt_t frames_read;
649 if ((frames_read = read_unlocked (buf, current_frame, frames_to_read)) != frames_to_read) {
650 error << string_compose(_("%1: could not write read raw data for peak computation (%2)"), _name, strerror (errno)) << endmsg;
651 done_with_peakfile_writes (false);
655 if (compute_and_write_peaks (buf, current_frame, frames_read, true, false, _FPP)) {
659 current_frame += frames_read;
668 done_with_peakfile_writes ((cnt == 0));
672 Glib::Mutex::Lock lm (_peaks_ready_lock);
675 PeaksReady (); /* EMIT SIGNAL */
682 unlink (peakpath.c_str());
691 AudioSource::prepare_for_peakfile_writes ()
693 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
694 error << string_compose(_("AudioSource: cannot open peakpath (c) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
701 AudioSource::done_with_peakfile_writes (bool done)
703 if (peak_leftover_cnt) {
704 compute_and_write_peaks (0, 0, 0, true, false, _FPP);
718 AudioSource::compute_and_write_peaks (Sample* buf, framepos_t first_frame, framecnt_t cnt,
719 bool force, bool intermediate_peaks_ready)
721 return compute_and_write_peaks (buf, first_frame, cnt, force, intermediate_peaks_ready, _FPP);
725 AudioSource::compute_and_write_peaks (Sample* buf, framepos_t first_frame, framecnt_t cnt,
726 bool force, bool intermediate_peaks_ready, framecnt_t fpp)
730 uint32_t peaks_computed;
731 PeakData* peakbuf = 0;
733 framepos_t current_frame;
734 framecnt_t frames_done;
735 const size_t blocksize = (128 * 1024);
736 off_t first_peak_byte;
739 prepare_for_peakfile_writes ();
743 if (peak_leftover_cnt) {
745 if (first_frame != peak_leftover_frame + peak_leftover_cnt) {
747 /* uh-oh, ::seek() since the last ::compute_and_write_peaks(),
748 and we have leftovers. flush a single peak (since the leftovers
749 never represent more than that, and restart.
754 x.min = peak_leftovers[0];
755 x.max = peak_leftovers[0];
757 off_t byte = (peak_leftover_frame / fpp) * sizeof (PeakData);
759 if (::pwrite (peakfile, &x, sizeof (PeakData), byte) != sizeof (PeakData)) {
760 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
764 _peak_byte_max = max (_peak_byte_max, (off_t) (byte + sizeof(PeakData)));
767 Glib::Mutex::Lock lm (_peaks_ready_lock);
768 PeakRangeReady (peak_leftover_frame, peak_leftover_cnt); /* EMIT SIGNAL */
769 if (intermediate_peaks_ready) {
770 PeaksReady (); /* EMIT SIGNAL */
774 /* left overs are done */
776 peak_leftover_cnt = 0;
780 /* else ... had leftovers, but they immediately preceed the new data, so just
781 merge them and compute.
784 /* make a new contiguous buffer containing leftovers and the new stuff */
786 to_do = cnt + peak_leftover_cnt;
787 buf2 = new Sample[to_do];
790 memcpy (buf2, peak_leftovers, peak_leftover_cnt * sizeof (Sample));
793 memcpy (buf2+peak_leftover_cnt, buf, cnt * sizeof (Sample));
795 /* no more leftovers */
796 peak_leftover_cnt = 0;
798 /* use the temporary buffer */
801 /* make sure that when we write into the peakfile, we startup where we left off */
803 first_frame = peak_leftover_frame;
809 peakbuf = new PeakData[(to_do/fpp)+1];
811 current_frame = first_frame;
816 /* if some frames were passed in (i.e. we're not flushing leftovers)
817 and there are less than fpp to do, save them till
821 if (force && (to_do < fpp)) {
822 /* keep the left overs around for next time */
824 if (peak_leftover_size < to_do) {
825 delete [] peak_leftovers;
826 peak_leftovers = new Sample[to_do];
827 peak_leftover_size = to_do;
829 memcpy (peak_leftovers, buf, to_do * sizeof (Sample));
830 peak_leftover_cnt = to_do;
831 peak_leftover_frame = current_frame;
838 framecnt_t this_time = min (fpp, to_do);
840 peakbuf[peaks_computed].max = buf[0];
841 peakbuf[peaks_computed].min = buf[0];
843 ARDOUR::find_peaks (buf+1, this_time-1, &peakbuf[peaks_computed].min, &peakbuf[peaks_computed].max);
848 frames_done += this_time;
849 current_frame += this_time;
852 first_peak_byte = (first_frame / fpp) * sizeof (PeakData);
854 if (can_truncate_peaks()) {
856 /* on some filesystems (ext3, at least) this helps to reduce fragmentation of
857 the peakfiles. its not guaranteed to do so, and even on ext3 (as of december 2006)
858 it does not cause single-extent allocation even for peakfiles of
859 less than BLOCKSIZE bytes. only call ftruncate if we'll make the file larger.
862 off_t endpos = lseek (peakfile, 0, SEEK_END);
863 off_t target_length = blocksize * ((first_peak_byte + blocksize + 1) / blocksize);
865 if (endpos < target_length) {
866 ftruncate (peakfile, target_length);
867 /* error doesn't actually matter though, so continue on without testing */
871 if (::pwrite (peakfile, peakbuf, sizeof (PeakData) * peaks_computed, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaks_computed)) {
872 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
876 _peak_byte_max = max (_peak_byte_max, (off_t) (first_peak_byte + sizeof(PeakData)*peaks_computed));
879 Glib::Mutex::Lock lm (_peaks_ready_lock);
880 PeakRangeReady (first_frame, frames_done); /* EMIT SIGNAL */
881 if (intermediate_peaks_ready) {
882 PeaksReady (); /* EMIT SIGNAL */
896 AudioSource::truncate_peakfile ()
899 error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor")
904 /* truncate the peakfile down to its natural length if necessary */
906 off_t end = lseek (peakfile, 0, SEEK_END);
908 if (end > _peak_byte_max) {
909 ftruncate (peakfile, _peak_byte_max);
914 AudioSource::available_peaks (double zoom_factor) const
916 if (zoom_factor < _FPP) {
917 return length(_timeline_position); // peak data will come from the audio file
920 /* peak data comes from peakfile, but the filesize might not represent
921 the valid data due to ftruncate optimizations, so use _peak_byte_max state.
922 XXX - there might be some atomicity issues here, we should probably add a lock,
923 but _peak_byte_max only monotonically increases after initialization.
926 off_t end = _peak_byte_max;
928 return (end/sizeof(PeakData)) * _FPP;