9#include "dsp/midi_event.h"
10#include "utils/units.h"
14#include <juce_audio_basics/juce_audio_basics.h>
26class TimelineDataCache :
public QObject
31 using IntervalType = std::pair<units::sample_t, units::sample_t>;
33 explicit TimelineDataCache (QObject * parent =
nullptr) : QObject (parent) { }
35 ~TimelineDataCache ()
override;
69 finalize_changes_impl ();
98 if (interval.first >= interval.second)
100 throw std::invalid_argument (
101 "Interval start must be strictly less than end");
111 return a.second > b.first && a.first < b.second;
115 virtual void clear_impl () = 0;
116 virtual void finalize_changes_impl () = 0;
121 virtual std::vector<IntervalType> compute_cached_sample_ranges ()
const = 0;
130class MidiTimelineDataCache :
public TimelineDataCache
133 explicit MidiTimelineDataCache (QObject * parent =
nullptr)
134 : TimelineDataCache (parent)
145 IntervalType interval,
146 std::span<const SampleBasedMidiEvent> events);
155 return merged_midi_events_;
162 void clear_impl ()
override;
163 void finalize_changes_impl ()
override;
164 std::vector<IntervalType> compute_cached_sample_ranges ()
const override;
171 std::map<IntervalType, std::vector<SampleBasedMidiEvent>> midi_sequences_;
179 std::vector<SampleBasedMidiEvent> merged_midi_events_;
188class AudioTimelineDataCache :
public TimelineDataCache
191 explicit AudioTimelineDataCache (QObject * parent =
nullptr)
192 : TimelineDataCache (parent)
222 IntervalType interval,
223 const juce::AudioSampleBuffer &audio_buffer);
230 std::span<const AudioClipEntry>
audio_clips ()
const {
return audio_clips_; }
236 void clear_impl ()
override;
237 void finalize_changes_impl ()
override;
238 std::vector<IntervalType> compute_cached_sample_ranges ()
const override;
245 std::vector<AudioClipEntry> audio_clips_;
254class AutomationTimelineDataCache :
public TimelineDataCache
257 explicit AutomationTimelineDataCache (QObject * parent =
nullptr)
258 : TimelineDataCache (parent)
273 units::sample_t end_sample;
291 float curve_curviness;
329 return automation_sequences_;
336 void clear_impl ()
override;
337 void finalize_changes_impl ()
override;
338 std::vector<IntervalType> compute_cached_sample_ranges ()
const override;
345 std::vector<AutomationCacheEntry> automation_sequences_;
350Q_DECLARE_METATYPE (std::vector<zrythm::dsp::TimelineDataCache::IntervalType>)
std::span< const AudioClipEntry > audio_clips() const
Gets the cached audio clips.
void add_audio_clip(IntervalType interval, const juce::AudioSampleBuffer &audio_buffer)
Adds an audio clip for the given interval.
void remove_sequences_matching_interval(IntervalType interval) override
Removes cached data for intervals that truly overlap with the given interval.
bool has_content() const override
Checks if the cache has any content.
void remove_sequences_matching_interval(IntervalType interval) override
Removes cached data for intervals that truly overlap with the given interval.
std::span< const AutomationCacheEntry > automation_sequences() const
Gets the cached automation sequences.
bool has_content() const override
Checks if the cache has any content.
void add_automation_sequence(IntervalType interval, AutomationCacheEntry &&entry)
Adds an automation cache entry for the given interval.
Algorithm
The algorithm to use for curves.
void add_midi_sequence(IntervalType interval, std::span< const SampleBasedMidiEvent > events)
Adds a MIDI sequence for the given interval.
std::span< const SampleBasedMidiEvent > midi_events() const
Gets the cached MIDI events.
void remove_sequences_matching_interval(IntervalType interval) override
Removes cached data for intervals that truly overlap with the given interval.
bool has_content() const override
Checks if the cache has any content.
void finalize_changes()
Finalizes changes, prepares cached data for access, and emits cachedRangesChanged.
virtual bool has_content() const =0
Checks if the cache has any content.
Q_SIGNAL void cachedRangesChanged(std::vector< IntervalType > ranges) const
Emitted when the cache content changes (after finalize_changes or clear).
static bool intervals_overlap(IntervalType a, IntervalType b) noexcept
Returns true if two intervals truly overlap (adjacent intervals do not count).
virtual void remove_sequences_matching_interval(IntervalType interval)=0
Removes cached data for intervals that truly overlap with the given interval.
void clear()
Clears all cached data and emits cachedRangesChanged.
static void validate_interval(IntervalType interval)
Validates that the given interval has a positive width (start < end).
Audio clip entry for caching.
units::sample_t start_sample
Start position in samples.
units::sample_t end_sample
End position in samples.
juce::AudioSampleBuffer audio_buffer
Copy of the audio sample buffer.
Automation cache entry for caching.
units::sample_t end_sample
End position in samples.
units::sample_t start_sample
Start position in samples.
std::vector< CachedAutomationSegment > segments
Sorted constant-tempo segments covering the clip's span.
A constant-tempo sub-segment of an automation curve.
units::sample_t start_sample
Absolute sample positions (for binary search).
dsp::CurveOptions::Algorithm curve_algo
Curve parameters from the automation point that drives this segment.
float ratio_start
Position within the full automation point pair (0 = first point, 1 = second point).
float point_a_value
Values of the two automation points bracketing this segment.