MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MixProcessor.hpp
Go to the documentation of this file.
1#pragma once
3
4namespace MayaFlux::Buffers {
5
6class AudioBuffer;
7
8/**
9 * @struct MixSource
10 * @brief Represents a source audio buffer with its data and mixing properties.
11 *
12 * This structure holds a reference to an audio buffer and its data, along with
13 * the mix level and a flag indicating if it should be mixed only once.
14 */
15struct MAYAFLUX_API MixSource {
16 std::span<double> data;
17 double mix_level = 1.0;
18 bool once = false;
19
20 MixSource(std::shared_ptr<AudioBuffer> buffer, double level = 1.0, bool once_flag = false);
21
22 bool is_valid() const
23 {
24 return !buffer_ref.expired() && !data.empty();
25 }
26
27 bool refresh_data();
28
29 bool matches_buffer(const std::shared_ptr<AudioBuffer>& buffer) const
30 {
31 return !buffer_ref.expired() && buffer_ref.lock() == buffer;
32 }
33
34 inline bool has_sample_at(size_t index) const
35 {
36 return index < data.size();
37 }
38
39 inline double get_mixed_sample(size_t index) const
40 {
41 return data[index] * mix_level;
42 }
43
44private:
45 std::weak_ptr<AudioBuffer> buffer_ref;
46};
47
48/**
49 * @class MixProcessor
50 * @brief Processes multiple audio buffers and mixes their data into a single output buffer.
51 *
52 * This processor allows multiple audio sources to be registered, each with its own mix level.
53 * It can handle both continuous mixing and one-time mixes based on the `once` flag.
54 *
55 * This is the primary mechanism for single audio buffer to be mixed into multiple channels.
56 * Compared to Nodes, buffers are inherently single channel due to their transient nature and
57 * the architecture of MayaFlux that adds processors to buffers instead of processing buffers themselves
58 * Hence, process once and supply to multiple channels is the most efficient method to send concurrent data
59 * to multiple channels.
60 */
61class MAYAFLUX_API MixProcessor : public BufferProcessor {
62public:
63 /**
64 * @brief register an AudioBuffer source to be mixed into the output of specified channel
65 * @param source AudioBuffer to register
66 * @param mix_level Level of mixing for this source (default: 1.0)
67 * @param once If true, the source will be mixed only once and then removed (default: false)
68 */
69 bool register_source(std::shared_ptr<AudioBuffer> source, double mix_level = 1.0, bool once = false);
70
71 /** @brief the mechanism to mix output from one buffer to another channel */
72 void processing_function(std::shared_ptr<Buffer> buffer) override;
73
74 /**
75 * @brief Removes a source buffer from the mix
76 * @param buffer Buffer to remove
77 * @return true if the source was successfully removed, false if it was not found
78 */
79 bool remove_source(std::shared_ptr<AudioBuffer> buffer);
80
81private:
82 void cleanup();
83
84 void validate_sources();
85
86 std::vector<MixSource> m_sources;
87};
88
89}
Central computational transformation interface for continuous buffer processing.
std::vector< MixSource > m_sources
Processes multiple audio buffers and mixes their data into a single output buffer.
std::weak_ptr< AudioBuffer > buffer_ref
bool matches_buffer(const std::shared_ptr< AudioBuffer > &buffer) const
double get_mixed_sample(size_t index) const
bool has_sample_at(size_t index) const
Represents a source audio buffer with its data and mixing properties.