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.
18 $Id: source.cc 404 2006-03-17 17:39:21Z pauld $
34 #include <pbd/xml++.h>
35 #include <pbd/pthread_utils.h>
37 #include <ardour/audiosource.h>
42 using namespace ARDOUR;
45 pthread_t AudioSource::peak_thread;
46 bool AudioSource::have_peak_thread = false;
47 vector<boost::shared_ptr<AudioSource> > AudioSource::pending_peak_sources;
48 Glib::Mutex* AudioSource::pending_peak_sources_lock = 0;
49 int AudioSource::peak_request_pipe[2];
51 bool AudioSource::_build_missing_peakfiles = false;
52 bool AudioSource::_build_peakfiles = false;
54 AudioSource::AudioSource (Session& s, string name)
57 if (pending_peak_sources_lock == 0) {
58 pending_peak_sources_lock = new Glib::Mutex;
62 next_peak_clear_should_notify = true;
64 _write_data_count = 0;
67 AudioSource::AudioSource (Session& s, const XMLNode& node)
70 if (pending_peak_sources_lock == 0) {
71 pending_peak_sources_lock = new Glib::Mutex;
75 next_peak_clear_should_notify = true;
77 _write_data_count = 0;
79 if (set_state (node)) {
80 throw failed_constructor();
84 AudioSource::~AudioSource ()
89 AudioSource::get_state ()
91 XMLNode& node (Source::get_state());
93 if (_captured_for.length()) {
94 node.add_property ("captured-for", _captured_for);
101 AudioSource::set_state (const XMLNode& node)
103 const XMLProperty* prop;
105 Source::set_state (node);
107 if ((prop = node.property ("captured-for")) != 0) {
108 _captured_for = prop->value();
114 /***********************************************************************
116 ***********************************************************************/
119 AudioSource::peak_thread_work (void* arg)
121 PBD::ThreadCreated (pthread_self(), X_("Peak"));
122 struct pollfd pfd[1];
124 if (pending_peak_sources_lock == 0) {
125 pending_peak_sources_lock = new Glib::Mutex;
128 Glib::Mutex::Lock lm (*pending_peak_sources_lock);
132 pfd[0].fd = peak_request_pipe[0];
133 pfd[0].events = POLLIN|POLLERR|POLLHUP;
135 pending_peak_sources_lock->unlock ();
137 if (poll (pfd, 1, -1) < 0) {
139 if (errno == EINTR) {
140 pending_peak_sources_lock->lock ();
144 error << string_compose (_("poll on peak request pipe failed (%1)"),
150 if (pfd[0].revents & ~POLLIN) {
151 error << _("Error on peak thread request pipe") << endmsg;
155 if (pfd[0].revents & POLLIN) {
159 /* empty the pipe of all current requests */
162 size_t nread = ::read (peak_request_pipe[0], &req, sizeof (req));
165 switch ((PeakRequest::Type) req) {
167 case PeakRequest::Build:
170 case PeakRequest::Quit:
171 pthread_exit_pbd (0);
179 } else if (nread == 0) {
181 } else if (errno == EAGAIN) {
184 fatal << _("Error reading from peak request pipe") << endmsg;
190 pending_peak_sources_lock->lock ();
192 while (!pending_peak_sources.empty()) {
194 boost::shared_ptr<AudioSource> s = pending_peak_sources.front();
195 pending_peak_sources.erase (pending_peak_sources.begin());
197 pending_peak_sources_lock->unlock ();
199 pending_peak_sources_lock->lock ();
203 pthread_exit_pbd (0);
209 AudioSource::start_peak_thread ()
211 if (!_build_peakfiles) {
215 if (pipe (peak_request_pipe)) {
216 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
220 if (fcntl (peak_request_pipe[0], F_SETFL, O_NONBLOCK)) {
221 error << string_compose(_("UI: cannot set O_NONBLOCK on peak request pipe (%1)"), strerror (errno)) << endmsg;
225 if (fcntl (peak_request_pipe[1], F_SETFL, O_NONBLOCK)) {
226 error << string_compose(_("UI: cannot set O_NONBLOCK on peak request pipe (%1)"), strerror (errno)) << endmsg;
230 if (pthread_create_and_store ("peak file builder", &peak_thread, 0, peak_thread_work, 0)) {
231 error << _("AudioSource: could not create peak thread") << endmsg;
235 have_peak_thread = true;
240 AudioSource::stop_peak_thread ()
242 if (!have_peak_thread) {
248 char c = (char) PeakRequest::Quit;
249 ::write (peak_request_pipe[1], &c, 1);
250 pthread_join (peak_thread, &status);
254 AudioSource::queue_for_peaks (boost::shared_ptr<AudioSource> source)
256 if (have_peak_thread) {
258 Glib::Mutex::Lock lm (*pending_peak_sources_lock);
260 source->next_peak_clear_should_notify = true;
262 if (find (pending_peak_sources.begin(),
263 pending_peak_sources.end(),
264 source) == pending_peak_sources.end()) {
265 pending_peak_sources.push_back (source);
268 char c = (char) PeakRequest::Build;
269 ::write (peak_request_pipe[1], &c, 1);
273 void AudioSource::clear_queue_for_peaks ()
275 /* this is done to cancel a group of running peak builds */
276 if (have_peak_thread) {
277 Glib::Mutex::Lock lm (*pending_peak_sources_lock);
278 pending_peak_sources.clear ();
284 AudioSource::peaks_ready (sigc::slot<void> the_slot, sigc::connection& conn) const
287 Glib::Mutex::Lock lm (_lock);
289 /* check to see if the peak data is ready. if not
290 connect the slot while still holding the lock.
293 if (!(ret = _peaks_built)) {
294 conn = PeaksReady.connect (the_slot);
301 AudioSource::touch_peakfile ()
305 if (stat (peakpath.c_str(), &statbuf) != 0 || statbuf.st_size == 0) {
311 tbuf.actime = statbuf.st_atime;
312 tbuf.modtime = time ((time_t) 0);
314 utime (peakpath.c_str(), &tbuf);
318 AudioSource::rename_peakfile (string newpath)
320 /* caller must hold _lock */
322 string oldpath = peakpath;
324 if (access (oldpath.c_str(), F_OK) == 0) {
325 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
326 error << string_compose (_("cannot rename peakfile for %1 from %2 to %3 (%4)"), _name, oldpath, newpath, strerror (errno)) << endmsg;
337 AudioSource::initialize_peakfile (bool newfile, string audio_path)
341 peakpath = peak_path (audio_path);
343 /* Nasty band-aid for older sessions that were created before we
344 used libsndfile for all audio files.
347 if (!newfile && access (peakpath.c_str(), R_OK) != 0) {
348 string str = old_peak_path (audio_path);
349 if (access (str.c_str(), R_OK) == 0) {
356 if (!_build_peakfiles) {
360 _peaks_built = false;
364 if (stat (peakpath.c_str(), &statbuf)) {
365 if (errno != ENOENT) {
366 /* it exists in the peaks dir, but there is some kind of error */
368 error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg;
373 /* we found it in the peaks dir */
376 if (statbuf.st_size == 0) {
377 _peaks_built = false;
379 // Check if the audio file has changed since the peakfile was built.
380 struct stat stat_file;
381 int err = stat (audio_path.c_str(), &stat_file);
383 if (!err && stat_file.st_mtime > statbuf.st_mtime){
384 _peaks_built = false;
391 if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) {
392 build_peaks_from_scratch ();
399 AudioSource::read (Sample *dst, nframes_t start, nframes_t cnt) const
401 Glib::Mutex::Lock lm (_lock);
402 return read_unlocked (dst, start, cnt);
406 AudioSource::write (Sample *dst, nframes_t cnt)
408 Glib::Mutex::Lock lm (_lock);
409 return write_unlocked (dst, cnt);
413 AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_visual_peak) const
415 Glib::Mutex::Lock lm (_lock);
417 double expected_peaks;
418 PeakData::PeakDatum xmax;
419 PeakData::PeakDatum xmin;
422 nframes_t zero_fill = 0;
424 PeakData* staging = 0;
425 Sample* raw_staging = 0;
428 expected_peaks = (cnt / (double) frames_per_peak);
429 scale = npeaks/expected_peaks;
432 cerr << "======>RP: npeaks = " << npeaks
433 << " start = " << start
435 << " len = " << _length
436 << " samples_per_visual_peak =" << samples_per_visual_peak
437 << " expected was " << expected_peaks << " ... scale = " << scale
438 << " PD ptr = " << peaks
443 /* fix for near-end-of-file conditions */
445 if (cnt > _length - start) {
446 // cerr << "too close to end @ " << _length << " given " << start << " + " << cnt << endl;
447 cnt = _length - start;
448 nframes_t old = npeaks;
449 npeaks = min ((nframes_t) floor (cnt / samples_per_visual_peak), npeaks);
450 zero_fill = old - npeaks;
453 // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
457 cerr << "RAW DATA\n";
459 /* no scaling at all, just get the sample data and duplicate it for
460 both max and min peak values.
463 Sample* raw_staging = new Sample[cnt];
465 if (read_unlocked (raw_staging, start, cnt) != cnt) {
466 error << _("cannot read sample data for unscaled peak computation") << endmsg;
470 for (nframes_t i = 0; i < npeaks; ++i) {
471 peaks[i].max = raw_staging[i];
472 peaks[i].min = raw_staging[i];
475 delete [] raw_staging;
481 off_t first_peak_byte = (start / frames_per_peak) * sizeof (PeakData);
483 /* open, read, close */
485 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
486 error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
490 // cerr << "DIRECT PEAKS\n";
492 nread = ::pread (peakfile, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
495 if (nread != sizeof (PeakData) * npeaks) {
496 cerr << "AudioSource["
498 << "]: cannot read peaks from peakfile! (read only "
512 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
523 // cerr << "DOWNSAMPLE\n";
527 - more frames-per-peak (lower resolution) than the peakfile, or to put it another way,
528 - less peaks than the peakfile holds for the same range
530 So, read a block into a staging area, and then downsample from there.
532 to avoid confusion, I'll refer to the requested peaks as visual_peaks and the peakfile peaks as stored_peaks
535 const uint32_t chunksize = (uint32_t) min (expected_peaks, 4096.0);
537 staging = new PeakData[chunksize];
539 /* compute the rounded up frame position */
541 nframes_t current_frame = start;
542 nframes_t current_stored_peak = (nframes_t) ceil (current_frame / (double) frames_per_peak);
543 uint32_t next_visual_peak = (uint32_t) ceil (current_frame / samples_per_visual_peak);
544 double next_visual_peak_frame = next_visual_peak * samples_per_visual_peak;
545 uint32_t stored_peak_before_next_visual_peak = (nframes_t) next_visual_peak_frame / frames_per_peak;
546 uint32_t nvisual_peaks = 0;
547 uint32_t stored_peaks_read = 0;
550 /* handle the case where the initial visual peak is on a pixel boundary */
552 current_stored_peak = min (current_stored_peak, stored_peak_before_next_visual_peak);
554 /* open ... close during out: handling */
556 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
557 error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
561 while (nvisual_peaks < npeaks) {
563 if (i == stored_peaks_read) {
565 uint32_t start_byte = current_stored_peak * sizeof(PeakData);
566 tnp = min ((_length/frames_per_peak - current_stored_peak), (nframes_t) expected_peaks);
567 to_read = min (chunksize, tnp);
569 off_t fend = lseek (peakfile, 0, SEEK_END);
571 if ((nread = ::pread (peakfile, staging, sizeof (PeakData) * to_read, start_byte))
572 != sizeof (PeakData) * to_read) {
573 cerr << "AudioSource["
575 << "]: cannot read peak data from peakfile ("
576 << (nread / sizeof(PeakData))
577 << " peaks instead of "
582 << " at start_byte = " << start_byte
583 << " _length = " << _length << " versus len = " << fend
584 << " expected maxpeaks = " << (_length - current_frame)/frames_per_peak
585 << " npeaks was " << npeaks
591 stored_peaks_read = nread / sizeof(PeakData);
597 while ((i < stored_peaks_read) && (current_stored_peak <= stored_peak_before_next_visual_peak)) {
599 xmax = max (xmax, staging[i].max);
600 xmin = min (xmin, staging[i].min);
602 ++current_stored_peak;
606 peaks[nvisual_peaks].max = xmax;
607 peaks[nvisual_peaks].min = xmin;
611 //next_visual_peak_frame = min ((next_visual_peak * samples_per_visual_peak), (next_visual_peak_frame+samples_per_visual_peak) );
612 next_visual_peak_frame = min ((double) start+cnt, (next_visual_peak_frame+samples_per_visual_peak) );
613 stored_peak_before_next_visual_peak = (uint32_t) next_visual_peak_frame / frames_per_peak;
617 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
624 // cerr << "UPSAMPLE\n";
628 - less frames-per-peak (more resolution)
629 - more peaks than stored in the Peakfile
631 So, fetch data from the raw source, and generate peak
635 nframes_t frames_read = 0;
636 nframes_t current_frame = start;
638 nframes_t nvisual_peaks = 0;
639 nframes_t chunksize = (nframes_t) min (cnt, (nframes_t) 4096);
640 raw_staging = new Sample[chunksize];
642 nframes_t frame_pos = start;
643 double pixel_pos = floor (frame_pos / samples_per_visual_peak);
644 double next_pixel_pos = ceil (frame_pos / samples_per_visual_peak);
645 double pixels_per_frame = 1.0 / samples_per_visual_peak;
650 while (nvisual_peaks < npeaks) {
652 if (i == frames_read) {
654 to_read = min (chunksize, (_length - current_frame));
656 if ((frames_read = read_unlocked (raw_staging, current_frame, to_read)) == 0) {
657 error << string_compose(_("AudioSource[%1]: peak read - cannot read %2 samples at offset %3")
658 , _name, to_read, current_frame)
666 xmax = max (xmax, raw_staging[i]);
667 xmin = min (xmin, raw_staging[i]);
670 pixel_pos += pixels_per_frame;
672 if (pixel_pos >= next_pixel_pos) {
674 peaks[nvisual_peaks].max = xmax;
675 peaks[nvisual_peaks].min = xmin;
680 next_pixel_pos = ceil (pixel_pos + 0.5);
685 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
701 delete [] raw_staging;
707 #define DEBUG_PEAK_BUILD
710 AudioSource::build_peaks ()
712 vector<PeakBuildRecord*> built;
714 bool pr_signal = false;
715 list<PeakBuildRecord*> copy;
718 Glib::Mutex::Lock lm (_lock);
719 copy = pending_peak_builds;
720 pending_peak_builds.clear ();
723 #ifdef DEBUG_PEAK_BUILD
724 cerr << "build peaks with " << copy.size() << " requests pending\n";
727 for (list<PeakBuildRecord *>::iterator i = copy.begin(); i != copy.end(); ++i) {
729 if ((status = do_build_peak ((*i)->frame, (*i)->cnt)) != 0) {
730 unlink (peakpath.c_str());
733 built.push_back (new PeakBuildRecord (*(*i)));
738 Glib::Mutex::Lock lm (_lock);
743 if (next_peak_clear_should_notify) {
744 next_peak_clear_should_notify = false;
751 for (vector<PeakBuildRecord *>::iterator i = built.begin(); i != built.end(); ++i) {
752 PeakRangeReady ((*i)->frame, (*i)->cnt); /* EMIT SIGNAL */
757 PeaksReady (); /* EMIT SIGNAL */
765 AudioSource::do_build_peak (nframes_t first_frame, nframes_t cnt)
767 nframes_t current_frame;
768 Sample buf[frames_per_peak];
772 nframes_t frames_read;
773 nframes_t frames_to_read;
774 off_t first_peak_byte;
778 #ifdef DEBUG_PEAK_BUILD
779 cerr << pthread_self() << ": " << _name << ": building peaks for " << first_frame << " to " << first_frame + cnt - 1 << endl;
782 first_peak_byte = (first_frame / frames_per_peak) * sizeof (PeakData);
784 #ifdef DEBUG_PEAK_BUILD
785 cerr << "seeking to " << first_peak_byte << " before writing new peak data\n";
788 current_frame = first_frame;
789 peakbuf = new PeakData[(cnt/frames_per_peak)+1];
792 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
793 error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
799 frames_to_read = min (frames_per_peak, cnt);
801 /* lock for every read */
803 if ((frames_read = read (buf, current_frame, frames_to_read)) != frames_to_read) {
804 error << string_compose(_("%1: could not write read raw data for peak computation (%2)"), _name, strerror (errno)) << endmsg;
811 for (nframes_t n = 1; n < frames_read; ++n) {
812 xmax = max (xmax, buf[n]);
813 xmin = min (xmin, buf[n]);
815 // if (current_frame < frames_read) {
816 // cerr << "sample = " << buf[n] << " max = " << xmax << " min = " << xmin << " max of 2 = " << max (xmax, buf[n]) << endl;
820 peakbuf[peaki].max = xmax;
821 peakbuf[peaki].min = xmin;
824 current_frame += frames_read;
828 if (::pwrite (peakfile, peakbuf, sizeof (PeakData) * peaki, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaki)) {
829 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
844 AudioSource::build_peaks_from_scratch ()
846 Glib::Mutex::Lock lp (_lock);
848 next_peak_clear_should_notify = true;
849 pending_peak_builds.push_back (new PeakBuildRecord (0, _length));
850 queue_for_peaks (shared_from_this());
854 AudioSource::file_changed (string path)
856 struct stat stat_file;
857 struct stat stat_peak;
859 int e1 = stat (path.c_str(), &stat_file);
860 int e2 = stat (peak_path(path).c_str(), &stat_peak);
862 if (!e1 && !e2 && stat_file.st_mtime > stat_peak.st_mtime){
870 AudioSource::available_peaks (double zoom_factor) const
875 if (zoom_factor < frames_per_peak) {
876 return length(); // peak data will come from the audio file
879 /* peak data comes from peakfile */
881 if ((peakfile = ::open (peakpath.c_str(), O_RDONLY)) < 0) {
882 error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
887 Glib::Mutex::Lock lm (_lock);
888 end = lseek (peakfile, 0, SEEK_END);
893 return (end/sizeof(PeakData)) * frames_per_peak;
897 AudioSource::update_length (nframes_t pos, nframes_t cnt)
899 if (pos + cnt > _length) {