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.
32 #include <pbd/xml++.h>
33 #include <pbd/pthread_utils.h>
35 #include <ardour/audiosource.h>
36 #include <ardour/cycle_timer.h>
37 #include <ardour/runtime_functions.h>
42 using namespace ARDOUR;
45 bool AudioSource::_build_missing_peakfiles = false;
46 bool AudioSource::_build_peakfiles = false;
48 AudioSource::AudioSource (Session& s, ustring name)
49 : Source (s, name, DataType::AUDIO)
55 _write_data_count = 0;
56 peak_leftover_cnt = 0;
57 peak_leftover_size = 0;
61 AudioSource::AudioSource (Session& s, const XMLNode& node)
68 _write_data_count = 0;
69 peak_leftover_cnt = 0;
70 peak_leftover_size = 0;
73 if (set_state (node)) {
74 throw failed_constructor();
78 AudioSource::~AudioSource ()
80 /* shouldn't happen but make sure we don't leak file descriptors anyway */
82 if (peak_leftover_cnt) {
83 cerr << "AudioSource destroyed with leftover peak data pending" << endl;
91 delete [] peak_leftovers;
96 AudioSource::get_state ()
98 XMLNode& node (Source::get_state());
100 if (_captured_for.length()) {
101 node.add_property ("captured-for", _captured_for);
108 AudioSource::set_state (const XMLNode& node)
110 const XMLProperty* prop;
112 Source::set_state (node);
114 if ((prop = node.property ("captured-for")) != 0) {
115 _captured_for = prop->value();
121 /***********************************************************************
123 ***********************************************************************/
126 AudioSource::peaks_ready (sigc::slot<void> the_slot, sigc::connection& conn) const
129 Glib::Mutex::Lock lm (_lock);
131 /* check to see if the peak data is ready. if not
132 connect the slot while still holding the lock.
135 if (!(ret = _peaks_built)) {
136 conn = PeaksReady.connect (the_slot);
143 AudioSource::touch_peakfile ()
147 if (stat (peakpath.c_str(), &statbuf) != 0 || statbuf.st_size == 0) {
153 tbuf.actime = statbuf.st_atime;
154 tbuf.modtime = time ((time_t) 0);
156 utime (peakpath.c_str(), &tbuf);
160 AudioSource::rename_peakfile (ustring newpath)
162 /* caller must hold _lock */
164 ustring oldpath = peakpath;
166 if (access (oldpath.c_str(), F_OK) == 0) {
167 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
168 error << string_compose (_("cannot rename peakfile for %1 from %2 to %3 (%4)"), _name, oldpath, newpath, strerror (errno)) << endmsg;
179 AudioSource::initialize_peakfile (bool newfile, ustring audio_path)
183 peakpath = peak_path (audio_path);
185 /* Nasty band-aid for older sessions that were created before we
186 used libsndfile for all audio files.
189 if (!newfile && access (peakpath.c_str(), R_OK) != 0) {
190 ustring str = old_peak_path (audio_path);
191 if (access (str.c_str(), R_OK) == 0) {
198 if (!_build_peakfiles) {
202 _peaks_built = false;
206 if (stat (peakpath.c_str(), &statbuf)) {
207 if (errno != ENOENT) {
208 /* it exists in the peaks dir, but there is some kind of error */
210 error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg;
214 _peaks_built = false;
218 /* we found it in the peaks dir, so check it out */
220 if (statbuf.st_size == 0) {
221 _peaks_built = false;
223 // Check if the audio file has changed since the peakfile was built.
224 struct stat stat_file;
225 int err = stat (audio_path.c_str(), &stat_file);
227 if (!err && stat_file.st_mtime > statbuf.st_mtime){
228 _peaks_built = false;
232 _peak_byte_max = statbuf.st_size;
238 if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) {
239 build_peaks_from_scratch ();
246 AudioSource::read (Sample *dst, nframes_t start, nframes_t cnt) const
248 Glib::Mutex::Lock lm (_lock);
249 return read_unlocked (dst, start, cnt);
253 AudioSource::write (Sample *dst, nframes_t cnt)
255 Glib::Mutex::Lock lm (_lock);
256 return write_unlocked (dst, cnt);
260 AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_visual_peak) const
262 Glib::Mutex::Lock lm (_lock);
264 double expected_peaks;
265 PeakData::PeakDatum xmax;
266 PeakData::PeakDatum xmin;
269 nframes_t zero_fill = 0;
271 PeakData* staging = 0;
272 Sample* raw_staging = 0;
275 expected_peaks = (cnt / (double) frames_per_peak);
276 scale = npeaks/expected_peaks;
278 #undef DEBUG_READ_PEAKS
279 #ifdef DEBUG_READ_PEAKS
280 cerr << "======>RP: npeaks = " << npeaks
281 << " start = " << start
283 << " len = " << _length
284 << " samples_per_visual_peak =" << samples_per_visual_peak
285 << " expected was " << expected_peaks << " ... scale = " << scale
286 << " PD ptr = " << peaks
291 /* fix for near-end-of-file conditions */
293 if (cnt > _length - start) {
294 // cerr << "too close to end @ " << _length << " given " << start << " + " << cnt << endl;
295 cnt = _length - start;
296 nframes_t old = npeaks;
297 npeaks = min ((nframes_t) floor (cnt / samples_per_visual_peak), npeaks);
298 zero_fill = old - npeaks;
301 // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
305 #ifdef DEBUG_READ_PEAKS
306 cerr << "RAW DATA\n";
308 /* no scaling at all, just get the sample data and duplicate it for
309 both max and min peak values.
312 Sample* raw_staging = new Sample[cnt];
314 if (read_unlocked (raw_staging, start, cnt) != cnt) {
315 error << _("cannot read sample data for unscaled peak computation") << endmsg;
319 for (nframes_t i = 0; i < npeaks; ++i) {
320 peaks[i].max = raw_staging[i];
321 peaks[i].min = raw_staging[i];
324 delete [] raw_staging;
330 off_t first_peak_byte = (start / frames_per_peak) * sizeof (PeakData);
332 /* open, read, close */
334 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
335 error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
339 #ifdef DEBUG_READ_PEAKS
340 cerr << "DIRECT PEAKS\n";
343 nread = ::pread (_peakfile, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
346 if (nread != sizeof (PeakData) * npeaks) {
347 cerr << "AudioSource["
349 << "]: cannot read peaks from peakfile! (read only "
363 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
374 #ifdef DEBUG_READ_PEAKS
375 cerr << "DOWNSAMPLE\n";
379 - more frames-per-peak (lower resolution) than the peakfile, or to put it another way,
380 - less peaks than the peakfile holds for the same range
382 So, read a block into a staging area, and then downsample from there.
384 to avoid confusion, I'll refer to the requested peaks as visual_peaks and the peakfile peaks as stored_peaks
387 const uint32_t chunksize = (uint32_t) min (expected_peaks, 65536.0);
389 staging = new PeakData[chunksize];
391 /* compute the rounded up frame position */
393 nframes_t current_frame = start;
394 nframes_t current_stored_peak = (nframes_t) ceil (current_frame / (double) frames_per_peak);
395 uint32_t next_visual_peak = (uint32_t) ceil (current_frame / samples_per_visual_peak);
396 double next_visual_peak_frame = next_visual_peak * samples_per_visual_peak;
397 uint32_t stored_peak_before_next_visual_peak = (nframes_t) next_visual_peak_frame / frames_per_peak;
398 uint32_t nvisual_peaks = 0;
399 uint32_t stored_peaks_read = 0;
402 /* handle the case where the initial visual peak is on a pixel boundary */
404 current_stored_peak = min (current_stored_peak, stored_peak_before_next_visual_peak);
406 /* open ... close during out: handling */
408 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
409 error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
413 while (nvisual_peaks < npeaks) {
415 if (i == stored_peaks_read) {
417 uint32_t start_byte = current_stored_peak * sizeof(PeakData);
418 tnp = min ((_length/frames_per_peak - current_stored_peak), (nframes_t) expected_peaks);
419 to_read = min (chunksize, tnp);
421 #ifdef DEBUG_READ_PEAKS
422 cerr << "read " << sizeof (PeakData) * to_read << " from peakfile @ " << start_byte << endl;
425 if ((nread = ::pread (_peakfile, staging, sizeof (PeakData) * to_read, start_byte))
426 != sizeof (PeakData) * to_read) {
428 off_t fend = lseek (_peakfile, 0, SEEK_END);
430 cerr << "AudioSource["
432 << "]: cannot read peak data from peakfile ("
433 << (nread / sizeof(PeakData))
434 << " peaks instead of "
439 << " at start_byte = " << start_byte
440 << " _length = " << _length << " versus len = " << fend
441 << " expected maxpeaks = " << (_length - current_frame)/frames_per_peak
442 << " npeaks was " << npeaks
448 stored_peaks_read = nread / sizeof(PeakData);
454 while ((i < stored_peaks_read) && (current_stored_peak <= stored_peak_before_next_visual_peak)) {
456 xmax = max (xmax, staging[i].max);
457 xmin = min (xmin, staging[i].min);
459 ++current_stored_peak;
463 peaks[nvisual_peaks].max = xmax;
464 peaks[nvisual_peaks].min = xmin;
468 //next_visual_peak_frame = min ((next_visual_peak * samples_per_visual_peak), (next_visual_peak_frame+samples_per_visual_peak) );
469 next_visual_peak_frame = min ((double) start+cnt, (next_visual_peak_frame+samples_per_visual_peak) );
470 stored_peak_before_next_visual_peak = (uint32_t) next_visual_peak_frame / frames_per_peak;
474 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
481 #ifdef DEBUG_READ_PEAKS
482 cerr << "UPSAMPLE\n";
486 - less frames-per-peak (more resolution)
487 - more peaks than stored in the Peakfile
489 So, fetch data from the raw source, and generate peak
493 nframes_t frames_read = 0;
494 nframes_t current_frame = start;
496 nframes_t nvisual_peaks = 0;
497 nframes_t chunksize = (nframes_t) min (cnt, (nframes_t) 4096);
498 raw_staging = new Sample[chunksize];
500 nframes_t frame_pos = start;
501 double pixel_pos = floor (frame_pos / samples_per_visual_peak);
502 double next_pixel_pos = ceil (frame_pos / samples_per_visual_peak);
503 double pixels_per_frame = 1.0 / samples_per_visual_peak;
508 while (nvisual_peaks < npeaks) {
510 if (i == frames_read) {
512 to_read = min (chunksize, (_length - current_frame));
515 /* XXX ARGH .. out by one error ... need to figure out why this happens
516 and fix it rather than do this band-aid move.
518 zero_fill = npeaks - nvisual_peaks;
522 if ((frames_read = read_unlocked (raw_staging, current_frame, to_read)) == 0) {
523 error << string_compose(_("AudioSource[%1]: peak read - cannot read %2 samples at offset %3 of %4 (%5)"),
524 _name, to_read, current_frame, _length, strerror (errno))
532 xmax = max (xmax, raw_staging[i]);
533 xmin = min (xmin, raw_staging[i]);
536 pixel_pos += pixels_per_frame;
538 if (pixel_pos >= next_pixel_pos) {
540 peaks[nvisual_peaks].max = xmax;
541 peaks[nvisual_peaks].min = xmin;
546 next_pixel_pos = ceil (pixel_pos + 0.5);
551 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
558 if (_peakfile >= 0) {
567 delete [] raw_staging;
570 #ifdef DEBUG_READ_PEAKS
577 #undef DEBUG_PEAK_BUILD
580 AudioSource::build_peaks_from_scratch ()
582 nframes_t current_frame;
584 Sample buf[frames_per_peak];
585 nframes_t frames_read;
586 nframes_t frames_to_read;
590 /* hold lock while building peaks */
592 Glib::Mutex::Lock lp (_lock);
594 if (prepare_for_peakfile_writes ()) {
600 _peaks_built = false;
604 frames_to_read = min (frames_per_peak, cnt);
606 if ((frames_read = read_unlocked (buf, current_frame, frames_to_read)) != frames_to_read) {
607 error << string_compose(_("%1: could not write read raw data for peak computation (%2)"), _name, strerror (errno)) << endmsg;
608 done_with_peakfile_writes ();
612 if (compute_and_write_peaks (buf, current_frame, frames_read, true)) {
616 current_frame += frames_read;
626 done_with_peakfile_writes ();
629 /* lock no longer held, safe to signal */
632 PeaksReady (); /* EMIT SIGNAL */
638 unlink (peakpath.c_str());
645 AudioSource::prepare_for_peakfile_writes ()
647 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
648 error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
655 AudioSource::done_with_peakfile_writes ()
657 if (peak_leftover_cnt) {
658 compute_and_write_peaks (0, 0, 0, true);
668 AudioSource::compute_and_write_peaks (Sample* buf, nframes_t first_frame, nframes_t cnt, bool force)
672 uint32_t peaks_computed;
673 PeakData* peakbuf = 0;
675 nframes_t current_frame;
676 nframes_t frames_done;
677 const size_t blocksize = (128 * 1024);
678 off_t first_peak_byte;
681 prepare_for_peakfile_writes ();
685 if (peak_leftover_cnt) {
687 if (first_frame != peak_leftover_frame + peak_leftover_cnt) {
689 /* uh-oh, ::seek() since the last ::compute_and_write_peaks(),
690 and we have leftovers. flush a single peak (since the leftovers
691 never represent more than that, and restart.
696 x.min = peak_leftovers[0];
697 x.max = peak_leftovers[0];
698 find_peaks (peak_leftovers + 1, peak_leftover_cnt - 1, &x.min, &x.max);
700 off_t byte = (peak_leftover_frame / frames_per_peak) * sizeof (PeakData);
702 if (::pwrite (peakfile, &x, sizeof (PeakData), byte) != sizeof (PeakData)) {
703 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
707 _peak_byte_max = max (_peak_byte_max, (off_t) (byte + sizeof(PeakData)));
709 PeakRangeReady (peak_leftover_frame, peak_leftover_cnt); /* EMIT SIGNAL */
710 PeaksReady (); /* EMIT SIGNAL */
712 /* left overs are done */
714 peak_leftover_cnt = 0;
718 /* else ... had leftovers, but they immediately preceed the new data, so just
719 merge them and compute.
722 /* make a new contiguous buffer containing leftovers and the new stuff */
724 to_do = cnt + peak_leftover_cnt;
725 buf2 = new Sample[to_do];
728 memcpy (buf2, peak_leftovers, peak_leftover_cnt * sizeof (Sample));
731 memcpy (buf2+peak_leftover_cnt, buf, cnt * sizeof (Sample));
733 /* no more leftovers */
734 peak_leftover_cnt = 0;
736 /* use the temporary buffer */
739 /* make sure that when we write into the peakfile, we startup where we left off */
741 first_frame = peak_leftover_frame;
747 peakbuf = new PeakData[(to_do/frames_per_peak)+1];
749 current_frame = first_frame;
754 /* if some frames were passed in (i.e. we're not flushing leftovers)
755 and there are less than frames_per_peak to do, save them till
759 if (force && (to_do < frames_per_peak)) {
760 /* keep the left overs around for next time */
762 if (peak_leftover_size < to_do) {
763 delete [] peak_leftovers;
764 peak_leftovers = new Sample[to_do];
765 peak_leftover_size = to_do;
767 memcpy (peak_leftovers, buf, to_do * sizeof (Sample));
768 peak_leftover_cnt = to_do;
769 peak_leftover_frame = current_frame;
776 nframes_t this_time = min (frames_per_peak, to_do);
778 peakbuf[peaks_computed].max = buf[0];
779 peakbuf[peaks_computed].min = buf[0];
781 find_peaks (buf+1, this_time-1, &peakbuf[peaks_computed].min, &peakbuf[peaks_computed].max);
786 frames_done += this_time;
787 current_frame += this_time;
790 first_peak_byte = (first_frame / frames_per_peak) * sizeof (PeakData);
792 if (can_truncate_peaks()) {
794 /* on some filesystems (ext3, at least) this helps to reduce fragmentation of
795 the peakfiles. its not guaranteed to do so, and even on ext3 (as of december 2006)
796 it does not cause single-extent allocation even for peakfiles of
797 less than BLOCKSIZE bytes. only call ftruncate if we'll make the file larger.
800 off_t endpos = lseek (peakfile, 0, SEEK_END);
801 off_t target_length = blocksize * ((first_peak_byte + blocksize + 1) / blocksize);
803 if (endpos < target_length) {
804 ftruncate (peakfile, target_length);
805 /* error doesn't actually matter though, so continue on without testing */
809 if (::pwrite (peakfile, peakbuf, sizeof (PeakData) * peaks_computed, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaks_computed)) {
810 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
814 _peak_byte_max = max (_peak_byte_max, (off_t) (first_peak_byte + sizeof(PeakData)*peaks_computed));
817 PeakRangeReady (first_frame, frames_done); /* EMIT SIGNAL */
818 PeaksReady (); /* EMIT SIGNAL */
832 AudioSource::truncate_peakfile ()
835 error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor")
840 /* truncate the peakfile down to its natural length if necessary */
842 off_t end = lseek (peakfile, 0, SEEK_END);
844 if (end > _peak_byte_max) {
845 ftruncate (peakfile, _peak_byte_max);
850 AudioSource::file_changed (ustring path)
852 struct stat stat_file;
853 struct stat stat_peak;
855 int e1 = stat (path.c_str(), &stat_file);
856 int e2 = stat (peak_path(path).c_str(), &stat_peak);
858 if (!e1 && !e2 && stat_file.st_mtime > stat_peak.st_mtime){
866 AudioSource::available_peaks (double zoom_factor) const
870 if (zoom_factor < frames_per_peak) {
871 return length(); // peak data will come from the audio file
874 /* peak data comes from peakfile, but the filesize might not represent
875 the valid data due to ftruncate optimizations, so use _peak_byte_max state.
876 XXX - there might be some atomicity issues here, we should probably add a lock,
877 but _peak_byte_max only monotonically increases after initialization.
880 end = _peak_byte_max;
882 return (end/sizeof(PeakData)) * frames_per_peak;