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