From c9582da23371bfd40f3087f5a9112d081ef012da Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 20 Feb 2009 00:30:42 +0000 Subject: [PATCH] Make source length a dynamic thing. Update MIDI region length (actually and visually) when position changes. git-svn-id: svn://localhost/ardour2/branches/3.0@4644 d708f5d6-7413-0410-9779-e7cbd77b26cf --- gtk2_ardour/audio_streamview.cc | 2 +- gtk2_ardour/editor_audio_import.cc | 8 ++++---- gtk2_ardour/editor_export_audio.cc | 2 +- gtk2_ardour/editor_region_list.cc | 2 +- gtk2_ardour/midi_region_view.cc | 1 + gtk2_ardour/midi_streamview.cc | 2 +- gtk2_ardour/sfdb_ui.cc | 4 +++- libs/ardour/analyser.cc | 3 +-- libs/ardour/ardour/audiosource.h | 4 ++++ libs/ardour/ardour/crossfade.h | 2 +- libs/ardour/ardour/midi_region.h | 2 ++ libs/ardour/ardour/midi_source.h | 14 ++++++++++---- libs/ardour/ardour/region.h | 4 +++- libs/ardour/ardour/source.h | 12 +++++------- libs/ardour/audio_diskstream.cc | 8 ++++---- libs/ardour/audio_track.cc | 9 +++++---- libs/ardour/audioregion.cc | 8 ++++++-- libs/ardour/audiosource.cc | 25 ++++++++++++++++++++----- libs/ardour/crossfade.cc | 8 ++++---- libs/ardour/file_source.cc | 3 +-- libs/ardour/midi_region.cc | 13 +++++++++++++ libs/ardour/midi_source.cc | 29 ++++++++++++++++++++++++++--- libs/ardour/region.cc | 16 +++++++++++----- libs/ardour/region_factory.cc | 2 +- libs/ardour/session.cc | 5 +++-- libs/ardour/session_state.cc | 4 ++-- libs/ardour/smf_source.cc | 16 ++++++++++------ libs/ardour/source.cc | 10 ---------- 28 files changed, 144 insertions(+), 74 deletions(-) diff --git a/gtk2_ardour/audio_streamview.cc b/gtk2_ardour/audio_streamview.cc index d205e400d2..f102674fb4 100644 --- a/gtk2_ardour/audio_streamview.cc +++ b/gtk2_ardour/audio_streamview.cc @@ -699,7 +699,7 @@ AudioStreamView::update_rec_regions () if (nlen != region->length()) { - if (region->source(0)->length() >= region->start() + nlen) { + if (region->source_length(0) >= region->start() + nlen) { region->freeze (); region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this); diff --git a/gtk2_ardour/editor_audio_import.cc b/gtk2_ardour/editor_audio_import.cc index ab5cea3f7b..730063655a 100644 --- a/gtk2_ardour/editor_audio_import.cc +++ b/gtk2_ardour/editor_audio_import.cc @@ -732,8 +732,8 @@ Editor::add_sources (vector paths, SourceList& sources, nframes64 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false); - regions.push_back (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0, - Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))); + regions.push_back (RegionFactory::create (sources, 0, sources[0]->length(pos), region_name, 0, + Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))); } else if (target_regions == -1 || target_regions > 1) { @@ -750,8 +750,8 @@ Editor::add_sources (vector paths, SourceList& sources, nframes64 region_name = region_name_from_path ((*x)->path(), false, false, sources.size(), n); - regions.push_back (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0, - Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))); + regions.push_back (RegionFactory::create (just_one, 0, (*x)->length(pos), region_name, 0, + Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))); } } diff --git a/gtk2_ardour/editor_export_audio.cc b/gtk2_ardour/editor_export_audio.cc index 7586a813cf..ea1876ca94 100644 --- a/gtk2_ardour/editor_export_audio.cc +++ b/gtk2_ardour/editor_export_audio.cc @@ -170,7 +170,7 @@ Editor::write_region (string path, boost::shared_ptr region) /* don't do duplicate of the entire source if that's what is going on here */ - if (region->start() == 0 && region->length() == region->source()->length()) { + if (region->start() == 0 && region->length() == region->source_length(0)) { /* XXX should link(2) to create a new inode with "path" */ return true; } diff --git a/gtk2_ardour/editor_region_list.cc b/gtk2_ardour/editor_region_list.cc index 694d9033dc..b97c8acc9e 100644 --- a/gtk2_ardour/editor_region_list.cc +++ b/gtk2_ardour/editor_region_list.cc @@ -989,7 +989,7 @@ Editor::region_list_sorter (TreeModel::iterator a, TreeModel::iterator b) break; case BySourceFileLength: - cmp = region1->source()->length() - region2->source()->length(); + cmp = region1->source_length(0) - region2->source_length(0); break; case BySourceFileCreationDate: diff --git a/gtk2_ardour/midi_region_view.cc b/gtk2_ardour/midi_region_view.cc index 3087d3436e..aca13c8c44 100644 --- a/gtk2_ardour/midi_region_view.cc +++ b/gtk2_ardour/midi_region_view.cc @@ -685,6 +685,7 @@ MidiRegionView::region_resized (Change what_changed) RegionView::region_resized(what_changed); if (what_changed & ARDOUR::PositionChanged) { + set_duration(_region->length(), 0); if (_enable_display) { redisplay_model(); } diff --git a/gtk2_ardour/midi_streamview.cc b/gtk2_ardour/midi_streamview.cc index 4e1afffbfb..ed364e7156 100644 --- a/gtk2_ardour/midi_streamview.cc +++ b/gtk2_ardour/midi_streamview.cc @@ -594,7 +594,7 @@ MidiStreamView::update_rec_regions (boost::shared_ptr data, nframes_t if (nlen != region->length()) { - if (region->source(0)->length() >= region->position() + nlen) { + if (region->source_length(0) >= region->position() + nlen) { region->freeze (); region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this); diff --git a/gtk2_ardour/sfdb_ui.cc b/gtk2_ardour/sfdb_ui.cc index bb95da5c00..0ee8935f1b 100644 --- a/gtk2_ardour/sfdb_ui.cc +++ b/gtk2_ardour/sfdb_ui.cc @@ -352,7 +352,9 @@ SoundFileBox::audition () afs = boost::dynamic_pointer_cast (srclist[0]); string rname = region_name_from_path (afs->path(), false); - r = boost::dynamic_pointer_cast (RegionFactory::create (srclist, 0, srclist[0]->length(), rname, 0, Region::DefaultFlags, false)); + r = boost::dynamic_pointer_cast (RegionFactory::create (srclist, 0, + srclist[0]->length(srclist[0]->timeline_position()), + rname, 0, Region::DefaultFlags, false)); _session->audition_region(r); } diff --git a/libs/ardour/analyser.cc b/libs/ardour/analyser.cc index 28eeeb190c..6d2392eb20 100644 --- a/libs/ardour/analyser.cc +++ b/libs/ardour/analyser.cc @@ -95,7 +95,7 @@ Analyser::work () boost::shared_ptr afs = boost::dynamic_pointer_cast (src); - if (afs && afs->length()) { + if (afs && afs->length(afs->timeline_position())) { analyse_audio_file_source (afs); } } @@ -116,4 +116,3 @@ Analyser::analyse_audio_file_source (boost::shared_ptr src) } - diff --git a/libs/ardour/ardour/audiosource.h b/libs/ardour/ardour/audiosource.h index 08435bf3b1..4f2ad0eee1 100644 --- a/libs/ardour/ardour/audiosource.h +++ b/libs/ardour/ardour/audiosource.h @@ -54,6 +54,9 @@ class AudioSource : virtual public Source, nframes64_t readable_length() const { return _length; } uint32_t n_channels() const { return 1; } + + sframes_t length (sframes_t pos) const; + void update_length (sframes_t pos, sframes_t cnt); virtual nframes_t available_peaks (double zoom) const; @@ -108,6 +111,7 @@ class AudioSource : virtual public Source, static bool _build_missing_peakfiles; static bool _build_peakfiles; + sframes_t _length; bool _peaks_built; mutable Glib::Mutex _peaks_ready_lock; Glib::ustring peakpath; diff --git a/libs/ardour/ardour/crossfade.h b/libs/ardour/ardour/crossfade.h index 2b893478d4..4ad2ab551d 100644 --- a/libs/ardour/ardour/crossfade.h +++ b/libs/ardour/ardour/crossfade.h @@ -129,7 +129,7 @@ class Crossfade : public ARDOUR::AudioRegion AutomationList& fade_in() { return _fade_in; } AutomationList& fade_out() { return _fade_out; } - nframes_t set_length (nframes_t); + nframes_t set_xfade_length (nframes_t); bool is_dependent() const { return true; } bool depends_on (boost::shared_ptr other) const { diff --git a/libs/ardour/ardour/midi_region.h b/libs/ardour/ardour/midi_region.h index 0760fb7268..9f4041060e 100644 --- a/libs/ardour/ardour/midi_region.h +++ b/libs/ardour/ardour/midi_region.h @@ -115,6 +115,8 @@ class MidiRegion : public Region void recompute_at_start (); void recompute_at_end (); + + void set_position_internal (nframes_t pos, bool allow_bbt_recompute); void switch_source(boost::shared_ptr source); diff --git a/libs/ardour/ardour/midi_source.h b/libs/ardour/ardour/midi_source.h index ec454803dc..0c18b096bd 100644 --- a/libs/ardour/ardour/midi_source.h +++ b/libs/ardour/ardour/midi_source.h @@ -69,6 +69,9 @@ class MidiSource : virtual public Source virtual void append_event_unlocked_frames(const Evoral::Event& ev, sframes_t position) = 0; + + virtual sframes_t length (sframes_t pos) const; + virtual void update_length (sframes_t pos, sframes_t cnt); virtual void mark_streaming_midi_write_started (NoteMode mode, sframes_t start_time); virtual void mark_streaming_write_started (); @@ -118,15 +121,18 @@ class MidiSource : virtual public Source sframes_t position, nframes_t cnt) = 0; - std::string _captured_for; - mutable uint32_t _read_data_count; ///< modified in read() - mutable uint32_t _write_data_count; ///< modified in write() + std::string _captured_for; + mutable uint32_t _read_data_count; ///< modified in read() + mutable uint32_t _write_data_count; ///< modified in write() boost::shared_ptr _model; bool _writing; mutable Evoral::Sequence::const_iterator _model_iter; - mutable sframes_t _last_read_end; + + mutable double _length_beats; + mutable sframes_t _last_read_end; + sframes_t _last_write_end; private: bool file_changed (std::string path); diff --git a/libs/ardour/ardour/region.h b/libs/ardour/ardour/region.h index a4ce35b61b..2696c7da91 100644 --- a/libs/ardour/ardour/region.h +++ b/libs/ardour/ardour/region.h @@ -120,6 +120,8 @@ class Region nframes_t length() const { return _length; } layer_t layer () const { return _layer; } + sframes_t source_length(uint32_t n) const; + /* these two are valid ONLY during a StateChanged signal handler */ nframes_t last_position() const { return _last_position; } @@ -288,7 +290,7 @@ class Region void send_change (Change); void trim_to_internal (nframes_t position, nframes_t length, void *src); - void set_position_internal (nframes_t pos, bool allow_bbt_recompute); + virtual void set_position_internal (nframes_t pos, bool allow_bbt_recompute); bool copied() const { return _flags & Copied; } void maybe_uncopy (); diff --git a/libs/ardour/ardour/source.h b/libs/ardour/ardour/source.h index 36d7e1fe96..7905debbcc 100644 --- a/libs/ardour/ardour/source.h +++ b/libs/ardour/ardour/source.h @@ -61,7 +61,8 @@ class Source : public SessionObject, public boost::noncopyable time_t timestamp() const { return _timestamp; } void stamp (time_t when) { _timestamp = when; } - sframes_t length() const { return _length; } + virtual sframes_t length (sframes_t pos) const = 0; + virtual void update_length (sframes_t pos, sframes_t cnt) = 0; virtual const Glib::ustring& path() const = 0; @@ -104,10 +105,8 @@ class Source : public SessionObject, public boost::noncopyable std::string get_transients_path() const; int load_transients (const std::string&); - void update_length (sframes_t pos, sframes_t cnt); - - int64_t timeline_position() const { return _timeline_position; } - virtual void set_timeline_position (int64_t pos); + sframes_t timeline_position() const { return _timeline_position; } + virtual void set_timeline_position (sframes_t pos); void set_allow_remove_if_empty (bool yn); @@ -118,8 +117,7 @@ class Source : public SessionObject, public boost::noncopyable DataType _type; Flag _flags; time_t _timestamp; - sframes_t _length; - int64_t _timeline_position; + sframes_t _timeline_position; bool _analysed; mutable Glib::Mutex _lock; mutable Glib::Mutex _analysis_lock; diff --git a/libs/ardour/audio_diskstream.cc b/libs/ardour/audio_diskstream.cc index ed8027ec59..e08118c0fc 100644 --- a/libs/ardour/audio_diskstream.cc +++ b/libs/ardour/audio_diskstream.cc @@ -2373,9 +2373,9 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node) try { region = boost::dynamic_pointer_cast (RegionFactory::create ( - pending_sources, 0, first_fs->length(), - region_name_from_path (first_fs->name(), true), 0, - Region::Flag (Region::DefaultFlags|Region::Automatic|Region::WholeFile))); + pending_sources, 0, first_fs->length(first_fs->timeline_position()), + region_name_from_path (first_fs->name(), true), 0, + Region::Flag (Region::DefaultFlags|Region::Automatic|Region::WholeFile))); region->special_set_position (0); } @@ -2389,7 +2389,7 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node) try { region = boost::dynamic_pointer_cast (RegionFactory::create ( - pending_sources, 0, first_fs->length(), + pending_sources, 0, first_fs->length(first_fs->timeline_position()), region_name_from_path (first_fs->name(), true))); } diff --git a/libs/ardour/audio_track.cc b/libs/ardour/audio_track.cc index df648bc055..2e1a266577 100644 --- a/libs/ardour/audio_track.cc +++ b/libs/ardour/audio_track.cc @@ -897,10 +897,11 @@ AudioTrack::freeze (InterThreadInfo& itt) /* create a new region from all filesources, keep it private */ - boost::shared_ptr region (RegionFactory::create (srcs, 0, srcs[0]->length(), - region_name, 0, - (Region::Flag) (Region::WholeFile|Region::DefaultFlags), - false)); + boost::shared_ptr region (RegionFactory::create (srcs, 0, + srcs[0]->length(srcs[0]->timeline_position()), + region_name, 0, + (Region::Flag) (Region::WholeFile|Region::DefaultFlags), + false)); new_playlist->set_orig_diskstream_id (diskstream->id()); new_playlist->add_region (region, _session.current_start_frame()); diff --git a/libs/ardour/audioregion.cc b/libs/ardour/audioregion.cc index aafa67e7d3..3ea00deb59 100644 --- a/libs/ardour/audioregion.cc +++ b/libs/ardour/audioregion.cc @@ -348,14 +348,18 @@ AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t read_frames, nframes_t skip_frames) const { /* regular diskstream/butler read complete with fades etc */ - return _read_at (_sources, _length, buf, mixdown_buffer, gain_buffer, file_position, cnt, chan_n, read_frames, skip_frames, ReadOps (~0)); + return _read_at (_sources, _length, buf, mixdown_buffer, gain_buffer, + file_position, cnt, chan_n, read_frames, skip_frames, ReadOps (~0)); } nframes_t AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, sframes_t position, nframes_t cnt, uint32_t chan_n) const { - return _read_at (_master_sources, _master_sources.front()->length(), buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0); + return _read_at (_master_sources, + _master_sources.front()->length(_master_sources.front()->timeline_position()), + buf, mixdown_buffer, gain_buffer, + position, cnt, chan_n, 0, 0); } nframes_t diff --git a/libs/ardour/audiosource.cc b/libs/ardour/audiosource.cc index 15f35bf1f1..24ca406b38 100644 --- a/libs/ardour/audiosource.cc +++ b/libs/ardour/audiosource.cc @@ -57,6 +57,7 @@ bool AudioSource::_build_peakfiles = false; AudioSource::AudioSource (Session& s, ustring name) : Source (s, DataType::AUDIO, name) + , _length (0) { _peaks_built = false; _peak_byte_max = 0; @@ -70,6 +71,7 @@ AudioSource::AudioSource (Session& s, ustring name) AudioSource::AudioSource (Session& s, const XMLNode& node) : Source (s, node) + , _length (0) { _peaks_built = false; @@ -125,6 +127,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 ***********************************************************************/ @@ -211,7 +228,7 @@ AudioSource::initialize_peakfile (bool newfile, ustring audio_path) /* 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 { @@ -895,10 +912,8 @@ AudioSource::file_changed (ustring path) 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 @@ -907,7 +922,7 @@ AudioSource::available_peaks (double zoom_factor) const but _peak_byte_max only monotonically increases after initialization. */ - end = _peak_byte_max; + off_t end = _peak_byte_max; return (end/sizeof(PeakData)) * _FPP; } diff --git a/libs/ardour/crossfade.cc b/libs/ardour/crossfade.cc index 5d180ceccc..9d10f1d1cc 100644 --- a/libs/ardour/crossfade.cc +++ b/libs/ardour/crossfade.cc @@ -187,7 +187,7 @@ Crossfade::Crossfade (boost::shared_ptr orig, boost::shared_ptr