2 #include <Process/Dataflow/TimeSignature.hpp>
4 #include <Vst/EffectModel.hpp>
6 #include <ossia/dataflow/fx_node.hpp>
7 #include <ossia/dataflow/graph_node.hpp>
8 #include <ossia/dataflow/port.hpp>
9 #include <ossia/detail/pod_vector.hpp>
10 #include <ossia/editor/scenario/time_signature.hpp>
17 std::shared_ptr<AEffectWrapper> fx{};
31 ossia::value_port* port{};
35 int32_t opcode, int32_t index = 0, intptr_t value = 0,
void* ptr =
nullptr,
38 fx->dispatch(opcode, index, value, ptr, opt);
42 ossia::small_vector<vst_control, 16> controls;
46 for(vst_control& p : controls)
48 const auto& vec = p.port->get_data();
51 auto t = ossia::convert<float>(last(vec));
53 p.value = ossia::clamp<float>(t, 0.f, 1.f);
54 fx->fx->setParameter(fx->fx, p.idx, p.value);
59 auto& prepareInput(int64_t offset, int64_t samples)
61 const auto bs = offset + samples;
62 auto& p = *m_inlets[0]->template target<ossia::audio_port>();
67 p.channel(0).resize(bs);
68 p.channel(1).resize(bs);
73 p.channel(0).resize(bs);
74 p.channel(1).assign(p.channel(0).begin(), p.channel(0).end());
87 auto& prepareOutput(int64_t offset, int64_t samples)
89 const auto bs = offset + samples;
90 auto& p = *m_outlets[0]->template target<ossia::audio_port>();
93 chan.resize(bs, boost::container::default_init);
97 void setupTimeInfo(
const ossia::token_request& tk, ossia::exec_state_facade st)
99 static const constexpr
double ppq_reference = 960.;
101 auto& time_info = fx->info;
102 time_info.samplePos = this->m_processed_frames;
103 time_info.sampleRate = st.sampleRate();
104 time_info.nanoSeconds = st.currentDate() - st.startDate();
105 time_info.ppqPos = tk.musical_start_position;
106 time_info.tempo = tk.tempo;
107 time_info.barStartPos = tk.musical_start_last_bar;
108 time_info.cycleStartPos = 0.;
109 time_info.cycleEndPos = 0.;
110 time_info.timeSigNumerator = tk.signature.upper;
111 time_info.timeSigDenominator = tk.signature.lower;
112 time_info.smpteOffset = 0;
113 time_info.smpteFrameRate = 0;
114 time_info.samplesToNextClock = 0;
115 time_info.flags = kVstTransportPlaying | kVstNanosValid | kVstPpqPosValid
116 | kVstTempoValid | kVstBarsValid | kVstTimeSigValid
121 template <
bool UseDouble,
bool IsSynth>
125 static constexpr
bool synth = IsSynth;
126 VstSpeakerArrangement i_arr{};
127 VstSpeakerArrangement o_arr{};
130 vst_node(std::shared_ptr<AEffectWrapper> dat,
int sampleRate,
int bs)
135 m_inlets.push_back(
new ossia::audio_inlet);
136 if constexpr(IsSynth)
137 m_inlets.push_back(
new ossia::midi_inlet);
140 m_outlets.push_back(
new ossia::audio_outlet);
143 memset(&i_arr, 0,
sizeof(i_arr));
144 memset(&o_arr, 0,
sizeof(o_arr));
145 i_arr.type = kSpeakerArrStereo;
146 i_arr.numChannels = 2;
147 i_arr.speakers[0].type = kSpeakerL;
148 i_arr.speakers[1].type = kSpeakerR;
150 o_arr.type = kSpeakerArrStereo;
151 o_arr.numChannels = 2;
152 o_arr.speakers[0].type = kSpeakerL;
153 o_arr.speakers[1].type = kSpeakerR;
154 dispatch(effSetSpeakerArrangement, 0, (intptr_t)&i_arr, (
void*)&o_arr, 0);
157 dispatch(effSetSampleRate, 0, sampleRate,
nullptr, sampleRate);
158 dispatch(effSetBlockSize, 0, bs,
nullptr, bs);
160 effSetProcessPrecision, 0,
161 UseDouble ? kVstProcessPrecision64 : kVstProcessPrecision32);
162 dispatch(effMainsChanged, 0, 1);
163 dispatch(effStartProcess);
165 fx->fx->resvd2 =
reinterpret_cast<intptr_t
>(
this);
171 dispatch(effStopProcess);
172 dispatch(effMainsChanged, 0, 0);
175 std::string label()
const noexcept
override {
return ""; }
177 void all_notes_off() noexcept
override
179 if constexpr(IsSynth)
183 constexpr
auto sz =
sizeof(VstEvents) +
sizeof(
void*) * 32 * 2;
184 VstEvents* events = (VstEvents*)alloca(sz);
185 std::memset(events, 0, sz);
187 events->numEvents = 32;
189 VstMidiEvent ev[64] = {};
190 memset(&ev, 0,
sizeof(ev));
193 for(
int i = 0; i < 16; i++)
196 e.type = kVstMidiType;
197 e.flags = kVstMidiEventIsRealtime;
198 e.byteSize =
sizeof(VstMidiEvent);
200 e.midiData[0] = (char)(uint8_t)176 + i;
201 e.midiData[1] = (char)(uint8_t)123;
205 events->events[i] =
reinterpret_cast<VstEvent*
>(&e);
209 for(
int i = 0; i < 16; i++)
211 auto& e = ev[16 + i];
212 e.type = kVstMidiType;
213 e.flags = kVstMidiEventIsRealtime;
214 e.byteSize =
sizeof(VstMidiEvent);
216 e.midiData[0] = (char)(uint8_t)176 + i;
217 e.midiData[1] = (char)(uint8_t)121;
221 events->events[16 + i] =
reinterpret_cast<VstEvent*
>(&e);
224 dispatch(effProcessEvents, 0, 0, events, 0.f);
226 if constexpr(!UseDouble)
228 float* dummy = (
float*)alloca(
sizeof(
float) * m_bs);
229 std::fill_n(dummy, m_bs, 0.f);
232 = (
float**)alloca(
sizeof(
float*) * std::max(2, this->fx->fx->numInputs));
233 for(
int i = 0; i < this->fx->fx->numInputs; i++)
237 = (
float**)alloca(
sizeof(
float*) * std::max(2, this->fx->fx->numOutputs));
238 for(
int i = 0; i < this->fx->fx->numOutputs; i++)
241 fx->fx->processReplacing(fx->fx, input, output, m_bs);
245 double* dummy = (
double*)alloca(
sizeof(
double) * m_bs);
246 std::fill_n(dummy, m_bs, 0.);
249 = (
double**)alloca(
sizeof(
double*) * std::max(2, this->fx->fx->numInputs));
250 for(
int i = 0; i < this->fx->fx->numInputs; i++)
254 = (
double**)alloca(
sizeof(
double*) * std::max(2, this->fx->fx->numOutputs));
255 for(
int i = 0; i < this->fx->fx->numOutputs; i++)
258 fx->fx->processDoubleReplacing(fx->fx, input, output, m_bs);
266 template <
typename Fun>
267 void dispatchMidi(int64_t offset, Fun&& f)
270 auto& ip =
static_cast<ossia::midi_inlet*
>(m_inlets[1])->data.messages;
271 const auto n_mess = ip.size();
279 const auto sz =
sizeof(VstEvents) +
sizeof(
void*) * n_mess * 2;
280 VstEvents* events = (VstEvents*)alloca(sz);
281 std::memset(events, 0, sz);
282 events->numEvents = n_mess;
284 ossia::small_vector<VstMidiEvent, 16> vec;
287 for(libremidi::message& mess : ip)
289 VstMidiEvent& e = vec[i];
290 std::memset(&e, 0,
sizeof(VstMidiEvent));
292 e.type = kVstMidiType;
293 e.byteSize =
sizeof(VstMidiEvent);
294 e.deltaFrames = mess.timestamp - offset;
295 e.flags = kVstMidiEventIsRealtime;
298 e.midiData, mess.bytes.data(), std::min(mess.bytes.size(), (std::size_t)4));
300 events->events[i] =
reinterpret_cast<VstEvent*
>(&e);
303 dispatch(effProcessEvents, 0, 0, events, 0.f);
307 void run(
const ossia::token_request& tk, ossia::exec_state_facade st) noexcept
override
309 if(!muted() && tk.date > tk.prev_date)
311 const auto timings = st.timings(tk);
313 this->setupTimeInfo(tk, st);
315 if constexpr(UseDouble)
317 if constexpr(IsSynth)
319 dispatchMidi(timings.start_sample, [
this, timings] {
320 processDouble(timings.start_sample, timings.length);
325 processDouble(timings.start_sample, timings.length);
330 if constexpr(IsSynth)
332 dispatchMidi(timings.start_sample, [
this, timings] {
333 processFloat(timings.start_sample, timings.length);
338 processFloat(timings.start_sample, timings.length);
343 if(this->fx->fx->numOutputs == 1)
345 auto& op = m_outlets[0]->template target<ossia::audio_port>()->get();
346 op[1].assign(op[0].begin(), op[0].end());
351 void processFloat(int64_t offset, int64_t samples)
353 if constexpr(!UseDouble)
358 SCORE_ASSERT(m_bs >= offset + samples);
360 const auto max_i = std::max(2, this->fx->fx->numInputs);
361 const auto max_o = std::max(2, this->fx->fx->numOutputs);
362 const auto max_io = std::max(max_i, max_o);
364 const auto max_samples = std::max(samples, (int64_t)m_bs);
369 auto& ip = prepareInput(offset, samples);
370 SCORE_ASSERT(ip.size() >= 2);
372 auto& op = prepareOutput(offset, samples);
373 SCORE_ASSERT(op.size() >= 2);
376 float_v[0].resize(max_samples);
377 float_v[1].resize(max_samples);
379 std::copy_n(ip[0].data() + offset, samples, float_v[0].data());
380 std::copy_n(ip[1].data() + offset, samples, float_v[1].data());
382 float** io = (
float**)alloca(
sizeof(
float*) * max_io);
383 io[0] = float_v[0].data();
384 io[1] = float_v[1].data();
390 float* dummy = (
float*)alloca(
sizeof(
float) * max_samples);
391 std::fill_n(dummy, max_samples, 0.f);
393 for(
int i = 2; i < max_io; i++)
397 fx->fx->processReplacing(fx->fx, io, io, samples);
399 std::copy_n(float_v[0].data(), samples, op[0].data() + offset);
400 std::copy_n(float_v[1].data(), samples, op[1].data() + offset);
407 void processDouble(int64_t offset, int64_t samples)
409 if constexpr(UseDouble)
414 SCORE_ASSERT(m_bs >= offset + samples);
416 const auto max_i = std::max(2, this->fx->fx->numInputs);
417 const auto max_o = std::max(2, this->fx->fx->numOutputs);
418 const auto max_io = std::max(max_i, max_o);
421 auto& ip = prepareInput(offset, samples);
422 SCORE_ASSERT(ip.size() >= 2);
424 auto& op = prepareOutput(offset, samples);
425 SCORE_ASSERT(op.size() >= 2);
427 double** input = (
double**)alloca(
sizeof(
double*) * max_i);
428 input[0] = ip[0].data() + offset;
429 input[1] = ip[1].data() + offset;
431 double** output = (
double**)alloca(
sizeof(
double*) * max_o);
432 output[0] = op[0].data() + offset;
433 output[1] = op[1].data() + offset;
439 double* dummy = (
double*)alloca(
sizeof(
double) * m_bs);
440 std::fill_n(dummy, m_bs, 0.);
441 for(
int i = 2; i < this->fx->fx->numInputs; i++)
443 for(
int i = 2; i < this->fx->fx->numOutputs; i++)
447 fx->fx->processDoubleReplacing(fx->fx, input, output, samples);
454 std::conditional_t<!UseDouble, std::array<ossia::float_vector, 2>,
dummy_t> float_v;
457 template <
bool b1,
bool b2,
typename... Args>
458 auto make_vst_fx(Args&... args)
460 return ossia::make_node<vst_node<b1, b2>>(args...);
Definition: score-plugin-vst/Vst/Node.hpp:15
Definition: score-plugin-vst/Vst/Node.hpp:123
Definition: score-plugin-vst/Vst/Node.hpp:452
Definition: score-plugin-vst/Vst/Node.hpp:28