6#include "dsp/graph_node.h"
7#include "dsp/itransport.h"
8#include "dsp/timeline_data_cache.h"
9#include "structure/arrangement/region_renderer.h"
10#include "utils/expandable_tick_range.h"
13#include <farbot/RealtimeObject.hpp>
15namespace zrythm::structure::arrangement
25class TimelineDataProvider :
public QObject
30 using IntervalType = std::pair<units::sample_t, units::sample_t>;
32 explicit TimelineDataProvider (QObject * parent =
nullptr) : QObject (parent)
36 ~TimelineDataProvider ()
override;
50 template <RegionObject RegionType>
53 const dsp::TempoMap &tempo_map,
58 const auto sample_interval = [&affected_range, &tempo_map] ()
59 -> std::pair<units::sample_t, units::sample_t> {
62 const auto tick_range = affected_range.
range ().value ();
63 return std::make_pair (
64 tempo_map.tick_to_samples_rounded (units::ticks (tick_range.first)),
65 tempo_map.tick_to_samples_rounded (units::ticks (tick_range.second)));
67 return std::make_pair (
68 units::samples (
static_cast<int64_t
> (0)),
69 units::samples (std::numeric_limits<int64_t>::max ()));
77 self.get_base_cache ()->clear ();
81 self.get_base_cache ()->remove_sequences_matching_interval (
85 const auto regions_inside_interval_filter_func =
86 [&affected_range, sample_interval] (
const auto ®ion) {
90 return region->bounds ()->is_hit_by_range (sample_interval);
93 const auto cache_region = [&] (
const auto * r) {
95 if (r->mute ()->muted ())
99 std::is_same_v<RegionType, arrangement::MidiRegion>
100 || std::is_same_v<RegionType, arrangement::ChordRegion>)
102 self.cache_midi_region (*r, tempo_map);
104 else if constexpr (std::is_same_v<RegionType, arrangement::AudioRegion>)
106 self.cache_audio_region (*r);
109 std::is_same_v<RegionType, arrangement::AutomationRegion>)
111 self.cache_automation_region (*r, tempo_map);
116 std::ranges::for_each (
117 std::views::filter (regions, regions_inside_interval_filter_func),
121 self.get_base_cache ()->finalize_changes ();
124 virtual void clear_all_caches () = 0;
126 remove_sequences_matching_interval_from_all_caches (IntervalType interval) = 0;
134 dsp::ITransport::PlayState::Paused
147class MidiTimelineDataProvider :
public TimelineDataProvider
149 friend class TimelineDataProvider;
152 explicit MidiTimelineDataProvider (QObject * parent =
nullptr);
156 return midi_cache_.get ();
164 dsp::ITransport::PlayState transport_state,
167 void clear_all_caches ()
override;
168 void remove_sequences_matching_interval_from_all_caches (
169 IntervalType interval)
override;
171 const juce::MidiMessageSequence &get_midi_events ()
const;
184 const dsp::TempoMap &tempo_map,
189 tempo_map, midi_regions, affected_range);
190 set_midi_events (midi_cache_->get_midi_events ());
204 const dsp::TempoMap &tempo_map,
209 tempo_map, chord_regions, affected_range);
210 set_midi_events (midi_cache_->get_midi_events ());
216 return midi_cache_.get ();
223 template <
typename M
idiRegionType>
225 cache_midi_region (
const MidiRegionType ®ion,
const dsp::TempoMap &tempo_map)
228 juce::MidiMessageSequence region_seq;
232 region_seq.addTimeToMessages (region.position ()->ticks ());
235 for (
auto &event : region_seq)
237 event->message.setTimeStamp (
238 static_cast<double> (
240 .tick_to_samples_rounded (
241 units::ticks (event->message.getTimeStamp ()))
242 .in (units::samples)));
246 midi_cache_->add_midi_sequence (
248 units::samples (region.position ()->samples ()),
249 region.bounds ()->get_end_position_samples (
true)),
258 void set_midi_events (
const juce::MidiMessageSequence &events);
260 utils::QObjectUniquePtr<dsp::MidiTimelineDataCache> midi_cache_;
262 farbot::RealtimeObject<
263 juce::MidiMessageSequence,
264 farbot::RealtimeObjectOptions::nonRealtimeMutatable>
265 active_midi_playback_sequence_;
274class AudioTimelineDataProvider :
public TimelineDataProvider
276 friend class TimelineDataProvider;
279 explicit AudioTimelineDataProvider (QObject * parent =
nullptr);
283 return audio_cache_.get ();
291 dsp::ITransport::PlayState transport_state,
292 std::span<float> output_left,
293 std::span<float> output_right)
noexcept [[clang::nonblocking]];
295 void clear_all_caches ()
override;
296 void remove_sequences_matching_interval_from_all_caches (
297 IntervalType interval)
override;
299 const std::vector<dsp::AudioTimelineDataCache::AudioRegionEntry> &
300 get_audio_regions ()
const;
313 const dsp::TempoMap &tempo_map,
318 tempo_map, audio_regions, affected_range);
319 set_audio_regions (audio_cache_->get_audio_regions ());
325 return audio_cache_.get ();
339 void set_audio_regions (
340 const std::vector<dsp::AudioTimelineDataCache::AudioRegionEntry> ®ions);
344 farbot::RealtimeObject<
345 std::vector<dsp::AudioTimelineDataCache::AudioRegionEntry>,
346 farbot::RealtimeObjectOptions::nonRealtimeMutatable>
347 active_audio_regions_;
356class AutomationTimelineDataProvider :
public TimelineDataProvider
358 friend class TimelineDataProvider;
361 explicit AutomationTimelineDataProvider (QObject * parent =
nullptr);
365 return automation_cache_.get ();
373 dsp::ITransport::PlayState transport_state,
374 std::span<float> output_values)
noexcept [[clang::nonblocking]];
384 [[clang::nonblocking]];
386 void clear_all_caches ()
override;
387 void remove_sequences_matching_interval_from_all_caches (
388 IntervalType interval)
override;
390 const std::vector<dsp::AutomationTimelineDataCache::AutomationCacheEntry> &
391 get_automation_sequences ()
const;
404 const dsp::TempoMap &tempo_map,
409 tempo_map, automation_regions, affected_range);
410 set_automation_sequences (automation_cache_->get_automation_sequences ());
416 return automation_cache_.get ();
423 void cache_automation_region (
425 const dsp::TempoMap &tempo_map);
432 void set_automation_sequences (
433 const std::vector<dsp::AutomationTimelineDataCache::AutomationCacheEntry>
438 farbot::RealtimeObject<
439 std::vector<dsp::AutomationTimelineDataCache::AutomationCacheEntry>,
440 farbot::RealtimeObjectOptions::nonRealtimeMutatable>
441 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::ProcessBlockInfo &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::ProcessBlockInfo &time_nfo, dsp::ITransport::PlayState transport_state, std::span< float > output_values) noexcept
Process automation events for the given time range.
void process_midi_events(const dsp::graph::ProcessBlockInfo &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::ChordRegion * > auto chord_regions, utils::ExpandableTickRange affected_range)
Generate the MIDI event sequence to be used during realtime processing.
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.