fix VST shell-scanner (double free), support for VST2.4
[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/session.h"
27 #include "ardour/tempo.h"
28 #include "ardour/windows_vst_plugin.h"
29 #include "ardour/vestige/aeffectx.h"
30 #include "ardour/vst_types.h"
31 #ifdef WINDOWS_VST_SUPPORT
32 #include <fst.h>
33 #endif
34
35 #include "i18n.h"
36
37 #define DEBUG_CALLBACKS
38 static int debug_callbacks = -1;
39
40 #ifdef DEBUG_CALLBACKS
41 #define SHOW_CALLBACK if (debug_callbacks) printf
42 #else
43 #define SHOW_CALLBACK(...)
44 #endif
45
46 using namespace ARDOUR;
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_("supportShell"),
53         X_("shellCategory")
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         static VstTimeInfo _timeInfo;
67         VSTPlugin* plug;
68         Session* session;
69
70         if (debug_callbacks < 0) {
71                 debug_callbacks = (getenv ("ARDOUR_DEBUG_VST_CALLBACKS") != 0);
72         }
73
74         if (effect && effect->user) {
75                 plug = (VSTPlugin *) (effect->user);
76                 session = &plug->session();
77 #ifdef COMPILER_MSVC
78                 SHOW_CALLBACK ("am callback 0x%x, opcode = %d, plugin = \"%s\" ", (int) pthread_self().p, opcode, plug->name());
79 #else
80                 SHOW_CALLBACK ("am callback 0x%x, opcode = %d, plugin = \"%s\" ", (int) pthread_self(), opcode, plug->name());
81 #endif
82         } else {
83                 plug = 0;
84                 session = 0;
85 #ifdef COMPILER_MSVC
86                 SHOW_CALLBACK ("am callback 0x%x, opcode = %d", (int) pthread_self().p, opcode);
87 #else
88                 SHOW_CALLBACK ("am callback 0x%x, opcode = %d", (int) pthread_self(), opcode);
89 #endif
90         }
91
92         switch(opcode){
93
94         case audioMasterAutomate:
95                 SHOW_CALLBACK ("amc: audioMasterAutomate\n");
96                 // index, value, returns 0
97                 if (plug) {
98                         plug->set_parameter (index, opt);
99                 }
100                 return 0;
101
102         case audioMasterVersion:
103                 SHOW_CALLBACK ("amc: audioMasterVersion\n");
104                 // vst version, currently 2 (0 for older)
105                 return 2400;
106
107         case audioMasterCurrentId:
108                 SHOW_CALLBACK ("amc: audioMasterCurrentId\n");
109                 // returns the unique id of a plug that's currently loading
110                 return vst_current_loading_id;
111
112         case audioMasterIdle:
113                 SHOW_CALLBACK ("amc: audioMasterIdle\n");
114 #ifdef WINDOWS_VST_SUPPORT
115                 fst_audio_master_idle();
116 #endif
117                 if (effect) {
118                         effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
119                 }
120                 return 0;
121
122         case audioMasterPinConnected:
123                 SHOW_CALLBACK ("amc: audioMasterPinConnected\n");
124                 // inquire if an input or output is beeing connected;
125                 // index enumerates input or output counting from zero:
126                 // value is 0 for input and != 0 otherwise. note: the
127                 // return value is 0 for <true> such that older versions
128                 // will always return true.
129                 return 1;
130
131         case audioMasterWantMidi:
132                 SHOW_CALLBACK ("amc: audioMasterWantMidi\n");
133                 // <value> is a filter which is currently ignored
134                 if (plug) {
135                         plug->get_info()->n_inputs.set_midi (1);
136                 }
137                 return 0;
138
139         case audioMasterGetTime:
140                 SHOW_CALLBACK ("amc: audioMasterGetTime\n");
141                 // returns const VstTimeInfo* (or 0 if not supported)
142                 // <value> should contain a mask indicating which fields are required
143                 // (see valid masks above), as some items may require extensive
144                 // conversions
145                 _timeInfo.flags = 0;
146
147                 if (session) {
148                         framepos_t now = session->transport_frame();
149
150                         _timeInfo.samplePos = now;
151                         _timeInfo.sampleRate = session->frame_rate();
152
153                         const TempoMetric& tm (session->tempo_map().metric_at (now));
154
155                         if (value & (kVstTempoValid)) {
156                                 const Tempo& t (tm.tempo());
157                                 _timeInfo.tempo = t.beats_per_minute ();
158                                 _timeInfo.flags |= (kVstTempoValid);
159                         }
160                         if (value & (kVstTimeSigValid)) {
161                                 const Meter& m (tm.meter());
162                                 _timeInfo.timeSigNumerator = m.divisions_per_bar ();
163                                 _timeInfo.timeSigDenominator = m.note_divisor ();
164                                 _timeInfo.flags |= (kVstTimeSigValid);
165                         }
166                         if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
167                                 Timecode::BBT_Time bbt;
168
169                                 try {
170                                         session->tempo_map().bbt_time_rt (now, bbt);
171
172                                         /* PPQ = pulse per quarter
173                                          * VST's "pulse" is our "division".
174                                          *
175                                          * 8 divisions per bar, 1 division = quarter, so 8 quarters per bar, ppq = 1
176                                          * 8 divisions per bar, 1 division = eighth, so  4 quarters per bar, ppq = 2
177                                          * 4 divisions per bar, 1 division = quarter, so  4 quarters per bar, ppq = 1
178                                          * 4 divisions per bar, 1 division = half, so 8 quarters per bar, ppq = 0.5
179                                          * 4 divisions per bar, 1 division = fifth, so (4 * 5/4) quarters per bar, ppq = 5/4
180                                          *
181                                          * general: divs_per_bar / (note_type / 4.0)
182                                          */
183                                         double ppq_scaling =  tm.meter().note_divisor() / 4.0;
184
185                                         /* Note that this assumes constant meter/tempo throughout the session. Stupid VST */
186                                         double ppqBar = double(bbt.bars - 1) * tm.meter().divisions_per_bar();
187                                         double ppqBeat = double(bbt.beats - 1);
188                                         double ppqTick = double(bbt.ticks) / Timecode::BBT_Time::ticks_per_beat;
189
190                                         ppqBar *= ppq_scaling;
191                                         ppqBeat *= ppq_scaling;
192                                         ppqTick *= ppq_scaling;
193
194                                         if (value & (kVstPpqPosValid)) {
195                                                 _timeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
196                                                 _timeInfo.flags |= (kVstPpqPosValid);
197                                         }
198
199                                         if (value & (kVstBarsValid)) {
200                                                 _timeInfo.barStartPos = ppqBar;
201                                                 _timeInfo.flags |= (kVstBarsValid);
202                                         }
203
204                                 } catch (...) {
205                                         /* relax */
206                                 }
207                         }
208
209                         if (value & (kVstSmpteValid)) {
210                                 Timecode::Time t;
211
212                                 session->timecode_time (now, t);
213
214                                 _timeInfo.smpteOffset = (t.hours * t.rate * 60.0 * 60.0) +
215                                         (t.minutes * t.rate * 60.0) +
216                                         (t.seconds * t.rate) +
217                                         (t.frames) +
218                                         (t.subframes);
219
220                                 _timeInfo.smpteOffset *= 80.0; /* VST spec is 1/80th frames */
221
222                                 if (session->timecode_drop_frames()) {
223                                         if (session->timecode_frames_per_second() == 30.0) {
224                                                 _timeInfo.smpteFrameRate = 5;
225                                         } else {
226                                                 _timeInfo.smpteFrameRate = 4; /* 29.97 assumed, thanks VST */
227                                         }
228                                 } else {
229                                         if (session->timecode_frames_per_second() == 24.0) {
230                                                 _timeInfo.smpteFrameRate = 0;
231                                         } else if (session->timecode_frames_per_second() == 24.975) {
232                                                 _timeInfo.smpteFrameRate = 2;
233                                         } else if (session->timecode_frames_per_second() == 25.0) {
234                                                 _timeInfo.smpteFrameRate = 1;
235                                         } else {
236                                                 _timeInfo.smpteFrameRate = 3; /* 30 fps */
237                                         }
238                                 }
239                                 _timeInfo.flags |= (kVstSmpteValid);
240                         }
241
242                         if (session->transport_speed() != 0.0f) {
243                                 _timeInfo.flags |= (kVstTransportPlaying);
244                         }
245
246                         if (session->get_play_loop()) {
247                         }
248
249                 } else {
250                         _timeInfo.samplePos = 0;
251                         _timeInfo.sampleRate = AudioEngine::instance()->sample_rate();
252                 }
253
254                 return (intptr_t) &_timeInfo;
255
256         case audioMasterProcessEvents:
257                 SHOW_CALLBACK ("amc: audioMasterProcessEvents\n");
258                 // VstEvents* in <ptr>
259                 return 0;
260
261         case audioMasterSetTime:
262                 SHOW_CALLBACK ("amc: audioMasterSetTime\n");
263                 // VstTimenfo* in <ptr>, filter in <value>, not supported
264
265         case audioMasterTempoAt:
266                 SHOW_CALLBACK ("amc: audioMasterTempoAt\n");
267                 // returns tempo (in bpm * 10000) at sample frame location passed in <value>
268                 if (session) {
269                         const Tempo& t (session->tempo_map().tempo_at (value));
270                         return t.beats_per_minute() * 1000;
271                 } else {
272                         return 0;
273                 }
274                 break;
275
276         case audioMasterGetNumAutomatableParameters:
277                 SHOW_CALLBACK ("amc: audioMasterGetNumAutomatableParameters\n");
278                 return 0;
279
280         case audioMasterGetParameterQuantization:
281                 SHOW_CALLBACK ("amc: audioMasterGetParameterQuantization\n");
282                 // returns the integer value for +1.0 representation,
283                 // or 1 if full single float precision is maintained
284                 // in automation. parameter index in <value> (-1: all, any)
285                 return 0;
286
287         case audioMasterIOChanged:
288                 SHOW_CALLBACK ("amc: audioMasterIOChanged\n");
289                 // numInputs and/or numOutputs has changed
290                 return 0;
291
292         case audioMasterNeedIdle:
293                 SHOW_CALLBACK ("amc: audioMasterNeedIdle\n");
294                 // plug needs idle calls (outside its editor window)
295                 if (plug) {
296                         plug->state()->wantIdle = 1;
297                 }
298                 return 0;
299
300         case audioMasterSizeWindow:
301                 SHOW_CALLBACK ("amc: audioMasterSizeWindow\n");
302                 // index: width, value: height
303                 return 0;
304
305         case audioMasterGetSampleRate:
306                 SHOW_CALLBACK ("amc: audioMasterGetSampleRate\n");
307                 if (session) {
308                         return session->frame_rate();
309                 }
310                 return 0;
311
312         case audioMasterGetBlockSize:
313                 SHOW_CALLBACK ("amc: audioMasterGetBlockSize\n");
314                 if (session) {
315                         return session->get_block_size();
316                 }
317                 return 0;
318
319         case audioMasterGetInputLatency:
320                 SHOW_CALLBACK ("amc: audioMasterGetInputLatency\n");
321                 return 0;
322
323         case audioMasterGetOutputLatency:
324                 SHOW_CALLBACK ("amc: audioMasterGetOutputLatency\n");
325                 return 0;
326
327         case audioMasterGetPreviousPlug:
328                 SHOW_CALLBACK ("amc: audioMasterGetPreviousPlug\n");
329                 // input pin in <value> (-1: first to come), returns cEffect*
330                 return 0;
331
332         case audioMasterGetNextPlug:
333                 SHOW_CALLBACK ("amc: audioMasterGetNextPlug\n");
334                 // output pin in <value> (-1: first to come), returns cEffect*
335
336         case audioMasterWillReplaceOrAccumulate:
337                 SHOW_CALLBACK ("amc: audioMasterWillReplaceOrAccumulate\n");
338                 // returns: 0: not supported, 1: replace, 2: accumulate
339                 return 0;
340
341         case audioMasterGetCurrentProcessLevel:
342                 SHOW_CALLBACK ("amc: audioMasterGetCurrentProcessLevel\n");
343                 // returns: 0: not supported,
344                 // 1: currently in user thread (gui)
345                 // 2: currently in audio thread (where process is called)
346                 // 3: currently in 'sequencer' thread (midi, timer etc)
347                 // 4: currently offline processing and thus in user thread
348                 // other: not defined, but probably pre-empting user thread.
349                 return 0;
350
351         case audioMasterGetAutomationState:
352                 SHOW_CALLBACK ("amc: audioMasterGetAutomationState\n");
353                 // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
354                 // offline
355                 return 0;
356
357         case audioMasterOfflineStart:
358                 SHOW_CALLBACK ("amc: audioMasterOfflineStart\n");
359                 return 0;
360
361         case audioMasterOfflineRead:
362                 SHOW_CALLBACK ("amc: audioMasterOfflineRead\n");
363                 // ptr points to offline structure, see below. return 0: error, 1 ok
364                 return 0;
365
366         case audioMasterOfflineWrite:
367                 SHOW_CALLBACK ("amc: audioMasterOfflineWrite\n");
368                 // same as read
369                 return 0;
370
371         case audioMasterOfflineGetCurrentPass:
372                 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentPass\n");
373                 return 0;
374
375         case audioMasterOfflineGetCurrentMetaPass:
376                 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentMetaPass\n");
377                 return 0;
378
379         case audioMasterSetOutputSampleRate:
380                 SHOW_CALLBACK ("amc: audioMasterSetOutputSampleRate\n");
381                 // for variable i/o, sample rate in <opt>
382                 return 0;
383
384         case audioMasterGetSpeakerArrangement:
385                 SHOW_CALLBACK ("amc: audioMasterGetSpeakerArrangement\n");
386                 // (long)input in <value>, output in <ptr>
387                 return 0;
388
389         case audioMasterGetVendorString:
390                 SHOW_CALLBACK ("amc: audioMasterGetVendorString\n");
391                 // fills <ptr> with a string identifying the vendor (max 64 char)
392                 strcpy ((char*) ptr, "Linux Audio Systems");
393                 return 0;
394
395         case audioMasterGetProductString:
396                 SHOW_CALLBACK ("amc: audioMasterGetProductString\n");
397                 // fills <ptr> with a string with product name (max 64 char)
398                 strcpy ((char*) ptr, PROGRAM_NAME);
399                 return 0;
400
401         case audioMasterGetVendorVersion:
402                 SHOW_CALLBACK ("amc: audioMasterGetVendorVersion\n");
403                 // returns vendor-specific version
404                 return 900;
405
406         case audioMasterVendorSpecific:
407                 SHOW_CALLBACK ("amc: audioMasterVendorSpecific\n");
408                 // no definition, vendor specific handling
409                 return 0;
410
411         case audioMasterSetIcon:
412                 SHOW_CALLBACK ("amc: audioMasterSetIcon\n");
413                 // void* in <ptr>, format not defined yet
414                 return 0;
415
416         case audioMasterCanDo:
417                 SHOW_CALLBACK ("amc: audioMasterCanDo\n");
418                 // string in ptr,  (const char*)ptr
419                 for (int i = 0; i < vst_can_do_string_count; i++) {
420                         if (! strcmp(vst_can_do_strings[i], (const char*)ptr)) {
421                                 return 1;
422                         }
423                 }
424                 return 0;
425
426         case audioMasterGetLanguage:
427                 SHOW_CALLBACK ("amc: audioMasterGetLanguage\n");
428                 // see enum
429                 return 0;
430
431         case audioMasterOpenWindow:
432                 SHOW_CALLBACK ("amc: audioMasterOpenWindow\n");
433                 // returns platform specific ptr
434                 return 0;
435
436         case audioMasterCloseWindow:
437                 SHOW_CALLBACK ("amc: audioMasterCloseWindow\n");
438                 // close window, platform specific handle in <ptr>
439                 return 0;
440
441         case audioMasterGetDirectory:
442                 SHOW_CALLBACK ("amc: audioMasterGetDirectory\n");
443                 // get plug directory, FSSpec on MAC, else char*
444                 return 0;
445
446         case audioMasterUpdateDisplay:
447                 SHOW_CALLBACK ("amc: audioMasterUpdateDisplay\n");
448                 // something has changed, update 'multi-fx' display
449                 if (effect) {
450                         effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
451                 }
452                 return 0;
453
454         case audioMasterBeginEdit:
455                 SHOW_CALLBACK ("amc: audioMasterBeginEdit\n");
456                 // begin of automation session (when mouse down), parameter index in <index>
457                 return 0;
458
459         case audioMasterEndEdit:
460                 SHOW_CALLBACK ("amc: audioMasterEndEdit\n");
461                 // end of automation session (when mouse up),     parameter index in <index>
462                 return 0;
463
464         case audioMasterOpenFileSelector:
465                 SHOW_CALLBACK ("amc: audioMasterOpenFileSelector\n");
466                 // open a fileselector window with VstFileSelect* in <ptr>
467                 return 0;
468
469         default:
470                 SHOW_CALLBACK ("VST master dispatcher: undefed: %d\n", opcode);
471                 break;
472         }
473
474         return 0;
475 }
476