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