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