6#include "dsp/itransport.h"
7#include "dsp/timeline_data_cache.h"
8#include "structure/arrangement/region_renderer.h"
9#include "utils/expandable_tick_range.h"
12#include <farbot/RealtimeObject.hpp>
14namespace zrythm::structure::arrangement
24class TimelineDataProvider :
public QObject
29 using IntervalType = std::pair<units::sample_t, units::sample_t>;
31 explicit TimelineDataProvider (QObject * parent =
nullptr) : QObject (parent)
35 ~TimelineDataProvider ()
override;
49 template <RegionObject RegionType>
52 const dsp::TempoMap &tempo_map,
57 const auto sample_interval = [&affected_range, &tempo_map] ()
58 -> std::pair<units::sample_t, units::sample_t> {
61 const auto tick_range = affected_range.
range ().value ();
62 return std::make_pair (
63 tempo_map.tick_to_samples_rounded (units::ticks (tick_range.first)),
64 tempo_map.tick_to_samples_rounded (units::ticks (tick_range.second)));
66 return std::make_pair (
67 units::samples (
static_cast<int64_t
> (0)),
68 units::samples (std::numeric_limits<int64_t>::max ()));
76 self.get_base_cache ()->clear ();
80 self.get_base_cache ()->remove_sequences_matching_interval (
84 const auto regions_inside_interval_filter_func =
85 [&affected_range, sample_interval] (
const auto ®ion) {
89 return region->bounds ()->is_hit_by_range (sample_interval);
92 const auto cache_region = [&] (
const auto * r) {
94 if (r->mute ()->muted ())
98 std::is_same_v<RegionType, arrangement::MidiRegion>
99 || std::is_same_v<RegionType, arrangement::ChordRegion>)
101 self.cache_midi_region (*r, tempo_map);
103 else if constexpr (std::is_same_v<RegionType, arrangement::AudioRegion>)
105 self.cache_audio_region (*r);
108 std::is_same_v<RegionType, arrangement::AutomationRegion>)
110 self.cache_automation_region (*r, tempo_map);
115 std::ranges::for_each (
116 std::views::filter (regions, regions_inside_interval_filter_func),
120 self.get_base_cache ()->finalize_changes ();
123 virtual void clear_all_caches () = 0;
125 remove_sequences_matching_interval_from_all_caches (IntervalType interval) = 0;
133 dsp::ITransport::PlayState::Paused
146class MidiTimelineDataProvider :
public TimelineDataProvider
148 friend class TimelineDataProvider;
151 explicit MidiTimelineDataProvider (QObject * parent =
nullptr);
155 return midi_cache_.get ();
163 dsp::ITransport::PlayState transport_state,
166 void clear_all_caches ()
override;
167 void remove_sequences_matching_interval_from_all_caches (
168 IntervalType interval)
override;
170 const juce::MidiMessageSequence &get_midi_events ()
const;
183 const dsp::TempoMap &tempo_map,
188 tempo_map, midi_regions, affected_range);
189 set_midi_events (midi_cache_->get_midi_events ());
203 const dsp::TempoMap &tempo_map,
208 tempo_map, chord_regions, affected_range);
209 set_midi_events (midi_cache_->get_midi_events ());
215 return midi_cache_.get ();
222 template <
typename M
idiRegionType>
224 cache_midi_region (
const MidiRegionType ®ion,
const dsp::TempoMap &tempo_map)
227 juce::MidiMessageSequence region_seq;
231 region_seq.addTimeToMessages (region.position ()->ticks ());
234 for (
auto &event : region_seq)
236 event->message.setTimeStamp (
237 static_cast<double> (
239 .tick_to_samples_rounded (
240 units::ticks (event->message.getTimeStamp ()))
241 .in (units::samples)));
245 midi_cache_->add_midi_sequence (
247 units::samples (region.position ()->samples ()),
248 region.bounds ()->get_end_position_samples (
true)),
257 void set_midi_events (
const juce::MidiMessageSequence &events);
259 utils::QObjectUniquePtr<dsp::MidiTimelineDataCache> midi_cache_;
261 farbot::RealtimeObject<
262 juce::MidiMessageSequence,
263 farbot::RealtimeObjectOptions::nonRealtimeMutatable>
264 active_midi_playback_sequence_;
273class AudioTimelineDataProvider :
public TimelineDataProvider
275 friend class TimelineDataProvider;
278 explicit AudioTimelineDataProvider (QObject * parent =
nullptr);
282 return audio_cache_.get ();
290 dsp::ITransport::PlayState transport_state,
291 std::span<float> output_left,
292 std::span<float> output_right)
noexcept [[clang::nonblocking]];
294 void clear_all_caches ()
override;
295 void remove_sequences_matching_interval_from_all_caches (
296 IntervalType interval)
override;
298 const std::vector<dsp::AudioTimelineDataCache::AudioRegionEntry> &
299 get_audio_regions ()
const;
312 const dsp::TempoMap &tempo_map,
317 tempo_map, audio_regions, affected_range);
318 set_audio_regions (audio_cache_->get_audio_regions ());
324 return audio_cache_.get ();
338 void set_audio_regions (
339 const std::vector<dsp::AudioTimelineDataCache::AudioRegionEntry> ®ions);
343 farbot::RealtimeObject<
344 std::vector<dsp::AudioTimelineDataCache::AudioRegionEntry>,
345 farbot::RealtimeObjectOptions::nonRealtimeMutatable>
346 active_audio_regions_;
355class AutomationTimelineDataProvider :
public TimelineDataProvider
357 friend class TimelineDataProvider;
360 explicit AutomationTimelineDataProvider (QObject * parent =
nullptr);
364 return automation_cache_.get ();
372 dsp::ITransport::PlayState transport_state,
373 std::span<float> output_values)
noexcept [[clang::nonblocking]];
383 [[clang::nonblocking]];
385 void clear_all_caches ()
override;
386 void remove_sequences_matching_interval_from_all_caches (
387 IntervalType interval)
override;
389 const std::vector<dsp::AutomationTimelineDataCache::AutomationCacheEntry> &
390 get_automation_sequences ()
const;
403 const dsp::TempoMap &tempo_map,
408 tempo_map, automation_regions, affected_range);
409 set_automation_sequences (automation_cache_->get_automation_sequences ());
415 return automation_cache_.get ();
422 void cache_automation_region (
424 const dsp::TempoMap &tempo_map);
431 void set_automation_sequences (
432 const std::vector<dsp::AutomationTimelineDataCache::AutomationCacheEntry>
437 farbot::RealtimeObject<
438 std::vector<dsp::AutomationTimelineDataCache::AutomationCacheEntry>,
439 farbot::RealtimeObjectOptions::nonRealtimeMutatable>
440 active_automation_sequences_;
A lock-free thread-safe vector of MidiEvents.
Base class for timeline data caches.
A region for playing back audio samples.
void process_audio_events(const dsp::graph::EngineProcessTimeInfo &time_nfo, dsp::ITransport::PlayState transport_state, std::span< float > output_left, std::span< float > output_right) noexcept
Process audio events for the given time range.
void generate_audio_events(const dsp::TempoMap &tempo_map, RangeOf< const arrangement::AudioRegion * > auto audio_regions, utils::ExpandableTickRange affected_range)
Generate the audio event sequence to be used during realtime processing.
Represents an automation region, which contains a collection of automation points.
void generate_automation_events(const dsp::TempoMap &tempo_map, RangeOf< const arrangement::AutomationRegion * > auto automation_regions, utils::ExpandableTickRange affected_range)
Generate the automation event sequence to be used during realtime processing.
std::optional< float > get_automation_value_rt(units::sample_t sample_position) noexcept
Get the automation value for a specific sample position.
void process_automation_events(const dsp::graph::EngineProcessTimeInfo &time_nfo, dsp::ITransport::PlayState transport_state, std::span< float > output_values) noexcept
Process automation events for the given time range.
void generate_midi_events(const dsp::TempoMap &tempo_map, RangeOf< const arrangement::ChordRegion * > auto chord_regions, utils::ExpandableTickRange affected_range)
Generate the MIDI event sequence to be used during realtime processing.
void process_midi_events(const dsp::graph::EngineProcessTimeInfo &time_nfo, dsp::ITransport::PlayState transport_state, dsp::MidiEventVector &output_buffer) noexcept
Process MIDI events for the given time range.
void generate_midi_events(const dsp::TempoMap &tempo_map, RangeOf< const arrangement::MidiRegion * > auto midi_regions, utils::ExpandableTickRange affected_range)
Generate the MIDI event sequence to be used during realtime processing.
static void serialize_to_sequence(const MidiRegion ®ion, juce::MidiMessageSequence &events, std::optional< std::pair< double, double > > timeline_range_ticks=std::nullopt)
Serializes a MIDI region to a MIDI message sequence.
void generate_events(this auto &self, const dsp::TempoMap &tempo_map, RangeOf< const RegionType * > auto regions, utils::ExpandableTickRange affected_range)
Generate the event sequence to be used during realtime processing.
dsp::ITransport::PlayState last_seen_transport_state_
Last transport state we've seen.
units::sample_t next_expected_transport_position_
Next expected transport position (for detecting jumps).
bool is_full_content() const
Returns whether the range is the full content (ie, there is no range).
auto range() const -> std::optional< std::pair< double, double > >
Returns the range, or nullopt if the full content is covered.
A unique pointer for QObject objects that also works with QObject-based ownership.
Common struct to pass around during processing to avoid repeating the data in function arguments.