Windows VST GUI related rework
[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; // XXX 2400
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
106 #if 0 // TODO -> emit to GUI OR better delegete to fst/fst
107
108                 // This allows the main GUI window to update if needed.
109                 // Some plugins take over the GUI event loop
110                 // which causes the main GUI to freeze while the plugin GUI continues to run. This code
111                 // prevents the main GUI from being frozen.
112
113                 do {
114 #ifdef GDK_WINDOWING_X11
115                         gtk_main_iteration_do(false);
116 #else
117                         gtk_main_iteration()
118 #endif
119                 } while (gtk_events_pending());
120 #endif
121                 printf("audioMasterIdle\n");
122
123                 if (effect) {
124                         effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
125                 }
126                 return 0;
127
128         case audioMasterPinConnected:
129                 SHOW_CALLBACK ("amc: audioMasterPinConnected\n");
130                 // inquire if an input or output is beeing connected;
131                 // index enumerates input or output counting from zero:
132                 // value is 0 for input and != 0 otherwise. note: the
133                 // return value is 0 for <true> such that older versions
134                 // will always return true.
135                 return 1;
136
137         case audioMasterWantMidi:
138                 SHOW_CALLBACK ("amc: audioMasterWantMidi\n");
139                 // <value> is a filter which is currently ignored
140                 if (plug) {
141                         plug->get_info()->n_inputs.set_midi (1);
142                 }
143                 return 0;
144
145         case audioMasterGetTime:
146                 SHOW_CALLBACK ("amc: audioMasterGetTime\n");
147                 // returns const VstTimeInfo* (or 0 if not supported)
148                 // <value> should contain a mask indicating which fields are required
149                 // (see valid masks above), as some items may require extensive
150                 // conversions
151                 _timeInfo.flags = 0;
152
153                 if (session) {
154                         framepos_t now = session->transport_frame();
155
156                         _timeInfo.samplePos = now;
157                         _timeInfo.sampleRate = session->frame_rate();
158                         
159                         const TempoMetric& tm (session->tempo_map().metric_at (now));
160
161                         if (value & (kVstTempoValid)) {
162                                 const Tempo& t (tm.tempo());
163                                 _timeInfo.tempo = t.beats_per_minute ();
164                                 _timeInfo.flags |= (kVstTempoValid);
165                         }
166                         if (value & (kVstTimeSigValid)) {
167                                 const Meter& m (tm.meter());
168                                 _timeInfo.timeSigNumerator = m.divisions_per_bar ();
169                                 _timeInfo.timeSigDenominator = m.note_divisor ();
170                                 _timeInfo.flags |= (kVstTimeSigValid);
171                         }
172                         if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
173                                 Timecode::BBT_Time bbt;
174
175                                 try {
176                                         session->tempo_map().bbt_time_rt (now, bbt);
177                                         
178                                         /* PPQ = pulse per quarter
179                                            VST's "pulse" is our "division".
180
181                                            8 divisions per bar, 1 division = quarter, so 8 quarters per bar, ppq = 1
182                                            8 divisions per bar, 1 division = eighth, so  4 quarters per bar, ppq = 2
183                                            4 divisions per bar, 1 division = quarter, so  4 quarters per bar, ppq = 1
184                                            4 divisions per bar, 1 division = half, so 8 quarters per bar, ppq = 0.5
185                                            4 divisions per bar, 1 division = fifth, so (4 * 5/4) quarters per bar, ppq = 5/4
186                                            
187                                            general: divs_per_bar / (note_type / 4.0)
188                                         */
189                                         double ppq_scaling =  tm.meter().note_divisor() / 4.0;
190
191                                         /* Note that this assumes constant meter/tempo throughout the session. Stupid VST
192                                         */
193                                         double ppqBar = double(bbt.bars - 1) * tm.meter().divisions_per_bar();
194                                         double ppqBeat = double(bbt.beats - 1);
195                                         double ppqTick = double(bbt.ticks) / Timecode::BBT_Time::ticks_per_beat;
196
197                                         ppqBar *= ppq_scaling;
198                                         ppqBeat *= ppq_scaling;
199                                         ppqTick *= ppq_scaling;
200                                         
201                                         if (value & (kVstPpqPosValid)) {
202                                                 _timeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
203                                                 _timeInfo.flags |= (kVstPpqPosValid);
204                                         }
205                                         
206                                         if (value & (kVstBarsValid)) {
207                                                 _timeInfo.barStartPos = ppqBar;
208                                                 _timeInfo.flags |= (kVstBarsValid);
209                                         }
210                                         
211                                 } catch (...) {
212                                         /* relax */
213                                 }
214                         }
215
216                         if (value & (kVstSmpteValid)) {
217                                 Timecode::Time t;
218                                 
219                                 session->timecode_time (now, t);
220                                 
221                                 _timeInfo.smpteOffset = (t.hours * t.rate * 60.0 * 60.0) + 
222                                         (t.minutes * t.rate * 60.0) + 
223                                         (t.seconds * t.rate) + 
224                                         (t.frames) + 
225                                         (t.subframes);
226
227                                 _timeInfo.smpteOffset *= 80.0; /* VST spec is 1/80th frames */
228
229                                 if (session->timecode_drop_frames()) {
230                                         if (session->timecode_frames_per_second() == 30.0) {
231                                                 _timeInfo.smpteFrameRate = 5;
232                                         } else {
233                                                 _timeInfo.smpteFrameRate = 4; /* 29.97 assumed, thanks VST */
234                                         }
235                                 } else {
236                                         if (session->timecode_frames_per_second() == 24.0) {
237                                                 _timeInfo.smpteFrameRate = 0;
238                                         } else if (session->timecode_frames_per_second() == 24.975) {
239                                                 _timeInfo.smpteFrameRate = 2;
240                                         } else if (session->timecode_frames_per_second() == 25.0) {
241                                                 _timeInfo.smpteFrameRate = 1;
242                                         } else {
243                                                 _timeInfo.smpteFrameRate = 3; /* 30 fps */
244                                         }
245                                 }
246                                 _timeInfo.flags |= (kVstSmpteValid);
247                         }
248
249                         if (session->transport_speed() != 0.0f) {
250                                 _timeInfo.flags |= (kVstTransportPlaying);
251                         }
252
253                         if (session->get_play_loop()) {
254                         }
255
256                 } else {
257                         _timeInfo.samplePos = 0;
258                         _timeInfo.sampleRate = AudioEngine::instance()->sample_rate();
259                 }
260                 
261                 return (intptr_t) &_timeInfo;
262
263         case audioMasterProcessEvents:
264                 SHOW_CALLBACK ("amc: audioMasterProcessEvents\n");
265                 // VstEvents* in <ptr>
266                 return 0;
267
268         case audioMasterSetTime:
269                 SHOW_CALLBACK ("amc: audioMasterSetTime\n");
270                 // VstTimenfo* in <ptr>, filter in <value>, not supported
271
272         case audioMasterTempoAt:
273                 SHOW_CALLBACK ("amc: audioMasterTempoAt\n");
274                 // returns tempo (in bpm * 10000) at sample frame location passed in <value>
275                 if (session) {
276                         const Tempo& t (session->tempo_map().tempo_at (value));
277                         return t.beats_per_minute() * 1000;
278                 } else {
279                         return 0;
280                 }
281                 break;
282
283         case audioMasterGetNumAutomatableParameters:
284                 SHOW_CALLBACK ("amc: audioMasterGetNumAutomatableParameters\n");
285                 return 0;
286
287         case audioMasterGetParameterQuantization:
288                 SHOW_CALLBACK ("amc: audioMasterGetParameterQuantization\n");
289                // returns the integer value for +1.0 representation,
290                // or 1 if full single float precision is maintained
291                // in automation. parameter index in <value> (-1: all, any)
292                 return 0;
293
294         case audioMasterIOChanged:
295                 SHOW_CALLBACK ("amc: audioMasterIOChanged\n");
296                // numInputs and/or numOutputs has changed
297                 return 0;
298
299         case audioMasterNeedIdle:
300                 SHOW_CALLBACK ("amc: audioMasterNeedIdle\n");
301                 // plug needs idle calls (outside its editor window)
302                 if (plug) {
303                         plug->state()->wantIdle = 1;
304                 }
305                 return 0;
306
307         case audioMasterSizeWindow:
308                 SHOW_CALLBACK ("amc: audioMasterSizeWindow\n");
309                 // index: width, value: height
310                 return 0;
311
312         case audioMasterGetSampleRate:
313                 SHOW_CALLBACK ("amc: audioMasterGetSampleRate\n");
314                 if (session) {
315                         return session->frame_rate();
316                 }
317                 return 0;
318
319         case audioMasterGetBlockSize:
320                 SHOW_CALLBACK ("amc: audioMasterGetBlockSize\n");
321                 if (session) {
322                         return session->get_block_size();
323                 }
324                 return 0;
325
326         case audioMasterGetInputLatency:
327                 SHOW_CALLBACK ("amc: audioMasterGetInputLatency\n");
328                 return 0;
329
330         case audioMasterGetOutputLatency:
331                 SHOW_CALLBACK ("amc: audioMasterGetOutputLatency\n");
332                 return 0;
333
334         case audioMasterGetPreviousPlug:
335                 SHOW_CALLBACK ("amc: audioMasterGetPreviousPlug\n");
336                // input pin in <value> (-1: first to come), returns cEffect*
337                 return 0;
338
339         case audioMasterGetNextPlug:
340                 SHOW_CALLBACK ("amc: audioMasterGetNextPlug\n");
341                // output pin in <value> (-1: first to come), returns cEffect*
342
343         case audioMasterWillReplaceOrAccumulate:
344                 SHOW_CALLBACK ("amc: audioMasterWillReplaceOrAccumulate\n");
345                // returns: 0: not supported, 1: replace, 2: accumulate
346                 return 0;
347
348         case audioMasterGetCurrentProcessLevel:
349                 SHOW_CALLBACK ("amc: audioMasterGetCurrentProcessLevel\n");
350                 // returns: 0: not supported,
351                 // 1: currently in user thread (gui)
352                 // 2: currently in audio thread (where process is called)
353                 // 3: currently in 'sequencer' thread (midi, timer etc)
354                 // 4: currently offline processing and thus in user thread
355                 // other: not defined, but probably pre-empting user thread.
356                 return 0;
357
358         case audioMasterGetAutomationState:
359                 SHOW_CALLBACK ("amc: audioMasterGetAutomationState\n");
360                 // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
361                 // offline
362                 return 0;
363
364         case audioMasterOfflineStart:
365                 SHOW_CALLBACK ("amc: audioMasterOfflineStart\n");
366                 return 0;
367                 
368         case audioMasterOfflineRead:
369                 SHOW_CALLBACK ("amc: audioMasterOfflineRead\n");
370                // ptr points to offline structure, see below. return 0: error, 1 ok
371                 return 0;
372
373         case audioMasterOfflineWrite:
374                 SHOW_CALLBACK ("amc: audioMasterOfflineWrite\n");
375                 // same as read
376                 return 0;
377
378         case audioMasterOfflineGetCurrentPass:
379                 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentPass\n");
380                 return 0;
381                 
382         case audioMasterOfflineGetCurrentMetaPass:
383                 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentMetaPass\n");
384                 return 0;
385
386         case audioMasterSetOutputSampleRate:
387                 SHOW_CALLBACK ("amc: audioMasterSetOutputSampleRate\n");
388                 // for variable i/o, sample rate in <opt>
389                 return 0;
390
391         case audioMasterGetSpeakerArrangement:
392                 SHOW_CALLBACK ("amc: audioMasterGetSpeakerArrangement\n");
393                 // (long)input in <value>, output in <ptr>
394                 return 0;
395
396         case audioMasterGetVendorString:
397                 SHOW_CALLBACK ("amc: audioMasterGetVendorString\n");
398                 // fills <ptr> with a string identifying the vendor (max 64 char)
399                 strcpy ((char*) ptr, "Linux Audio Systems");
400                 return 0;
401
402         case audioMasterGetProductString:
403                 SHOW_CALLBACK ("amc: audioMasterGetProductString\n");
404                 // fills <ptr> with a string with product name (max 64 char)
405                 strcpy ((char*) ptr, PROGRAM_NAME);
406                 return 0;
407
408         case audioMasterGetVendorVersion:
409                 SHOW_CALLBACK ("amc: audioMasterGetVendorVersion\n");
410                 // returns vendor-specific version
411                 return 900;
412
413         case audioMasterVendorSpecific:
414                 SHOW_CALLBACK ("amc: audioMasterVendorSpecific\n");
415                 // no definition, vendor specific handling
416                 return 0;
417
418         case audioMasterSetIcon:
419                 SHOW_CALLBACK ("amc: audioMasterSetIcon\n");
420                 // void* in <ptr>, format not defined yet
421                 return 0;
422
423         case audioMasterCanDo:
424                 SHOW_CALLBACK ("amc: audioMasterCanDo\n");
425                 // string in ptr,  (const char*)ptr
426                 return 0;
427
428         case audioMasterGetLanguage:
429                 SHOW_CALLBACK ("amc: audioMasterGetLanguage\n");
430                 // see enum
431                 return 0;
432
433         case audioMasterOpenWindow:
434                 SHOW_CALLBACK ("amc: audioMasterOpenWindow\n");
435                 // returns platform specific ptr
436                 return 0;
437
438         case audioMasterCloseWindow:
439                 SHOW_CALLBACK ("amc: audioMasterCloseWindow\n");
440                 // close window, platform specific handle in <ptr>
441                 return 0;
442
443         case audioMasterGetDirectory:
444                 SHOW_CALLBACK ("amc: audioMasterGetDirectory\n");
445                 // get plug directory, FSSpec on MAC, else char*
446                 return 0;
447
448         case audioMasterUpdateDisplay:
449                 SHOW_CALLBACK ("amc: audioMasterUpdateDisplay\n");
450                 // something has changed, update 'multi-fx' display
451                 if (effect) {
452                         effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
453                 }
454                 return 0;
455
456         case audioMasterBeginEdit:
457                 SHOW_CALLBACK ("amc: audioMasterBeginEdit\n");
458                 // begin of automation session (when mouse down), parameter index in <index>
459                 return 0;
460
461         case audioMasterEndEdit:
462                 SHOW_CALLBACK ("amc: audioMasterEndEdit\n");
463                 // end of automation session (when mouse up),     parameter index in <index>
464                 return 0;
465
466         case audioMasterOpenFileSelector:
467                 SHOW_CALLBACK ("amc: audioMasterOpenFileSelector\n");
468                 // open a fileselector window with VstFileSelect* in <ptr>
469                 return 0;
470
471         default:
472                 SHOW_CALLBACK ("VST master dispatcher: undefed: %d\n", opcode);
473                 break;
474         }
475
476         return 0;
477 }
478