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
35 #include "pbd/debug.h"
39 #define DEBUG_CALLBACKS
40 static int debug_callbacks = -1;
42 #ifdef DEBUG_CALLBACKS
43 #define SHOW_CALLBACK if (debug_callbacks) printf
45 #define SHOW_CALLBACK(...)
48 using namespace ARDOUR;
50 int Session::vst_current_loading_id = 0;
51 const char* Session::vst_can_do_strings[] = {
53 X_("sendVstTimeInfo"),
55 X_("sendVstMidiEvent"),
56 X_("receiveVstEvents"),
57 X_("receiveVstMidiEvent"),
60 X_("shellCategorycurID")
62 const int Session::vst_can_do_string_count = sizeof (vst_can_do_strings) / sizeof (char*);
64 intptr_t Session::vst_callback (
73 static VstTimeInfo _timeInfo;
77 if (debug_callbacks < 0) {
78 debug_callbacks = (getenv ("ARDOUR_DEBUG_VST_CALLBACKS") != 0);
81 if (effect && effect->user) {
82 plug = (VSTPlugin *) (effect->user);
83 session = &plug->session();
84 SHOW_CALLBACK ("am callback 0x%p, opcode = %d, plugin = \"%s\" ", (void*) DEBUG_THREAD_SELF, opcode, plug->name());
88 SHOW_CALLBACK ("am callback 0x%p, opcode = %d", (void*) DEBUG_THREAD_SELF, opcode);
93 case audioMasterAutomate:
94 SHOW_CALLBACK ("amc: audioMasterAutomate\n");
95 // index, value, returns 0
97 plug->set_parameter (index, opt);
101 case audioMasterVersion:
102 SHOW_CALLBACK ("amc: audioMasterVersion\n");
103 // vst version, currently 2 (0 for older)
106 case audioMasterCurrentId:
107 SHOW_CALLBACK ("amc: audioMasterCurrentId\n");
108 // returns the unique id of a plug that's currently loading
109 return vst_current_loading_id;
111 case audioMasterIdle:
112 SHOW_CALLBACK ("amc: audioMasterIdle\n");
113 #ifdef WINDOWS_VST_SUPPORT
114 fst_audio_master_idle();
117 effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
121 case audioMasterPinConnected:
122 SHOW_CALLBACK ("amc: audioMasterPinConnected\n");
123 // inquire if an input or output is beeing connected;
124 // index enumerates input or output counting from zero:
125 // value is 0 for input and != 0 otherwise. note: the
126 // return value is 0 for <true> such that older versions
127 // will always return true.
130 case audioMasterWantMidi:
131 SHOW_CALLBACK ("amc: audioMasterWantMidi\n");
132 // <value> is a filter which is currently ignored
133 if (plug && plug->get_info() != NULL) {
134 plug->get_info()->n_inputs.set_midi (1);
138 case audioMasterGetTime:
139 SHOW_CALLBACK ("amc: audioMasterGetTime\n");
140 // returns const VstTimeInfo* (or 0 if not supported)
141 // <value> should contain a mask indicating which fields are required
142 // (see valid masks above), as some items may require extensive
147 framepos_t now = session->transport_frame();
149 _timeInfo.samplePos = now;
150 _timeInfo.sampleRate = session->frame_rate();
152 const TempoMetric& tm (session->tempo_map().metric_at (now));
154 if (value & (kVstTempoValid)) {
155 const Tempo& t (tm.tempo());
156 _timeInfo.tempo = t.beats_per_minute ();
157 _timeInfo.flags |= (kVstTempoValid);
159 if (value & (kVstTimeSigValid)) {
160 const Meter& m (tm.meter());
161 _timeInfo.timeSigNumerator = m.divisions_per_bar ();
162 _timeInfo.timeSigDenominator = m.note_divisor ();
163 _timeInfo.flags |= (kVstTimeSigValid);
165 if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
166 Timecode::BBT_Time bbt;
169 session->tempo_map().bbt_time_rt (now, bbt);
171 /* PPQ = pulse per quarter
172 * VST's "pulse" is our "division".
174 * 8 divisions per bar, 1 division = quarter, so 8 quarters per bar, ppq = 1
175 * 8 divisions per bar, 1 division = eighth, so 4 quarters per bar, ppq = 2
176 * 4 divisions per bar, 1 division = quarter, so 4 quarters per bar, ppq = 1
177 * 4 divisions per bar, 1 division = half, so 8 quarters per bar, ppq = 0.5
178 * 4 divisions per bar, 1 division = fifth, so (4 * 5/4) quarters per bar, ppq = 5/4
180 * general: divs_per_bar / (note_type / 4.0)
182 double ppq_scaling = tm.meter().note_divisor() / 4.0;
184 /* Note that this assumes constant meter/tempo throughout the session. Stupid VST */
185 double ppqBar = double(bbt.bars - 1) * tm.meter().divisions_per_bar();
186 double ppqBeat = double(bbt.beats - 1);
187 double ppqTick = double(bbt.ticks) / Timecode::BBT_Time::ticks_per_beat;
189 ppqBar *= ppq_scaling;
190 ppqBeat *= ppq_scaling;
191 ppqTick *= ppq_scaling;
193 if (value & (kVstPpqPosValid)) {
194 _timeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
195 _timeInfo.flags |= (kVstPpqPosValid);
198 if (value & (kVstBarsValid)) {
199 _timeInfo.barStartPos = ppqBar;
200 _timeInfo.flags |= (kVstBarsValid);
208 if (value & (kVstSmpteValid)) {
211 session->timecode_time (now, t);
213 _timeInfo.smpteOffset = (t.hours * t.rate * 60.0 * 60.0) +
214 (t.minutes * t.rate * 60.0) +
215 (t.seconds * t.rate) +
219 _timeInfo.smpteOffset *= 80.0; /* VST spec is 1/80th frames */
221 if (session->timecode_drop_frames()) {
222 if (session->timecode_frames_per_second() == 30.0) {
223 _timeInfo.smpteFrameRate = 5;
225 _timeInfo.smpteFrameRate = 4; /* 29.97 assumed, thanks VST */
228 if (session->timecode_frames_per_second() == 24.0) {
229 _timeInfo.smpteFrameRate = 0;
230 } else if (session->timecode_frames_per_second() == 24.975) {
231 _timeInfo.smpteFrameRate = 2;
232 } else if (session->timecode_frames_per_second() == 25.0) {
233 _timeInfo.smpteFrameRate = 1;
235 _timeInfo.smpteFrameRate = 3; /* 30 fps */
238 _timeInfo.flags |= (kVstSmpteValid);
241 if (session->transport_speed() != 0.0f) {
242 _timeInfo.flags |= (kVstTransportPlaying);
245 if (session->get_play_loop()) {
249 _timeInfo.samplePos = 0;
250 _timeInfo.sampleRate = AudioEngine::instance()->sample_rate();
253 return (intptr_t) &_timeInfo;
255 case audioMasterProcessEvents:
256 SHOW_CALLBACK ("amc: audioMasterProcessEvents\n");
257 // VstEvents* in <ptr>
258 if (plug && plug->midi_buffer()) {
259 VstEvents* v = (VstEvents*)ptr;
260 for (int n = 0 ; n < v->numEvents; ++n) {
261 VstMidiEvent *vme = (VstMidiEvent*) (v->events[n]->dump);
262 if (vme->type == kVstMidiType) {
263 plug->midi_buffer()->push_back(vme->deltaFrames, 3, (uint8_t*)vme->midiData);
269 case audioMasterSetTime:
270 SHOW_CALLBACK ("amc: audioMasterSetTime\n");
271 // VstTimenfo* in <ptr>, filter in <value>, not supported
273 case audioMasterTempoAt:
274 SHOW_CALLBACK ("amc: audioMasterTempoAt\n");
275 // returns tempo (in bpm * 10000) at sample frame location passed in <value>
277 const Tempo& t (session->tempo_map().tempo_at (value));
278 return t.beats_per_minute() * 1000;
284 case audioMasterGetNumAutomatableParameters:
285 SHOW_CALLBACK ("amc: audioMasterGetNumAutomatableParameters\n");
288 case audioMasterGetParameterQuantization:
289 SHOW_CALLBACK ("amc: audioMasterGetParameterQuantization\n");
290 // returns the integer value for +1.0 representation,
291 // or 1 if full single float precision is maintained
292 // in automation. parameter index in <value> (-1: all, any)
295 case audioMasterIOChanged:
296 SHOW_CALLBACK ("amc: audioMasterIOChanged\n");
297 // numInputs and/or numOutputs has changed
300 case audioMasterNeedIdle:
301 SHOW_CALLBACK ("amc: audioMasterNeedIdle\n");
302 // plug needs idle calls (outside its editor window)
304 plug->state()->wantIdle = 1;
308 case audioMasterSizeWindow:
309 SHOW_CALLBACK ("amc: audioMasterSizeWindow\n");
310 // index: width, value: height
313 case audioMasterGetSampleRate:
314 SHOW_CALLBACK ("amc: audioMasterGetSampleRate\n");
316 return session->frame_rate();
320 case audioMasterGetBlockSize:
321 SHOW_CALLBACK ("amc: audioMasterGetBlockSize\n");
323 return session->get_block_size();
327 case audioMasterGetInputLatency:
328 SHOW_CALLBACK ("amc: audioMasterGetInputLatency\n");
331 case audioMasterGetOutputLatency:
332 SHOW_CALLBACK ("amc: audioMasterGetOutputLatency\n");
335 case audioMasterGetPreviousPlug:
336 SHOW_CALLBACK ("amc: audioMasterGetPreviousPlug\n");
337 // input pin in <value> (-1: first to come), returns cEffect*
340 case audioMasterGetNextPlug:
341 SHOW_CALLBACK ("amc: audioMasterGetNextPlug\n");
342 // output pin in <value> (-1: first to come), returns cEffect*
344 case audioMasterWillReplaceOrAccumulate:
345 SHOW_CALLBACK ("amc: audioMasterWillReplaceOrAccumulate\n");
346 // returns: 0: not supported, 1: replace, 2: accumulate
349 case audioMasterGetCurrentProcessLevel:
350 SHOW_CALLBACK ("amc: audioMasterGetCurrentProcessLevel\n");
351 // returns: 0: not supported,
352 // 1: currently in user thread (gui)
353 // 2: currently in audio thread (where process is called)
354 // 3: currently in 'sequencer' thread (midi, timer etc)
355 // 4: currently offline processing and thus in user thread
356 // other: not defined, but probably pre-empting user thread.
359 case audioMasterGetAutomationState:
360 SHOW_CALLBACK ("amc: audioMasterGetAutomationState\n");
361 // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
365 case audioMasterOfflineStart:
366 SHOW_CALLBACK ("amc: audioMasterOfflineStart\n");
369 case audioMasterOfflineRead:
370 SHOW_CALLBACK ("amc: audioMasterOfflineRead\n");
371 // ptr points to offline structure, see below. return 0: error, 1 ok
374 case audioMasterOfflineWrite:
375 SHOW_CALLBACK ("amc: audioMasterOfflineWrite\n");
379 case audioMasterOfflineGetCurrentPass:
380 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentPass\n");
383 case audioMasterOfflineGetCurrentMetaPass:
384 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentMetaPass\n");
387 case audioMasterSetOutputSampleRate:
388 SHOW_CALLBACK ("amc: audioMasterSetOutputSampleRate\n");
389 // for variable i/o, sample rate in <opt>
392 case audioMasterGetSpeakerArrangement:
393 SHOW_CALLBACK ("amc: audioMasterGetSpeakerArrangement\n");
394 // (long)input in <value>, output in <ptr>
397 case audioMasterGetVendorString:
398 SHOW_CALLBACK ("amc: audioMasterGetVendorString\n");
399 // fills <ptr> with a string identifying the vendor (max 64 char)
400 strcpy ((char*) ptr, "Linux Audio Systems");
403 case audioMasterGetProductString:
404 SHOW_CALLBACK ("amc: audioMasterGetProductString\n");
405 // fills <ptr> with a string with product name (max 64 char)
406 strcpy ((char*) ptr, PROGRAM_NAME);
409 case audioMasterGetVendorVersion:
410 SHOW_CALLBACK ("amc: audioMasterGetVendorVersion\n");
411 // returns vendor-specific version
414 case audioMasterVendorSpecific:
415 SHOW_CALLBACK ("amc: audioMasterVendorSpecific\n");
416 // no definition, vendor specific handling
419 case audioMasterSetIcon:
420 SHOW_CALLBACK ("amc: audioMasterSetIcon\n");
421 // void* in <ptr>, format not defined yet
424 case audioMasterCanDo:
425 SHOW_CALLBACK ("amc: audioMasterCanDo\n");
426 // string in ptr, (const char*)ptr
427 for (int i = 0; i < vst_can_do_string_count; i++) {
428 if (! strcmp(vst_can_do_strings[i], (const char*)ptr)) {
434 case audioMasterGetLanguage:
435 SHOW_CALLBACK ("amc: audioMasterGetLanguage\n");
439 case audioMasterOpenWindow:
440 SHOW_CALLBACK ("amc: audioMasterOpenWindow\n");
441 // returns platform specific ptr
444 case audioMasterCloseWindow:
445 SHOW_CALLBACK ("amc: audioMasterCloseWindow\n");
446 // close window, platform specific handle in <ptr>
449 case audioMasterGetDirectory:
450 SHOW_CALLBACK ("amc: audioMasterGetDirectory\n");
451 // get plug directory, FSSpec on MAC, else char*
454 case audioMasterUpdateDisplay:
455 SHOW_CALLBACK ("amc: audioMasterUpdateDisplay\n");
456 // something has changed, update 'multi-fx' display
458 effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
462 case audioMasterBeginEdit:
463 SHOW_CALLBACK ("amc: audioMasterBeginEdit\n");
464 // begin of automation session (when mouse down), parameter index in <index>
467 case audioMasterEndEdit:
468 SHOW_CALLBACK ("amc: audioMasterEndEdit\n");
469 // end of automation session (when mouse up), parameter index in <index>
472 case audioMasterOpenFileSelector:
473 SHOW_CALLBACK ("amc: audioMasterOpenFileSelector\n");
474 // open a fileselector window with VstFileSelect* in <ptr>
478 SHOW_CALLBACK ("VST master dispatcher: undefed: %d\n", opcode);