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 ***********************************************************************/
151 /** Checks to see if peaks are ready. If so, we return true. If not, we return false, and
152 * things are set up so that doThisWhenReady is called when the peaks are ready.
153 * A new PBD::ScopedConnection is created for the associated connection and written to
154 * *connect_here_if_not.
156 * @param doThisWhenReady Function to call when peaks are ready (if they are not already).
157 * @param connect_here_if_not Address to write new ScopedConnection to.
158 * @param event_loop Event loop for doThisWhenReady to be called in.
161 AudioSource::peaks_ready (boost::function<void()> doThisWhenReady, ScopedConnection** connect_here_if_not, EventLoop* event_loop) const
164 Glib::Mutex::Lock lm (_peaks_ready_lock);
166 /* check to see if the peak data is ready. if not
167 connect the slot while still holding the lock.
170 if (!(ret = _peaks_built)) {
171 *connect_here_if_not = new ScopedConnection;
172 PeaksReady.connect (**connect_here_if_not, MISSING_INVALIDATOR, doThisWhenReady, event_loop);
179 AudioSource::touch_peakfile ()
183 if (stat (peakpath.c_str(), &statbuf) != 0 || statbuf.st_size == 0) {
189 tbuf.actime = statbuf.st_atime;
190 tbuf.modtime = time ((time_t) 0);
192 utime (peakpath.c_str(), &tbuf);
196 AudioSource::rename_peakfile (ustring newpath)
198 /* caller must hold _lock */
200 ustring oldpath = peakpath;
202 if (access (oldpath.c_str(), F_OK) == 0) {
203 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
204 error << string_compose (_("cannot rename peakfile for %1 from %2 to %3 (%4)"), _name, oldpath, newpath, strerror (errno)) << endmsg;
215 AudioSource::initialize_peakfile (bool newfile, ustring audio_path)
219 peakpath = peak_path (audio_path);
221 /* if the peak file should be there, but isn't .... */
223 if (!newfile && !Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) {
224 peakpath = find_broken_peakfile (peakpath, audio_path);
227 if (stat (peakpath.c_str(), &statbuf)) {
228 if (errno != ENOENT) {
229 /* it exists in the peaks dir, but there is some kind of error */
231 error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg;
235 /* peakfile does not exist */
237 _peaks_built = false;
241 /* we found it in the peaks dir, so check it out */
243 if (statbuf.st_size == 0 || ((nframes_t) statbuf.st_size < ((length(_timeline_position) / _FPP) * sizeof (PeakData)))) {
245 _peaks_built = false;
247 // Check if the audio file has changed since the peakfile was built.
248 struct stat stat_file;
249 int err = stat (audio_path.c_str(), &stat_file);
252 _peaks_built = false;
256 /* allow 6 seconds slop on checking peak vs. file times because of various
260 if (stat_file.st_mtime > statbuf.st_mtime && (stat_file.st_mtime - statbuf.st_mtime > 6)) {
261 _peaks_built = false;
265 _peak_byte_max = statbuf.st_size;
271 if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) {
272 build_peaks_from_scratch ();
279 AudioSource::read (Sample *dst, framepos_t start, framecnt_t cnt, int /*channel*/) const
281 Glib::Mutex::Lock lm (_lock);
282 return read_unlocked (dst, start, cnt);
286 AudioSource::write (Sample *dst, framecnt_t cnt)
288 Glib::Mutex::Lock lm (_lock);
289 /* any write makes the fill not removable */
290 _flags = Flag (_flags & ~Removable);
291 return write_unlocked (dst, cnt);
295 AudioSource::read_peaks (PeakData *peaks, framecnt_t npeaks, framepos_t start, framecnt_t cnt, double samples_per_visual_peak) const
297 return read_peaks_with_fpp (peaks, npeaks, start, cnt, samples_per_visual_peak, _FPP);
300 /** @param peaks Buffer to write peak data.
301 * @param npeaks Number of peaks to write.
305 AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t start, framecnt_t cnt,
306 double samples_per_visual_peak, framecnt_t samples_per_file_peak) const
308 Glib::Mutex::Lock lm (_lock);
310 double expected_peaks;
311 PeakData::PeakDatum xmax;
312 PeakData::PeakDatum xmin;
315 framecnt_t zero_fill = 0;
317 PeakData* staging = 0;
318 Sample* raw_staging = 0;
321 expected_peaks = (cnt / (double) samples_per_file_peak);
322 scale = npeaks/expected_peaks;
324 #undef DEBUG_READ_PEAKS
325 #ifdef DEBUG_READ_PEAKS
326 cerr << "======>RP: npeaks = " << npeaks
327 << " start = " << start
329 << " len = " << _length
330 << " samples_per_visual_peak =" << samples_per_visual_peak
331 << " expected was " << expected_peaks << " ... scale = " << scale
332 << " PD ptr = " << peaks
337 /* fix for near-end-of-file conditions */
339 if (cnt > _length - start) {
340 // cerr << "too close to end @ " << _length << " given " << start << " + " << cnt << endl;
341 cnt = _length - start;
342 framecnt_t old = npeaks;
343 npeaks = min ((framecnt_t) floor (cnt / samples_per_visual_peak), npeaks);
344 zero_fill = old - npeaks;
347 // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
351 #ifdef DEBUG_READ_PEAKS
352 cerr << "RAW DATA\n";
354 /* no scaling at all, just get the sample data and duplicate it for
355 both max and min peak values.
358 Sample* raw_staging = new Sample[cnt];
360 if (read_unlocked (raw_staging, start, cnt) != cnt) {
361 error << _("cannot read sample data for unscaled peak computation") << endmsg;
365 for (framecnt_t i = 0; i < npeaks; ++i) {
366 peaks[i].max = raw_staging[i];
367 peaks[i].min = raw_staging[i];
370 delete [] raw_staging;
376 off_t first_peak_byte = (start / samples_per_file_peak) * sizeof (PeakData);
378 /* open, read, close */
380 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
381 error << string_compose(_("AudioSource: cannot open peakpath (a) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
385 #ifdef DEBUG_READ_PEAKS
386 cerr << "DIRECT PEAKS\n";
389 nread = ::pread (_peakfile, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
392 if (nread != sizeof (PeakData) * npeaks) {
393 cerr << "AudioSource["
395 << "]: cannot read peaks from peakfile! (read only "
409 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
420 #ifdef DEBUG_READ_PEAKS
421 cerr << "DOWNSAMPLE\n";
425 - more frames-per-peak (lower resolution) than the peakfile, or to put it another way,
426 - less peaks than the peakfile holds for the same range
428 So, read a block into a staging area, and then downsample from there.
430 to avoid confusion, I'll refer to the requested peaks as visual_peaks and the peakfile peaks as stored_peaks
433 const framecnt_t chunksize = (framecnt_t) min (expected_peaks, 65536.0);
435 staging = new PeakData[chunksize];
437 /* compute the rounded up frame position */
439 framepos_t current_frame = start;
440 framepos_t current_stored_peak = (framepos_t) ceil (current_frame / (double) samples_per_file_peak);
441 framepos_t next_visual_peak = (framepos_t) ceil (current_frame / samples_per_visual_peak);
442 double next_visual_peak_frame = next_visual_peak * samples_per_visual_peak;
443 framepos_t stored_peak_before_next_visual_peak = (framepos_t) next_visual_peak_frame / samples_per_file_peak;
444 framecnt_t nvisual_peaks = 0;
445 framecnt_t stored_peaks_read = 0;
448 /* handle the case where the initial visual peak is on a pixel boundary */
450 current_stored_peak = min (current_stored_peak, stored_peak_before_next_visual_peak);
452 /* open ... close during out: handling */
454 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
455 error << string_compose(_("AudioSource: cannot open peakpath (b) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
460 while (nvisual_peaks < npeaks) {
462 if (i == stored_peaks_read) {
464 uint32_t start_byte = current_stored_peak * sizeof(PeakData);
465 tnp = min ((framecnt_t)(_length/samples_per_file_peak - current_stored_peak), (framecnt_t) expected_peaks);
466 to_read = min (chunksize, tnp);
468 #ifdef DEBUG_READ_PEAKS
469 cerr << "read " << sizeof (PeakData) * to_read << " from peakfile @ " << start_byte << endl;
472 if ((nread = ::pread (_peakfile, staging, sizeof (PeakData) * to_read, start_byte))
473 != sizeof (PeakData) * to_read) {
475 off_t fend = lseek (_peakfile, 0, SEEK_END);
477 cerr << "AudioSource["
479 << "]: cannot read peak data from peakfile ("
480 << (nread / sizeof(PeakData))
481 << " peaks instead of "
486 << " at start_byte = " << start_byte
487 << " _length = " << _length << " versus len = " << fend
488 << " expected maxpeaks = " << (_length - current_frame)/samples_per_file_peak
489 << " npeaks was " << npeaks
495 stored_peaks_read = nread / sizeof(PeakData);
501 while ((i < stored_peaks_read) && (current_stored_peak <= stored_peak_before_next_visual_peak)) {
503 xmax = max (xmax, staging[i].max);
504 xmin = min (xmin, staging[i].min);
506 ++current_stored_peak;
510 peaks[nvisual_peaks].max = xmax;
511 peaks[nvisual_peaks].min = xmin;
515 //next_visual_peak_frame = min ((next_visual_peak * samples_per_visual_peak), (next_visual_peak_frame+samples_per_visual_peak) );
516 next_visual_peak_frame = min ((double) start+cnt, (next_visual_peak_frame+samples_per_visual_peak) );
517 stored_peak_before_next_visual_peak = (uint32_t) next_visual_peak_frame / samples_per_file_peak;
521 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
528 #ifdef DEBUG_READ_PEAKS
529 cerr << "UPSAMPLE\n";
533 - less frames-per-peak (more resolution)
534 - more peaks than stored in the Peakfile
536 So, fetch data from the raw source, and generate peak
540 framecnt_t frames_read = 0;
541 framepos_t current_frame = start;
543 framecnt_t nvisual_peaks = 0;
544 framecnt_t chunksize = (framecnt_t) min (cnt, (framecnt_t) 4096);
545 raw_staging = new Sample[chunksize];
547 framepos_t frame_pos = start;
548 double pixel_pos = floor (frame_pos / samples_per_visual_peak);
549 double next_pixel_pos = ceil (frame_pos / samples_per_visual_peak);
550 double pixels_per_frame = 1.0 / samples_per_visual_peak;
555 while (nvisual_peaks < npeaks) {
557 if (i == frames_read) {
559 to_read = min (chunksize, (framecnt_t)(_length - current_frame));
561 if (current_frame >= _length) {
563 /* hmm, error condition - we've reached the end of the file
564 without generating all the peak data. cook up a zero-filled
565 data buffer and then use it. this is simpler than
566 adjusting zero_fill and npeaks and then breaking out of
570 memset (raw_staging, 0, sizeof (Sample) * chunksize);
574 to_read = min (chunksize, (_length - current_frame));
577 if ((frames_read = read_unlocked (raw_staging, current_frame, to_read)) == 0) {
578 error << string_compose(_("AudioSource[%1]: peak read - cannot read %2 samples at offset %3 of %4 (%5)"),
579 _name, to_read, current_frame, _length, strerror (errno))
588 xmax = max (xmax, raw_staging[i]);
589 xmin = min (xmin, raw_staging[i]);
592 pixel_pos += pixels_per_frame;
594 if (pixel_pos >= next_pixel_pos) {
596 peaks[nvisual_peaks].max = xmax;
597 peaks[nvisual_peaks].min = xmin;
602 next_pixel_pos = ceil (pixel_pos + 0.5);
607 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
614 if (_peakfile >= 0) {
619 delete [] raw_staging;
621 #ifdef DEBUG_READ_PEAKS
628 #undef DEBUG_PEAK_BUILD
631 AudioSource::build_peaks_from_scratch ()
635 const framecnt_t bufsize = 65536; // 256kB per disk read for mono data is about ideal
640 /* hold lock while building peaks */
642 Glib::Mutex::Lock lp (_lock);
644 if (prepare_for_peakfile_writes ()) {
648 framepos_t current_frame = 0;
649 framecnt_t cnt = _length;
651 _peaks_built = false;
652 buf = new Sample[bufsize];
656 framecnt_t frames_to_read = min (bufsize, cnt);
657 framecnt_t frames_read;
659 if ((frames_read = read_unlocked (buf, current_frame, frames_to_read)) != frames_to_read) {
660 error << string_compose(_("%1: could not write read raw data for peak computation (%2)"), _name, strerror (errno)) << endmsg;
661 done_with_peakfile_writes (false);
665 if (compute_and_write_peaks (buf, current_frame, frames_read, true, false, _FPP)) {
669 current_frame += frames_read;
678 done_with_peakfile_writes ((cnt == 0));
682 Glib::Mutex::Lock lm (_peaks_ready_lock);
685 PeaksReady (); /* EMIT SIGNAL */
692 unlink (peakpath.c_str());
701 AudioSource::prepare_for_peakfile_writes ()
703 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
704 error << string_compose(_("AudioSource: cannot open peakpath (c) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
711 AudioSource::done_with_peakfile_writes (bool done)
713 if (peak_leftover_cnt) {
714 compute_and_write_peaks (0, 0, 0, true, false, _FPP);
728 AudioSource::compute_and_write_peaks (Sample* buf, framepos_t first_frame, framecnt_t cnt,
729 bool force, bool intermediate_peaks_ready)
731 return compute_and_write_peaks (buf, first_frame, cnt, force, intermediate_peaks_ready, _FPP);
735 AudioSource::compute_and_write_peaks (Sample* buf, framepos_t first_frame, framecnt_t cnt,
736 bool force, bool intermediate_peaks_ready, framecnt_t fpp)
740 uint32_t peaks_computed;
741 PeakData* peakbuf = 0;
743 framepos_t current_frame;
744 framecnt_t frames_done;
745 const size_t blocksize = (128 * 1024);
746 off_t first_peak_byte;
749 prepare_for_peakfile_writes ();
753 if (peak_leftover_cnt) {
755 if (first_frame != peak_leftover_frame + peak_leftover_cnt) {
757 /* uh-oh, ::seek() since the last ::compute_and_write_peaks(),
758 and we have leftovers. flush a single peak (since the leftovers
759 never represent more than that, and restart.
764 x.min = peak_leftovers[0];
765 x.max = peak_leftovers[0];
767 off_t byte = (peak_leftover_frame / fpp) * sizeof (PeakData);
769 if (::pwrite (peakfile, &x, sizeof (PeakData), byte) != sizeof (PeakData)) {
770 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
774 _peak_byte_max = max (_peak_byte_max, (off_t) (byte + sizeof(PeakData)));
777 Glib::Mutex::Lock lm (_peaks_ready_lock);
778 PeakRangeReady (peak_leftover_frame, peak_leftover_cnt); /* EMIT SIGNAL */
779 if (intermediate_peaks_ready) {
780 PeaksReady (); /* EMIT SIGNAL */
784 /* left overs are done */
786 peak_leftover_cnt = 0;
790 /* else ... had leftovers, but they immediately preceed the new data, so just
791 merge them and compute.
794 /* make a new contiguous buffer containing leftovers and the new stuff */
796 to_do = cnt + peak_leftover_cnt;
797 buf2 = new Sample[to_do];
800 memcpy (buf2, peak_leftovers, peak_leftover_cnt * sizeof (Sample));
803 memcpy (buf2+peak_leftover_cnt, buf, cnt * sizeof (Sample));
805 /* no more leftovers */
806 peak_leftover_cnt = 0;
808 /* use the temporary buffer */
811 /* make sure that when we write into the peakfile, we startup where we left off */
813 first_frame = peak_leftover_frame;
819 peakbuf = new PeakData[(to_do/fpp)+1];
821 current_frame = first_frame;
826 /* if some frames were passed in (i.e. we're not flushing leftovers)
827 and there are less than fpp to do, save them till
831 if (force && (to_do < fpp)) {
832 /* keep the left overs around for next time */
834 if (peak_leftover_size < to_do) {
835 delete [] peak_leftovers;
836 peak_leftovers = new Sample[to_do];
837 peak_leftover_size = to_do;
839 memcpy (peak_leftovers, buf, to_do * sizeof (Sample));
840 peak_leftover_cnt = to_do;
841 peak_leftover_frame = current_frame;
848 framecnt_t this_time = min (fpp, to_do);
850 peakbuf[peaks_computed].max = buf[0];
851 peakbuf[peaks_computed].min = buf[0];
853 ARDOUR::find_peaks (buf+1, this_time-1, &peakbuf[peaks_computed].min, &peakbuf[peaks_computed].max);
858 frames_done += this_time;
859 current_frame += this_time;
862 first_peak_byte = (first_frame / fpp) * sizeof (PeakData);
864 if (can_truncate_peaks()) {
866 /* on some filesystems (ext3, at least) this helps to reduce fragmentation of
867 the peakfiles. its not guaranteed to do so, and even on ext3 (as of december 2006)
868 it does not cause single-extent allocation even for peakfiles of
869 less than BLOCKSIZE bytes. only call ftruncate if we'll make the file larger.
872 off_t endpos = lseek (peakfile, 0, SEEK_END);
873 off_t target_length = blocksize * ((first_peak_byte + blocksize + 1) / blocksize);
875 if (endpos < target_length) {
876 ftruncate (peakfile, target_length);
877 /* error doesn't actually matter though, so continue on without testing */
881 if (::pwrite (peakfile, peakbuf, sizeof (PeakData) * peaks_computed, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaks_computed)) {
882 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
886 _peak_byte_max = max (_peak_byte_max, (off_t) (first_peak_byte + sizeof(PeakData)*peaks_computed));
889 Glib::Mutex::Lock lm (_peaks_ready_lock);
890 PeakRangeReady (first_frame, frames_done); /* EMIT SIGNAL */
891 if (intermediate_peaks_ready) {
892 PeaksReady (); /* EMIT SIGNAL */
906 AudioSource::truncate_peakfile ()
909 error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor")
914 /* truncate the peakfile down to its natural length if necessary */
916 off_t end = lseek (peakfile, 0, SEEK_END);
918 if (end > _peak_byte_max) {
919 ftruncate (peakfile, _peak_byte_max);
924 AudioSource::available_peaks (double zoom_factor) const
926 if (zoom_factor < _FPP) {
927 return length(_timeline_position); // peak data will come from the audio file
930 /* peak data comes from peakfile, but the filesize might not represent
931 the valid data due to ftruncate optimizations, so use _peak_byte_max state.
932 XXX - there might be some atomicity issues here, we should probably add a lock,
933 but _peak_byte_max only monotonically increases after initialization.
936 off_t end = _peak_byte_max;
938 return (end/sizeof(PeakData)) * _FPP;