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