Zrythm v2.0.0-DEV
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 (
20 : dsp::ProcessorBase (dependencies)
21 {
22
23 auto out_ref = dependencies.port_registry_.create_object<dsp::AudioPort> (
24 u8"Stereo Out", PortFlow::Output, AudioPort::BusLayout::Stereo, 2);
25 out_ref.get_object_as<dsp::AudioPort> ()->set_symbol (u8"stereo_out");
26 add_output_port (out_ref);
27 set_name (u8"Audio Sample Processor");
28 }
29
35 struct PlayableSampleSingleChannel
36 {
37 using UnmutableSampleSpan = std::span<const float>;
38 PlayableSampleSingleChannel (
39 UnmutableSampleSpan buf,
40 uint8_t channel_index,
41 float volume,
42 units::sample_u32_t start_offset,
43 std::source_location source_location)
44 : buf_ (buf), channel_index_ (channel_index), volume_ (volume),
45 start_offset_ (start_offset), source_location_ (source_location)
46 {
47 }
49 UnmutableSampleSpan buf_;
50
55
57 units::sample_u64_t offset_;
58
61 float volume_ = 1.0f;
62
65 units::sample_u32_t start_offset_;
66
67 // For debugging purposes
68 std::source_location source_location_;
69 };
70
71 using QueueSingleChannelSampleCallback =
72 std::function<void (PlayableSampleSingleChannel)>;
73
83 {
84 if (sample.buf_.empty ()) [[unlikely]]
85 {
86 // ignore empty samples
87 return;
88 }
89
90 samples_to_play_.emplace_back (sample);
91 if (queue_sample_cb_.has_value ())
92 {
93 std::invoke (queue_sample_cb_.value (), sample);
94 }
95 }
96
97 void set_queue_sample_callback (QueueSingleChannelSampleCallback cb)
98 {
99 queue_sample_cb_ = std::move (cb);
100 }
101
102 auto get_output_audio_port_non_rt () const
103 {
104 return get_output_ports ()[0].get_object_as<dsp::AudioPort> ();
105 }
106
107 auto get_output_audio_port_rt () const { return audio_out_; }
108
111 const dsp::ITransport &transport,
112 const dsp::TempoMap &tempo_map) noexcept override;
113
114 void custom_prepare_for_processing (
115 const graph::GraphNode * node,
116 units::sample_rate_t sample_rate,
117 units::sample_u32_t max_block_length) override
118 {
119 samples_to_play_.clear ();
120 audio_out_ = get_output_ports ()[0].get_object_as<dsp::AudioPort> ();
121 }
122
123 void custom_release_resources () override
124 {
125 samples_to_play_.clear ();
126 audio_out_ = nullptr;
127 }
128
129private:
135 boost::container::static_vector<PlayableSampleSingleChannel, 128>
136 samples_to_play_;
137
143 std::optional<QueueSingleChannelSampleCallback> queue_sample_cb_;
144
145 AudioPort * audio_out_{};
146};
147} // namespace zrythm::dsp
Audio port specifics.
Definition audio_port.h:26
void add_sample_to_process(PlayableSampleSingleChannel sample)
Adds a sample to the queue.
void custom_process_block(dsp::graph::EngineProcessTimeInfo 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.
void set_name(const utils::Utf8String &name)
Set a custom name to be used in the DSP graph.
Represents a node in a DSP graph.
Definition graph_node.h:160
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