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