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