X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Faudiosource.cc;h=0c820eed4d29b9c923fb0a14150b2c978b52b305;hb=7d96960b162d25da87c388a3083775e8770bba56;hp=8e68b315877e0b87e0499db9f6bcc5e8113a947e;hpb=68e943265edf04e63a8e8b8f62bab20f99d9c637;p=ardour.git diff --git a/libs/ardour/audiosource.cc b/libs/ardour/audiosource.cc index 8e68b31587..0c820eed4d 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 @@ -34,13 +34,14 @@ #include #include -#include -#include +#include "pbd/xml++.h" +#include "pbd/pthread_utils.h" -#include -#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" @@ -50,12 +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; @@ -67,10 +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; @@ -80,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(); } } @@ -88,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; } @@ -97,9 +102,7 @@ AudioSource::~AudioSource () ::close (peakfile); } - if (peak_leftovers) { - delete [] peak_leftovers; - } + delete [] peak_leftovers; } XMLNode& @@ -115,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(); } @@ -128,12 +129,27 @@ 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 ***********************************************************************/ bool -AudioSource::peaks_ready (sigc::slot the_slot, sigc::connection& conn) const +AudioSource::peaks_ready (boost::function doThisWhenReady, Connection& connect_here_if_not, EventLoop* event_loop) const { bool ret; Glib::Mutex::Lock lm (_peaks_ready_lock); @@ -143,7 +159,7 @@ AudioSource::peaks_ready (sigc::slot the_slot, sigc::connection& conn) con */ if (!(ret = _peaks_built)) { - conn = PeaksReady.connect (the_slot); + PeaksReady.connect (connect_here_if_not, doThisWhenReady, event_loop); } return ret; @@ -157,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); } @@ -193,7 +209,7 @@ 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); } @@ -201,27 +217,27 @@ AudioSource::initialize_peakfile (bool newfile, ustring audio_path) 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; } /* peakfile does not exist */ - + _peaks_built = false; - + } else { - + /* we found it in the peaks dir, so check it out */ - - if (statbuf.st_size == 0 || ((nframes_t) statbuf.st_size < ((length() / _FPP) * sizeof (PeakData)))) { + + if (statbuf.st_size == 0 || ((nframes_t) statbuf.st_size < ((length(_timeline_position) / _FPP) * sizeof (PeakData)))) { // empty _peaks_built = false; } 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) { _peaks_built = false; _peak_byte_max = 0; @@ -244,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); @@ -260,17 +276,23 @@ nframes_t AudioSource::write (Sample *dst, nframes_t cnt) { Glib::Mutex::Lock lm (_lock); + /* any write makes the fill not removable */ + _flags = Flag (_flags & ~Removable); 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 +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); } -int -AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, +/** @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); @@ -291,15 +313,15 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, nframes_t s #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); } } -bool -AudioSource::file_changed (ustring path) -{ - struct stat stat_file; - struct stat stat_peak; - - 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 { - return false; - } -} - nframes_t AudioSource::available_peaks (double zoom_factor) const { - off_t end; - if (zoom_factor < _FPP) { - return length(); // peak data will come from the audio file - } - + return length(_timeline_position); // 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)) * _FPP; } -void -AudioSource::update_length (nframes_t pos, nframes_t cnt) -{ - if (pos + cnt > _length) { - _length = pos+cnt; - } -} -