19 std::shared_ptr<AEffectWrapper> fx{};
25 this->set_not_fp_safe();
34 ossia::value_port* port{};
38 int32_t opcode, int32_t index = 0, intptr_t value = 0,
void* ptr =
nullptr,
41 fx->dispatch(opcode, index, value, ptr, opt);
45 ossia::small_vector<vst_control, 16> controls;
49 for(vst_control& p : controls)
51 const auto& vec = p.port->get_data();
54 auto t = ossia::convert<float>(last(vec));
56 p.value = ossia::clamp<float>(t, 0.f, 1.f);
57 fx->fx->setParameter(fx->fx, p.idx, p.value);
62 auto& prepareInput(int64_t offset, int64_t samples)
64 const auto bs = offset + samples;
65 auto& p = *m_inlets[0]->template target<ossia::audio_port>();
70 p.channel(0).resize(bs);
71 p.channel(1).resize(bs);
76 p.channel(0).resize(bs);
77 p.channel(1).assign(p.channel(0).begin(), p.channel(0).end());
90 auto& prepareOutput(int64_t offset, int64_t samples)
92 const auto bs = offset + samples;
93 auto& p = *m_outlets[0]->template target<ossia::audio_port>();
96 chan.resize(bs, boost::container::default_init);
100 void setupTimeInfo(
const ossia::token_request& tk, ossia::exec_state_facade st)
102 static const constexpr double ppq_reference = 960.;
104 auto& time_info = fx->info;
105 time_info.samplePos = this->m_processed_frames;
106 time_info.sampleRate = st.sampleRate();
107 time_info.nanoSeconds = st.currentDate() - st.startDate();
108 time_info.ppqPos = tk.musical_start_position;
109 time_info.tempo = tk.tempo;
110 time_info.barStartPos = tk.musical_start_last_bar;
111 time_info.cycleStartPos = 0.;
112 time_info.cycleEndPos = 0.;
113 time_info.timeSigNumerator = tk.signature.upper;
114 time_info.timeSigDenominator = tk.signature.lower;
115 time_info.smpteOffset = 0;
116 time_info.smpteFrameRate = 0;
117 time_info.samplesToNextClock = 0;
118 time_info.flags = kVstTransportPlaying | kVstNanosValid | kVstPpqPosValid
119 | kVstTempoValid | kVstBarsValid | kVstTimeSigValid
128 static constexpr bool synth = IsSynth;
129 VstSpeakerArrangement i_arr{};
130 VstSpeakerArrangement o_arr{};
133 vst_node(std::shared_ptr<AEffectWrapper> dat,
int sampleRate,
int bs)
138 m_inlets.push_back(
new ossia::audio_inlet);
139 if constexpr(IsSynth)
140 m_inlets.push_back(
new ossia::midi_inlet);
143 m_outlets.push_back(
new ossia::audio_outlet);
146 memset(&i_arr, 0,
sizeof(i_arr));
147 memset(&o_arr, 0,
sizeof(o_arr));
148 i_arr.type = kSpeakerArrStereo;
149 i_arr.numChannels = 2;
150 i_arr.speakers[0].type = kSpeakerL;
151 i_arr.speakers[1].type = kSpeakerR;
153 o_arr.type = kSpeakerArrStereo;
154 o_arr.numChannels = 2;
155 o_arr.speakers[0].type = kSpeakerL;
156 o_arr.speakers[1].type = kSpeakerR;
157 dispatch(effSetSpeakerArrangement, 0, (intptr_t)&i_arr, (
void*)&o_arr, 0);
160 dispatch(effSetSampleRate, 0, sampleRate,
nullptr, sampleRate);
161 dispatch(effSetBlockSize, 0, bs,
nullptr, bs);
163 effSetProcessPrecision, 0,
164 UseDouble ? kVstProcessPrecision64 : kVstProcessPrecision32);
165 dispatch(effMainsChanged, 0, 1);
166 dispatch(effStartProcess);
168 fx->fx->resvd2 =
reinterpret_cast<intptr_t
>(
this);
174 dispatch(effStopProcess);
175 dispatch(effMainsChanged, 0, 0);
178 std::string label()
const noexcept override {
return ""; }
180 void all_notes_off()
noexcept override
182 if constexpr(IsSynth)
186 constexpr auto sz =
sizeof(VstEvents) +
sizeof(
void*) * 32 * 2;
187 VstEvents* events = (VstEvents*)alloca(sz);
188 std::memset(events, 0, sz);
190 events->numEvents = 32;
192 VstMidiEvent ev[64] = {};
193 memset(&ev, 0,
sizeof(ev));
196 for(
int i = 0; i < 16; i++)
199 e.type = kVstMidiType;
200 e.flags = kVstMidiEventIsRealtime;
201 e.byteSize =
sizeof(VstMidiEvent);
203 e.midiData[0] = (char)(uint8_t)176 + i;
204 e.midiData[1] = (char)(uint8_t)123;
208 events->events[i] =
reinterpret_cast<VstEvent*
>(&e);
212 for(
int i = 0; i < 16; i++)
214 auto& e = ev[16 + i];
215 e.type = kVstMidiType;
216 e.flags = kVstMidiEventIsRealtime;
217 e.byteSize =
sizeof(VstMidiEvent);
219 e.midiData[0] = (char)(uint8_t)176 + i;
220 e.midiData[1] = (char)(uint8_t)121;
224 events->events[16 + i] =
reinterpret_cast<VstEvent*
>(&e);
227 dispatch(effProcessEvents, 0, 0, events, 0.f);
229 if constexpr(!UseDouble)
231 float* dummy = (
float*)alloca(
sizeof(
float) * m_bs);
232 std::fill_n(dummy, m_bs, 0.f);
235 = (
float**)alloca(
sizeof(
float*) * std::max(2, this->fx->fx->numInputs));
236 for(
int i = 0; i < this->fx->fx->numInputs; i++)
240 = (
float**)alloca(
sizeof(
float*) * std::max(2, this->fx->fx->numOutputs));
241 for(
int i = 0; i < this->fx->fx->numOutputs; i++)
244 fx->fx->processReplacing(fx->fx, input, output, m_bs);
248 double* dummy = (
double*)alloca(
sizeof(
double) * m_bs);
249 std::fill_n(dummy, m_bs, 0.);
252 = (
double**)alloca(
sizeof(
double*) * std::max(2, this->fx->fx->numInputs));
253 for(
int i = 0; i < this->fx->fx->numInputs; i++)
257 = (
double**)alloca(
sizeof(
double*) * std::max(2, this->fx->fx->numOutputs));
258 for(
int i = 0; i < this->fx->fx->numOutputs; i++)
261 fx->fx->processDoubleReplacing(fx->fx, input, output, m_bs);
269 template <
typename Fun>
270 void dispatchMidi(int64_t offset, Fun&& f)
273 auto& ip =
static_cast<ossia::midi_inlet*
>(m_inlets[1])->data.messages;
274 const auto n_mess = ip.size();
282 const auto sz =
sizeof(VstEvents) +
sizeof(
void*) * n_mess * 2;
283 VstEvents* events = (VstEvents*)alloca(sz);
284 std::memset(events, 0, sz);
285 events->numEvents = n_mess;
287 ossia::small_vector<VstMidiEvent, 16> vec;
290 for(libremidi::ump& mess : ip)
292 if(
auto type = mess.get_type();
293 (type == libremidi::midi2::message_type::SYSEX7)
294 || (type == libremidi::midi2::message_type::SYSEX8_MDS))
297 VstMidiEvent& e = vec[i];
298 std::memset(&e, 0,
sizeof(VstMidiEvent));
299 e.type = kVstMidiType;
300 e.byteSize =
sizeof(VstMidiEvent);
301 e.deltaFrames = mess.timestamp - offset;
302 e.flags = kVstMidiEventIsRealtime;
304 if(
auto n = cmidi2_convert_single_ump_to_midi1((uint8_t*)e.midiData, 4, mess.data);
307 events->events[i] =
reinterpret_cast<VstEvent*
>(&e);
315 dispatch(effProcessEvents, 0, 0, events, 0.f);
319 void run(
const ossia::token_request& tk, ossia::exec_state_facade st)
noexcept override
321 if(!muted() && tk.date > tk.prev_date)
323 const auto timings = st.timings(tk);
325 this->setupTimeInfo(tk, st);
327 if constexpr(UseDouble)
329 if constexpr(IsSynth)
331 dispatchMidi(timings.start_sample, [
this, timings] {
332 processDouble(timings.start_sample, timings.length);
337 processDouble(timings.start_sample, timings.length);
342 if constexpr(IsSynth)
344 dispatchMidi(timings.start_sample, [
this, timings] {
345 processFloat(timings.start_sample, timings.length);
350 processFloat(timings.start_sample, timings.length);
355 if(this->fx->fx->numOutputs == 1)
357 auto& op = m_outlets[0]->template target<ossia::audio_port>()->get();
358 op[1].assign(op[0].begin(), op[0].end());
363 void processFloat(int64_t offset, int64_t samples)
365 if constexpr(!UseDouble)
370 SCORE_ASSERT(m_bs >= offset + samples);
372 const auto max_i = std::max(2, this->fx->fx->numInputs);
373 const auto max_o = std::max(2, this->fx->fx->numOutputs);
374 const auto max_io = std::max(max_i, max_o);
376 const auto max_samples = std::max(samples, (int64_t)m_bs);
381 auto& ip = prepareInput(offset, samples);
382 SCORE_ASSERT(ip.size() >= 2);
384 auto& op = prepareOutput(offset, samples);
385 SCORE_ASSERT(op.size() >= 2);
388 float_v[0].resize(max_samples);
389 float_v[1].resize(max_samples);
391 std::copy_n(ip[0].data() + offset, samples, float_v[0].data());
392 std::copy_n(ip[1].data() + offset, samples, float_v[1].data());
394 float** io = (
float**)alloca(
sizeof(
float*) * max_io);
395 io[0] = float_v[0].data();
396 io[1] = float_v[1].data();
402 float* dummy = (
float*)alloca(
sizeof(
float) * max_samples);
403 std::fill_n(dummy, max_samples, 0.f);
405 for(
int i = 2; i < max_io; i++)
409 fx->fx->processReplacing(fx->fx, io, io, samples);
411 std::copy_n(float_v[0].data(), samples, op[0].data() + offset);
412 std::copy_n(float_v[1].data(), samples, op[1].data() + offset);
419 void processDouble(int64_t offset, int64_t samples)
421 if constexpr(UseDouble)
426 SCORE_ASSERT(m_bs >= offset + samples);
428 const auto max_i = std::max(2, this->fx->fx->numInputs);
429 const auto max_o = std::max(2, this->fx->fx->numOutputs);
430 const auto max_io = std::max(max_i, max_o);
433 auto& ip = prepareInput(offset, samples);
434 SCORE_ASSERT(ip.size() >= 2);
436 auto& op = prepareOutput(offset, samples);
437 SCORE_ASSERT(op.size() >= 2);
439 double** input = (
double**)alloca(
sizeof(
double*) * max_i);
440 input[0] = ip[0].data() + offset;
441 input[1] = ip[1].data() + offset;
443 double** output = (
double**)alloca(
sizeof(
double*) * max_o);
444 output[0] = op[0].data() + offset;
445 output[1] = op[1].data() + offset;
451 double* dummy = (
double*)alloca(
sizeof(
double) * m_bs);
452 std::fill_n(dummy, m_bs, 0.);
453 for(
int i = 2; i < this->fx->fx->numInputs; i++)
455 for(
int i = 2; i < this->fx->fx->numOutputs; i++)
459 fx->fx->processDoubleReplacing(fx->fx, input, output, samples);
466 std::conditional_t<!UseDouble, std::array<ossia::float_vector, 2>,
dummy_t> float_v;