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.
33 #include <pbd/xml++.h>
34 #include <pbd/pthread_utils.h>
36 #include <ardour/audiosource.h>
37 #include <ardour/cycle_timer.h>
38 #include <ardour/session.h>
43 using namespace ARDOUR;
46 bool AudioSource::_build_missing_peakfiles = false;
47 bool AudioSource::_build_peakfiles = false;
49 AudioSource::AudioSource (Session& s, ustring name)
50 : Source (s, name, DataType::AUDIO)
56 _write_data_count = 0;
57 peak_leftover_cnt = 0;
58 peak_leftover_size = 0;
62 AudioSource::AudioSource (Session& s, const XMLNode& node)
69 _write_data_count = 0;
70 peak_leftover_cnt = 0;
71 peak_leftover_size = 0;
74 if (set_state (node)) {
75 throw failed_constructor();
79 AudioSource::~AudioSource ()
81 /* shouldn't happen but make sure we don't leak file descriptors anyway */
83 if (peak_leftover_cnt) {
84 cerr << "AudioSource destroyed with leftover peak data pending" << endl;
92 delete [] peak_leftovers;
97 AudioSource::get_state ()
99 XMLNode& node (Source::get_state());
101 if (_captured_for.length()) {
102 node.add_property ("captured-for", _captured_for);
109 AudioSource::set_state (const XMLNode& node)
111 const XMLProperty* prop;
113 Source::set_state (node);
115 if ((prop = node.property ("captured-for")) != 0) {
116 _captured_for = prop->value();
122 /***********************************************************************
124 ***********************************************************************/
127 AudioSource::peaks_ready (sigc::slot<void> the_slot, sigc::connection& conn) const
130 Glib::Mutex::Lock lm (_lock);
132 /* check to see if the peak data is ready. if not
133 connect the slot while still holding the lock.
136 if (!(ret = _peaks_built)) {
137 conn = PeaksReady.connect (the_slot);
144 AudioSource::touch_peakfile ()
148 if (stat (peakpath.c_str(), &statbuf) != 0 || statbuf.st_size == 0) {
154 tbuf.actime = statbuf.st_atime;
155 tbuf.modtime = time ((time_t) 0);
157 utime (peakpath.c_str(), &tbuf);
161 AudioSource::rename_peakfile (ustring newpath)
163 /* caller must hold _lock */
165 ustring oldpath = peakpath;
167 if (access (oldpath.c_str(), F_OK) == 0) {
168 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
169 error << string_compose (_("cannot rename peakfile for %1 from %2 to %3 (%4)"), _name, oldpath, newpath, strerror (errno)) << endmsg;
180 AudioSource::initialize_peakfile (bool newfile, ustring audio_path)
184 peakpath = peak_path (audio_path);
186 /* Nasty band-aid for older sessions that were created before we
187 used libsndfile for all audio files.
190 if (!newfile && access (peakpath.c_str(), R_OK) != 0) {
191 ustring str = old_peak_path (audio_path);
192 if (access (str.c_str(), R_OK) == 0) {
199 if (!_build_peakfiles) {
203 _peaks_built = false;
207 if (stat (peakpath.c_str(), &statbuf)) {
208 if (errno != ENOENT) {
209 /* it exists in the peaks dir, but there is some kind of error */
211 error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg;
215 _peaks_built = false;
219 /* we found it in the peaks dir, so check it out */
221 if (statbuf.st_size == 0) {
222 _peaks_built = false;
224 // Check if the audio file has changed since the peakfile was built.
225 struct stat stat_file;
226 int err = stat (audio_path.c_str(), &stat_file);
228 if (!err && stat_file.st_mtime > statbuf.st_mtime){
229 _peaks_built = false;
233 _peak_byte_max = statbuf.st_size;
239 if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) {
240 build_peaks_from_scratch ();
247 AudioSource::read (Sample *dst, nframes_t start, nframes_t cnt) const
249 Glib::Mutex::Lock lm (_lock);
250 return read_unlocked (dst, start, cnt);
254 AudioSource::write (Sample *dst, nframes_t cnt)
256 Glib::Mutex::Lock lm (_lock);
257 return write_unlocked (dst, cnt);
261 AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_visual_peak) const
263 Glib::Mutex::Lock lm (_lock);
265 double expected_peaks;
266 PeakData::PeakDatum xmax;
267 PeakData::PeakDatum xmin;
270 nframes_t zero_fill = 0;
272 PeakData* staging = 0;
273 Sample* raw_staging = 0;
276 expected_peaks = (cnt / (double) frames_per_peak);
277 scale = npeaks/expected_peaks;
279 #undef DEBUG_READ_PEAKS
280 #ifdef DEBUG_READ_PEAKS
281 cerr << "======>RP: npeaks = " << npeaks
282 << " start = " << start
284 << " len = " << _length
285 << " samples_per_visual_peak =" << samples_per_visual_peak
286 << " expected was " << expected_peaks << " ... scale = " << scale
287 << " PD ptr = " << peaks
292 /* fix for near-end-of-file conditions */
294 if (cnt > _length - start) {
295 // cerr << "too close to end @ " << _length << " given " << start << " + " << cnt << endl;
296 cnt = _length - start;
297 nframes_t old = npeaks;
298 npeaks = min ((nframes_t) floor (cnt / samples_per_visual_peak), npeaks);
299 zero_fill = old - npeaks;
302 // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
306 #ifdef DEBUG_READ_PEAKS
307 cerr << "RAW DATA\n";
309 /* no scaling at all, just get the sample data and duplicate it for
310 both max and min peak values.
313 Sample* raw_staging = new Sample[cnt];
315 if (read_unlocked (raw_staging, start, cnt) != cnt) {
316 error << _("cannot read sample data for unscaled peak computation") << endmsg;
320 for (nframes_t i = 0; i < npeaks; ++i) {
321 peaks[i].max = raw_staging[i];
322 peaks[i].min = raw_staging[i];
325 delete [] raw_staging;
331 off_t first_peak_byte = (start / frames_per_peak) * sizeof (PeakData);
333 /* open, read, close */
335 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
336 error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
340 #ifdef DEBUG_READ_PEAKS
341 cerr << "DIRECT PEAKS\n";
344 nread = ::pread (_peakfile, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
347 if (nread != sizeof (PeakData) * npeaks) {
348 cerr << "AudioSource["
350 << "]: cannot read peaks from peakfile! (read only "
364 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
375 #ifdef DEBUG_READ_PEAKS
376 cerr << "DOWNSAMPLE\n";
380 - more frames-per-peak (lower resolution) than the peakfile, or to put it another way,
381 - less peaks than the peakfile holds for the same range
383 So, read a block into a staging area, and then downsample from there.
385 to avoid confusion, I'll refer to the requested peaks as visual_peaks and the peakfile peaks as stored_peaks
388 const uint32_t chunksize = (uint32_t) min (expected_peaks, 65536.0);
390 staging = new PeakData[chunksize];
392 /* compute the rounded up frame position */
394 nframes_t current_frame = start;
395 nframes_t current_stored_peak = (nframes_t) ceil (current_frame / (double) frames_per_peak);
396 uint32_t next_visual_peak = (uint32_t) ceil (current_frame / samples_per_visual_peak);
397 double next_visual_peak_frame = next_visual_peak * samples_per_visual_peak;
398 uint32_t stored_peak_before_next_visual_peak = (nframes_t) next_visual_peak_frame / frames_per_peak;
399 uint32_t nvisual_peaks = 0;
400 uint32_t stored_peaks_read = 0;
403 /* handle the case where the initial visual peak is on a pixel boundary */
405 current_stored_peak = min (current_stored_peak, stored_peak_before_next_visual_peak);
407 /* open ... close during out: handling */
409 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
410 error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
414 while (nvisual_peaks < npeaks) {
416 if (i == stored_peaks_read) {
418 uint32_t start_byte = current_stored_peak * sizeof(PeakData);
419 tnp = min ((_length/frames_per_peak - current_stored_peak), (nframes_t) expected_peaks);
420 to_read = min (chunksize, tnp);
422 #ifdef DEBUG_READ_PEAKS
423 cerr << "read " << sizeof (PeakData) * to_read << " from peakfile @ " << start_byte << endl;
426 if ((nread = ::pread (_peakfile, staging, sizeof (PeakData) * to_read, start_byte))
427 != sizeof (PeakData) * to_read) {
429 off_t fend = lseek (_peakfile, 0, SEEK_END);
431 cerr << "AudioSource["
433 << "]: cannot read peak data from peakfile ("
434 << (nread / sizeof(PeakData))
435 << " peaks instead of "
440 << " at start_byte = " << start_byte
441 << " _length = " << _length << " versus len = " << fend
442 << " expected maxpeaks = " << (_length - current_frame)/frames_per_peak
443 << " npeaks was " << npeaks
449 stored_peaks_read = nread / sizeof(PeakData);
455 while ((i < stored_peaks_read) && (current_stored_peak <= stored_peak_before_next_visual_peak)) {
457 xmax = max (xmax, staging[i].max);
458 xmin = min (xmin, staging[i].min);
460 ++current_stored_peak;
464 peaks[nvisual_peaks].max = xmax;
465 peaks[nvisual_peaks].min = xmin;
469 //next_visual_peak_frame = min ((next_visual_peak * samples_per_visual_peak), (next_visual_peak_frame+samples_per_visual_peak) );
470 next_visual_peak_frame = min ((double) start+cnt, (next_visual_peak_frame+samples_per_visual_peak) );
471 stored_peak_before_next_visual_peak = (uint32_t) next_visual_peak_frame / frames_per_peak;
475 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
482 #ifdef DEBUG_READ_PEAKS
483 cerr << "UPSAMPLE\n";
487 - less frames-per-peak (more resolution)
488 - more peaks than stored in the Peakfile
490 So, fetch data from the raw source, and generate peak
494 nframes_t frames_read = 0;
495 nframes_t current_frame = start;
497 nframes_t nvisual_peaks = 0;
498 nframes_t chunksize = (nframes_t) min (cnt, (nframes_t) 4096);
499 raw_staging = new Sample[chunksize];
501 nframes_t frame_pos = start;
502 double pixel_pos = floor (frame_pos / samples_per_visual_peak);
503 double next_pixel_pos = ceil (frame_pos / samples_per_visual_peak);
504 double pixels_per_frame = 1.0 / samples_per_visual_peak;
509 while (nvisual_peaks < npeaks) {
511 if (i == frames_read) {
513 to_read = min (chunksize, (_length - current_frame));
516 /* XXX ARGH .. out by one error ... need to figure out why this happens
517 and fix it rather than do this band-aid move.
519 zero_fill = npeaks - nvisual_peaks;
523 if ((frames_read = read_unlocked (raw_staging, current_frame, to_read)) == 0) {
524 error << string_compose(_("AudioSource[%1]: peak read - cannot read %2 samples at offset %3 of %4 (%5)"),
525 _name, to_read, current_frame, _length, strerror (errno))
533 xmax = max (xmax, raw_staging[i]);
534 xmin = min (xmin, raw_staging[i]);
537 pixel_pos += pixels_per_frame;
539 if (pixel_pos >= next_pixel_pos) {
541 peaks[nvisual_peaks].max = xmax;
542 peaks[nvisual_peaks].min = xmin;
547 next_pixel_pos = ceil (pixel_pos + 0.5);
552 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
559 if (_peakfile >= 0) {
568 delete [] raw_staging;
571 #ifdef DEBUG_READ_PEAKS
578 #undef DEBUG_PEAK_BUILD
581 AudioSource::build_peaks_from_scratch ()
583 nframes_t current_frame;
585 Sample buf[frames_per_peak];
586 nframes_t frames_read;
587 nframes_t frames_to_read;
591 /* hold lock while building peaks */
593 Glib::Mutex::Lock lp (_lock);
595 if (prepare_for_peakfile_writes ()) {
601 _peaks_built = false;
605 frames_to_read = min (frames_per_peak, cnt);
607 if ((frames_read = read_unlocked (buf, current_frame, frames_to_read)) != frames_to_read) {
608 error << string_compose(_("%1: could not write read raw data for peak computation (%2)"), _name, strerror (errno)) << endmsg;
609 done_with_peakfile_writes ();
613 if (compute_and_write_peaks (buf, current_frame, frames_read, true)) {
617 current_frame += frames_read;
627 done_with_peakfile_writes ();
630 /* lock no longer held, safe to signal */
633 PeaksReady (); /* EMIT SIGNAL */
639 unlink (peakpath.c_str());
646 AudioSource::prepare_for_peakfile_writes ()
648 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
649 error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
656 AudioSource::done_with_peakfile_writes ()
658 if (peak_leftover_cnt) {
659 compute_and_write_peaks (0, 0, 0, true);
669 AudioSource::compute_and_write_peaks (Sample* buf, nframes_t first_frame, nframes_t cnt, bool force)
673 uint32_t peaks_computed;
674 PeakData* peakbuf = 0;
676 nframes_t current_frame;
677 nframes_t frames_done;
678 const size_t blocksize = (128 * 1024);
679 off_t first_peak_byte;
682 prepare_for_peakfile_writes ();
686 if (peak_leftover_cnt) {
688 if (first_frame != peak_leftover_frame + peak_leftover_cnt) {
690 /* uh-oh, ::seek() since the last ::compute_and_write_peaks(),
691 and we have leftovers. flush a single peak (since the leftovers
692 never represent more than that, and restart.
697 x.min = peak_leftovers[0];
698 x.max = peak_leftovers[0];
699 Session::find_peaks (peak_leftovers + 1, peak_leftover_cnt - 1, &x.min, &x.max);
701 off_t byte = (peak_leftover_frame / frames_per_peak) * sizeof (PeakData);
703 if (::pwrite (peakfile, &x, sizeof (PeakData), byte) != sizeof (PeakData)) {
704 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
708 _peak_byte_max = max (_peak_byte_max, (off_t) (byte + sizeof(PeakData)));
710 PeakRangeReady (peak_leftover_frame, peak_leftover_cnt); /* EMIT SIGNAL */
711 PeaksReady (); /* EMIT SIGNAL */
713 /* left overs are done */
715 peak_leftover_cnt = 0;
719 /* else ... had leftovers, but they immediately preceed the new data, so just
720 merge them and compute.
723 /* make a new contiguous buffer containing leftovers and the new stuff */
725 to_do = cnt + peak_leftover_cnt;
726 buf2 = new Sample[to_do];
729 memcpy (buf2, peak_leftovers, peak_leftover_cnt * sizeof (Sample));
732 memcpy (buf2+peak_leftover_cnt, buf, cnt * sizeof (Sample));
734 /* no more leftovers */
735 peak_leftover_cnt = 0;
737 /* use the temporary buffer */
740 /* make sure that when we write into the peakfile, we startup where we left off */
742 first_frame = peak_leftover_frame;
748 peakbuf = new PeakData[(to_do/frames_per_peak)+1];
750 current_frame = first_frame;
755 /* if some frames were passed in (i.e. we're not flushing leftovers)
756 and there are less than frames_per_peak to do, save them till
760 if (force && (to_do < frames_per_peak)) {
761 /* keep the left overs around for next time */
763 if (peak_leftover_size < to_do) {
764 delete [] peak_leftovers;
765 peak_leftovers = new Sample[to_do];
766 peak_leftover_size = to_do;
768 memcpy (peak_leftovers, buf, to_do * sizeof (Sample));
769 peak_leftover_cnt = to_do;
770 peak_leftover_frame = current_frame;
777 nframes_t this_time = min (frames_per_peak, to_do);
779 peakbuf[peaks_computed].max = buf[0];
780 peakbuf[peaks_computed].min = buf[0];
782 Session::find_peaks (buf+1, this_time-1, &peakbuf[peaks_computed].min, &peakbuf[peaks_computed].max);
787 frames_done += this_time;
788 current_frame += this_time;
791 first_peak_byte = (first_frame / frames_per_peak) * sizeof (PeakData);
793 if (can_truncate_peaks()) {
795 /* on some filesystems (ext3, at least) this helps to reduce fragmentation of
796 the peakfiles. its not guaranteed to do so, and even on ext3 (as of december 2006)
797 it does not cause single-extent allocation even for peakfiles of
798 less than BLOCKSIZE bytes. only call ftruncate if we'll make the file larger.
801 off_t endpos = lseek (peakfile, 0, SEEK_END);
802 off_t target_length = blocksize * ((first_peak_byte + blocksize + 1) / blocksize);
804 if (endpos < target_length) {
805 ftruncate (peakfile, target_length);
806 /* error doesn't actually matter though, so continue on without testing */
810 if (::pwrite (peakfile, peakbuf, sizeof (PeakData) * peaks_computed, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaks_computed)) {
811 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
815 _peak_byte_max = max (_peak_byte_max, (off_t) (first_peak_byte + sizeof(PeakData)*peaks_computed));
818 PeakRangeReady (first_frame, frames_done); /* EMIT SIGNAL */
819 PeaksReady (); /* EMIT SIGNAL */
833 AudioSource::truncate_peakfile ()
836 error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor")
841 /* truncate the peakfile down to its natural length if necessary */
843 off_t end = lseek (peakfile, 0, SEEK_END);
845 if (end > _peak_byte_max) {
846 ftruncate (peakfile, _peak_byte_max);
851 AudioSource::file_changed (ustring path)
853 struct stat stat_file;
854 struct stat stat_peak;
856 int e1 = stat (path.c_str(), &stat_file);
857 int e2 = stat (peak_path(path).c_str(), &stat_peak);
859 if (!e1 && !e2 && stat_file.st_mtime > stat_peak.st_mtime){
867 AudioSource::available_peaks (double zoom_factor) const
871 if (zoom_factor < frames_per_peak) {
872 return length(); // peak data will come from the audio file
875 /* peak data comes from peakfile, but the filesize might not represent
876 the valid data due to ftruncate optimizations, so use _peak_byte_max state.
877 XXX - there might be some atomicity issues here, we should probably add a lock,
878 but _peak_byte_max only monotonically increases after initialization.
881 end = _peak_byte_max;
883 return (end/sizeof(PeakData)) * frames_per_peak;