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