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