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