Collect plugin runtime profile statistics.
[ardour.git] / libs / ardour / session_vst.cc
1 /*
2     Copyright (C) 2004
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef COMPILER_MSVC
21 #include <stdbool.h>
22 #endif
23 #include <cstdio>
24
25 #include "ardour/audioengine.h"
26 #include "ardour/debug.h"
27 #include "ardour/session.h"
28 #include "ardour/tempo.h"
29 #include "ardour/plugin_insert.h"
30 #include "ardour/windows_vst_plugin.h"
31 #include "ardour/vestige/aeffectx.h"
32 #include "ardour/vst_types.h"
33 #ifdef WINDOWS_VST_SUPPORT
34 #include <fst.h>
35 #endif
36
37 #include "pbd/i18n.h"
38
39 using namespace ARDOUR;
40
41 #define SHOW_CALLBACK(MSG) DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose (MSG " val = %1 idx = %2\n", index, value))
42
43 int Session::vst_current_loading_id = 0;
44 const char* Session::vst_can_do_strings[] = {
45         X_("supplyIdle"),
46         X_("sendVstTimeInfo"),
47         X_("sendVstEvents"),
48         X_("sendVstMidiEvent"),
49         X_("receiveVstEvents"),
50         X_("receiveVstMidiEvent"),
51         X_("supportShell"),
52         X_("shellCategory"),
53         X_("shellCategorycurID")
54 };
55 const int Session::vst_can_do_string_count = sizeof (vst_can_do_strings) / sizeof (char*);
56
57 intptr_t Session::vst_callback (
58         AEffect* effect,
59         int32_t opcode,
60         int32_t index,
61         intptr_t value,
62         void* ptr,
63         float opt
64         )
65 {
66         VSTPlugin* plug;
67         Session* session;
68         static VstTimeInfo _timeinfo; // only uses as fallback
69         VstTimeInfo* timeinfo;
70         int32_t newflags = 0;
71
72         if (effect && effect->ptr1) {
73                 plug = (VSTPlugin *) (effect->ptr1);
74                 session = &plug->session();
75                 timeinfo = plug->timeinfo ();
76                 DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("am callback 0x%1%2, opcode = %3%4, plugin = \"%5\"\n",
77                                         std::hex, (void*) DEBUG_THREAD_SELF,
78                                         std::dec, opcode, plug->name()));
79         } else {
80                 plug = 0;
81                 session = 0;
82                 timeinfo = &_timeinfo;
83                 DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("am callback 0x%1%2, opcode = %3%4\n",
84                                         std::hex, (void*) DEBUG_THREAD_SELF,
85                                         std::dec, opcode));
86         }
87
88         switch(opcode){
89
90         case audioMasterAutomate:
91                 SHOW_CALLBACK ("audioMasterAutomate");
92                 // index, value, returns 0
93                 if (plug) {
94                         plug->parameter_changed_externally (index, opt);
95                 }
96                 return 0;
97
98         case audioMasterVersion:
99                 SHOW_CALLBACK ("audioMasterVersion");
100                 // vst version, currently 2 (0 for older)
101                 return 2400;
102
103         case audioMasterCurrentId:
104                 SHOW_CALLBACK ("audioMasterCurrentId");
105                 // returns the unique id of a plug that's currently loading
106                 return vst_current_loading_id;
107
108         case audioMasterIdle:
109                 SHOW_CALLBACK ("audioMasterIdle");
110 #ifdef WINDOWS_VST_SUPPORT
111                 fst_audio_master_idle();
112 #endif
113                 if (effect) {
114                         effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
115                 }
116                 return 0;
117
118         case audioMasterPinConnected:
119                 SHOW_CALLBACK ("audioMasterPinConnected");
120                 // inquire if an input or output is beeing connected;
121                 // index enumerates input or output counting from zero:
122                 // value is 0 for input and != 0 otherwise. note: the
123                 // return value is 0 for <true> such that older versions
124                 // will always return true.
125                 if (!plug) {
126                         // we don't know.
127                         // but ardour always connects all buffers, so we're good
128                         return 0;
129                 }
130                 switch (value) {
131                         case 0:
132                                 if (plug->plugin_insert ()) {
133                                         bool valid;
134                                         const ChanMapping& map (plug->plugin_insert ()->input_map (plug->plugin_number ()));
135                                         map.get (DataType::AUDIO, index, &valid);
136                                         return valid ? 0 : 1;
137                                 }
138                                 if (index < plug->plugin()->numInputs) {
139                                         return 0;
140                                 }
141                                 break;
142                         case 1:
143 #if 0 // investigate, the outputs *are* connected to scratch buffers
144                                 if (plug->plugin_insert ()) {
145                                         bool valid;
146                                         const ChanMapping& map (plug->plugin_insert ()->output_map (plug->plugin_number ()));
147                                         map.get (DataType::AUDIO, index, &valid);
148                                         return valid ? 0 : 1;
149                                 }
150 #endif
151                                 if (index < plug->plugin()->numOutputs) {
152                                         return 0;
153                                 }
154                                 break;
155                         default:
156                                 break;
157                 }
158                 return 1;
159
160         case audioMasterWantMidi:
161                 SHOW_CALLBACK ("audioMasterWantMidi");
162                 // <value> is a filter which is currently ignored
163                 if (plug && plug->get_info() != NULL) {
164                         plug->get_info()->n_inputs.set_midi (1);
165                 }
166                 return 0;
167
168         case audioMasterGetTime:
169                 SHOW_CALLBACK ("audioMasterGetTime");
170                 newflags = kVstNanosValid | kVstAutomationWriting | kVstAutomationReading;
171
172                 timeinfo->nanoSeconds = g_get_monotonic_time () * 1000;
173
174                 if (plug && session) {
175                         samplepos_t now = plug->transport_sample();
176
177                         timeinfo->samplePos = now;
178                         timeinfo->sampleRate = session->sample_rate();
179
180                         if (value & (kVstTempoValid)) {
181                                 const Tempo& t (session->tempo_map().tempo_at_sample (now));
182                                 timeinfo->tempo = t.quarter_notes_per_minute ();
183                                 newflags |= (kVstTempoValid);
184                         }
185                         if (value & (kVstTimeSigValid)) {
186                                 const MeterSection& ms (session->tempo_map().meter_section_at_sample (now));
187                                 timeinfo->timeSigNumerator = ms.divisions_per_bar ();
188                                 timeinfo->timeSigDenominator = ms.note_divisor ();
189                                 newflags |= (kVstTimeSigValid);
190                         }
191                         if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
192                                 Timecode::BBT_Time bbt;
193
194                                 try {
195                                         bbt = session->tempo_map().bbt_at_sample_rt (now);
196                                         bbt.beats = 1;
197                                         bbt.ticks = 0;
198                                         /* exact quarter note */
199                                         double ppqBar = session->tempo_map().quarter_note_at_bbt_rt (bbt);
200                                         /* quarter note at sample position (not rounded to note subdivision) */
201                                         double ppqPos = session->tempo_map().quarter_note_at_sample_rt (now);
202                                         if (value & (kVstPpqPosValid)) {
203                                                 timeinfo->ppqPos = ppqPos;
204                                                 newflags |= kVstPpqPosValid;
205                                         }
206
207                                         if (value & (kVstBarsValid)) {
208                                                 timeinfo->barStartPos = ppqBar;
209                                                 newflags |= kVstBarsValid;
210                                         }
211
212                                 } catch (...) {
213                                         /* relax */
214                                 }
215                         }
216
217                         if (value & (kVstSmpteValid)) {
218                                 Timecode::Time t;
219
220                                 session->timecode_time (now, t);
221
222                                 timeinfo->smpteOffset = (t.hours * t.rate * 60.0 * 60.0) +
223                                         (t.minutes * t.rate * 60.0) +
224                                         (t.seconds * t.rate) +
225                                         (t.frames) +
226                                         (t.subframes);
227
228                                 timeinfo->smpteOffset *= 80.0; /* VST spec is 1/80th samples */
229
230                                 if (session->timecode_drop_frames()) {
231                                         if (session->timecode_frames_per_second() == 30.0) {
232                                                 timeinfo->smpteFrameRate = 5;
233                                         } else {
234                                                 timeinfo->smpteFrameRate = 4; /* 29.97 assumed, thanks VST */
235                                         }
236                                 } else {
237                                         if (session->timecode_frames_per_second() == 24.0) {
238                                                 timeinfo->smpteFrameRate = 0;
239                                         } else if (session->timecode_frames_per_second() == 24.975) {
240                                                 timeinfo->smpteFrameRate = 2;
241                                         } else if (session->timecode_frames_per_second() == 25.0) {
242                                                 timeinfo->smpteFrameRate = 1;
243                                         } else {
244                                                 timeinfo->smpteFrameRate = 3; /* 30 fps */
245                                         }
246                                 }
247                                 newflags |= (kVstSmpteValid);
248                         }
249
250                         if (session->actively_recording ()) {
251                                 newflags |= kVstTransportRecording;
252                         }
253
254                         if (plug->transport_speed () != 0.0f) {
255                                 newflags |= kVstTransportPlaying;
256                         }
257
258                         if (session->get_play_loop ()) {
259                                 newflags |= kVstTransportCycleActive;
260                                 Location * looploc = session->locations ()->auto_loop_location ();
261                                 if (looploc) try {
262                                         timeinfo->cycleStartPos = session->tempo_map ().quarter_note_at_sample_rt (looploc->start ());
263                                         timeinfo->cycleEndPos = session->tempo_map ().quarter_note_at_sample_rt (looploc->end ());
264
265                                         newflags |= kVstCyclePosValid;
266                                 } catch (...) { }
267                         }
268
269                 } else {
270                         timeinfo->samplePos = 0;
271                         timeinfo->sampleRate = AudioEngine::instance()->sample_rate();
272                 }
273
274                 if ((timeinfo->flags & (kVstTransportPlaying | kVstTransportRecording | kVstTransportCycleActive))
275                     !=
276                     (newflags        & (kVstTransportPlaying | kVstTransportRecording | kVstTransportCycleActive)))
277                 {
278                         newflags |= kVstTransportChanged;
279                 }
280
281                 timeinfo->flags = newflags;
282                 return (intptr_t) timeinfo;
283
284         case audioMasterProcessEvents:
285                 SHOW_CALLBACK ("audioMasterProcessEvents");
286                 // VstEvents* in <ptr>
287                 if (plug && plug->midi_buffer()) {
288                         VstEvents* v = (VstEvents*)ptr;
289                         for (int n = 0 ; n < v->numEvents; ++n) {
290                                 VstMidiEvent *vme = (VstMidiEvent*) (v->events[n]->dump);
291                                 if (vme->type == kVstMidiType) {
292                                         plug->midi_buffer()->push_back(vme->deltaSamples, 3, (uint8_t*)vme->midiData);
293                                 }
294                         }
295                 }
296                 return 0;
297
298         case audioMasterSetTime:
299                 SHOW_CALLBACK ("audioMasterSetTime");
300                 // VstTimenfo* in <ptr>, filter in <value>, not supported
301
302         case audioMasterTempoAt:
303                 SHOW_CALLBACK ("audioMasterTempoAt");
304                 // returns tempo (in bpm * 10000) at sample sample location passed in <value>
305                 if (session) {
306                         const Tempo& t (session->tempo_map().tempo_at_sample (value));
307                         return t.quarter_notes_per_minute() * 1000;
308                 } else {
309                         return 0;
310                 }
311                 break;
312
313         case audioMasterGetNumAutomatableParameters:
314                 SHOW_CALLBACK ("audioMasterGetNumAutomatableParameters");
315                 return 0;
316
317         case audioMasterGetParameterQuantization:
318                 SHOW_CALLBACK ("audioMasterGetParameterQuantization");
319                 // returns the integer value for +1.0 representation,
320                 // or 1 if full single float precision is maintained
321                 // in automation. parameter index in <value> (-1: all, any)
322                 return 0;
323
324         case audioMasterIOChanged:
325                 SHOW_CALLBACK ("audioMasterIOChanged");
326                 // numInputs and/or numOutputs has changed
327                 return 0;
328
329         case audioMasterNeedIdle:
330                 SHOW_CALLBACK ("audioMasterNeedIdle");
331                 // plug needs idle calls (outside its editor window)
332                 if (plug) {
333                         plug->state()->wantIdle = 1;
334                 }
335                 return 0;
336
337         case audioMasterSizeWindow:
338                 SHOW_CALLBACK ("audioMasterSizeWindow");
339                 if (plug && plug->state()) {
340                         if (plug->state()->width != index || plug->state()->height != value) {
341                                 plug->state()->width = index;
342                                 plug->state()->height = value;
343 #ifndef NDEBUG
344                                 printf ("audioMasterSizeWindow %d %d\n", plug->state()->width, plug->state()->height);
345 #endif
346                                 plug->VSTSizeWindow (); /* EMIT SIGNAL */
347                         }
348                 }
349                 return 0;
350
351         case audioMasterGetSampleRate:
352                 SHOW_CALLBACK ("audioMasterGetSampleRate");
353                 if (session) {
354                         return session->sample_rate();
355                 }
356                 return 0;
357
358         case audioMasterGetBlockSize:
359                 SHOW_CALLBACK ("audioMasterGetBlockSize");
360                 if (session) {
361                         return session->get_block_size();
362                 }
363                 return 0;
364
365         case audioMasterGetInputLatency:
366                 SHOW_CALLBACK ("audioMasterGetInputLatency");
367                 return 0;
368
369         case audioMasterGetOutputLatency:
370                 SHOW_CALLBACK ("audioMasterGetOutputLatency");
371                 return 0;
372
373         case audioMasterGetPreviousPlug:
374                 SHOW_CALLBACK ("audioMasterGetPreviousPlug");
375                 // input pin in <value> (-1: first to come), returns cEffect*
376                 return 0;
377
378         case audioMasterGetNextPlug:
379                 SHOW_CALLBACK ("audioMasterGetNextPlug");
380                 // output pin in <value> (-1: first to come), returns cEffect*
381
382         case audioMasterWillReplaceOrAccumulate:
383                 SHOW_CALLBACK ("audioMasterWillReplaceOrAccumulate");
384                 // returns: 0: not supported, 1: replace, 2: accumulate
385                 return 0;
386
387         case audioMasterGetCurrentProcessLevel:
388                 SHOW_CALLBACK ("audioMasterGetCurrentProcessLevel");
389                 // returns: 0: not supported,
390                 // 1: currently in user thread (gui)
391                 // 2: currently in audio thread (where process is called)
392                 // 3: currently in 'sequencer' thread (midi, timer etc)
393                 // 4: currently offline processing and thus in user thread
394                 // other: not defined, but probably pre-empting user thread.
395                 return 0;
396
397         case audioMasterGetAutomationState:
398                 SHOW_CALLBACK ("audioMasterGetAutomationState");
399                 // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
400                 // offline
401                 return 0;
402
403         case audioMasterOfflineStart:
404                 SHOW_CALLBACK ("audioMasterOfflineStart");
405                 return 0;
406
407         case audioMasterOfflineRead:
408                 SHOW_CALLBACK ("audioMasterOfflineRead");
409                 // ptr points to offline structure, see below. return 0: error, 1 ok
410                 return 0;
411
412         case audioMasterOfflineWrite:
413                 SHOW_CALLBACK ("audioMasterOfflineWrite");
414                 // same as read
415                 return 0;
416
417         case audioMasterOfflineGetCurrentPass:
418                 SHOW_CALLBACK ("audioMasterOfflineGetCurrentPass");
419                 return 0;
420
421         case audioMasterOfflineGetCurrentMetaPass:
422                 SHOW_CALLBACK ("audioMasterOfflineGetCurrentMetaPass");
423                 return 0;
424
425         case audioMasterSetOutputSampleRate:
426                 SHOW_CALLBACK ("audioMasterSetOutputSampleRate");
427                 // for variable i/o, sample rate in <opt>
428                 return 0;
429
430         case audioMasterGetSpeakerArrangement:
431                 SHOW_CALLBACK ("audioMasterGetSpeakerArrangement");
432                 // (long)input in <value>, output in <ptr>
433                 return 0;
434
435         case audioMasterGetVendorString:
436                 SHOW_CALLBACK ("audioMasterGetVendorString");
437                 // fills <ptr> with a string identifying the vendor (max 64 char)
438                 strcpy ((char*) ptr, "Linux Audio Systems");
439                 return 0;
440
441         case audioMasterGetProductString:
442                 SHOW_CALLBACK ("audioMasterGetProductString");
443                 // fills <ptr> with a string with product name (max 64 char)
444                 strcpy ((char*) ptr, PROGRAM_NAME);
445                 return 1;
446
447         case audioMasterGetVendorVersion:
448                 SHOW_CALLBACK ("audioMasterGetVendorVersion");
449                 // returns vendor-specific version
450                 return 900;
451
452         case audioMasterVendorSpecific:
453                 SHOW_CALLBACK ("audioMasterVendorSpecific");
454                 // no definition, vendor specific handling
455                 return 0;
456
457         case audioMasterSetIcon:
458                 SHOW_CALLBACK ("audioMasterSetIcon");
459                 // void* in <ptr>, format not defined yet
460                 return 0;
461
462         case audioMasterCanDo:
463                 SHOW_CALLBACK ("audioMasterCanDo");
464                 // string in ptr,  (const char*)ptr
465                 for (int i = 0; i < vst_can_do_string_count; i++) {
466                         if (! strcmp(vst_can_do_strings[i], (const char*)ptr)) {
467                                 return 1;
468                         }
469                 }
470                 return 0;
471
472         case audioMasterGetLanguage:
473                 SHOW_CALLBACK ("audioMasterGetLanguage");
474                 // see enum
475                 return 0;
476
477         case audioMasterOpenWindow:
478                 SHOW_CALLBACK ("audioMasterOpenWindow");
479                 // returns platform specific ptr
480                 return 0;
481
482         case audioMasterCloseWindow:
483                 SHOW_CALLBACK ("audioMasterCloseWindow");
484                 // close window, platform specific handle in <ptr>
485                 return 0;
486
487         case audioMasterGetDirectory:
488                 SHOW_CALLBACK ("audioMasterGetDirectory");
489                 // get plug directory, FSSpec on MAC, else char*
490                 return 0;
491
492         case audioMasterUpdateDisplay:
493                 SHOW_CALLBACK ("audioMasterUpdateDisplay");
494                 // something has changed, update 'multi-fx' display
495                 if (effect) {
496                         effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
497                 }
498                 return 0;
499
500         case audioMasterBeginEdit:
501                 SHOW_CALLBACK ("audioMasterBeginEdit");
502                 // begin of automation session (when mouse down), parameter index in <index>
503                 if (plug && plug->plugin_insert ()) {
504                         boost::shared_ptr<AutomationControl> ac = plug->plugin_insert ()->automation_control (Evoral::Parameter (PluginAutomation, 0, index));
505                         if (ac) {
506                                 ac->start_touch (ac->session().transport_sample());
507                         }
508                 }
509                 return 0;
510
511         case audioMasterEndEdit:
512                 SHOW_CALLBACK ("audioMasterEndEdit");
513                 // end of automation session (when mouse up),     parameter index in <index>
514                 if (plug && plug->plugin_insert ()) {
515                         boost::shared_ptr<AutomationControl> ac = plug->plugin_insert ()->automation_control (Evoral::Parameter (PluginAutomation, 0, index));
516                         if (ac) {
517                                 ac->stop_touch (ac->session().transport_sample());
518                         }
519                 }
520                 return 0;
521
522         case audioMasterOpenFileSelector:
523                 SHOW_CALLBACK ("audioMasterOpenFileSelector");
524                 // open a fileselector window with VstFileSelect* in <ptr>
525                 return 0;
526
527         default:
528                 DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("VST master dispatcher: undefed: %1\n", opcode));
529                 break;
530         }
531
532         return 0;
533 }