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