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