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