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>
48 using namespace ARDOUR;
52 bool AudioSource::_build_missing_peakfiles = false;
53 bool AudioSource::_build_peakfiles = false;
57 AudioSource::AudioSource (Session& s, ustring name)
58 : Source (s, name, DataType::AUDIO)
64 _write_data_count = 0;
65 peak_leftover_cnt = 0;
66 peak_leftover_size = 0;
70 AudioSource::AudioSource (Session& s, const XMLNode& node)
78 _write_data_count = 0;
79 peak_leftover_cnt = 0;
80 peak_leftover_size = 0;
83 if (set_state (node)) {
84 throw failed_constructor();
88 AudioSource::~AudioSource ()
90 /* shouldn't happen but make sure we don't leak file descriptors anyway */
92 if (peak_leftover_cnt) {
93 cerr << "AudioSource destroyed with leftover peak data pending" << endl;
100 if (peak_leftovers) {
101 delete [] peak_leftovers;
106 AudioSource::get_state ()
108 XMLNode& node (Source::get_state());
110 if (_captured_for.length()) {
111 node.add_property ("captured-for", _captured_for);
118 AudioSource::set_state (const XMLNode& node)
120 const XMLProperty* prop;
122 Source::set_state (node);
124 if ((prop = node.property ("captured-for")) != 0) {
125 _captured_for = prop->value();
131 /***********************************************************************
133 ***********************************************************************/
136 AudioSource::peaks_ready (sigc::slot<void> the_slot, sigc::connection& conn) const
139 Glib::Mutex::Lock lm (_peaks_ready_lock);
141 /* check to see if the peak data is ready. if not
142 connect the slot while still holding the lock.
145 if (!(ret = _peaks_built)) {
146 conn = PeaksReady.connect (the_slot);
153 AudioSource::touch_peakfile ()
157 if (stat (peakpath.c_str(), &statbuf) != 0 || statbuf.st_size == 0) {
163 tbuf.actime = statbuf.st_atime;
164 tbuf.modtime = time ((time_t) 0);
166 utime (peakpath.c_str(), &tbuf);
170 AudioSource::rename_peakfile (ustring newpath)
172 /* caller must hold _lock */
174 ustring oldpath = peakpath;
176 if (access (oldpath.c_str(), F_OK) == 0) {
177 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
178 error << string_compose (_("cannot rename peakfile for %1 from %2 to %3 (%4)"), _name, oldpath, newpath, strerror (errno)) << endmsg;
189 AudioSource::initialize_peakfile (bool newfile, ustring audio_path)
193 peakpath = peak_path (audio_path);
195 /* if the peak file should be there, but isn't .... */
197 if (!newfile && !Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) {
198 peakpath = find_broken_peakfile (peakpath, audio_path);
201 if (stat (peakpath.c_str(), &statbuf)) {
202 if (errno != ENOENT) {
203 /* it exists in the peaks dir, but there is some kind of error */
205 error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg;
209 /* peakfile does not exist */
211 _peaks_built = false;
215 /* we found it in the peaks dir, so check it out */
217 if (statbuf.st_size == 0 || (statbuf.st_size < ((length() / _FPP) * sizeof (PeakData)))) {
219 _peaks_built = false;
221 // Check if the audio file has changed since the peakfile was built.
222 struct stat stat_file;
223 int err = stat (audio_path.c_str(), &stat_file);
226 _peaks_built = false;
230 /* allow 6 seconds slop on checking peak vs. file times because of various
234 if (stat_file.st_mtime > statbuf.st_mtime && (stat_file.st_mtime - statbuf.st_mtime > 6)) {
235 _peaks_built = false;
239 _peak_byte_max = statbuf.st_size;
245 if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) {
246 build_peaks_from_scratch ();
253 AudioSource::read (Sample *dst, nframes_t start, nframes_t cnt) const
255 Glib::Mutex::Lock lm (_lock);
256 return read_unlocked (dst, start, cnt);
260 AudioSource::write (Sample *dst, nframes_t cnt)
262 Glib::Mutex::Lock lm (_lock);
263 return write_unlocked (dst, cnt);
267 AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_visual_peak) const
269 return read_peaks_with_fpp (peaks, npeaks, start, cnt, samples_per_visual_peak, _FPP);
273 AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt,
274 double samples_per_visual_peak, nframes_t samples_per_file_peak) const
276 Glib::Mutex::Lock lm (_lock);
278 double expected_peaks;
279 PeakData::PeakDatum xmax;
280 PeakData::PeakDatum xmin;
283 nframes_t zero_fill = 0;
285 PeakData* staging = 0;
286 Sample* raw_staging = 0;
289 expected_peaks = (cnt / (double) samples_per_file_peak);
290 scale = npeaks/expected_peaks;
292 #undef DEBUG_READ_PEAKS
293 #ifdef DEBUG_READ_PEAKS
294 cerr << "======>RP: npeaks = " << npeaks
295 << " start = " << start
297 << " len = " << _length
298 << " samples_per_visual_peak =" << samples_per_visual_peak
299 << " expected was " << expected_peaks << " ... scale = " << scale
300 << " PD ptr = " << peaks
305 /* fix for near-end-of-file conditions */
307 if (cnt > _length - start) {
308 // cerr << "too close to end @ " << _length << " given " << start << " + " << cnt << endl;
309 cnt = _length - start;
310 nframes_t old = npeaks;
311 npeaks = min ((nframes_t) floor (cnt / samples_per_visual_peak), npeaks);
312 zero_fill = old - npeaks;
315 // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
319 #ifdef DEBUG_READ_PEAKS
320 cerr << "RAW DATA\n";
322 /* no scaling at all, just get the sample data and duplicate it for
323 both max and min peak values.
326 Sample* raw_staging = new Sample[cnt];
328 if (read_unlocked (raw_staging, start, cnt) != cnt) {
329 error << _("cannot read sample data for unscaled peak computation") << endmsg;
333 for (nframes_t i = 0; i < npeaks; ++i) {
334 peaks[i].max = raw_staging[i];
335 peaks[i].min = raw_staging[i];
338 delete [] raw_staging;
344 off_t first_peak_byte = (start / samples_per_file_peak) * sizeof (PeakData);
346 /* open, read, close */
348 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
349 error << string_compose(_("AudioSource: cannot open peakpath (a) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
353 #ifdef DEBUG_READ_PEAKS
354 cerr << "DIRECT PEAKS\n";
357 nread = ::pread (_peakfile, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
360 if (nread != sizeof (PeakData) * npeaks) {
361 cerr << "AudioSource["
363 << "]: cannot read peaks from peakfile! (read only "
377 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
388 #ifdef DEBUG_READ_PEAKS
389 cerr << "DOWNSAMPLE\n";
393 - more frames-per-peak (lower resolution) than the peakfile, or to put it another way,
394 - less peaks than the peakfile holds for the same range
396 So, read a block into a staging area, and then downsample from there.
398 to avoid confusion, I'll refer to the requested peaks as visual_peaks and the peakfile peaks as stored_peaks
401 const uint32_t chunksize = (uint32_t) min (expected_peaks, 65536.0);
403 staging = new PeakData[chunksize];
405 /* compute the rounded up frame position */
407 nframes_t current_frame = start;
408 nframes_t current_stored_peak = (nframes_t) ceil (current_frame / (double) samples_per_file_peak);
409 uint32_t next_visual_peak = (uint32_t) ceil (current_frame / samples_per_visual_peak);
410 double next_visual_peak_frame = next_visual_peak * samples_per_visual_peak;
411 uint32_t stored_peak_before_next_visual_peak = (nframes_t) next_visual_peak_frame / samples_per_file_peak;
412 uint32_t nvisual_peaks = 0;
413 uint32_t stored_peaks_read = 0;
416 /* handle the case where the initial visual peak is on a pixel boundary */
418 current_stored_peak = min (current_stored_peak, stored_peak_before_next_visual_peak);
420 /* open ... close during out: handling */
422 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
423 error << string_compose(_("AudioSource: cannot open peakpath (b) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
427 while (nvisual_peaks < npeaks) {
429 if (i == stored_peaks_read) {
431 uint32_t start_byte = current_stored_peak * sizeof(PeakData);
432 tnp = min ((_length/samples_per_file_peak - current_stored_peak), (nframes_t) expected_peaks);
433 to_read = min (chunksize, tnp);
435 #ifdef DEBUG_READ_PEAKS
436 cerr << "read " << sizeof (PeakData) * to_read << " from peakfile @ " << start_byte << endl;
439 if ((nread = ::pread (_peakfile, staging, sizeof (PeakData) * to_read, start_byte))
440 != sizeof (PeakData) * to_read) {
442 off_t fend = lseek (_peakfile, 0, SEEK_END);
444 cerr << "AudioSource["
446 << "]: cannot read peak data from peakfile ("
447 << (nread / sizeof(PeakData))
448 << " peaks instead of "
453 << " at start_byte = " << start_byte
454 << " _length = " << _length << " versus len = " << fend
455 << " expected maxpeaks = " << (_length - current_frame)/samples_per_file_peak
456 << " npeaks was " << npeaks
462 stored_peaks_read = nread / sizeof(PeakData);
468 while ((i < stored_peaks_read) && (current_stored_peak <= stored_peak_before_next_visual_peak)) {
470 xmax = max (xmax, staging[i].max);
471 xmin = min (xmin, staging[i].min);
473 ++current_stored_peak;
477 peaks[nvisual_peaks].max = xmax;
478 peaks[nvisual_peaks].min = xmin;
482 //next_visual_peak_frame = min ((next_visual_peak * samples_per_visual_peak), (next_visual_peak_frame+samples_per_visual_peak) );
483 next_visual_peak_frame = min ((double) start+cnt, (next_visual_peak_frame+samples_per_visual_peak) );
484 stored_peak_before_next_visual_peak = (uint32_t) next_visual_peak_frame / samples_per_file_peak;
488 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
495 #ifdef DEBUG_READ_PEAKS
496 cerr << "UPSAMPLE\n";
500 - less frames-per-peak (more resolution)
501 - more peaks than stored in the Peakfile
503 So, fetch data from the raw source, and generate peak
507 nframes_t frames_read = 0;
508 nframes_t current_frame = start;
510 nframes_t nvisual_peaks = 0;
511 nframes_t chunksize = (nframes_t) min (cnt, (nframes_t) 4096);
512 raw_staging = new Sample[chunksize];
514 nframes_t frame_pos = start;
515 double pixel_pos = floor (frame_pos / samples_per_visual_peak);
516 double next_pixel_pos = ceil (frame_pos / samples_per_visual_peak);
517 double pixels_per_frame = 1.0 / samples_per_visual_peak;
522 while (nvisual_peaks < npeaks) {
524 if (i == frames_read) {
526 to_read = min (chunksize, (_length - current_frame));
529 /* XXX ARGH .. out by one error ... need to figure out why this happens
530 and fix it rather than do this band-aid move.
532 zero_fill = npeaks - nvisual_peaks;
536 if ((frames_read = read_unlocked (raw_staging, current_frame, to_read)) == 0) {
537 error << string_compose(_("AudioSource[%1]: peak read - cannot read %2 samples at offset %3 of %4 (%5)"),
538 _name, to_read, current_frame, _length, strerror (errno))
546 xmax = max (xmax, raw_staging[i]);
547 xmin = min (xmin, raw_staging[i]);
550 pixel_pos += pixels_per_frame;
552 if (pixel_pos >= next_pixel_pos) {
554 peaks[nvisual_peaks].max = xmax;
555 peaks[nvisual_peaks].min = xmin;
560 next_pixel_pos = ceil (pixel_pos + 0.5);
565 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
572 if (_peakfile >= 0) {
581 delete [] raw_staging;
584 #ifdef DEBUG_READ_PEAKS
591 #undef DEBUG_PEAK_BUILD
594 AudioSource::build_peaks_from_scratch ()
596 nframes_t current_frame;
599 nframes_t frames_read;
600 nframes_t frames_to_read;
601 const nframes_t bufsize = 65536; // 256kB per disk read for mono data is about ideal
606 /* hold lock while building peaks */
608 Glib::Mutex::Lock lp (_lock);
610 if (prepare_for_peakfile_writes ()) {
616 _peaks_built = false;
617 buf = new Sample[bufsize];
621 frames_to_read = min (bufsize, cnt);
623 if ((frames_read = read_unlocked (buf, current_frame, frames_to_read)) != frames_to_read) {
624 error << string_compose(_("%1: could not write read raw data for peak computation (%2)"), _name, strerror (errno)) << endmsg;
625 done_with_peakfile_writes (false);
629 if (compute_and_write_peaks (buf, current_frame, frames_read, true, false, _FPP)) {
633 current_frame += frames_read;
642 done_with_peakfile_writes ((cnt == 0));
646 Glib::Mutex::Lock lm (_peaks_ready_lock);
649 PeaksReady (); /* EMIT SIGNAL */
656 unlink (peakpath.c_str());
667 AudioSource::prepare_for_peakfile_writes ()
669 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
670 error << string_compose(_("AudioSource: cannot open peakpath (c) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
677 AudioSource::done_with_peakfile_writes (bool done)
679 if (peak_leftover_cnt) {
680 compute_and_write_peaks (0, 0, 0, true, false, _FPP);
694 AudioSource::compute_and_write_peaks (Sample* buf, nframes_t first_frame, nframes_t cnt, bool force, bool intermediate_peaks_ready)
696 return compute_and_write_peaks (buf, first_frame, cnt, force, intermediate_peaks_ready, _FPP);
700 AudioSource::compute_and_write_peaks (Sample* buf, nframes_t first_frame, nframes_t cnt, bool force,
701 bool intermediate_peaks_ready, nframes_t fpp)
705 uint32_t peaks_computed;
706 PeakData* peakbuf = 0;
708 nframes_t current_frame;
709 nframes_t frames_done;
710 const size_t blocksize = (128 * 1024);
711 off_t first_peak_byte;
714 prepare_for_peakfile_writes ();
718 if (peak_leftover_cnt) {
720 if (first_frame != peak_leftover_frame + peak_leftover_cnt) {
722 /* uh-oh, ::seek() since the last ::compute_and_write_peaks(),
723 and we have leftovers. flush a single peak (since the leftovers
724 never represent more than that, and restart.
729 x.min = peak_leftovers[0];
730 x.max = peak_leftovers[0];
732 off_t byte = (peak_leftover_frame / fpp) * sizeof (PeakData);
734 if (::pwrite (peakfile, &x, sizeof (PeakData), byte) != sizeof (PeakData)) {
735 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
739 _peak_byte_max = max (_peak_byte_max, (off_t) (byte + sizeof(PeakData)));
742 Glib::Mutex::Lock lm (_peaks_ready_lock);
743 PeakRangeReady (peak_leftover_frame, peak_leftover_cnt); /* EMIT SIGNAL */
744 if (intermediate_peaks_ready) {
745 PeaksReady (); /* EMIT SIGNAL */
749 /* left overs are done */
751 peak_leftover_cnt = 0;
755 /* else ... had leftovers, but they immediately preceed the new data, so just
756 merge them and compute.
759 /* make a new contiguous buffer containing leftovers and the new stuff */
761 to_do = cnt + peak_leftover_cnt;
762 buf2 = new Sample[to_do];
765 memcpy (buf2, peak_leftovers, peak_leftover_cnt * sizeof (Sample));
768 memcpy (buf2+peak_leftover_cnt, buf, cnt * sizeof (Sample));
770 /* no more leftovers */
771 peak_leftover_cnt = 0;
773 /* use the temporary buffer */
776 /* make sure that when we write into the peakfile, we startup where we left off */
778 first_frame = peak_leftover_frame;
784 peakbuf = new PeakData[(to_do/fpp)+1];
786 current_frame = first_frame;
791 /* if some frames were passed in (i.e. we're not flushing leftovers)
792 and there are less than fpp to do, save them till
796 if (force && (to_do < fpp)) {
797 /* keep the left overs around for next time */
799 if (peak_leftover_size < to_do) {
800 delete [] peak_leftovers;
801 peak_leftovers = new Sample[to_do];
802 peak_leftover_size = to_do;
804 memcpy (peak_leftovers, buf, to_do * sizeof (Sample));
805 peak_leftover_cnt = to_do;
806 peak_leftover_frame = current_frame;
813 nframes_t this_time = min (fpp, to_do);
815 peakbuf[peaks_computed].max = buf[0];
816 peakbuf[peaks_computed].min = buf[0];
818 ARDOUR::find_peaks (buf+1, this_time-1, &peakbuf[peaks_computed].min, &peakbuf[peaks_computed].max);
823 frames_done += this_time;
824 current_frame += this_time;
827 first_peak_byte = (first_frame / fpp) * sizeof (PeakData);
829 if (can_truncate_peaks()) {
831 /* on some filesystems (ext3, at least) this helps to reduce fragmentation of
832 the peakfiles. its not guaranteed to do so, and even on ext3 (as of december 2006)
833 it does not cause single-extent allocation even for peakfiles of
834 less than BLOCKSIZE bytes. only call ftruncate if we'll make the file larger.
837 off_t endpos = lseek (peakfile, 0, SEEK_END);
838 off_t target_length = blocksize * ((first_peak_byte + blocksize + 1) / blocksize);
840 if (endpos < target_length) {
841 ftruncate (peakfile, target_length);
842 /* error doesn't actually matter though, so continue on without testing */
846 if (::pwrite (peakfile, peakbuf, sizeof (PeakData) * peaks_computed, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaks_computed)) {
847 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
851 _peak_byte_max = max (_peak_byte_max, (off_t) (first_peak_byte + sizeof(PeakData)*peaks_computed));
854 Glib::Mutex::Lock lm (_peaks_ready_lock);
855 PeakRangeReady (first_frame, frames_done); /* EMIT SIGNAL */
856 if (intermediate_peaks_ready) {
857 PeaksReady (); /* EMIT SIGNAL */
872 AudioSource::truncate_peakfile ()
875 error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor")
880 /* truncate the peakfile down to its natural length if necessary */
882 off_t end = lseek (peakfile, 0, SEEK_END);
884 if (end > _peak_byte_max) {
885 ftruncate (peakfile, _peak_byte_max);
890 AudioSource::file_changed (ustring path)
892 struct stat stat_file;
893 struct stat stat_peak;
895 int e1 = stat (path.c_str(), &stat_file);
896 int e2 = stat (peak_path(path).c_str(), &stat_peak);
898 if (!e1 && !e2 && stat_file.st_mtime > stat_peak.st_mtime){
906 AudioSource::available_peaks (double zoom_factor) const
910 if (zoom_factor < _FPP) {
911 return length(); // peak data will come from the audio file
914 /* peak data comes from peakfile, but the filesize might not represent
915 the valid data due to ftruncate optimizations, so use _peak_byte_max state.
916 XXX - there might be some atomicity issues here, we should probably add a lock,
917 but _peak_byte_max only monotonically increases after initialization.
920 end = _peak_byte_max;
922 return (end/sizeof(PeakData)) * _FPP;
926 AudioSource::update_length (nframes_t pos, nframes_t cnt)
928 if (pos + cnt > _length) {