OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
merger.hpp
1#pragma once
2#include <ossia/dataflow/graph_node.hpp>
3#include <ossia/dataflow/port.hpp>
4
5namespace ossia::nodes
6{
7class mono_merger final : public ossia::graph_node
8{
9 int m_count{};
10
11public:
12 explicit mono_merger(int count)
13 : m_count{count}
14 {
15 for(int i = 0; i < count; i++)
16 {
17 auto inl = new ossia::audio_inlet;
18 inl->target<ossia::audio_port>()->set_channels(1);
19 m_inlets.push_back(std::move(inl));
20 }
21
22 m_outlets.push_back(new ossia::audio_outlet);
23 m_outlets.back()->target<ossia::audio_port>()->set_channels(count);
24 }
25
26 ~mono_merger() override = default;
27
28 void run(const ossia::token_request& t, ossia::exec_state_facade e) noexcept override
29 {
30 auto& op = *m_outlets.back()->target<ossia::audio_port>();
31 op.set_channels(this->m_count);
32 auto& out = op.get();
33 std::size_t cur = 0;
34 for(int i = 0; i < m_count; i++)
35 {
36 auto& in = m_inlets[i]->target<ossia::audio_port>()->get();
37
38 if(in.size() > 0)
39 out[cur++] = in[0];
40 else
41 out[cur++].resize(e.bufferSize());
42 }
43
44 for(auto& c : out)
45 {
46 if(c.size() < e.bufferSize())
47 {
48 c.resize(e.bufferSize());
49 }
50 }
51 }
52
53 [[nodiscard]] std::string label() const noexcept override { return "Mono Merger"; }
54};
55class merger final : public ossia::graph_node
56{
57 int m_count{};
58
59public:
60 explicit merger(int count)
61 : m_count{count}
62 {
63 for(int i = 0; i < count; i++)
64 {
65 auto inl = new ossia::audio_inlet;
66 inl->target<ossia::audio_port>()->set_channels(2);
67 for(auto& channel : *inl->target<ossia::audio_port>())
68 {
69 channel.reserve(512);
70 }
71 m_inlets.push_back(std::move(inl));
72 }
73
74 m_outlets.push_back(new ossia::audio_outlet);
75 m_outlets.back()->target<ossia::audio_port>()->set_channels(2 * count);
76 for(auto& channel : *m_outlets.back()->target<ossia::audio_port>())
77 {
78 channel.reserve(512);
79 }
80 }
81
82 ~merger() override = default;
83
84 void run(const ossia::token_request& t, ossia::exec_state_facade e) noexcept override
85 {
86 auto& op = *m_outlets.back()->target<ossia::audio_port>();
87 op.set_channels(2 * this->m_count);
88 auto& out = op.get();
89 std::size_t cur = 0;
90 for(int i = 0; i < m_count; i++)
91 {
92 auto& in = m_inlets[i]->target<ossia::audio_port>()->get();
93
94 switch(in.size())
95 {
96 case 1:
97 out[cur++] = in[0];
98 out[cur++] = in[0];
99 break;
100 case 2:
101 out[cur++] = in[0];
102 out[cur++] = in[1];
103 break;
104 default:
105 out[cur++].resize(e.bufferSize());
106 out[cur++].resize(e.bufferSize());
107 break;
108 }
109 }
110
111 for(auto& c : out)
112 {
113 if(c.size() < e.bufferSize())
114 {
115 c.resize(e.bufferSize());
116 }
117 }
118 }
119
120 [[nodiscard]] std::string label() const noexcept override { return "Stereo Merger"; }
121};
122}
Definition git_info.h:7