X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Faudiosource.cc;h=cd8c192d131d43f401a13f38524c80048f1a3b79;hb=5a8bc070cd5ca79a53487f536c6df966ffc8df39;hp=add9364cadd09a1207866cac7f9f115589d6a03a;hpb=7ff370e79895d7eb293e7214689b791bd98415fb;p=ardour.git diff --git a/libs/ardour/audiosource.cc b/libs/ardour/audiosource.cc index add9364cad..cd8c192d13 100644 --- a/libs/ardour/audiosource.cc +++ b/libs/ardour/audiosource.cc @@ -23,11 +23,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include @@ -40,18 +42,17 @@ using namespace std; using namespace ARDOUR; using namespace PBD; -sigc::signal AudioSource::AudioSourceCreated; pthread_t AudioSource::peak_thread; bool AudioSource::have_peak_thread = false; -vector AudioSource::pending_peak_sources; +vector > AudioSource::pending_peak_sources; Glib::Mutex* AudioSource::pending_peak_sources_lock = 0; int AudioSource::peak_request_pipe[2]; bool AudioSource::_build_missing_peakfiles = false; bool AudioSource::_build_peakfiles = false; -AudioSource::AudioSource (string name) - : Source (name) +AudioSource::AudioSource (Session& s, string name) + : Source (s, name) { if (pending_peak_sources_lock == 0) { pending_peak_sources_lock = new Glib::Mutex; @@ -63,8 +64,8 @@ AudioSource::AudioSource (string name) _write_data_count = 0; } -AudioSource::AudioSource (const XMLNode& node) - : Source (node) +AudioSource::AudioSource (Session& s, const XMLNode& node) + : Source (s, node) { if (pending_peak_sources_lock == 0) { pending_peak_sources_lock = new Glib::Mutex; @@ -190,7 +191,7 @@ AudioSource::peak_thread_work (void* arg) while (!pending_peak_sources.empty()) { - AudioSource* s = pending_peak_sources.front(); + boost::shared_ptr s = pending_peak_sources.front(); pending_peak_sources.erase (pending_peak_sources.begin()); pending_peak_sources_lock->unlock (); @@ -250,18 +251,18 @@ AudioSource::stop_peak_thread () } void -AudioSource::queue_for_peaks (AudioSource& source) +AudioSource::queue_for_peaks (boost::shared_ptr source) { if (have_peak_thread) { - + Glib::Mutex::Lock lm (*pending_peak_sources_lock); - source.next_peak_clear_should_notify = true; + source->next_peak_clear_should_notify = true; if (find (pending_peak_sources.begin(), pending_peak_sources.end(), - &source) == pending_peak_sources.end()) { - pending_peak_sources.push_back (&source); + source) == pending_peak_sources.end()) { + pending_peak_sources.push_back (source); } char c = (char) PeakRequest::Build; @@ -296,6 +297,23 @@ AudioSource::peaks_ready (sigc::slot the_slot, sigc::connection& conn) con return ret; } +void +AudioSource::touch_peakfile () +{ + struct stat statbuf; + + 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); +} + int AudioSource::rename_peakfile (string newpath) { @@ -350,7 +368,6 @@ AudioSource::initialize_peakfile (bool newfile, string audio_path) error << string_compose(_("AudioSource: cannot stat peakfile \"%1\""), peakpath) << endmsg; return -1; } - } else { /* we found it in the peaks dir */ @@ -370,30 +387,30 @@ AudioSource::initialize_peakfile (bool newfile, string audio_path) } } } - + if (!newfile && !_peaks_built && _build_missing_peakfiles && _build_peakfiles) { build_peaks_from_scratch (); } - + return 0; } -jack_nframes_t -AudioSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const +nframes_t +AudioSource::read (Sample *dst, nframes_t start, nframes_t cnt) const { Glib::Mutex::Lock lm (_lock); - return read_unlocked (dst, start, cnt, workbuf); + return read_unlocked (dst, start, cnt); } -jack_nframes_t -AudioSource::write (Sample *dst, jack_nframes_t cnt, char * workbuf) +nframes_t +AudioSource::write (Sample *dst, nframes_t cnt) { Glib::Mutex::Lock lm (_lock); - return write_unlocked (dst, cnt, workbuf); + return write_unlocked (dst, cnt); } int -AudioSource::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t start, jack_nframes_t cnt, double samples_per_visual_peak) const +AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_visual_peak) const { Glib::Mutex::Lock lm (_lock); double scale; @@ -402,11 +419,10 @@ AudioSource::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t PeakData::PeakDatum xmin; int32_t to_read; uint32_t nread; - jack_nframes_t zero_fill = 0; + nframes_t zero_fill = 0; int ret = -1; PeakData* staging = 0; Sample* raw_staging = 0; - char * workbuf = 0; int peakfile = -1; expected_peaks = (cnt / (double) frames_per_peak); @@ -429,8 +445,8 @@ AudioSource::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t if (cnt > _length - start) { // cerr << "too close to end @ " << _length << " given " << start << " + " << cnt << endl; cnt = _length - start; - jack_nframes_t old = npeaks; - npeaks = min ((jack_nframes_t) floor (cnt / samples_per_visual_peak), npeaks); + nframes_t old = npeaks; + npeaks = min ((nframes_t) floor (cnt / samples_per_visual_peak), npeaks); zero_fill = old - npeaks; } @@ -438,27 +454,25 @@ AudioSource::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t if (npeaks == cnt) { - // cerr << "RAW DATA\n"; + cerr << "RAW DATA\n"; /* no scaling at all, just get the sample data and duplicate it for both max and min peak values. */ Sample* raw_staging = new Sample[cnt]; - workbuf = new char[cnt*4]; - if (read_unlocked (raw_staging, start, cnt, workbuf) != cnt) { + if (read_unlocked (raw_staging, start, cnt) != cnt) { error << _("cannot read sample data for unscaled peak computation") << endmsg; return -1; } - for (jack_nframes_t i = 0; i < npeaks; ++i) { + for (nframes_t i = 0; i < npeaks; ++i) { peaks[i].max = raw_staging[i]; peaks[i].min = raw_staging[i]; } delete [] raw_staging; - delete [] workbuf; return 0; } @@ -502,7 +516,7 @@ AudioSource::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t } - jack_nframes_t tnp; + nframes_t tnp; if (scale < 1.0) { @@ -524,11 +538,11 @@ AudioSource::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t /* compute the rounded up frame position */ - jack_nframes_t current_frame = start; - jack_nframes_t current_stored_peak = (jack_nframes_t) ceil (current_frame / (double) frames_per_peak); + nframes_t current_frame = start; + nframes_t current_stored_peak = (nframes_t) ceil (current_frame / (double) frames_per_peak); uint32_t next_visual_peak = (uint32_t) ceil (current_frame / samples_per_visual_peak); double next_visual_peak_frame = next_visual_peak * samples_per_visual_peak; - uint32_t stored_peak_before_next_visual_peak = (jack_nframes_t) next_visual_peak_frame / frames_per_peak; + uint32_t stored_peak_before_next_visual_peak = (nframes_t) next_visual_peak_frame / frames_per_peak; uint32_t nvisual_peaks = 0; uint32_t stored_peaks_read = 0; uint32_t i = 0; @@ -549,7 +563,7 @@ AudioSource::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t if (i == stored_peaks_read) { uint32_t start_byte = current_stored_peak * sizeof(PeakData); - tnp = min ((_length/frames_per_peak - current_stored_peak), (jack_nframes_t) expected_peaks); + tnp = min ((_length/frames_per_peak - current_stored_peak), (nframes_t) expected_peaks); to_read = min (chunksize, tnp); off_t fend = lseek (peakfile, 0, SEEK_END); @@ -618,15 +632,14 @@ AudioSource::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t data on the fly. */ - jack_nframes_t frames_read = 0; - jack_nframes_t current_frame = start; - jack_nframes_t i = 0; - jack_nframes_t nvisual_peaks = 0; - jack_nframes_t chunksize = (jack_nframes_t) min (cnt, (jack_nframes_t) 4096); + nframes_t frames_read = 0; + nframes_t current_frame = start; + nframes_t i = 0; + nframes_t nvisual_peaks = 0; + nframes_t chunksize = (nframes_t) min (cnt, (nframes_t) 4096); raw_staging = new Sample[chunksize]; - workbuf = new char[chunksize *4]; - jack_nframes_t frame_pos = start; + nframes_t frame_pos = start; double pixel_pos = floor (frame_pos / samples_per_visual_peak); double next_pixel_pos = ceil (frame_pos / samples_per_visual_peak); double pixels_per_frame = 1.0 / samples_per_visual_peak; @@ -639,8 +652,8 @@ AudioSource::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t if (i == frames_read) { to_read = min (chunksize, (_length - current_frame)); - - if ((frames_read = read_unlocked (raw_staging, current_frame, to_read, workbuf)) < 0) { + + if ((frames_read = read_unlocked (raw_staging, current_frame, to_read)) == 0) { error << string_compose(_("AudioSource[%1]: peak read - cannot read %2 samples at offset %3") , _name, to_read, current_frame) << endmsg; @@ -688,10 +701,6 @@ AudioSource::read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t delete [] raw_staging; } - if (workbuf) { - delete [] workbuf; - } - return ret; } @@ -753,16 +762,15 @@ AudioSource::build_peaks () } int -AudioSource::do_build_peak (jack_nframes_t first_frame, jack_nframes_t cnt) +AudioSource::do_build_peak (nframes_t first_frame, nframes_t cnt) { - jack_nframes_t current_frame; + nframes_t current_frame; Sample buf[frames_per_peak]; Sample xmin, xmax; uint32_t peaki; PeakData* peakbuf; - char * workbuf = 0; - jack_nframes_t frames_read; - jack_nframes_t frames_to_read; + nframes_t frames_read; + nframes_t frames_to_read; off_t first_peak_byte; int peakfile = -1; int ret = -1; @@ -781,8 +789,6 @@ AudioSource::do_build_peak (jack_nframes_t first_frame, jack_nframes_t cnt) peakbuf = new PeakData[(cnt/frames_per_peak)+1]; peaki = 0; - workbuf = new char[max(frames_per_peak, cnt) * 4]; - if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) { error << string_compose(_("AudioSource: cannot open peakpath \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg; return -1; @@ -794,7 +800,7 @@ AudioSource::do_build_peak (jack_nframes_t first_frame, jack_nframes_t cnt) /* lock for every read */ - if ((frames_read = read (buf, current_frame, frames_to_read, workbuf)) != frames_to_read) { + if ((frames_read = read (buf, current_frame, frames_to_read)) != frames_to_read) { error << string_compose(_("%1: could not write read raw data for peak computation (%2)"), _name, strerror (errno)) << endmsg; goto out; } @@ -802,7 +808,7 @@ AudioSource::do_build_peak (jack_nframes_t first_frame, jack_nframes_t cnt) xmin = buf[0]; xmax = buf[0]; - for (jack_nframes_t n = 1; n < frames_read; ++n) { + for (nframes_t n = 1; n < frames_read; ++n) { xmax = max (xmax, buf[n]); xmin = min (xmin, buf[n]); @@ -831,8 +837,6 @@ AudioSource::do_build_peak (jack_nframes_t first_frame, jack_nframes_t cnt) if (peakfile >= 0) { close (peakfile); } - if (workbuf) - delete [] workbuf; return ret; } @@ -843,7 +847,7 @@ AudioSource::build_peaks_from_scratch () next_peak_clear_should_notify = true; pending_peak_builds.push_back (new PeakBuildRecord (0, _length)); - queue_for_peaks (*this); + queue_for_peaks (shared_from_this()); } bool @@ -862,7 +866,7 @@ AudioSource::file_changed (string path) } } -jack_nframes_t +nframes_t AudioSource::available_peaks (double zoom_factor) const { int peakfile; @@ -890,7 +894,7 @@ AudioSource::available_peaks (double zoom_factor) const } void -AudioSource::update_length (jack_nframes_t pos, jack_nframes_t cnt) +AudioSource::update_length (nframes_t pos, nframes_t cnt) { if (pos + cnt > _length) { _length = pos+cnt;