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"
10#include "utils/types.h"
12#include <farbot/RealtimeObject.hpp>
14namespace zrythm::structure::arrangement
27 using IntervalType = std::pair<units::sample_t, units::sample_t>;
43 template <RegionObject RegionType>
46 const dsp::TempoMap &tempo_map,
51 const auto sample_interval = [&affected_range, &tempo_map] ()
52 -> std::pair<units::sample_t, units::sample_t> {
55 const auto tick_range = affected_range.
range ().value ();
56 return std::make_pair (
57 tempo_map.tick_to_samples_rounded (units::ticks (tick_range.first)),
58 tempo_map.tick_to_samples_rounded (units::ticks (tick_range.second)));
60 return std::make_pair (
61 units::samples (
static_cast<int64_t
> (0)),
62 units::samples (std::numeric_limits<int64_t>::max ()));
70 self.get_base_cache ()->clear ();
74 self.get_base_cache ()->remove_sequences_matching_interval (
78 const auto regions_inside_interval_filter_func =
79 [&affected_range, sample_interval] (
const auto ®ion) {
83 return region->bounds ()->is_hit_by_range (sample_interval);
86 const auto cache_region = [&] (
const auto * r) {
88 if (r->mute ()->muted ())
92 std::is_same_v<RegionType, arrangement::MidiRegion>
93 || std::is_same_v<RegionType, arrangement::ChordRegion>)
95 self.cache_midi_region (*r, tempo_map);
97 else if constexpr (std::is_same_v<RegionType, arrangement::AudioRegion>)
99 self.cache_audio_region (*r);
102 std::is_same_v<RegionType, arrangement::AutomationRegion>)
104 self.cache_automation_region (*r, tempo_map);
109 std::ranges::for_each (
110 std::views::filter (regions, regions_inside_interval_filter_func),
114 self.get_base_cache ()->finalize_changes ();
117 virtual void clear_all_caches () = 0;
119 remove_sequences_matching_interval_from_all_caches (IntervalType interval) = 0;
127 dsp::ITransport::PlayState::Paused
142 friend class TimelineDataProvider;
150 dsp::ITransport::PlayState transport_state,
154 void clear_all_caches ()
override;
155 void remove_sequences_matching_interval_from_all_caches (
156 IntervalType interval)
override;
158 const juce::MidiMessageSequence &get_midi_events ()
const;
171 const dsp::TempoMap &tempo_map,
176 tempo_map, midi_regions, affected_range);
177 set_midi_events (midi_cache_.get_midi_events ());
191 const dsp::TempoMap &tempo_map,
196 tempo_map, chord_regions, affected_range);
197 set_midi_events (midi_cache_.get_midi_events ());
207 template <
typename M
idiRegionType>
209 cache_midi_region (
const MidiRegionType ®ion,
const dsp::TempoMap &tempo_map)
212 juce::MidiMessageSequence region_seq;
216 region_seq.addTimeToMessages (region.position ()->ticks ());
219 for (
auto &event : region_seq)
221 event->message.setTimeStamp (
222 static_cast<double> (
224 .tick_to_samples_rounded (
225 units::ticks (event->message.getTimeStamp ()))
226 .in (units::samples)));
232 units::samples (region.position ()->samples ()),
233 region.bounds ()->get_end_position_samples (
true)),
242 void set_midi_events (
const juce::MidiMessageSequence &events);
246 farbot::RealtimeObject<
247 juce::MidiMessageSequence,
248 farbot::RealtimeObjectOptions::nonRealtimeMutatable>
249 active_midi_playback_sequence_;
260 friend class TimelineDataProvider;
268 dsp::ITransport::PlayState transport_state,
269 std::span<float> output_left,
270 std::span<float> output_right)
noexcept [[clang::nonblocking]];
273 void clear_all_caches ()
override;
274 void remove_sequences_matching_interval_from_all_caches (
275 IntervalType interval)
override;
277 const std::vector<dsp::AudioTimelineDataCache::AudioRegionEntry> &
278 get_audio_regions ()
const;
291 const dsp::TempoMap &tempo_map,
296 tempo_map, audio_regions, affected_range);
297 set_audio_regions (audio_cache_.get_audio_regions ());
310 auto audio_buffer = std::make_unique<juce::AudioSampleBuffer> ();
318 units::samples (region.position ()->samples ()),
319 region.bounds ()->get_end_position_samples (
true)),
328 void set_audio_regions (
329 const std::vector<dsp::AudioTimelineDataCache::AudioRegionEntry> ®ions);
333 farbot::RealtimeObject<
334 std::vector<dsp::AudioTimelineDataCache::AudioRegionEntry>,
335 farbot::RealtimeObjectOptions::nonRealtimeMutatable>
336 active_audio_regions_;
347 friend class TimelineDataProvider;
355 dsp::ITransport::PlayState transport_state,
356 std::span<float> output_values)
noexcept [[clang::nonblocking]];
366 [[clang::nonblocking]];
369 void clear_all_caches ()
override;
370 void remove_sequences_matching_interval_from_all_caches (
371 IntervalType interval)
override;
373 const std::vector<dsp::AutomationTimelineDataCache::AutomationCacheEntry> &
374 get_automation_sequences ()
const;
387 const dsp::TempoMap &tempo_map,
392 tempo_map, automation_regions, affected_range);
393 set_automation_sequences (automation_cache_.get_automation_sequences ());
399 return &automation_cache_;
406 void cache_automation_region (
408 const dsp::TempoMap &tempo_map)
411 const auto start_sample = units::samples (region.position ()->samples ());
412 const auto end_sample = region.bounds ()->get_end_position_samples (
true);
413 const auto num_samples = end_sample - start_sample;
415 std::vector<float> automation_values (num_samples.in (units::samples));
419 region, automation_values);
422 std::make_pair (start_sample, end_sample), automation_values);
430 void set_automation_sequences (
431 const std::vector<dsp::AutomationTimelineDataCache::AutomationCacheEntry>
436 farbot::RealtimeObject<
437 std::vector<dsp::AutomationTimelineDataCache::AutomationCacheEntry>,
438 farbot::RealtimeObjectOptions::nonRealtimeMutatable>
439 active_automation_sequences_;
Audio-specific timeline data cache.
void add_audio_region(IntervalType interval, const juce::AudioSampleBuffer &audio_buffer)
Adds an audio region for the given interval.
Automation-specific timeline data cache.
void add_automation_sequence(IntervalType interval, const std::vector< float > &automation_values)
Adds an automation sequence for the given interval.
A lock-free thread-safe vector of MidiEvents.
MIDI-specific timeline data cache.
void add_midi_sequence(IntervalType interval, const juce::MidiMessageSequence &sequence)
Adds a MIDI sequence for the given interval.
Base class for timeline data caches.
A region for playing back audio samples.
Audio-specific timeline data provider.
void process_audio_events(const 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.
Automation-specific timeline data provider.
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 EngineProcessTimeInfo &time_nfo, dsp::ITransport::PlayState transport_state, std::span< float > output_values) noexcept
Process automation events for the given time range.
MIDI-specific timeline data provider.
void process_midi_events(const 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::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.
static void serialize_to_automation_values(const AutomationRegion ®ion, std::vector< float > &values, std::optional< std::pair< double, double > > timeline_range_ticks=std::nullopt)
Serializes an Automation region to sample-accurate automation values.
static void serialize_to_buffer(const AudioRegion ®ion, juce::AudioSampleBuffer &buffer, std::optional< std::pair< double, double > > timeline_range_ticks=std::nullopt)
Serializes an Audio region to an audio sample buffer.
Base class for timeline data providers.
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.
Common struct to pass around during processing to avoid repeating the data in function arguments.