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 <glibmm/thread.h>
33 #include <pbd/xml++.h>
34 #include <pbd/pthread_utils.h>
36 #include <ardour/source.h>
43 using namespace ARDOUR;
45 sigc::signal<void,Source *> Source::SourceCreated;
46 pthread_t Source::peak_thread;
47 bool Source::have_peak_thread = false;
48 vector<Source*> Source::pending_peak_sources;
49 Glib::StaticMutex Source::pending_peak_sources_lock = GLIBMM_STATIC_MUTEX_INIT;
50 int Source::peak_request_pipe[2];
52 bool Source::_build_missing_peakfiles = false;
53 bool Source::_build_peakfiles = false;
55 Source::Source (bool announce)
57 _id = ARDOUR::new_id();
60 next_peak_clear_should_notify = true;
63 _write_data_count = 0;
66 Source::Source (const XMLNode& node)
70 next_peak_clear_should_notify = true;
73 _write_data_count = 0;
75 if (set_state (node)) {
76 throw failed_constructor();
87 XMLNode *node = new XMLNode ("Source");
90 node->add_property ("name", _name);
91 snprintf (buf, sizeof(buf)-1, "%" PRIu64, _id);
92 node->add_property ("id", buf);
94 if (_timestamp != 0) {
95 snprintf (buf, sizeof (buf), "%ld", _timestamp);
96 node->add_property ("timestamp", buf);
99 if (_captured_for.length()) {
100 node->add_property ("captured-for", _captured_for);
107 Source::set_state (const XMLNode& node)
109 const XMLProperty* prop;
111 if ((prop = node.property ("name")) != 0) {
112 _name = prop->value();
117 if ((prop = node.property ("id")) != 0) {
118 sscanf (prop->value().c_str(), "%" PRIu64, &_id);
123 if ((prop = node.property ("timestamp")) != 0) {
124 sscanf (prop->value().c_str(), "%ld", &_timestamp);
127 if ((prop = node.property ("captured-for")) != 0) {
128 _captured_for = prop->value();
134 /***********************************************************************
136 ***********************************************************************/
139 Source::peak_thread_work (void* arg)
141 PBD::ThreadCreated (pthread_self(), X_("Peak"));
142 struct pollfd pfd[1];
144 Glib::Mutex::Lock lm (pending_peak_sources_lock);
148 pfd[0].fd = peak_request_pipe[0];
149 pfd[0].events = POLLIN|POLLERR|POLLHUP;
151 pending_peak_sources_lock.unlock();
153 if (poll (pfd, 1, -1) < 0) {
155 if (errno == EINTR) {
156 pending_peak_sources_lock.lock();
160 error << string_compose (_("poll on peak request pipe failed (%1)"),
166 if (pfd[0].revents & ~POLLIN) {
167 error << _("Error on peak thread request pipe") << endmsg;
171 if (pfd[0].revents & POLLIN) {
175 /* empty the pipe of all current requests */
178 size_t nread = ::read (peak_request_pipe[0], &req, sizeof (req));
181 switch ((PeakRequest::Type) req) {
183 case PeakRequest::Build:
186 case PeakRequest::Quit:
187 pthread_exit_pbd (0);
195 } else if (nread == 0) {
197 } else if (errno == EAGAIN) {
200 fatal << _("Error reading from peak request pipe") << endmsg;
206 pending_peak_sources_lock.lock();
208 while (!pending_peak_sources.empty()) {
210 Source* s = pending_peak_sources.front();
211 pending_peak_sources.erase (pending_peak_sources.begin());
213 pending_peak_sources_lock.unlock();
215 pending_peak_sources_lock.lock();
219 pthread_exit_pbd (0);
225 Source::start_peak_thread ()
227 if (!_build_peakfiles) {
231 if (pipe (peak_request_pipe)) {
232 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
236 if (fcntl (peak_request_pipe[0], F_SETFL, O_NONBLOCK)) {
237 error << string_compose(_("UI: cannot set O_NONBLOCK on peak request pipe (%1)"), strerror (errno)) << endmsg;
241 if (fcntl (peak_request_pipe[1], F_SETFL, O_NONBLOCK)) {
242 error << string_compose(_("UI: cannot set O_NONBLOCK on peak request pipe (%1)"), strerror (errno)) << endmsg;
246 if (pthread_create_and_store ("peak file builder", &peak_thread, 0, peak_thread_work, 0)) {
247 error << _("Source: could not create peak thread") << endmsg;
251 have_peak_thread = true;
256 Source::stop_peak_thread ()
258 if (!have_peak_thread) {
264 char c = (char) PeakRequest::Quit;
265 ::write (peak_request_pipe[1], &c, 1);
266 pthread_join (peak_thread, &status);
270 Source::queue_for_peaks (Source& source)
272 if (have_peak_thread) {
274 Glib::Mutex::Lock lm (pending_peak_sources_lock);
276 source.next_peak_clear_should_notify = true;
278 if (find (pending_peak_sources.begin(),
279 pending_peak_sources.end(),
280 &source) == pending_peak_sources.end()) {
281 pending_peak_sources.push_back (&source);
284 char c = (char) PeakRequest::Build;
285 ::write (peak_request_pipe[1], &c, 1);
289 void Source::clear_queue_for_peaks ()
291 /* this is done to cancel a group of running peak builds */
292 if (have_peak_thread) {
293 Glib::Mutex::Lock lm (pending_peak_sources_lock);
294 pending_peak_sources.clear ();
300 Source::peaks_ready (sigc::slot<void> the_slot, sigc::connection& conn) const
303 Glib::Mutex::Lock lm (_lock);
305 /* check to see if the peak data is ready. if not
306 connect the slot while still holding the lock.
309 if (!(ret = _peaks_built)) {
310 conn = PeaksReady.connect (the_slot);
317 Source::rename_peakfile (string newpath)
319 /* caller must hold _lock */
321 string oldpath = peakpath;
323 if (access (oldpath.c_str(), F_OK) == 0) {
324 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
325 error << string_compose (_("cannot rename peakfile for %1 from %2 to %3 (%4)"), _name, oldpath, newpath, strerror (errno)) << endmsg;
336 Source::initialize_peakfile (bool newfile, string audio_path)
340 peakpath = peak_path (audio_path);
344 if (!_build_peakfiles) {
348 _peaks_built = false;
352 if (stat (peakpath.c_str(), &statbuf)) {
353 if (errno != ENOENT) {
354 /* it exists in the peaks dir, but there is some kind of error */
356 error << string_compose(_("Source: cannot stat peakfile \"%1\""), peakpath) << endmsg;
362 /* we found it in the peaks dir */
365 if (statbuf.st_size == 0) {
366 _peaks_built = false;
368 // Check if the audio file has changed since the peakfile was built.
369 struct stat stat_file;
370 int err = stat (audio_path.c_str(), &stat_file);
372 if (!err && stat_file.st_mtime > statbuf.st_mtime){
373 _peaks_built = false;
380 if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) {
381 build_peaks_from_scratch ();
388 Source::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t start, jack_nframes_t cnt, double samples_per_visual_peak) const
390 Glib::Mutex::Lock lm (_lock);
392 double expected_peaks;
393 PeakData::PeakDatum xmax;
394 PeakData::PeakDatum xmin;
397 jack_nframes_t zero_fill = 0;
399 PeakData* staging = 0;
400 Sample* raw_staging = 0;
404 expected_peaks = (cnt / (double) frames_per_peak);
405 scale = npeaks/expected_peaks;
408 cerr << "======>RP: npeaks = " << npeaks
409 << " start = " << start
411 << " len = " << _length
412 << " samples_per_visual_peak =" << samples_per_visual_peak
413 << " expected was " << expected_peaks << " ... scale = " << scale
414 << " PD ptr = " << peaks
419 /* fix for near-end-of-file conditions */
421 if (cnt > _length - start) {
422 // cerr << "too close to end @ " << _length << " given " << start << " + " << cnt << endl;
423 cnt = _length - start;
424 jack_nframes_t old = npeaks;
425 npeaks = min ((jack_nframes_t) floor (cnt / samples_per_visual_peak), npeaks);
426 zero_fill = old - npeaks;
429 // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
433 // cerr << "RAW DATA\n";
435 /* no scaling at all, just get the sample data and duplicate it for
436 both max and min peak values.
439 Sample* raw_staging = new Sample[cnt];
440 workbuf = new char[cnt*4];
442 if (read_unlocked (raw_staging, start, cnt, workbuf) != cnt) {
443 error << _("cannot read sample data for unscaled peak computation") << endmsg;
447 for (jack_nframes_t i = 0; i < npeaks; ++i) {
448 peaks[i].max = raw_staging[i];
449 peaks[i].min = raw_staging[i];
452 delete [] raw_staging;
459 off_t first_peak_byte = (start / frames_per_peak) * sizeof (PeakData);
461 /* open, read, close */
463 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
464 error << string_compose(_("Source: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
468 // cerr << "DIRECT PEAKS\n";
470 nread = ::pread (peakfile, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
473 if (nread != sizeof (PeakData) * npeaks) {
476 << "]: cannot read peaks from peakfile! (read only "
490 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
501 // cerr << "DOWNSAMPLE\n";
505 - more frames-per-peak (lower resolution) than the peakfile, or to put it another way,
506 - less peaks than the peakfile holds for the same range
508 So, read a block into a staging area, and then downsample from there.
510 to avoid confusion, I'll refer to the requested peaks as visual_peaks and the peakfile peaks as stored_peaks
513 const uint32_t chunksize = (uint32_t) min (expected_peaks, 4096.0);
515 staging = new PeakData[chunksize];
517 /* compute the rounded up frame position */
519 jack_nframes_t current_frame = start;
520 jack_nframes_t current_stored_peak = (jack_nframes_t) ceil (current_frame / (double) frames_per_peak);
521 uint32_t next_visual_peak = (uint32_t) ceil (current_frame / samples_per_visual_peak);
522 double next_visual_peak_frame = next_visual_peak * samples_per_visual_peak;
523 uint32_t stored_peak_before_next_visual_peak = (jack_nframes_t) next_visual_peak_frame / frames_per_peak;
524 uint32_t nvisual_peaks = 0;
525 uint32_t stored_peaks_read = 0;
528 /* handle the case where the initial visual peak is on a pixel boundary */
530 current_stored_peak = min (current_stored_peak, stored_peak_before_next_visual_peak);
532 /* open ... close during out: handling */
534 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
535 error << string_compose(_("Source: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
539 while (nvisual_peaks < npeaks) {
541 if (i == stored_peaks_read) {
543 uint32_t start_byte = current_stored_peak * sizeof(PeakData);
544 tnp = min ((_length/frames_per_peak - current_stored_peak), (jack_nframes_t) expected_peaks);
545 to_read = min (chunksize, tnp);
547 off_t fend = lseek (peakfile, 0, SEEK_END);
549 if ((nread = ::pread (peakfile, staging, sizeof (PeakData) * to_read, start_byte))
550 != sizeof (PeakData) * to_read) {
553 << "]: cannot read peak data from peakfile ("
554 << (nread / sizeof(PeakData))
555 << " peaks instead of "
560 << " at start_byte = " << start_byte
561 << " _length = " << _length << " versus len = " << fend
562 << " expected maxpeaks = " << (_length - current_frame)/frames_per_peak
563 << " npeaks was " << npeaks
569 stored_peaks_read = nread / sizeof(PeakData);
575 while ((i < stored_peaks_read) && (current_stored_peak <= stored_peak_before_next_visual_peak)) {
577 xmax = max (xmax, staging[i].max);
578 xmin = min (xmin, staging[i].min);
580 ++current_stored_peak;
584 peaks[nvisual_peaks].max = xmax;
585 peaks[nvisual_peaks].min = xmin;
589 //next_visual_peak_frame = min ((next_visual_peak * samples_per_visual_peak), (next_visual_peak_frame+samples_per_visual_peak) );
590 next_visual_peak_frame = min ((double) start+cnt, (next_visual_peak_frame+samples_per_visual_peak) );
591 stored_peak_before_next_visual_peak = (uint32_t) next_visual_peak_frame / frames_per_peak;
595 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
602 // cerr << "UPSAMPLE\n";
606 - less frames-per-peak (more resolution)
607 - more peaks than stored in the Peakfile
609 So, fetch data from the raw source, and generate peak
613 jack_nframes_t frames_read = 0;
614 jack_nframes_t current_frame = start;
615 jack_nframes_t i = 0;
616 jack_nframes_t nvisual_peaks = 0;
617 jack_nframes_t chunksize = (jack_nframes_t) min (cnt, (jack_nframes_t) 4096);
618 raw_staging = new Sample[chunksize];
619 workbuf = new char[chunksize *4];
621 jack_nframes_t frame_pos = start;
622 double pixel_pos = floor (frame_pos / samples_per_visual_peak);
623 double next_pixel_pos = ceil (frame_pos / samples_per_visual_peak);
624 double pixels_per_frame = 1.0 / samples_per_visual_peak;
629 while (nvisual_peaks < npeaks) {
631 if (i == frames_read) {
633 to_read = min (chunksize, (_length - current_frame));
635 if ((frames_read = read_unlocked (raw_staging, current_frame, to_read, workbuf)) < 0) {
636 error << string_compose(_("Source[%1]: peak read - cannot read %2 samples at offset %3")
637 , _name, to_read, current_frame)
645 xmax = max (xmax, raw_staging[i]);
646 xmin = min (xmin, raw_staging[i]);
649 pixel_pos += pixels_per_frame;
651 if (pixel_pos >= next_pixel_pos) {
653 peaks[nvisual_peaks].max = xmax;
654 peaks[nvisual_peaks].min = xmin;
659 next_pixel_pos = ceil (pixel_pos + 0.5);
664 memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
680 delete [] raw_staging;
690 #undef DEBUG_PEAK_BUILD
693 Source::build_peaks ()
695 vector<PeakBuildRecord*> built;
697 bool pr_signal = false;
698 list<PeakBuildRecord*> copy;
701 Glib::Mutex::Lock lm (_lock);
702 copy = pending_peak_builds;
703 pending_peak_builds.clear ();
706 #ifdef DEBUG_PEAK_BUILD
707 cerr << "build peaks with " << copy.size() << " requests pending\n";
710 for (list<PeakBuildRecord *>::iterator i = copy.begin(); i != copy.end(); ++i) {
712 if ((status = do_build_peak ((*i)->frame, (*i)->cnt)) != 0) {
713 unlink (peakpath.c_str());
716 built.push_back (new PeakBuildRecord (*(*i)));
721 Glib::Mutex::Lock lm (_lock);
726 if (next_peak_clear_should_notify) {
727 next_peak_clear_should_notify = false;
734 for (vector<PeakBuildRecord *>::iterator i = built.begin(); i != built.end(); ++i) {
735 PeakRangeReady ((*i)->frame, (*i)->cnt); /* EMIT SIGNAL */
740 PeaksReady (); /* EMIT SIGNAL */
748 Source::do_build_peak (jack_nframes_t first_frame, jack_nframes_t cnt)
750 jack_nframes_t current_frame;
751 Sample buf[frames_per_peak];
756 jack_nframes_t frames_read;
757 jack_nframes_t frames_to_read;
758 off_t first_peak_byte;
762 #ifdef DEBUG_PEAK_BUILD
763 cerr << pthread_self() << ": " << _name << ": building peaks for " << first_frame << " to " << first_frame + cnt - 1 << endl;
766 first_peak_byte = (first_frame / frames_per_peak) * sizeof (PeakData);
768 #ifdef DEBUG_PEAK_BUILD
769 cerr << "seeking to " << first_peak_byte << " before writing new peak data\n";
772 current_frame = first_frame;
773 peakbuf = new PeakData[(cnt/frames_per_peak)+1];
776 workbuf = new char[max(frames_per_peak, cnt) * 4];
778 if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
779 error << string_compose(_("Source: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
785 frames_to_read = min (frames_per_peak, cnt);
787 if ((frames_read = read_unlocked (buf, current_frame, frames_to_read, workbuf)) != frames_to_read) {
788 error << string_compose(_("%1: could not write read raw data for peak computation (%2)"), _name, strerror (errno)) << endmsg;
795 for (jack_nframes_t n = 1; n < frames_read; ++n) {
796 xmax = max (xmax, buf[n]);
797 xmin = min (xmin, buf[n]);
799 // if (current_frame < frames_read) {
800 // cerr << "sample = " << buf[n] << " max = " << xmax << " min = " << xmin << " max of 2 = " << max (xmax, buf[n]) << endl;
804 peakbuf[peaki].max = xmax;
805 peakbuf[peaki].min = xmin;
808 current_frame += frames_read;
812 if (::pwrite (peakfile, peakbuf, sizeof (PeakData) * peaki, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaki)) {
813 error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
830 Source::build_peaks_from_scratch ()
832 Glib::Mutex::Lock lp (_lock);
834 next_peak_clear_should_notify = true;
835 pending_peak_builds.push_back (new PeakBuildRecord (0, _length));
836 queue_for_peaks (*this);
840 Source::file_changed (string path)
842 struct stat stat_file;
843 struct stat stat_peak;
845 int e1 = stat (path.c_str(), &stat_file);
846 int e2 = stat (peak_path(path).c_str(), &stat_peak);
848 if (!e1 && !e2 && stat_file.st_mtime > stat_peak.st_mtime){
864 if (_use_cnt) --_use_cnt;
868 Source::available_peaks (double zoom_factor) const
873 if (zoom_factor < frames_per_peak) {
874 return length(); // peak data will come from the audio file
877 /* peak data comes from peakfile */
879 if ((peakfile = ::open (peakpath.c_str(), O_RDONLY)) < 0) {
880 error << string_compose(_("Source: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
885 Glib::Mutex::Lock lm (_lock);
886 end = lseek (peakfile, 0, SEEK_END);
891 return (end/sizeof(PeakData)) * frames_per_peak;