Zrythm v2.0.0-alpha.1
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
audio_sample_processor.h
1// SPDX-FileCopyrightText: © 2025-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <source_location>
7
8#include "dsp/processor_base.h"
9
10#include <boost/container/static_vector.hpp>
11
12namespace zrythm::dsp
13{
14
15class AudioSampleProcessor : public dsp::ProcessorBase
16{
17public:
18 AudioSampleProcessor (utils::IObjectRegistry &registry);
19
25 struct PlayableSampleSingleChannel
26 {
27 using UnmutableSampleSpan = std::span<const float>;
28 PlayableSampleSingleChannel (
29 UnmutableSampleSpan buf,
30 uint8_t channel_index,
31 float volume,
32 units::sample_u32_t start_offset,
33 std::source_location source_location)
34 : buf_ (buf), channel_index_ (channel_index), volume_ (volume),
35 start_offset_ (start_offset), source_location_ (source_location)
36 {
37 }
39 UnmutableSampleSpan buf_;
40
45
47 units::sample_u64_t offset_;
48
51 float volume_ = 1.0f;
52
55 units::sample_u32_t start_offset_;
56
57 // For debugging purposes
58 std::source_location source_location_;
59 };
60
61 using QueueSingleChannelSampleCallback =
62 std::function<void (PlayableSampleSingleChannel)>;
63
73 {
74 if (sample.buf_.empty ()) [[unlikely]]
75 {
76 // ignore empty samples
77 return;
78 }
79
80 samples_to_play_.emplace_back (sample);
81 if (queue_sample_cb_.has_value ())
82 {
83 std::invoke (queue_sample_cb_.value (), sample);
84 }
85 }
86
87 void set_queue_sample_callback (QueueSingleChannelSampleCallback cb)
88 {
89 queue_sample_cb_ = std::move (cb);
90 }
91
92 auto get_output_audio_port_non_rt () const
93 {
94 return get_output_ports ()[0].get_object_as<dsp::AudioPort> ();
95 }
96
97 auto get_output_audio_port_rt () const { return audio_out_; }
98
101 const dsp::ITransport &transport,
102 const dsp::TempoMap &tempo_map) noexcept override;
103
104 void custom_prepare_for_processing (
105 const graph::GraphNode * node,
106 units::sample_rate_t sample_rate,
107 units::sample_u32_t max_block_length) override
108 {
109 samples_to_play_.clear ();
110 audio_out_ = get_output_ports ()[0].get_object_as<dsp::AudioPort> ();
111 }
112
113 void custom_release_resources () override
114 {
115 samples_to_play_.clear ();
116 audio_out_ = nullptr;
117 }
118
119private:
125 boost::container::static_vector<PlayableSampleSingleChannel, 128>
126 samples_to_play_;
127
133 std::optional<QueueSingleChannelSampleCallback> queue_sample_cb_;
134
135 AudioPort * audio_out_{};
136};
137} // namespace zrythm::dsp
Audio port specifics.
Definition audio_port.h:25
void add_sample_to_process(PlayableSampleSingleChannel sample)
Adds a sample to the queue.
void custom_process_block(dsp::graph::ProcessBlockInfo time_nfo, const dsp::ITransport &transport, const dsp::TempoMap &tempo_map) noexcept override
Custom processor logic after processing all owned parameters.
Interface for transport.
Definition itransport.h:16
A base class for processors in the DSP graph.
Represents a node in a DSP graph.
Definition graph_node.h:173
Abstract interface for a UUID-keyed object registry.
A sample playback handle to be used by the engine.
UnmutableSampleSpan buf_
Samples to play for a single channel.
units::sample_u32_t start_offset_
Offset relative to the current processing cycle to start playing the sample.
units::sample_u64_t offset_
The current offset in the buffer.
uint8_t channel_index_
Channel index to play this sample at.
float volume_
The volume to play the sample at (ratio from 0.0 to 2.0, where 1.0 is the normal volume).
Common struct to pass around during processing to avoid repeating the data in function arguments.
Definition graph_node.h:51