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