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;
54 bool AudioSource::_build_peakfiles = false;
58 AudioSource::AudioSource (Session& s, ustring name)
59 : Source (s, DataType::AUDIO, name)
66 _write_data_count = 0;
67 peak_leftover_cnt = 0;
68 peak_leftover_size = 0;
72 AudioSource::AudioSource (Session& s, const XMLNode& node)
81 _write_data_count = 0;
82 peak_leftover_cnt = 0;
83 peak_leftover_size = 0;
86 if (set_state (node)) {
87 throw failed_constructor();
91 AudioSource::~AudioSource ()
93 /* shouldn't happen but make sure we don't leak file descriptors anyway */
95 if (peak_leftover_cnt) {
96 cerr << "AudioSource destroyed with leftover peak data pending" << endl;
103 delete [] peak_leftovers;
107 AudioSource::get_state ()
109 XMLNode& node (Source::get_state());
111 if (_captured_for.length()) {
112 node.add_property ("captured-for", _captured_for);
119 AudioSource::set_state (const XMLNode& node)
121 const XMLProperty* prop;
123 if ((prop = node.property ("captured-for")) != 0) {
124 _captured_for = prop->value();
131 AudioSource::length (sframes_t pos) const
137 AudioSource::update_length (sframes_t pos, sframes_t cnt)
139 if (pos + cnt > _length) {
145 /***********************************************************************
147 ***********************************************************************/
150 AudioSource::peaks_ready (sigc::slot<void> the_slot, sigc::connection& conn) const
153 Glib::Mutex::Lock lm (_peaks_ready_lock);
155 /* check to see if the peak data is ready. if not
156 connect the slot while still holding the lock.
159 if (!(ret = _peaks_built)) {
160 conn = PeaksReady.connect (the_slot);
167 AudioSource::touch_peakfile ()
171 if (stat (peakpath.c_str(), &statbuf) != 0 || statbuf.st_size == 0) {
177 tbuf.actime = statbuf.st_atime;
178 tbuf.modtime = time ((time_t) 0);
180 utime (peakpath.c_str(), &tbuf);
184 AudioSource::rename_peakfile (ustring newpath)
186 /* caller must hold _lock */
188 ustring oldpath = peakpath;
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;
203 AudioSource::initialize_peakfile (bool newfile, ustring audio_path)
207 peakpath = peak_path (audio_path);
209 /* if the peak file should be there, but isn't .... */
211 if (!newfile && !Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) {
212 peakpath = find_broken_peakfile (peakpath, audio_path);
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 */
219 error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg;
223 /* peakfile does not exist */
225 _peaks_built = false;
229 /* we found it in the peaks dir, so check it out */
231 if (statbuf.st_size == 0 || ((nframes_t) statbuf.st_size < ((length(_timeline_position) / _FPP) * sizeof (PeakData)))) {
233 _peaks_built = false;
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);
240 _peaks_built = false;
244 /* allow 6 seconds slop on checking peak vs. file times because of various
248 if (stat_file.st_mtime > statbuf.st_mtime && (stat_file.st_mtime - statbuf.st_mtime > 6)) {
249 _peaks_built = false;
253 _peak_byte_max = statbuf.st_size;
259 if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) {
260 build_peaks_from_scratch ();
267 AudioSource::read (Sample *dst, sframes_t start, nframes_t cnt, int channel) const
269 Glib::Mutex::Lock lm (_lock);
270 return read_unlocked (dst, start, cnt);
274 AudioSource::write (Sample *dst, nframes_t cnt)
276 Glib::Mutex::Lock lm (_lock);
277 return write_unlocked (dst, cnt);
281 AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, sframes_t start, nframes_t cnt, double samples_per_visual_peak) const
283 return read_peaks_with_fpp (peaks, npeaks, start, cnt, samples_per_visual_peak, _FPP);
286 /** @param peaks Buffer to write peak data.
287 * @param npeaks Number of peaks to write.
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
294 Glib::Mutex::Lock lm (_lock);
296 double expected_peaks;
297 PeakData::PeakDatum xmax;
298 PeakData::PeakDatum xmin;
301 nframes_t zero_fill = 0;
303 PeakData* staging = 0;
304 Sample* raw_staging = 0;
307 expected_peaks = (cnt / (double) samples_per_file_peak);
308 scale = npeaks/expected_peaks;
310 #undef DEBUG_READ_PEAKS
311 #ifdef DEBUG_READ_PEAKS
312 cerr << "======>RP: npeaks = " << npeaks
313 << " start = " << start
315 << " len = " << _length
316 << " samples_per_visual_peak =" << samples_per_visual_peak
317 << " expected was " << expected_peaks << " ... scale = " << scale
318 << " PD ptr = " << peaks
323 /* fix for near-end-of-file conditions */
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;
333 // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
337 #ifdef DEBUG_READ_PEAKS
338 cerr << "RAW DATA\n";
340 /* no scaling at all, just get the sample data and duplicate it for
341 both max and min peak values.
344 Sample* raw_staging = new Sample[cnt];
346 if (read_unlocked (raw_staging, start, cnt) != cnt) {
347 error << _("cannot read sample data for unscaled peak computation") << endmsg;
351 for (nframes_t i = 0; i < npeaks; ++i) {
352 peaks[i].max = raw_staging[i];
353 peaks[i].min = raw_staging[i];
356 delete [] raw_staging;
362 off_t first_peak_byte = (start / samples_per_file_peak) * sizeof (PeakData);
364 /* open, read, close */
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;
371 #ifdef DEBUG_READ_PEAKS
372 cerr << "DIRECT PEAKS\n";
375 nread = ::pread (_peakfile, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
378 if (nread != sizeof (PeakData) * npeaks) {
379 cerr << "AudioSource["
381 << "]: cannot read peaks from peakfile! (read only "
395 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
406 #ifdef DEBUG_READ_PEAKS
407 cerr << "DOWNSAMPLE\n";
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
414 So, read a block into a staging area, and then downsample from there.
416 to avoid confusion, I'll refer to the requested peaks as visual_peaks and the peakfile peaks as stored_peaks
419 const uint32_t chunksize = (uint32_t) min (expected_peaks, 65536.0);
421 staging = new PeakData[chunksize];
423 /* compute the rounded up frame position */
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;
434 /* handle the case where the initial visual peak is on a pixel boundary */
436 current_stored_peak = min (current_stored_peak, stored_peak_before_next_visual_peak);
438 /* open ... close during out: handling */
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;
445 while (nvisual_peaks < npeaks) {
447 if (i == stored_peaks_read) {
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);
453 #ifdef DEBUG_READ_PEAKS
454 cerr << "read " << sizeof (PeakData) * to_read << " from peakfile @ " << start_byte << endl;
457 if ((nread = ::pread (_peakfile, staging, sizeof (PeakData) * to_read, start_byte))
458 != sizeof (PeakData) * to_read) {
460 off_t fend = lseek (_peakfile, 0, SEEK_END);
462 cerr << "AudioSource["
464 << "]: cannot read peak data from peakfile ("
465 << (nread / sizeof(PeakData))
466 << " peaks instead of "
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
480 stored_peaks_read = nread / sizeof(PeakData);
486 while ((i < stored_peaks_read) && (current_stored_peak <= stored_peak_before_next_visual_peak)) {
488 xmax = max (xmax, staging[i].max);
489 xmin = min (xmin, staging[i].min);
491 ++current_stored_peak;
495 peaks[nvisual_peaks].max = xmax;
496 peaks[nvisual_peaks].min = xmin;
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;
506 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
513 #ifdef DEBUG_READ_PEAKS
514 cerr << "UPSAMPLE\n";
518 - less frames-per-peak (more resolution)
519 - more peaks than stored in the Peakfile
521 So, fetch data from the raw source, and generate peak
525 nframes_t frames_read = 0;
526 nframes_t current_frame = start;
528 nframes_t nvisual_peaks = 0;
529 nframes_t chunksize = (nframes_t) min (cnt, (nframes_t) 4096);
530 raw_staging = new Sample[chunksize];
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;
540 while (nvisual_peaks < npeaks) {
542 if (i == frames_read) {
544 to_read = min (chunksize, nframes_t(_length - current_frame));
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.
550 zero_fill = npeaks - nvisual_peaks;
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))
565 xmax = max (xmax, raw_staging[i]);
566 xmin = min (xmin, raw_staging[i]);
569 pixel_pos += pixels_per_frame;
571 if (pixel_pos >= next_pixel_pos) {
573 peaks[nvisual_peaks].max = xmax;
574 peaks[nvisual_peaks].min = xmin;
579 next_pixel_pos = ceil (pixel_pos + 0.5);
584 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
591 if (_peakfile >= 0) {
596 delete [] raw_staging;
598 #ifdef DEBUG_READ_PEAKS
605 #undef DEBUG_PEAK_BUILD
608 AudioSource::build_peaks_from_scratch ()
610 nframes_t current_frame;
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
620 /* hold lock while building peaks */
622 Glib::Mutex::Lock lp (_lock);
624 if (prepare_for_peakfile_writes ()) {
630 _peaks_built = false;
631 buf = new Sample[bufsize];
635 frames_to_read = min (bufsize, cnt);
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);
643 if (compute_and_write_peaks (buf, current_frame, frames_read, true, false, _FPP)) {
647 current_frame += frames_read;
656 done_with_peakfile_writes ((cnt == 0));
660 Glib::Mutex::Lock lm (_peaks_ready_lock);
663 PeaksReady (); /* EMIT SIGNAL */
670 unlink (peakpath.c_str());
679 AudioSource::prepare_for_peakfile_writes ()
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;
689 AudioSource::done_with_peakfile_writes (bool done)
691 if (peak_leftover_cnt) {
692 compute_and_write_peaks (0, 0, 0, true, false, _FPP);
706 AudioSource::compute_and_write_peaks (Sample* buf, sframes_t first_frame, nframes_t cnt,
707 bool force, bool intermediate_peaks_ready)
709 return compute_and_write_peaks (buf, first_frame, cnt, force, intermediate_peaks_ready, _FPP);
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)
718 uint32_t peaks_computed;
719 PeakData* peakbuf = 0;
721 nframes_t current_frame;
722 nframes_t frames_done;
723 const size_t blocksize = (128 * 1024);
724 off_t first_peak_byte;
727 prepare_for_peakfile_writes ();
731 if (peak_leftover_cnt) {
733 if (first_frame != peak_leftover_frame + peak_leftover_cnt) {
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.
742 x.min = peak_leftovers[0];
743 x.max = peak_leftovers[0];
745 off_t byte = (peak_leftover_frame / fpp) * sizeof (PeakData);
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;
752 _peak_byte_max = max (_peak_byte_max, (off_t) (byte + sizeof(PeakData)));
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 */
762 /* left overs are done */
764 peak_leftover_cnt = 0;
768 /* else ... had leftovers, but they immediately preceed the new data, so just
769 merge them and compute.
772 /* make a new contiguous buffer containing leftovers and the new stuff */
774 to_do = cnt + peak_leftover_cnt;
775 buf2 = new Sample[to_do];
778 memcpy (buf2, peak_leftovers, peak_leftover_cnt * sizeof (Sample));
781 memcpy (buf2+peak_leftover_cnt, buf, cnt * sizeof (Sample));
783 /* no more leftovers */
784 peak_leftover_cnt = 0;
786 /* use the temporary buffer */
789 /* make sure that when we write into the peakfile, we startup where we left off */
791 first_frame = peak_leftover_frame;
797 peakbuf = new PeakData[(to_do/fpp)+1];
799 current_frame = first_frame;
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
809 if (force && (to_do < fpp)) {
810 /* keep the left overs around for next time */
812 if (peak_leftover_size < to_do) {
813 delete [] peak_leftovers;
814 peak_leftovers = new Sample[to_do];
815 peak_leftover_size = to_do;
817 memcpy (peak_leftovers, buf, to_do * sizeof (Sample));
818 peak_leftover_cnt = to_do;
819 peak_leftover_frame = current_frame;
826 nframes_t this_time = min (fpp, to_do);
828 peakbuf[peaks_computed].max = buf[0];
829 peakbuf[peaks_computed].min = buf[0];
831 ARDOUR::find_peaks (buf+1, this_time-1, &peakbuf[peaks_computed].min, &peakbuf[peaks_computed].max);
836 frames_done += this_time;
837 current_frame += this_time;
840 first_peak_byte = (first_frame / fpp) * sizeof (PeakData);
842 if (can_truncate_peaks()) {
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.
850 off_t endpos = lseek (peakfile, 0, SEEK_END);
851 off_t target_length = blocksize * ((first_peak_byte + blocksize + 1) / blocksize);
853 if (endpos < target_length) {
854 ftruncate (peakfile, target_length);
855 /* error doesn't actually matter though, so continue on without testing */
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;
864 _peak_byte_max = max (_peak_byte_max, (off_t) (first_peak_byte + sizeof(PeakData)*peaks_computed));
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 */
884 AudioSource::truncate_peakfile ()
887 error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor")
892 /* truncate the peakfile down to its natural length if necessary */
894 off_t end = lseek (peakfile, 0, SEEK_END);
896 if (end > _peak_byte_max) {
897 ftruncate (peakfile, _peak_byte_max);
902 AudioSource::file_changed (ustring path)
904 struct stat stat_file;
905 struct stat stat_peak;
907 int e1 = stat (path.c_str(), &stat_file);
908 int e2 = stat (peak_path(path).c_str(), &stat_peak);
910 if (!e1 && !e2 && stat_file.st_mtime > stat_peak.st_mtime){
918 AudioSource::available_peaks (double zoom_factor) const
920 if (zoom_factor < _FPP) {
921 return length(_timeline_position); // peak data will come from the audio file
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.
930 off_t end = _peak_byte_max;
932 return (end/sizeof(PeakData)) * _FPP;