NO-OP: whitespace/comments
[ardour.git] / libs / ardour / session_vst.cc
1 /*
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>
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifndef COMPILER_MSVC
26 #include <stdbool.h>
27 #endif
28 #include <cstdio>
29
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
39 #include <fst.h>
40 #endif
41
42 #include "pbd/i18n.h"
43
44 using namespace ARDOUR;
45
46 #define SHOW_CALLBACK(MSG) DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose (MSG " val = %1 idx = %2\n", index, value))
47
48 int Session::vst_current_loading_id = 0;
49 const char* Session::vst_can_do_strings[] = {
50         X_("supplyIdle"),
51         X_("sendVstTimeInfo"),
52         X_("sendVstEvents"),
53         X_("sendVstMidiEvent"),
54         X_("receiveVstEvents"),
55         X_("receiveVstMidiEvent"),
56         X_("supportShell"),
57         X_("shellCategory"),
58         X_("shellCategorycurID"),
59         X_("sizeWindow")
60 };
61 const int Session::vst_can_do_string_count = sizeof (vst_can_do_strings) / sizeof (char*);
62
63 intptr_t Session::vst_callback (
64         AEffect* effect,
65         int32_t opcode,
66         int32_t index,
67         intptr_t value,
68         void* ptr,
69         float opt
70         )
71 {
72         VSTPlugin* plug;
73         Session* session;
74         static VstTimeInfo _timeinfo; // only uses as fallback
75         VstTimeInfo* timeinfo;
76         int32_t newflags = 0;
77
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) {
86                         plug = 0;
87                 }
88         } else {
89                 plug = 0;
90                 session = 0;
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,
94                                         std::dec, opcode));
95         }
96
97         switch(opcode){
98
99         case audioMasterAutomate:
100                 SHOW_CALLBACK ("audioMasterAutomate");
101                 // index, value, returns 0
102                 if (plug) {
103                         plug->parameter_changed_externally (index, opt);
104                 }
105                 return 0;
106
107         case audioMasterVersion:
108                 SHOW_CALLBACK ("audioMasterVersion");
109                 // vst version, currently 2 (0 for older)
110                 return 2400;
111
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;
116
117         case audioMasterIdle:
118                 SHOW_CALLBACK ("audioMasterIdle");
119 #ifdef WINDOWS_VST_SUPPORT
120                 fst_audio_master_idle();
121 #endif
122                 if (effect) {
123                         effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
124                 }
125                 return 0;
126
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.
134                 if (!plug) {
135                         // we don't know.
136                         // but ardour always connects all buffers, so we're good
137                         return 0;
138                 }
139                 switch (value) {
140                         case 0:
141                                 if (plug->plugin_insert ()) {
142                                         bool valid;
143                                         const ChanMapping& map (plug->plugin_insert ()->input_map (plug->plugin_number ()));
144                                         map.get (DataType::AUDIO, index, &valid);
145                                         return valid ? 0 : 1;
146                                 }
147                                 if (index < plug->plugin()->numInputs) {
148                                         return 0;
149                                 }
150                                 break;
151                         case 1:
152 #if 0 // investigate, the outputs *are* connected to scratch buffers
153                                 if (plug->plugin_insert ()) {
154                                         bool valid;
155                                         const ChanMapping& map (plug->plugin_insert ()->output_map (plug->plugin_number ()));
156                                         map.get (DataType::AUDIO, index, &valid);
157                                         return valid ? 0 : 1;
158                                 }
159 #endif
160                                 if (index < plug->plugin()->numOutputs) {
161                                         return 0;
162                                 }
163                                 break;
164                         default:
165                                 break;
166                 }
167                 return 1;
168
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);
174                 }
175                 return 0;
176
177         case audioMasterGetTime:
178                 SHOW_CALLBACK ("audioMasterGetTime");
179                 newflags = kVstNanosValid | kVstAutomationWriting | kVstAutomationReading;
180
181                 timeinfo->nanoSeconds = g_get_monotonic_time () * 1000;
182
183                 if (plug && session) {
184                         samplepos_t now = plug->transport_sample();
185
186                         timeinfo->samplePos = now;
187                         timeinfo->sampleRate = session->sample_rate();
188
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);
193                         }
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);
199                         }
200                         if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
201                                 Timecode::BBT_Time bbt;
202
203                                 try {
204                                         bbt = session->tempo_map().bbt_at_sample_rt (now);
205                                         bbt.beats = 1;
206                                         bbt.ticks = 0;
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;
214                                         }
215
216                                         if (value & (kVstBarsValid)) {
217                                                 timeinfo->barStartPos = ppqBar;
218                                                 newflags |= kVstBarsValid;
219                                         }
220
221                                 } catch (...) {
222                                         /* relax */
223                                 }
224                         }
225
226                         if (value & (kVstSmpteValid)) {
227                                 Timecode::Time t;
228
229                                 session->timecode_time (now, t);
230
231                                 timeinfo->smpteOffset = (t.hours * t.rate * 60.0 * 60.0) +
232                                         (t.minutes * t.rate * 60.0) +
233                                         (t.seconds * t.rate) +
234                                         (t.frames) +
235                                         (t.subframes);
236
237                                 timeinfo->smpteOffset *= 80.0; /* VST spec is 1/80th samples */
238
239                                 if (session->timecode_drop_frames()) {
240                                         if (session->timecode_frames_per_second() == 30.0) {
241                                                 timeinfo->smpteFrameRate = 5;
242                                         } else {
243                                                 timeinfo->smpteFrameRate = 4; /* 29.97 assumed, thanks VST */
244                                         }
245                                 } else {
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;
252                                         } else {
253                                                 timeinfo->smpteFrameRate = 3; /* 30 fps */
254                                         }
255                                 }
256                                 newflags |= (kVstSmpteValid);
257                         }
258
259                         if (session->actively_recording ()) {
260                                 newflags |= kVstTransportRecording;
261                         }
262
263                         if (plug->transport_speed () != 0.0f) {
264                                 newflags |= kVstTransportPlaying;
265                         }
266
267                         if (session->get_play_loop ()) {
268                                 newflags |= kVstTransportCycleActive;
269                                 Location * looploc = session->locations ()->auto_loop_location ();
270                                 if (looploc) try {
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 ());
273
274                                         newflags |= kVstCyclePosValid;
275                                 } catch (...) { }
276                         }
277
278                 } else {
279                         timeinfo->samplePos = 0;
280                         timeinfo->sampleRate = AudioEngine::instance()->sample_rate();
281                 }
282
283                 if ((timeinfo->flags & (kVstTransportPlaying | kVstTransportRecording | kVstTransportCycleActive))
284                     !=
285                     (newflags        & (kVstTransportPlaying | kVstTransportRecording | kVstTransportCycleActive)))
286                 {
287                         newflags |= kVstTransportChanged;
288                 }
289
290                 timeinfo->flags = newflags;
291                 return (intptr_t) timeinfo;
292
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);
302                                 }
303                         }
304                 }
305                 return 0;
306
307         case audioMasterSetTime:
308                 SHOW_CALLBACK ("audioMasterSetTime");
309                 // VstTimenfo* in <ptr>, filter in <value>, not supported
310                 return 0;
311
312         case audioMasterTempoAt:
313                 SHOW_CALLBACK ("audioMasterTempoAt");
314                 // returns tempo (in bpm * 10000) at sample sample location passed in <value>
315                 if (session) {
316                         const Tempo& t (session->tempo_map().tempo_at_sample (value));
317                         return t.quarter_notes_per_minute() * 1000;
318                 } else {
319                         return 0;
320                 }
321                 break;
322
323         case audioMasterGetNumAutomatableParameters:
324                 SHOW_CALLBACK ("audioMasterGetNumAutomatableParameters");
325                 return 0;
326
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)
332                 return 0;
333
334         case audioMasterIOChanged:
335                 SHOW_CALLBACK ("audioMasterIOChanged");
336                 // numInputs and/or numOutputs has changed
337                 return 0;
338
339         case audioMasterNeedIdle:
340                 SHOW_CALLBACK ("audioMasterNeedIdle");
341                 // plug needs idle calls (outside its editor window)
342                 if (plug) {
343                         plug->state()->wantIdle = 1;
344                 }
345                 return 0;
346
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;
353 #ifndef NDEBUG
354                                 printf ("audioMasterSizeWindow %d %d\n", plug->state()->width, plug->state()->height);
355 #endif
356                                 plug->VSTSizeWindow (); /* EMIT SIGNAL */
357                         }
358                 }
359                 return 1;
360
361         case audioMasterGetSampleRate:
362                 SHOW_CALLBACK ("audioMasterGetSampleRate");
363                 if (session) {
364                         return session->sample_rate();
365                 }
366                 return 0;
367
368         case audioMasterGetBlockSize:
369                 SHOW_CALLBACK ("audioMasterGetBlockSize");
370                 if (session) {
371                         return session->get_block_size();
372                 }
373                 return 0;
374
375         case audioMasterGetInputLatency:
376                 SHOW_CALLBACK ("audioMasterGetInputLatency");
377                 return 0;
378
379         case audioMasterGetOutputLatency:
380                 SHOW_CALLBACK ("audioMasterGetOutputLatency");
381                 return 0;
382
383         case audioMasterGetPreviousPlug:
384                 SHOW_CALLBACK ("audioMasterGetPreviousPlug");
385                 // input pin in <value> (-1: first to come), returns cEffect*
386                 return 0;
387
388         case audioMasterGetNextPlug:
389                 SHOW_CALLBACK ("audioMasterGetNextPlug");
390                 // output pin in <value> (-1: first to come), returns cEffect*
391                 return 0;
392
393         case audioMasterWillReplaceOrAccumulate:
394                 SHOW_CALLBACK ("audioMasterWillReplaceOrAccumulate");
395                 // returns: 0: not supported, 1: replace, 2: accumulate
396                 return 0;
397
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.
406                 return 0;
407
408         case audioMasterGetAutomationState:
409                 SHOW_CALLBACK ("audioMasterGetAutomationState");
410                 // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
411                 // offline
412                 return 0;
413
414         case audioMasterOfflineStart:
415                 SHOW_CALLBACK ("audioMasterOfflineStart");
416                 return 0;
417
418         case audioMasterOfflineRead:
419                 SHOW_CALLBACK ("audioMasterOfflineRead");
420                 // ptr points to offline structure, see below. return 0: error, 1 ok
421                 return 0;
422
423         case audioMasterOfflineWrite:
424                 SHOW_CALLBACK ("audioMasterOfflineWrite");
425                 // same as read
426                 return 0;
427
428         case audioMasterOfflineGetCurrentPass:
429                 SHOW_CALLBACK ("audioMasterOfflineGetCurrentPass");
430                 return 0;
431
432         case audioMasterOfflineGetCurrentMetaPass:
433                 SHOW_CALLBACK ("audioMasterOfflineGetCurrentMetaPass");
434                 return 0;
435
436         case audioMasterSetOutputSampleRate:
437                 SHOW_CALLBACK ("audioMasterSetOutputSampleRate");
438                 // for variable i/o, sample rate in <opt>
439                 return 0;
440
441         case audioMasterGetSpeakerArrangement:
442                 SHOW_CALLBACK ("audioMasterGetSpeakerArrangement");
443                 // (long)input in <value>, output in <ptr>
444                 return 0;
445
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");
450                 return 1;
451
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);
456                 return 1;
457
458         case audioMasterGetVendorVersion:
459                 SHOW_CALLBACK ("audioMasterGetVendorVersion");
460                 // returns vendor-specific version
461                 return 900;
462
463         case audioMasterVendorSpecific:
464                 SHOW_CALLBACK ("audioMasterVendorSpecific");
465                 // no definition, vendor specific handling
466                 return 0;
467
468         case audioMasterSetIcon:
469                 SHOW_CALLBACK ("audioMasterSetIcon");
470                 // void* in <ptr>, format not defined yet
471                 return 0;
472
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)) {
478                                 return 1;
479                         }
480                 }
481                 return 0;
482
483         case audioMasterGetLanguage:
484                 SHOW_CALLBACK ("audioMasterGetLanguage");
485                 // see enum
486                 return 0;
487
488         case audioMasterOpenWindow:
489                 SHOW_CALLBACK ("audioMasterOpenWindow");
490                 // returns platform specific ptr
491                 return 0;
492
493         case audioMasterCloseWindow:
494                 SHOW_CALLBACK ("audioMasterCloseWindow");
495                 // close window, platform specific handle in <ptr>
496                 return 0;
497
498         case audioMasterGetDirectory:
499                 SHOW_CALLBACK ("audioMasterGetDirectory");
500                 // get plug directory, FSSpec on MAC, else char*
501                 return 0;
502
503         case audioMasterUpdateDisplay:
504                 SHOW_CALLBACK ("audioMasterUpdateDisplay");
505                 // something has changed, update 'multi-fx' display
506                 /* TODO: consider emitting  ParameterChangedExternally() for each ctrl input */
507                 if (session) {
508                         session->set_dirty ();
509                 }
510                 return 0;
511
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));
517                         if (ac) {
518                                 ac->start_touch (ac->session().transport_sample());
519                         }
520                 }
521                 return 0;
522
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));
528                         if (ac) {
529                                 ac->stop_touch (ac->session().transport_sample());
530                         }
531                 }
532                 return 0;
533
534         case audioMasterOpenFileSelector:
535                 SHOW_CALLBACK ("audioMasterOpenFileSelector");
536                 // open a fileselector window with VstFileSelect* in <ptr>
537                 return 0;
538
539         default:
540                 DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("VST master dispatcher: undefed: %1\n", opcode));
541                 break;
542         }
543
544         return 0;
545 }