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.
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.
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.
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
37 #define DEBUG_CALLBACKS
38 static int debug_callbacks = -1;
40 #ifdef DEBUG_CALLBACKS
41 #define SHOW_CALLBACK if (debug_callbacks) printf
43 #define SHOW_CALLBACK(...)
46 using namespace ARDOUR;
48 int Session::vst_current_loading_id = 0;
49 const char* Session::vst_can_do_strings[] = {
51 X_("sendVstTimeInfo"),
53 X_("sendVstMidiEvent"),
54 X_("receiveVstEvents"),
55 X_("receiveVstMidiEvent"),
58 X_("shellCategorycurID")
60 const int Session::vst_can_do_string_count = sizeof (vst_can_do_strings) / sizeof (char*);
62 intptr_t Session::vst_callback (
71 static VstTimeInfo _timeInfo;
75 if (debug_callbacks < 0) {
76 debug_callbacks = (getenv ("ARDOUR_DEBUG_VST_CALLBACKS") != 0);
79 if (effect && effect->user) {
80 plug = (VSTPlugin *) (effect->user);
81 session = &plug->session();
83 SHOW_CALLBACK ("am callback 0x%x, opcode = %d, plugin = \"%s\" ", (int) pthread_self().p, opcode, plug->name());
85 SHOW_CALLBACK ("am callback 0x%x, opcode = %d, plugin = \"%s\" ", (int) pthread_self(), opcode, plug->name());
91 SHOW_CALLBACK ("am callback 0x%x, opcode = %d", (int) pthread_self().p, opcode);
93 SHOW_CALLBACK ("am callback 0x%x, opcode = %d", (int) pthread_self(), opcode);
99 case audioMasterAutomate:
100 SHOW_CALLBACK ("amc: audioMasterAutomate\n");
101 // index, value, returns 0
103 plug->set_parameter (index, opt);
107 case audioMasterVersion:
108 SHOW_CALLBACK ("amc: audioMasterVersion\n");
109 // vst version, currently 2 (0 for older)
112 case audioMasterCurrentId:
113 SHOW_CALLBACK ("amc: audioMasterCurrentId\n");
114 // returns the unique id of a plug that's currently loading
115 return vst_current_loading_id;
117 case audioMasterIdle:
118 SHOW_CALLBACK ("amc: audioMasterIdle\n");
119 #ifdef WINDOWS_VST_SUPPORT
120 fst_audio_master_idle();
123 effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
127 case audioMasterPinConnected:
128 SHOW_CALLBACK ("amc: audioMasterPinConnected\n");
129 // inquire if an input or output is beeing connected;
130 // index enumerates input or output counting from zero:
131 // value is 0 for input and != 0 otherwise. note: the
132 // return value is 0 for <true> such that older versions
133 // will always return true.
136 case audioMasterWantMidi:
137 SHOW_CALLBACK ("amc: audioMasterWantMidi\n");
138 // <value> is a filter which is currently ignored
140 plug->get_info()->n_inputs.set_midi (1);
144 case audioMasterGetTime:
145 SHOW_CALLBACK ("amc: audioMasterGetTime\n");
146 // returns const VstTimeInfo* (or 0 if not supported)
147 // <value> should contain a mask indicating which fields are required
148 // (see valid masks above), as some items may require extensive
153 framepos_t now = session->transport_frame();
155 _timeInfo.samplePos = now;
156 _timeInfo.sampleRate = session->frame_rate();
158 const TempoMetric& tm (session->tempo_map().metric_at (now));
160 if (value & (kVstTempoValid)) {
161 const Tempo& t (tm.tempo());
162 _timeInfo.tempo = t.beats_per_minute ();
163 _timeInfo.flags |= (kVstTempoValid);
165 if (value & (kVstTimeSigValid)) {
166 const Meter& m (tm.meter());
167 _timeInfo.timeSigNumerator = m.divisions_per_bar ();
168 _timeInfo.timeSigDenominator = m.note_divisor ();
169 _timeInfo.flags |= (kVstTimeSigValid);
171 if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
172 Timecode::BBT_Time bbt;
175 session->tempo_map().bbt_time_rt (now, bbt);
177 /* PPQ = pulse per quarter
178 * VST's "pulse" is our "division".
180 * 8 divisions per bar, 1 division = quarter, so 8 quarters per bar, ppq = 1
181 * 8 divisions per bar, 1 division = eighth, so 4 quarters per bar, ppq = 2
182 * 4 divisions per bar, 1 division = quarter, so 4 quarters per bar, ppq = 1
183 * 4 divisions per bar, 1 division = half, so 8 quarters per bar, ppq = 0.5
184 * 4 divisions per bar, 1 division = fifth, so (4 * 5/4) quarters per bar, ppq = 5/4
186 * general: divs_per_bar / (note_type / 4.0)
188 double ppq_scaling = tm.meter().note_divisor() / 4.0;
190 /* Note that this assumes constant meter/tempo throughout the session. Stupid VST */
191 double ppqBar = double(bbt.bars - 1) * tm.meter().divisions_per_bar();
192 double ppqBeat = double(bbt.beats - 1);
193 double ppqTick = double(bbt.ticks) / Timecode::BBT_Time::ticks_per_beat;
195 ppqBar *= ppq_scaling;
196 ppqBeat *= ppq_scaling;
197 ppqTick *= ppq_scaling;
199 if (value & (kVstPpqPosValid)) {
200 _timeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
201 _timeInfo.flags |= (kVstPpqPosValid);
204 if (value & (kVstBarsValid)) {
205 _timeInfo.barStartPos = ppqBar;
206 _timeInfo.flags |= (kVstBarsValid);
214 if (value & (kVstSmpteValid)) {
217 session->timecode_time (now, t);
219 _timeInfo.smpteOffset = (t.hours * t.rate * 60.0 * 60.0) +
220 (t.minutes * t.rate * 60.0) +
221 (t.seconds * t.rate) +
225 _timeInfo.smpteOffset *= 80.0; /* VST spec is 1/80th frames */
227 if (session->timecode_drop_frames()) {
228 if (session->timecode_frames_per_second() == 30.0) {
229 _timeInfo.smpteFrameRate = 5;
231 _timeInfo.smpteFrameRate = 4; /* 29.97 assumed, thanks VST */
234 if (session->timecode_frames_per_second() == 24.0) {
235 _timeInfo.smpteFrameRate = 0;
236 } else if (session->timecode_frames_per_second() == 24.975) {
237 _timeInfo.smpteFrameRate = 2;
238 } else if (session->timecode_frames_per_second() == 25.0) {
239 _timeInfo.smpteFrameRate = 1;
241 _timeInfo.smpteFrameRate = 3; /* 30 fps */
244 _timeInfo.flags |= (kVstSmpteValid);
247 if (session->transport_speed() != 0.0f) {
248 _timeInfo.flags |= (kVstTransportPlaying);
251 if (session->get_play_loop()) {
255 _timeInfo.samplePos = 0;
256 _timeInfo.sampleRate = AudioEngine::instance()->sample_rate();
259 return (intptr_t) &_timeInfo;
261 case audioMasterProcessEvents:
262 SHOW_CALLBACK ("amc: audioMasterProcessEvents\n");
263 // VstEvents* in <ptr>
264 if (plug->midi_buffer()) {
265 VstEvents* v = (VstEvents*)ptr;
266 for (int n = 0 ; n < v->numEvents; ++n) {
267 VstMidiEvent *vme = (VstMidiEvent*) (v->events[n]->dump);
268 if (vme->type == kVstMidiType) {
269 plug->midi_buffer()->push_back(vme->deltaFrames, 3, (uint8_t*)vme->midiData);
275 case audioMasterSetTime:
276 SHOW_CALLBACK ("amc: audioMasterSetTime\n");
277 // VstTimenfo* in <ptr>, filter in <value>, not supported
279 case audioMasterTempoAt:
280 SHOW_CALLBACK ("amc: audioMasterTempoAt\n");
281 // returns tempo (in bpm * 10000) at sample frame location passed in <value>
283 const Tempo& t (session->tempo_map().tempo_at (value));
284 return t.beats_per_minute() * 1000;
290 case audioMasterGetNumAutomatableParameters:
291 SHOW_CALLBACK ("amc: audioMasterGetNumAutomatableParameters\n");
294 case audioMasterGetParameterQuantization:
295 SHOW_CALLBACK ("amc: audioMasterGetParameterQuantization\n");
296 // returns the integer value for +1.0 representation,
297 // or 1 if full single float precision is maintained
298 // in automation. parameter index in <value> (-1: all, any)
301 case audioMasterIOChanged:
302 SHOW_CALLBACK ("amc: audioMasterIOChanged\n");
303 // numInputs and/or numOutputs has changed
306 case audioMasterNeedIdle:
307 SHOW_CALLBACK ("amc: audioMasterNeedIdle\n");
308 // plug needs idle calls (outside its editor window)
310 plug->state()->wantIdle = 1;
314 case audioMasterSizeWindow:
315 SHOW_CALLBACK ("amc: audioMasterSizeWindow\n");
316 // index: width, value: height
319 case audioMasterGetSampleRate:
320 SHOW_CALLBACK ("amc: audioMasterGetSampleRate\n");
322 return session->frame_rate();
326 case audioMasterGetBlockSize:
327 SHOW_CALLBACK ("amc: audioMasterGetBlockSize\n");
329 return session->get_block_size();
333 case audioMasterGetInputLatency:
334 SHOW_CALLBACK ("amc: audioMasterGetInputLatency\n");
337 case audioMasterGetOutputLatency:
338 SHOW_CALLBACK ("amc: audioMasterGetOutputLatency\n");
341 case audioMasterGetPreviousPlug:
342 SHOW_CALLBACK ("amc: audioMasterGetPreviousPlug\n");
343 // input pin in <value> (-1: first to come), returns cEffect*
346 case audioMasterGetNextPlug:
347 SHOW_CALLBACK ("amc: audioMasterGetNextPlug\n");
348 // output pin in <value> (-1: first to come), returns cEffect*
350 case audioMasterWillReplaceOrAccumulate:
351 SHOW_CALLBACK ("amc: audioMasterWillReplaceOrAccumulate\n");
352 // returns: 0: not supported, 1: replace, 2: accumulate
355 case audioMasterGetCurrentProcessLevel:
356 SHOW_CALLBACK ("amc: audioMasterGetCurrentProcessLevel\n");
357 // returns: 0: not supported,
358 // 1: currently in user thread (gui)
359 // 2: currently in audio thread (where process is called)
360 // 3: currently in 'sequencer' thread (midi, timer etc)
361 // 4: currently offline processing and thus in user thread
362 // other: not defined, but probably pre-empting user thread.
365 case audioMasterGetAutomationState:
366 SHOW_CALLBACK ("amc: audioMasterGetAutomationState\n");
367 // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
371 case audioMasterOfflineStart:
372 SHOW_CALLBACK ("amc: audioMasterOfflineStart\n");
375 case audioMasterOfflineRead:
376 SHOW_CALLBACK ("amc: audioMasterOfflineRead\n");
377 // ptr points to offline structure, see below. return 0: error, 1 ok
380 case audioMasterOfflineWrite:
381 SHOW_CALLBACK ("amc: audioMasterOfflineWrite\n");
385 case audioMasterOfflineGetCurrentPass:
386 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentPass\n");
389 case audioMasterOfflineGetCurrentMetaPass:
390 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentMetaPass\n");
393 case audioMasterSetOutputSampleRate:
394 SHOW_CALLBACK ("amc: audioMasterSetOutputSampleRate\n");
395 // for variable i/o, sample rate in <opt>
398 case audioMasterGetSpeakerArrangement:
399 SHOW_CALLBACK ("amc: audioMasterGetSpeakerArrangement\n");
400 // (long)input in <value>, output in <ptr>
403 case audioMasterGetVendorString:
404 SHOW_CALLBACK ("amc: audioMasterGetVendorString\n");
405 // fills <ptr> with a string identifying the vendor (max 64 char)
406 strcpy ((char*) ptr, "Linux Audio Systems");
409 case audioMasterGetProductString:
410 SHOW_CALLBACK ("amc: audioMasterGetProductString\n");
411 // fills <ptr> with a string with product name (max 64 char)
412 strcpy ((char*) ptr, PROGRAM_NAME);
415 case audioMasterGetVendorVersion:
416 SHOW_CALLBACK ("amc: audioMasterGetVendorVersion\n");
417 // returns vendor-specific version
420 case audioMasterVendorSpecific:
421 SHOW_CALLBACK ("amc: audioMasterVendorSpecific\n");
422 // no definition, vendor specific handling
425 case audioMasterSetIcon:
426 SHOW_CALLBACK ("amc: audioMasterSetIcon\n");
427 // void* in <ptr>, format not defined yet
430 case audioMasterCanDo:
431 SHOW_CALLBACK ("amc: audioMasterCanDo\n");
432 // string in ptr, (const char*)ptr
433 for (int i = 0; i < vst_can_do_string_count; i++) {
434 if (! strcmp(vst_can_do_strings[i], (const char*)ptr)) {
440 case audioMasterGetLanguage:
441 SHOW_CALLBACK ("amc: audioMasterGetLanguage\n");
445 case audioMasterOpenWindow:
446 SHOW_CALLBACK ("amc: audioMasterOpenWindow\n");
447 // returns platform specific ptr
450 case audioMasterCloseWindow:
451 SHOW_CALLBACK ("amc: audioMasterCloseWindow\n");
452 // close window, platform specific handle in <ptr>
455 case audioMasterGetDirectory:
456 SHOW_CALLBACK ("amc: audioMasterGetDirectory\n");
457 // get plug directory, FSSpec on MAC, else char*
460 case audioMasterUpdateDisplay:
461 SHOW_CALLBACK ("amc: audioMasterUpdateDisplay\n");
462 // something has changed, update 'multi-fx' display
464 effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
468 case audioMasterBeginEdit:
469 SHOW_CALLBACK ("amc: audioMasterBeginEdit\n");
470 // begin of automation session (when mouse down), parameter index in <index>
473 case audioMasterEndEdit:
474 SHOW_CALLBACK ("amc: audioMasterEndEdit\n");
475 // end of automation session (when mouse up), parameter index in <index>
478 case audioMasterOpenFileSelector:
479 SHOW_CALLBACK ("amc: audioMasterOpenFileSelector\n");
480 // open a fileselector window with VstFileSelect* in <ptr>
484 SHOW_CALLBACK ("VST master dispatcher: undefed: %d\n", opcode);