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/session.h"
26 #include "ardour/tempo.h"
27 #include "ardour/windows_vst_plugin.h"
28 #include "ardour/vestige/aeffectx.h"
29 #include "ardour/vst_types.h"
30
31 #include "i18n.h"
32
33 #define DEBUG_CALLBACKS
34 static int debug_callbacks = -1;
35
36 #ifdef DEBUG_CALLBACKS
37 #define SHOW_CALLBACK if (debug_callbacks) printf
38 #else
39 #define SHOW_CALLBACK(...)
40 #endif
41
42 using namespace ARDOUR;
43
44 intptr_t Session::vst_callback (
45         AEffect* effect,
46         int32_t opcode,
47         int32_t index,
48         intptr_t value,
49         void* ptr,
50         float opt
51         )
52 {
53         static VstTimeInfo _timeInfo;
54         VSTPlugin* plug;
55         Session* session;
56
57         if (debug_callbacks < 0) {
58                 debug_callbacks = (getenv ("ARDOUR_DEBUG_VST_CALLBACKS") != 0);
59         }
60
61         if (effect && effect->user) {
62                 plug = (VSTPlugin *) (effect->user);
63                 session = &plug->session();
64 #ifdef COMPILER_MSVC
65                 SHOW_CALLBACK ("am callback 0x%x, opcode = %d, plugin = \"%s\" ", (int) pthread_self().p, opcode, plug->name());
66 #else
67                 SHOW_CALLBACK ("am callback 0x%x, opcode = %d, plugin = \"%s\" ", (int) pthread_self(), opcode, plug->name());
68 #endif
69         } else {
70                 plug = 0;
71                 session = 0;
72 #ifdef COMPILER_MSVC
73                 SHOW_CALLBACK ("am callback 0x%x, opcode = %d", (int) pthread_self().p, opcode);
74 #else
75                 SHOW_CALLBACK ("am callback 0x%x, opcode = %d", (int) pthread_self(), opcode);
76 #endif
77         }
78
79         switch(opcode){
80
81         case audioMasterAutomate:
82                 SHOW_CALLBACK ("amc: audioMasterAutomate\n");
83                 // index, value, returns 0
84                 if (plug) {
85                         plug->set_parameter (index, opt);
86                 }
87                 return 0;
88
89         case audioMasterVersion:
90                 SHOW_CALLBACK ("amc: audioMasterVersion\n");
91                 // vst version, currently 2 (0 for older)
92                 return 2;
93
94         case audioMasterCurrentId:
95                 SHOW_CALLBACK ("amc: audioMasterCurrentId\n");
96                 // returns the unique id of a plug that's currently
97                 // loading
98                 return 0;
99
100         case audioMasterIdle:
101                 SHOW_CALLBACK ("amc: audioMasterIdle\n");
102                 // call application idle routine (this will
103                 // call effEditIdle for all open editors too)
104                 if (effect) {
105                         effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
106                 }
107                 return 0;
108
109         case audioMasterPinConnected:
110                 SHOW_CALLBACK ("amc: audioMasterPinConnected\n");
111                 // inquire if an input or output is beeing connected;
112                 // index enumerates input or output counting from zero:
113                 // value is 0 for input and != 0 otherwise. note: the
114                 // return value is 0 for <true> such that older versions
115                 // will always return true.
116                 return 1;
117
118         case audioMasterWantMidi:
119                 SHOW_CALLBACK ("amc: audioMasterWantMidi\n");
120                 // <value> is a filter which is currently ignored
121                 if (plug) {
122                         plug->get_info()->n_inputs.set_midi (1);
123                 }
124                 return 0;
125
126         case audioMasterGetTime:
127                 SHOW_CALLBACK ("amc: audioMasterGetTime\n");
128                 // returns const VstTimeInfo* (or 0 if not supported)
129                 // <value> should contain a mask indicating which fields are required
130                 // (see valid masks above), as some items may require extensive
131                 // conversions
132                 memset(&_timeInfo, 0, sizeof(_timeInfo));
133                 if (session) {
134                         framepos_t now = session->transport_frame();
135                         _timeInfo.samplePos = now;
136                         _timeInfo.sampleRate = session->frame_rate();
137                         _timeInfo.flags = 0;
138
139                         const TempoMetric& tm (session->tempo_map().metric_at (now));
140
141                         if (value & (kVstTempoValid)) {
142                                 const Tempo& t (tm.tempo());
143                                 _timeInfo.tempo = t.beats_per_minute ();
144                                 _timeInfo.flags |= (kVstTempoValid);
145                         }
146                         if (value & (kVstBarsValid)) {
147                                 const Meter& m (tm.meter());
148                                 _timeInfo.timeSigNumerator = m.divisions_per_bar ();
149                                 _timeInfo.timeSigDenominator = m.note_divisor ();
150                                 _timeInfo.flags |= (kVstBarsValid);
151                         }
152                         if (value & (kVstPpqPosValid)) {
153                                 Timecode::BBT_Time bbt;
154                                 try {
155                                         session->tempo_map().bbt_time_rt (now, bbt);
156                                         
157                                         /* Note that this assumes constant
158                                            meter/tempo throughout the session. We
159                                            can do better than this, because
160                                            progressive rock fans demand it.
161                                         */
162                                         double ppqBar = double(bbt.bars - 1) * tm.meter().divisions_per_bar();
163                                         double ppqBeat = double(bbt.beats - 1);
164                                         double ppqTick = double(bbt.ticks) / Timecode::BBT_Time::ticks_per_beat;
165                                         // PPQ Pos
166                                         _timeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
167                                         _timeInfo.flags |= (kVstPpqPosValid);
168                                 } catch (...) {
169                                         /* relax */
170                                 }
171                         }
172
173                         _timeInfo.tempo = tm.tempo().beats_per_minute();
174                         _timeInfo.flags |= kVstTempoValid;
175                         
176                         // Bars
177                         // _timeInfo.barStartPos = ppqBar;
178                         // _timeInfo.flags |= kVstBarsValid;
179                         
180                         // Time Signature
181                         _timeInfo.timeSigNumerator = tm.meter().divisions_per_bar();
182                         _timeInfo.timeSigDenominator = tm.meter().note_divisor();
183                         _timeInfo.flags |= kVstTimeSigValid;
184
185                         if (session->transport_speed() != 0.0f) {
186                                 _timeInfo.flags |= kVstTransportPlaying;
187                         }
188                 }
189
190                 return (long)&_timeInfo;
191
192         case audioMasterProcessEvents:
193                 SHOW_CALLBACK ("amc: audioMasterProcessEvents\n");
194                 // VstEvents* in <ptr>
195                 return 0;
196
197         case audioMasterSetTime:
198                 SHOW_CALLBACK ("amc: audioMasterSetTime\n");
199                 // VstTimenfo* in <ptr>, filter in <value>, not supported
200
201         case audioMasterTempoAt:
202                 SHOW_CALLBACK ("amc: audioMasterTempoAt\n");
203                 // returns tempo (in bpm * 10000) at sample frame location passed in <value>
204                 if (session) {
205                         const Tempo& t (session->tempo_map().tempo_at (value));
206                         return t.beats_per_minute() * 1000;
207                 } else {
208                         return 0;
209                 }
210                 break;
211
212         case audioMasterGetNumAutomatableParameters:
213                 SHOW_CALLBACK ("amc: audioMasterGetNumAutomatableParameters\n");
214                 return 0;
215
216         case audioMasterGetParameterQuantization:
217                 SHOW_CALLBACK ("amc: audioMasterGetParameterQuantization\n");
218                // returns the integer value for +1.0 representation,
219                // or 1 if full single float precision is maintained
220                // in automation. parameter index in <value> (-1: all, any)
221                 return 0;
222
223         case audioMasterIOChanged:
224                 SHOW_CALLBACK ("amc: audioMasterIOChanged\n");
225                // numInputs and/or numOutputs has changed
226                 return 0;
227
228         case audioMasterNeedIdle:
229                 SHOW_CALLBACK ("amc: audioMasterNeedIdle\n");
230                 // plug needs idle calls (outside its editor window)
231                 if (plug) {
232                         plug->state()->wantIdle = 1;
233                 }
234                 return 0;
235
236         case audioMasterSizeWindow:
237                 SHOW_CALLBACK ("amc: audioMasterSizeWindow\n");
238                 // index: width, value: height
239                 return 0;
240
241         case audioMasterGetSampleRate:
242                 SHOW_CALLBACK ("amc: audioMasterGetSampleRate\n");
243                 if (session) {
244                         return session->frame_rate();
245                 }
246                 return 0;
247
248         case audioMasterGetBlockSize:
249                 SHOW_CALLBACK ("amc: audioMasterGetBlockSize\n");
250                 if (session) {
251                         return session->get_block_size();
252                 }
253                 return 0;
254
255         case audioMasterGetInputLatency:
256                 SHOW_CALLBACK ("amc: audioMasterGetInputLatency\n");
257                 return 0;
258
259         case audioMasterGetOutputLatency:
260                 SHOW_CALLBACK ("amc: audioMasterGetOutputLatency\n");
261                 return 0;
262
263         case audioMasterGetPreviousPlug:
264                 SHOW_CALLBACK ("amc: audioMasterGetPreviousPlug\n");
265                // input pin in <value> (-1: first to come), returns cEffect*
266                 return 0;
267
268         case audioMasterGetNextPlug:
269                 SHOW_CALLBACK ("amc: audioMasterGetNextPlug\n");
270                // output pin in <value> (-1: first to come), returns cEffect*
271
272         case audioMasterWillReplaceOrAccumulate:
273                 SHOW_CALLBACK ("amc: audioMasterWillReplaceOrAccumulate\n");
274                // returns: 0: not supported, 1: replace, 2: accumulate
275                 return 0;
276
277         case audioMasterGetCurrentProcessLevel:
278                 SHOW_CALLBACK ("amc: audioMasterGetCurrentProcessLevel\n");
279                 // returns: 0: not supported,
280                 // 1: currently in user thread (gui)
281                 // 2: currently in audio thread (where process is called)
282                 // 3: currently in 'sequencer' thread (midi, timer etc)
283                 // 4: currently offline processing and thus in user thread
284                 // other: not defined, but probably pre-empting user thread.
285                 return 0;
286
287         case audioMasterGetAutomationState:
288                 SHOW_CALLBACK ("amc: audioMasterGetAutomationState\n");
289                 // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
290                 // offline
291                 return 0;
292
293         case audioMasterOfflineStart:
294                 SHOW_CALLBACK ("amc: audioMasterOfflineStart\n");
295                 return 0;
296                 
297         case audioMasterOfflineRead:
298                 SHOW_CALLBACK ("amc: audioMasterOfflineRead\n");
299                // ptr points to offline structure, see below. return 0: error, 1 ok
300                 return 0;
301
302         case audioMasterOfflineWrite:
303                 SHOW_CALLBACK ("amc: audioMasterOfflineWrite\n");
304                 // same as read
305                 return 0;
306
307         case audioMasterOfflineGetCurrentPass:
308                 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentPass\n");
309                 return 0;
310                 
311         case audioMasterOfflineGetCurrentMetaPass:
312                 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentMetaPass\n");
313                 return 0;
314
315         case audioMasterSetOutputSampleRate:
316                 SHOW_CALLBACK ("amc: audioMasterSetOutputSampleRate\n");
317                 // for variable i/o, sample rate in <opt>
318                 return 0;
319
320         case audioMasterGetSpeakerArrangement:
321                 SHOW_CALLBACK ("amc: audioMasterGetSpeakerArrangement\n");
322                 // (long)input in <value>, output in <ptr>
323                 return 0;
324
325         case audioMasterGetVendorString:
326                 SHOW_CALLBACK ("amc: audioMasterGetVendorString\n");
327                 // fills <ptr> with a string identifying the vendor (max 64 char)
328                 strcpy ((char*) ptr, "Linux Audio Systems");
329                 return 0;
330
331         case audioMasterGetProductString:
332                 SHOW_CALLBACK ("amc: audioMasterGetProductString\n");
333                 // fills <ptr> with a string with product name (max 64 char)
334                 strcpy ((char*) ptr, PROGRAM_NAME);
335                 return 0;
336
337         case audioMasterGetVendorVersion:
338                 SHOW_CALLBACK ("amc: audioMasterGetVendorVersion\n");
339                 // returns vendor-specific version
340                 return 900;
341
342         case audioMasterVendorSpecific:
343                 SHOW_CALLBACK ("amc: audioMasterVendorSpecific\n");
344                 // no definition, vendor specific handling
345                 return 0;
346
347         case audioMasterSetIcon:
348                 SHOW_CALLBACK ("amc: audioMasterSetIcon\n");
349                 // void* in <ptr>, format not defined yet
350                 return 0;
351
352         case audioMasterCanDo:
353                 SHOW_CALLBACK ("amc: audioMasterCanDo\n");
354                 // string in ptr, see below
355                 return 0;
356
357         case audioMasterGetLanguage:
358                 SHOW_CALLBACK ("amc: audioMasterGetLanguage\n");
359                 // see enum
360                 return 0;
361
362         case audioMasterOpenWindow:
363                 SHOW_CALLBACK ("amc: audioMasterOpenWindow\n");
364                 // returns platform specific ptr
365                 return 0;
366
367         case audioMasterCloseWindow:
368                 SHOW_CALLBACK ("amc: audioMasterCloseWindow\n");
369                 // close window, platform specific handle in <ptr>
370                 return 0;
371
372         case audioMasterGetDirectory:
373                 SHOW_CALLBACK ("amc: audioMasterGetDirectory\n");
374                 // get plug directory, FSSpec on MAC, else char*
375                 return 0;
376
377         case audioMasterUpdateDisplay:
378                 SHOW_CALLBACK ("amc: audioMasterUpdateDisplay\n");
379                 // something has changed, update 'multi-fx' display
380                 if (effect) {
381                         effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
382                 }
383                 return 0;
384
385         case audioMasterBeginEdit:
386                 SHOW_CALLBACK ("amc: audioMasterBeginEdit\n");
387                 // begin of automation session (when mouse down), parameter index in <index>
388                 return 0;
389
390         case audioMasterEndEdit:
391                 SHOW_CALLBACK ("amc: audioMasterEndEdit\n");
392                 // end of automation session (when mouse up),     parameter index in <index>
393                 return 0;
394
395         case audioMasterOpenFileSelector:
396                 SHOW_CALLBACK ("amc: audioMasterOpenFileSelector\n");
397                 // open a fileselector window with VstFileSelect* in <ptr>
398                 return 0;
399
400         default:
401                 SHOW_CALLBACK ("VST master dispatcher: undefed: %d\n", opcode);
402                 break;
403         }
404
405         return 0;
406 }
407