2 * Copyright (C) 2007-2018 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2009 David Robillard <d@drobilla.net>
4 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2015-2016 Ben Loftis <ben@harrisonconsoles.com>
7 * Copyright (C) 2015-2016 Nick Mainsbridge <mainsbridge@gmail.com>
8 * Copyright (C) 2015-2018 John Emmas <john@creativepost.co.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "ardour/audioengine.h"
31 #include "ardour/debug.h"
32 #include "ardour/session.h"
33 #include "ardour/tempo.h"
34 #include "ardour/plugin_insert.h"
35 #include "ardour/windows_vst_plugin.h"
36 #include "ardour/vestige/vestige.h"
37 #include "ardour/vst_types.h"
38 #ifdef WINDOWS_VST_SUPPORT
44 using namespace ARDOUR;
46 #define SHOW_CALLBACK(MSG) DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose (MSG " val = %1 idx = %2\n", index, value))
48 int Session::vst_current_loading_id = 0;
49 const char* Session::vst_can_do_strings[] = {
51 X_("sendVstTimeInfo"),
53 X_("sendVstMidiEvent"),
54 X_("receiveVstEvents"),
55 X_("receiveVstMidiEvent"),
58 X_("shellCategorycurID"),
61 const int Session::vst_can_do_string_count = sizeof (vst_can_do_strings) / sizeof (char*);
63 intptr_t Session::vst_callback (
74 static VstTimeInfo _timeinfo; // only uses as fallback
75 VstTimeInfo* timeinfo;
78 if (effect && effect->ptr1) {
79 plug = (VSTPlugin *) (effect->ptr1);
80 session = &plug->session();
81 timeinfo = plug->timeinfo ();
82 DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("am callback 0x%1%2, opcode = %3%4, plugin = \"%5\"\n",
83 std::hex, (void*) DEBUG_THREAD_SELF,
84 std::dec, opcode, plug->name()));
85 if (plug->_for_impulse_analysis) {
91 timeinfo = &_timeinfo;
92 DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("am callback 0x%1%2, opcode = %3%4\n",
93 std::hex, (void*) DEBUG_THREAD_SELF,
99 case audioMasterAutomate:
100 SHOW_CALLBACK ("audioMasterAutomate");
101 // index, value, returns 0
103 plug->parameter_changed_externally (index, opt);
107 case audioMasterVersion:
108 SHOW_CALLBACK ("audioMasterVersion");
109 // vst version, currently 2 (0 for older)
112 case audioMasterCurrentId:
113 SHOW_CALLBACK ("audioMasterCurrentId");
114 // returns the unique id of a plug that's currently loading
115 return vst_current_loading_id;
117 case audioMasterIdle:
118 SHOW_CALLBACK ("audioMasterIdle");
119 #ifdef WINDOWS_VST_SUPPORT
120 fst_audio_master_idle();
123 effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
127 case audioMasterPinConnected:
128 SHOW_CALLBACK ("audioMasterPinConnected");
129 // inquire if an input or output is beeing connected;
130 // index enumerates input or output counting from zero:
131 // value is 0 for input and != 0 otherwise. note: the
132 // return value is 0 for <true> such that older versions
133 // will always return true.
136 // but ardour always connects all buffers, so we're good
141 if (plug->plugin_insert ()) {
143 const ChanMapping& map (plug->plugin_insert ()->input_map (plug->plugin_number ()));
144 map.get (DataType::AUDIO, index, &valid);
145 return valid ? 0 : 1;
147 if (index < plug->plugin()->numInputs) {
152 #if 0 // investigate, the outputs *are* connected to scratch buffers
153 if (plug->plugin_insert ()) {
155 const ChanMapping& map (plug->plugin_insert ()->output_map (plug->plugin_number ()));
156 map.get (DataType::AUDIO, index, &valid);
157 return valid ? 0 : 1;
160 if (index < plug->plugin()->numOutputs) {
169 case audioMasterWantMidi:
170 SHOW_CALLBACK ("audioMasterWantMidi");
171 // <value> is a filter which is currently ignored
172 if (plug && plug->get_info() != NULL) {
173 plug->get_info()->n_inputs.set_midi (1);
177 case audioMasterGetTime:
178 SHOW_CALLBACK ("audioMasterGetTime");
179 newflags = kVstNanosValid | kVstAutomationWriting | kVstAutomationReading;
181 timeinfo->nanoSeconds = g_get_monotonic_time () * 1000;
183 if (plug && session) {
184 samplepos_t now = plug->transport_sample();
186 timeinfo->samplePos = now;
187 timeinfo->sampleRate = session->sample_rate();
189 if (value & (kVstTempoValid)) {
190 const Tempo& t (session->tempo_map().tempo_at_sample (now));
191 timeinfo->tempo = t.quarter_notes_per_minute ();
192 newflags |= (kVstTempoValid);
194 if (value & (kVstTimeSigValid)) {
195 const MeterSection& ms (session->tempo_map().meter_section_at_sample (now));
196 timeinfo->timeSigNumerator = ms.divisions_per_bar ();
197 timeinfo->timeSigDenominator = ms.note_divisor ();
198 newflags |= (kVstTimeSigValid);
200 if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
201 Timecode::BBT_Time bbt;
204 bbt = session->tempo_map().bbt_at_sample_rt (now);
207 /* exact quarter note */
208 double ppqBar = session->tempo_map().quarter_note_at_bbt_rt (bbt);
209 /* quarter note at sample position (not rounded to note subdivision) */
210 double ppqPos = session->tempo_map().quarter_note_at_sample_rt (now);
211 if (value & (kVstPpqPosValid)) {
212 timeinfo->ppqPos = ppqPos;
213 newflags |= kVstPpqPosValid;
216 if (value & (kVstBarsValid)) {
217 timeinfo->barStartPos = ppqBar;
218 newflags |= kVstBarsValid;
226 if (value & (kVstSmpteValid)) {
229 session->timecode_time (now, t);
231 timeinfo->smpteOffset = (t.hours * t.rate * 60.0 * 60.0) +
232 (t.minutes * t.rate * 60.0) +
233 (t.seconds * t.rate) +
237 timeinfo->smpteOffset *= 80.0; /* VST spec is 1/80th samples */
239 if (session->timecode_drop_frames()) {
240 if (session->timecode_frames_per_second() == 30.0) {
241 timeinfo->smpteFrameRate = 5;
243 timeinfo->smpteFrameRate = 4; /* 29.97 assumed, thanks VST */
246 if (session->timecode_frames_per_second() == 24.0) {
247 timeinfo->smpteFrameRate = 0;
248 } else if (session->timecode_frames_per_second() == 24.975) {
249 timeinfo->smpteFrameRate = 2;
250 } else if (session->timecode_frames_per_second() == 25.0) {
251 timeinfo->smpteFrameRate = 1;
253 timeinfo->smpteFrameRate = 3; /* 30 fps */
256 newflags |= (kVstSmpteValid);
259 if (session->actively_recording ()) {
260 newflags |= kVstTransportRecording;
263 if (plug->transport_speed () != 0.0f) {
264 newflags |= kVstTransportPlaying;
267 if (session->get_play_loop ()) {
268 newflags |= kVstTransportCycleActive;
269 Location * looploc = session->locations ()->auto_loop_location ();
271 timeinfo->cycleStartPos = session->tempo_map ().quarter_note_at_sample_rt (looploc->start ());
272 timeinfo->cycleEndPos = session->tempo_map ().quarter_note_at_sample_rt (looploc->end ());
274 newflags |= kVstCyclePosValid;
279 timeinfo->samplePos = 0;
280 timeinfo->sampleRate = AudioEngine::instance()->sample_rate();
283 if ((timeinfo->flags & (kVstTransportPlaying | kVstTransportRecording | kVstTransportCycleActive))
285 (newflags & (kVstTransportPlaying | kVstTransportRecording | kVstTransportCycleActive)))
287 newflags |= kVstTransportChanged;
290 timeinfo->flags = newflags;
291 return (intptr_t) timeinfo;
293 case audioMasterProcessEvents:
294 SHOW_CALLBACK ("audioMasterProcessEvents");
295 // VstEvents* in <ptr>
296 if (plug && plug->midi_buffer()) {
297 VstEvents* v = (VstEvents*)ptr;
298 for (int n = 0 ; n < v->numEvents; ++n) {
299 VstMidiEvent *vme = (VstMidiEvent*) (v->events[n]->dump);
300 if (vme->type == kVstMidiType) {
301 plug->midi_buffer()->push_back(vme->deltaSamples, 3, (uint8_t*)vme->midiData);
307 case audioMasterSetTime:
308 SHOW_CALLBACK ("audioMasterSetTime");
309 // VstTimenfo* in <ptr>, filter in <value>, not supported
312 case audioMasterTempoAt:
313 SHOW_CALLBACK ("audioMasterTempoAt");
314 // returns tempo (in bpm * 10000) at sample sample location passed in <value>
316 const Tempo& t (session->tempo_map().tempo_at_sample (value));
317 return t.quarter_notes_per_minute() * 1000;
323 case audioMasterGetNumAutomatableParameters:
324 SHOW_CALLBACK ("audioMasterGetNumAutomatableParameters");
327 case audioMasterGetParameterQuantization:
328 SHOW_CALLBACK ("audioMasterGetParameterQuantization");
329 // returns the integer value for +1.0 representation,
330 // or 1 if full single float precision is maintained
331 // in automation. parameter index in <value> (-1: all, any)
334 case audioMasterIOChanged:
335 SHOW_CALLBACK ("audioMasterIOChanged");
336 // numInputs and/or numOutputs has changed
339 case audioMasterNeedIdle:
340 SHOW_CALLBACK ("audioMasterNeedIdle");
341 // plug needs idle calls (outside its editor window)
343 plug->state()->wantIdle = 1;
347 case audioMasterSizeWindow:
348 SHOW_CALLBACK ("audioMasterSizeWindow");
349 if (plug && plug->state()) {
350 if (plug->state()->width != index || plug->state()->height != value) {
351 plug->state()->width = index;
352 plug->state()->height = value;
354 printf ("audioMasterSizeWindow %d %d\n", plug->state()->width, plug->state()->height);
356 plug->VSTSizeWindow (); /* EMIT SIGNAL */
361 case audioMasterGetSampleRate:
362 SHOW_CALLBACK ("audioMasterGetSampleRate");
364 return session->sample_rate();
368 case audioMasterGetBlockSize:
369 SHOW_CALLBACK ("audioMasterGetBlockSize");
371 return session->get_block_size();
375 case audioMasterGetInputLatency:
376 SHOW_CALLBACK ("audioMasterGetInputLatency");
379 case audioMasterGetOutputLatency:
380 SHOW_CALLBACK ("audioMasterGetOutputLatency");
383 case audioMasterGetPreviousPlug:
384 SHOW_CALLBACK ("audioMasterGetPreviousPlug");
385 // input pin in <value> (-1: first to come), returns cEffect*
388 case audioMasterGetNextPlug:
389 SHOW_CALLBACK ("audioMasterGetNextPlug");
390 // output pin in <value> (-1: first to come), returns cEffect*
393 case audioMasterWillReplaceOrAccumulate:
394 SHOW_CALLBACK ("audioMasterWillReplaceOrAccumulate");
395 // returns: 0: not supported, 1: replace, 2: accumulate
398 case audioMasterGetCurrentProcessLevel:
399 SHOW_CALLBACK ("audioMasterGetCurrentProcessLevel");
400 // returns: 0: not supported,
401 // 1: currently in user thread (gui)
402 // 2: currently in audio thread (where process is called)
403 // 3: currently in 'sequencer' thread (midi, timer etc)
404 // 4: currently offline processing and thus in user thread
405 // other: not defined, but probably pre-empting user thread.
408 case audioMasterGetAutomationState:
409 SHOW_CALLBACK ("audioMasterGetAutomationState");
410 // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
414 case audioMasterOfflineStart:
415 SHOW_CALLBACK ("audioMasterOfflineStart");
418 case audioMasterOfflineRead:
419 SHOW_CALLBACK ("audioMasterOfflineRead");
420 // ptr points to offline structure, see below. return 0: error, 1 ok
423 case audioMasterOfflineWrite:
424 SHOW_CALLBACK ("audioMasterOfflineWrite");
428 case audioMasterOfflineGetCurrentPass:
429 SHOW_CALLBACK ("audioMasterOfflineGetCurrentPass");
432 case audioMasterOfflineGetCurrentMetaPass:
433 SHOW_CALLBACK ("audioMasterOfflineGetCurrentMetaPass");
436 case audioMasterSetOutputSampleRate:
437 SHOW_CALLBACK ("audioMasterSetOutputSampleRate");
438 // for variable i/o, sample rate in <opt>
441 case audioMasterGetSpeakerArrangement:
442 SHOW_CALLBACK ("audioMasterGetSpeakerArrangement");
443 // (long)input in <value>, output in <ptr>
446 case audioMasterGetVendorString:
447 SHOW_CALLBACK ("audioMasterGetVendorString");
448 // fills <ptr> with a string identifying the vendor (max 64 char)
449 strcpy ((char*) ptr, "Linux Audio Systems");
452 case audioMasterGetProductString:
453 SHOW_CALLBACK ("audioMasterGetProductString");
454 // fills <ptr> with a string with product name (max 64 char)
455 strcpy ((char*) ptr, PROGRAM_NAME);
458 case audioMasterGetVendorVersion:
459 SHOW_CALLBACK ("audioMasterGetVendorVersion");
460 // returns vendor-specific version
463 case audioMasterVendorSpecific:
464 SHOW_CALLBACK ("audioMasterVendorSpecific");
465 // no definition, vendor specific handling
468 case audioMasterSetIcon:
469 SHOW_CALLBACK ("audioMasterSetIcon");
470 // void* in <ptr>, format not defined yet
473 case audioMasterCanDo:
474 SHOW_CALLBACK ("audioMasterCanDo");
475 // string in ptr, (const char*)ptr
476 for (int i = 0; i < vst_can_do_string_count; i++) {
477 if (! strcmp(vst_can_do_strings[i], (const char*)ptr)) {
483 case audioMasterGetLanguage:
484 SHOW_CALLBACK ("audioMasterGetLanguage");
488 case audioMasterOpenWindow:
489 SHOW_CALLBACK ("audioMasterOpenWindow");
490 // returns platform specific ptr
493 case audioMasterCloseWindow:
494 SHOW_CALLBACK ("audioMasterCloseWindow");
495 // close window, platform specific handle in <ptr>
498 case audioMasterGetDirectory:
499 SHOW_CALLBACK ("audioMasterGetDirectory");
500 // get plug directory, FSSpec on MAC, else char*
503 case audioMasterUpdateDisplay:
504 SHOW_CALLBACK ("audioMasterUpdateDisplay");
505 // something has changed, update 'multi-fx' display
506 /* TODO: consider emitting ParameterChangedExternally() for each ctrl input */
508 session->set_dirty ();
512 case audioMasterBeginEdit:
513 SHOW_CALLBACK ("audioMasterBeginEdit");
514 // begin of automation session (when mouse down), parameter index in <index>
515 if (plug && plug->plugin_insert ()) {
516 boost::shared_ptr<AutomationControl> ac = plug->plugin_insert ()->automation_control (Evoral::Parameter (PluginAutomation, 0, index));
518 ac->start_touch (ac->session().transport_sample());
523 case audioMasterEndEdit:
524 SHOW_CALLBACK ("audioMasterEndEdit");
525 // end of automation session (when mouse up), parameter index in <index>
526 if (plug && plug->plugin_insert ()) {
527 boost::shared_ptr<AutomationControl> ac = plug->plugin_insert ()->automation_control (Evoral::Parameter (PluginAutomation, 0, index));
529 ac->stop_touch (ac->session().transport_sample());
534 case audioMasterOpenFileSelector:
535 SHOW_CALLBACK ("audioMasterOpenFileSelector");
536 // open a fileselector window with VstFileSelect* in <ptr>
540 DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("VST master dispatcher: undefed: %1\n", opcode));