X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Faudiosource.cc;h=93e201b8c2843171a86208fd77338b0d0de793a1;hb=8713667ec1a6cc9ba56c07f763e5a422cc47fbef;hp=a2ce7209f6f634e0802b71f69b4dd2d0e6cd8071;hpb=f7f9d6fdc40248b190ec9c6e1a886261d55777ae;p=ardour.git diff --git a/libs/ardour/audiosource.cc b/libs/ardour/audiosource.cc index a2ce7209f6..93e201b8c2 100644 --- a/libs/ardour/audiosource.cc +++ b/libs/ardour/audiosource.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2000 Paul Davis + Copyright (C) 2000 Paul Davis This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -27,17 +27,21 @@ #include #include #include +#include #include #include #include +#include -#include -#include +#include "pbd/xml++.h" +#include "pbd/pthread_utils.h" -#include -#include -#include +#include "ardour/audiosource.h" +#include "ardour/cycle_timer.h" +#include "ardour/session.h" +#include "ardour/transient_detector.h" +#include "ardour/runtime_functions.h" #include "i18n.h" @@ -47,10 +51,15 @@ using namespace PBD; using Glib::ustring; bool AudioSource::_build_missing_peakfiles = false; + +/** true if we want peakfiles (e.g. if we are displaying a GUI) */ bool AudioSource::_build_peakfiles = false; +#define _FPP 256 + AudioSource::AudioSource (Session& s, ustring name) - : Source (s, name, DataType::AUDIO) + : Source (s, DataType::AUDIO, name) + , _length (0) { _peaks_built = false; _peak_byte_max = 0; @@ -62,9 +71,11 @@ AudioSource::AudioSource (Session& s, ustring name) peak_leftovers = 0; } -AudioSource::AudioSource (Session& s, const XMLNode& node) +AudioSource::AudioSource (Session& s, const XMLNode& node) : Source (s, node) + , _length (0) { + _peaks_built = false; _peak_byte_max = 0; peakfile = -1; @@ -74,7 +85,7 @@ AudioSource::AudioSource (Session& s, const XMLNode& node) peak_leftover_size = 0; peak_leftovers = 0; - if (set_state (node)) { + if (set_state (node, Stateful::loading_state_version)) { throw failed_constructor(); } } @@ -82,7 +93,7 @@ AudioSource::AudioSource (Session& s, const XMLNode& node) AudioSource::~AudioSource () { /* shouldn't happen but make sure we don't leak file descriptors anyway */ - + if (peak_leftover_cnt) { cerr << "AudioSource destroyed with leftover peak data pending" << endl; } @@ -91,9 +102,7 @@ AudioSource::~AudioSource () ::close (peakfile); } - if (peak_leftovers) { - delete [] peak_leftovers; - } + delete [] peak_leftovers; } XMLNode& @@ -109,12 +118,10 @@ AudioSource::get_state () } int -AudioSource::set_state (const XMLNode& node) +AudioSource::set_state (const XMLNode& node, int version) { const XMLProperty* prop; - Source::set_state (node); - if ((prop = node.property ("captured-for")) != 0) { _captured_for = prop->value(); } @@ -122,6 +129,21 @@ AudioSource::set_state (const XMLNode& node) return 0; } +sframes_t +AudioSource::length (sframes_t /*pos*/) const +{ + return _length; +} + +void +AudioSource::update_length (sframes_t pos, sframes_t cnt) +{ + if (pos + cnt > _length) { + _length = pos + cnt; + } +} + + /*********************************************************************** PEAK FILE STUFF ***********************************************************************/ @@ -135,7 +157,7 @@ AudioSource::peaks_ready (sigc::slot the_slot, sigc::connection& conn) con /* check to see if the peak data is ready. if not connect the slot while still holding the lock. */ - + if (!(ret = _peaks_built)) { conn = PeaksReady.connect (the_slot); } @@ -151,12 +173,12 @@ AudioSource::touch_peakfile () if (stat (peakpath.c_str(), &statbuf) != 0 || statbuf.st_size == 0) { return; } - + struct utimbuf tbuf; - + tbuf.actime = statbuf.st_atime; tbuf.modtime = time ((time_t) 0); - + utime (peakpath.c_str(), &tbuf); } @@ -187,43 +209,45 @@ AudioSource::initialize_peakfile (bool newfile, ustring audio_path) peakpath = peak_path (audio_path); /* if the peak file should be there, but isn't .... */ - + if (!newfile && !Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) { peakpath = find_broken_peakfile (peakpath, audio_path); } - if (newfile) { + if (stat (peakpath.c_str(), &statbuf)) { + if (errno != ENOENT) { + /* it exists in the peaks dir, but there is some kind of error */ - if (!_build_peakfiles) { - return 0; + error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg; + return -1; } + /* peakfile does not exist */ + _peaks_built = false; } else { - if (stat (peakpath.c_str(), &statbuf)) { - if (errno != ENOENT) { - /* it exists in the peaks dir, but there is some kind of error */ - - error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg; - return -1; - } - - _peaks_built = false; + /* we found it in the peaks dir, so check it out */ + if (statbuf.st_size == 0 || ((nframes_t) statbuf.st_size < ((length(_timeline_position) / _FPP) * sizeof (PeakData)))) { + // empty + _peaks_built = false; } else { - - /* we found it in the peaks dir, so check it out */ + // Check if the audio file has changed since the peakfile was built. + struct stat stat_file; + int err = stat (audio_path.c_str(), &stat_file); - if (statbuf.st_size == 0) { + if (err) { _peaks_built = false; + _peak_byte_max = 0; } else { - // Check if the audio file has changed since the peakfile was built. - struct stat stat_file; - int err = stat (audio_path.c_str(), &stat_file); - - if (!err && stat_file.st_mtime > statbuf.st_mtime){ + + /* allow 6 seconds slop on checking peak vs. file times because of various + disk action "races" + */ + + if (stat_file.st_mtime > statbuf.st_mtime && (stat_file.st_mtime - statbuf.st_mtime > 6)) { _peaks_built = false; _peak_byte_max = 0; } else { @@ -236,13 +260,13 @@ AudioSource::initialize_peakfile (bool newfile, ustring audio_path) if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) { build_peaks_from_scratch (); - } - + } + return 0; } nframes_t -AudioSource::read (Sample *dst, nframes_t start, nframes_t cnt) const +AudioSource::read (Sample *dst, sframes_t start, nframes_t cnt, int /*channel*/) const { Glib::Mutex::Lock lm (_lock); return read_unlocked (dst, start, cnt); @@ -255,8 +279,19 @@ AudioSource::write (Sample *dst, nframes_t cnt) return write_unlocked (dst, cnt); } -int -AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_visual_peak) const +int +AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, sframes_t start, nframes_t cnt, double samples_per_visual_peak) const +{ + return read_peaks_with_fpp (peaks, npeaks, start, cnt, samples_per_visual_peak, _FPP); +} + +/** @param peaks Buffer to write peak data. + * @param npeaks Number of peaks to write. + */ + +int +AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, sframes_t start, nframes_t cnt, + double samples_per_visual_peak, nframes_t samples_per_file_peak) const { Glib::Mutex::Lock lm (_lock); double scale; @@ -271,20 +306,20 @@ AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nfr Sample* raw_staging = 0; int _peakfile = -1; - expected_peaks = (cnt / (double) frames_per_peak); + expected_peaks = (cnt / (double) samples_per_file_peak); scale = npeaks/expected_peaks; #undef DEBUG_READ_PEAKS #ifdef DEBUG_READ_PEAKS - cerr << "======>RP: npeaks = " << npeaks - << " start = " << start - << " cnt = " << cnt - << " len = " << _length - << " samples_per_visual_peak =" << samples_per_visual_peak + cerr << "======>RP: npeaks = " << npeaks + << " start = " << start + << " cnt = " << cnt + << " len = " << _length + << " samples_per_visual_peak =" << samples_per_visual_peak << " expected was " << expected_peaks << " ... scale = " << scale << " PD ptr = " << peaks < _peak_byte_max) { ftruncate (peakfile, _peak_byte_max); } @@ -874,7 +908,7 @@ AudioSource::file_changed (ustring path) int e1 = stat (path.c_str(), &stat_file); int e2 = stat (peak_path(path).c_str(), &stat_peak); - + if (!e1 && !e2 && stat_file.st_mtime > stat_peak.st_mtime){ return true; } else { @@ -885,28 +919,18 @@ AudioSource::file_changed (ustring path) nframes_t AudioSource::available_peaks (double zoom_factor) const { - off_t end; + if (zoom_factor < _FPP) { + return length(_timeline_position); // peak data will come from the audio file + } - if (zoom_factor < frames_per_peak) { - return length(); // peak data will come from the audio file - } - /* peak data comes from peakfile, but the filesize might not represent the valid data due to ftruncate optimizations, so use _peak_byte_max state. XXX - there might be some atomicity issues here, we should probably add a lock, but _peak_byte_max only monotonically increases after initialization. */ - end = _peak_byte_max; + off_t end = _peak_byte_max; - return (end/sizeof(PeakData)) * frames_per_peak; -} - -void -AudioSource::update_length (nframes_t pos, nframes_t cnt) -{ - if (pos + cnt > _length) { - _length = pos+cnt; - } + return (end/sizeof(PeakData)) * _FPP; }