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)
64 _write_data_count = 0;
65 peak_leftover_cnt = 0;
66 peak_leftover_size = 0;
70 AudioSource::AudioSource (Session& s, const XMLNode& node)
77 _write_data_count = 0;
78 peak_leftover_cnt = 0;
79 peak_leftover_size = 0;
82 if (set_state (node)) {
83 throw failed_constructor();
87 AudioSource::~AudioSource ()
89 /* shouldn't happen but make sure we don't leak file descriptors anyway */
91 if (peak_leftover_cnt) {
92 cerr << "AudioSource destroyed with leftover peak data pending" << endl;
100 delete [] peak_leftovers;
105 AudioSource::get_state ()
107 XMLNode& node (Source::get_state());
109 if (_captured_for.length()) {
110 node.add_property ("captured-for", _captured_for);
117 AudioSource::set_state (const XMLNode& node)
119 const XMLProperty* prop;
121 Source::set_state (node);
123 if ((prop = node.property ("captured-for")) != 0) {
124 _captured_for = prop->value();
130 /***********************************************************************
132 ***********************************************************************/
135 AudioSource::peaks_ready (sigc::slot<void> the_slot, sigc::connection& conn) const
138 Glib::Mutex::Lock lm (_peaks_ready_lock);
140 /* check to see if the peak data is ready. if not
141 connect the slot while still holding the lock.
144 if (!(ret = _peaks_built)) {
145 conn = PeaksReady.connect (the_slot);
152 AudioSource::touch_peakfile ()
156 if (stat (peakpath.c_str(), &statbuf) != 0 || statbuf.st_size == 0) {
162 tbuf.actime = statbuf.st_atime;
163 tbuf.modtime = time ((time_t) 0);
165 utime (peakpath.c_str(), &tbuf);
169 AudioSource::rename_peakfile (ustring newpath)
171 /* caller must hold _lock */
173 ustring oldpath = peakpath;
175 if (access (oldpath.c_str(), F_OK) == 0) {
176 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
177 error << string_compose (_("cannot rename peakfile for %1 from %2 to %3 (%4)"), _name, oldpath, newpath, strerror (errno)) << endmsg;
188 AudioSource::initialize_peakfile (bool newfile, ustring audio_path)
192 peakpath = peak_path (audio_path);
194 /* if the peak file should be there, but isn't .... */
196 if (!newfile && !Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) {
197 peakpath = find_broken_peakfile (peakpath, audio_path);
200 if (stat (peakpath.c_str(), &statbuf)) {
201 if (errno != ENOENT) {
202 /* it exists in the peaks dir, but there is some kind of error */
204 error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg;
208 /* peakfile does not exist */
210 _peaks_built = false;
214 /* we found it in the peaks dir, so check it out */
216 if (statbuf.st_size == 0) {
218 _peaks_built = false;
220 // Check if the audio file has changed since the peakfile was built.
221 struct stat stat_file;
222 int err = stat (audio_path.c_str(), &stat_file);
224 if (!err && stat_file.st_mtime > statbuf.st_mtime){
225 _peaks_built = false;
229 _peak_byte_max = statbuf.st_size;
234 if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) {
235 build_peaks_from_scratch ();
242 AudioSource::read (Sample *dst, nframes_t start, nframes_t cnt) const
244 Glib::Mutex::Lock lm (_lock);
245 return read_unlocked (dst, start, cnt);
249 AudioSource::write (Sample *dst, nframes_t cnt)
251 Glib::Mutex::Lock lm (_lock);
252 return write_unlocked (dst, cnt);
256 AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_visual_peak) const
258 return read_peaks_with_fpp (peaks, npeaks, start, cnt, samples_per_visual_peak, _FPP);
262 AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt,
263 double samples_per_visual_peak, nframes_t samples_per_file_peak) const
265 Glib::Mutex::Lock lm (_lock);
267 double expected_peaks;
268 PeakData::PeakDatum xmax;
269 PeakData::PeakDatum xmin;
272 nframes_t zero_fill = 0;
274 PeakData* staging = 0;
275 Sample* raw_staging = 0;
278 expected_peaks = (cnt / (double) samples_per_file_peak);
279 scale = npeaks/expected_peaks;
281 #undef DEBUG_READ_PEAKS
282 #ifdef DEBUG_READ_PEAKS
283 cerr << "======>RP: npeaks = " << npeaks
284 << " start = " << start
286 << " len = " << _length
287 << " samples_per_visual_peak =" << samples_per_visual_peak
288 << " expected was " << expected_peaks << " ... scale = " << scale
289 << " PD ptr = " << peaks
294 /* fix for near-end-of-file conditions */
296 if (cnt > _length - start) {
297 // cerr << "too close to end @ " << _length << " given " << start << " + " << cnt << endl;
298 cnt = _length - start;
299 nframes_t old = npeaks;
300 npeaks = min ((nframes_t) floor (cnt / samples_per_visual_peak), npeaks);
301 zero_fill = old - npeaks;
304 // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
308 #ifdef DEBUG_READ_PEAKS
309 cerr << "RAW DATA\n";
311 /* no scaling at all, just get the sample data and duplicate it for
312 both max and min peak values.
315 Sample* raw_staging = new Sample[cnt];
317 if (read_unlocked (raw_staging, start, cnt) != cnt) {
318 error << _("cannot read sample data for unscaled peak computation") << endmsg;
322 for (nframes_t i = 0; i < npeaks; ++i) {
323 peaks[i].max = raw_staging[i];
324 peaks[i].min = raw_staging[i];
327 delete [] raw_staging;
333 off_t first_peak_byte = (start / samples_per_file_peak) * sizeof (PeakData);
335 /* open, read, close */
337 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
338 error << string_compose(_("AudioSource: cannot open peakpath (a) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
342 #ifdef DEBUG_READ_PEAKS
343 cerr << "DIRECT PEAKS\n";
346 nread = ::pread (_peakfile, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
349 if (nread != sizeof (PeakData) * npeaks) {
350 cerr << "AudioSource["
352 << "]: cannot read peaks from peakfile! (read only "
366 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
377 #ifdef DEBUG_READ_PEAKS
378 cerr << "DOWNSAMPLE\n";
382 - more frames-per-peak (lower resolution) than the peakfile, or to put it another way,
383 - less peaks than the peakfile holds for the same range
385 So, read a block into a staging area, and then downsample from there.
387 to avoid confusion, I'll refer to the requested peaks as visual_peaks and the peakfile peaks as stored_peaks
390 const uint32_t chunksize = (uint32_t) min (expected_peaks, 65536.0);
392 staging = new PeakData[chunksize];
394 /* compute the rounded up frame position */
396 nframes_t current_frame = start;
397 nframes_t current_stored_peak = (nframes_t) ceil (current_frame / (double) samples_per_file_peak);
398 uint32_t next_visual_peak = (uint32_t) ceil (current_frame / samples_per_visual_peak);
399 double next_visual_peak_frame = next_visual_peak * samples_per_visual_peak;
400 uint32_t stored_peak_before_next_visual_peak = (nframes_t) next_visual_peak_frame / samples_per_file_peak;
401 uint32_t nvisual_peaks = 0;
402 uint32_t stored_peaks_read = 0;
405 /* handle the case where the initial visual peak is on a pixel boundary */
407 current_stored_peak = min (current_stored_peak, stored_peak_before_next_visual_peak);
409 /* open ... close during out: handling */
411 if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
412 error << string_compose(_("AudioSource: cannot open peakpath (b) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
416 while (nvisual_peaks < npeaks) {
418 if (i == stored_peaks_read) {
420 uint32_t start_byte = current_stored_peak * sizeof(PeakData);
421 tnp = min ((_length/samples_per_file_peak - current_stored_peak), (nframes_t) expected_peaks);
422 to_read = min (chunksize, tnp);
424 #ifdef DEBUG_READ_PEAKS
425 cerr << "read " << sizeof (PeakData) * to_read << " from peakfile @ " << start_byte << endl;
428 if ((nread = ::pread (_peakfile, staging, sizeof (PeakData) * to_read, start_byte))
429 != sizeof (PeakData) * to_read) {
431 off_t fend = lseek (_peakfile, 0, SEEK_END);
433 cerr << "AudioSource["
435 << "]: cannot read peak data from peakfile ("
436 << (nread / sizeof(PeakData))
437 << " peaks instead of "
442 << " at start_byte = " << start_byte
443 << " _length = " << _length << " versus len = " << fend
444 << " expected maxpeaks = " << (_length - current_frame)/samples_per_file_peak
445 << " npeaks was " << npeaks
451 stored_peaks_read = nread / sizeof(PeakData);
457 while ((i < stored_peaks_read) && (current_stored_peak <= stored_peak_before_next_visual_peak)) {
459 xmax = max (xmax, staging[i].max);
460 xmin = min (xmin, staging[i].min);
462 ++current_stored_peak;
466 peaks[nvisual_peaks].max = xmax;
467 peaks[nvisual_peaks].min = xmin;
471 //next_visual_peak_frame = min ((next_visual_peak * samples_per_visual_peak), (next_visual_peak_frame+samples_per_visual_peak) );
472 next_visual_peak_frame = min ((double) start+cnt, (next_visual_peak_frame+samples_per_visual_peak) );
473 stored_peak_before_next_visual_peak = (uint32_t) next_visual_peak_frame / samples_per_file_peak;
477 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
484 #ifdef DEBUG_READ_PEAKS
485 cerr << "UPSAMPLE\n";
489 - less frames-per-peak (more resolution)
490 - more peaks than stored in the Peakfile
492 So, fetch data from the raw source, and generate peak
496 nframes_t frames_read = 0;
497 nframes_t current_frame = start;
499 nframes_t nvisual_peaks = 0;
500 nframes_t chunksize = (nframes_t) min (cnt, (nframes_t) 4096);
501 raw_staging = new Sample[chunksize];
503 nframes_t frame_pos = start;
504 double pixel_pos = floor (frame_pos / samples_per_visual_peak);
505 double next_pixel_pos = ceil (frame_pos / samples_per_visual_peak);
506 double pixels_per_frame = 1.0 / samples_per_visual_peak;
511 while (nvisual_peaks < npeaks) {
513 if (i == frames_read) {
515 to_read = min (chunksize, (_length - current_frame));
518 /* XXX ARGH .. out by one error ... need to figure out why this happens
519 and fix it rather than do this band-aid move.
521 zero_fill = npeaks - nvisual_peaks;
525 if ((frames_read = read_unlocked (raw_staging, current_frame, to_read)) == 0) {
526 error << string_compose(_("AudioSource[%1]: peak read - cannot read %2 samples at offset %3 of %4 (%5)"),
527 _name, to_read, current_frame, _length, strerror (errno))
535 xmax = max (xmax, raw_staging[i]);
536 xmin = min (xmin, raw_staging[i]);
539 pixel_pos += pixels_per_frame;
541 if (pixel_pos >= next_pixel_pos) {
543 peaks[nvisual_peaks].max = xmax;
544 peaks[nvisual_peaks].min = xmin;
549 next_pixel_pos = ceil (pixel_pos + 0.5);
554 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
561 if (_peakfile >= 0) {
570 delete [] raw_staging;
573 #ifdef DEBUG_READ_PEAKS
580 #undef DEBUG_PEAK_BUILD
583 AudioSource::build_peaks_from_scratch ()
585 nframes_t current_frame;
588 nframes_t frames_read;
589 nframes_t frames_to_read;
590 const nframes_t bufsize = 65536; // 256kB per disk read for mono data is about ideal
595 /* hold lock while building peaks */
597 Glib::Mutex::Lock lp (_lock);
599 if (prepare_for_peakfile_writes ()) {
605 _peaks_built = false;
606 buf = new Sample[bufsize];
610 frames_to_read = min (bufsize, cnt);
612 if ((frames_read = read_unlocked (buf, current_frame, frames_to_read)) != frames_to_read) {
613 error << string_compose(_("%1: could not write read raw data for peak computation (%2)"), _name, strerror (errno)) << endmsg;
614 done_with_peakfile_writes (false);
618 if (compute_and_write_peaks (buf, current_frame, frames_read, true, false, _FPP)) {
622 current_frame += frames_read;
631 done_with_peakfile_writes ((cnt == 0));
635 Glib::Mutex::Lock lm (_peaks_ready_lock);
638 PeaksReady (); /* EMIT SIGNAL */
645 unlink (peakpath.c_str());
656 AudioSource::prepare_for_peakfile_writes ()
658 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
659 error << string_compose(_("AudioSource: cannot open peakpath (c) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
666 AudioSource::done_with_peakfile_writes (bool done)
668 if (peak_leftover_cnt) {
669 compute_and_write_peaks (0, 0, 0, true, false, _FPP);
683 AudioSource::compute_and_write_peaks (Sample* buf, nframes_t first_frame, nframes_t cnt, bool force, bool intermediate_peaks_ready)
685 return compute_and_write_peaks (buf, first_frame, cnt, force, intermediate_peaks_ready, _FPP);
689 AudioSource::compute_and_write_peaks (Sample* buf, nframes_t first_frame, nframes_t cnt, bool force,
690 bool intermediate_peaks_ready, nframes_t fpp)
694 uint32_t peaks_computed;
695 PeakData* peakbuf = 0;
697 nframes_t current_frame;
698 nframes_t frames_done;
699 const size_t blocksize = (128 * 1024);
700 off_t first_peak_byte;
703 prepare_for_peakfile_writes ();
707 if (peak_leftover_cnt) {
709 if (first_frame != peak_leftover_frame + peak_leftover_cnt) {
711 /* uh-oh, ::seek() since the last ::compute_and_write_peaks(),
712 and we have leftovers. flush a single peak (since the leftovers
713 never represent more than that, and restart.
718 x.min = peak_leftovers[0];
719 x.max = peak_leftovers[0];
720 Session::find_peaks (peak_leftovers + 1, peak_leftover_cnt - 1, &x.min, &x.max);
722 off_t byte = (peak_leftover_frame / fpp) * sizeof (PeakData);
724 if (::pwrite (peakfile, &x, sizeof (PeakData), byte) != sizeof (PeakData)) {
725 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
729 _peak_byte_max = max (_peak_byte_max, (off_t) (byte + sizeof(PeakData)));
732 Glib::Mutex::Lock lm (_peaks_ready_lock);
733 PeakRangeReady (peak_leftover_frame, peak_leftover_cnt); /* EMIT SIGNAL */
734 if (intermediate_peaks_ready) {
735 PeaksReady (); /* EMIT SIGNAL */
739 /* left overs are done */
741 peak_leftover_cnt = 0;
745 /* else ... had leftovers, but they immediately preceed the new data, so just
746 merge them and compute.
749 /* make a new contiguous buffer containing leftovers and the new stuff */
751 to_do = cnt + peak_leftover_cnt;
752 buf2 = new Sample[to_do];
755 memcpy (buf2, peak_leftovers, peak_leftover_cnt * sizeof (Sample));
758 memcpy (buf2+peak_leftover_cnt, buf, cnt * sizeof (Sample));
760 /* no more leftovers */
761 peak_leftover_cnt = 0;
763 /* use the temporary buffer */
766 /* make sure that when we write into the peakfile, we startup where we left off */
768 first_frame = peak_leftover_frame;
774 peakbuf = new PeakData[(to_do/fpp)+1];
776 current_frame = first_frame;
781 /* if some frames were passed in (i.e. we're not flushing leftovers)
782 and there are less than fpp to do, save them till
786 if (force && (to_do < fpp)) {
787 /* keep the left overs around for next time */
789 if (peak_leftover_size < to_do) {
790 delete [] peak_leftovers;
791 peak_leftovers = new Sample[to_do];
792 peak_leftover_size = to_do;
794 memcpy (peak_leftovers, buf, to_do * sizeof (Sample));
795 peak_leftover_cnt = to_do;
796 peak_leftover_frame = current_frame;
803 nframes_t this_time = min (fpp, to_do);
805 peakbuf[peaks_computed].max = buf[0];
806 peakbuf[peaks_computed].min = buf[0];
808 Session::find_peaks (buf+1, this_time-1, &peakbuf[peaks_computed].min, &peakbuf[peaks_computed].max);
813 frames_done += this_time;
814 current_frame += this_time;
817 first_peak_byte = (first_frame / fpp) * sizeof (PeakData);
819 if (can_truncate_peaks()) {
821 /* on some filesystems (ext3, at least) this helps to reduce fragmentation of
822 the peakfiles. its not guaranteed to do so, and even on ext3 (as of december 2006)
823 it does not cause single-extent allocation even for peakfiles of
824 less than BLOCKSIZE bytes. only call ftruncate if we'll make the file larger.
827 off_t endpos = lseek (peakfile, 0, SEEK_END);
828 off_t target_length = blocksize * ((first_peak_byte + blocksize + 1) / blocksize);
830 if (endpos < target_length) {
831 ftruncate (peakfile, target_length);
832 /* error doesn't actually matter though, so continue on without testing */
836 if (::pwrite (peakfile, peakbuf, sizeof (PeakData) * peaks_computed, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaks_computed)) {
837 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
841 _peak_byte_max = max (_peak_byte_max, (off_t) (first_peak_byte + sizeof(PeakData)*peaks_computed));
844 Glib::Mutex::Lock lm (_peaks_ready_lock);
845 PeakRangeReady (first_frame, frames_done); /* EMIT SIGNAL */
846 if (intermediate_peaks_ready) {
847 PeaksReady (); /* EMIT SIGNAL */
862 AudioSource::truncate_peakfile ()
865 error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor")
870 /* truncate the peakfile down to its natural length if necessary */
872 off_t end = lseek (peakfile, 0, SEEK_END);
874 if (end > _peak_byte_max) {
875 ftruncate (peakfile, _peak_byte_max);
880 AudioSource::file_changed (ustring path)
882 struct stat stat_file;
883 struct stat stat_peak;
885 int e1 = stat (path.c_str(), &stat_file);
886 int e2 = stat (peak_path(path).c_str(), &stat_peak);
888 if (!e1 && !e2 && stat_file.st_mtime > stat_peak.st_mtime){
896 AudioSource::available_peaks (double zoom_factor) const
900 if (zoom_factor < _FPP) {
901 return length(); // peak data will come from the audio file
904 /* peak data comes from peakfile, but the filesize might not represent
905 the valid data due to ftruncate optimizations, so use _peak_byte_max state.
906 XXX - there might be some atomicity issues here, we should probably add a lock,
907 but _peak_byte_max only monotonically increases after initialization.
910 end = _peak_byte_max;
912 return (end/sizeof(PeakData)) * _FPP;
916 AudioSource::update_length (nframes_t pos, nframes_t cnt)
918 if (pos + cnt > _length) {
924 AudioSource::load_transients (const string& path)
926 ifstream file (path.c_str());
937 while (file.good()) {
941 nframes64_t frame = (nframes64_t) floor (val * _session.frame_rate());
942 transients.push_back (frame);
950 AudioSource::get_transients_path () const
952 vector<string> parts;
955 /* old sessions may not have the analysis directory */
957 _session.ensure_subdirs ();
959 s = _session.analysis_dir ();
964 s += X_("transients");
967 return Glib::build_filename (parts);