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