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"),
57 const int Session::vst_can_do_string_count = sizeof (vst_can_do_strings) / sizeof (char*);
59 intptr_t Session::vst_callback (
68 static VstTimeInfo _timeInfo;
72 if (debug_callbacks < 0) {
73 debug_callbacks = (getenv ("ARDOUR_DEBUG_VST_CALLBACKS") != 0);
76 if (effect && effect->user) {
77 plug = (VSTPlugin *) (effect->user);
78 session = &plug->session();
80 SHOW_CALLBACK ("am callback 0x%x, opcode = %d, plugin = \"%s\" ", (int) pthread_self().p, opcode, plug->name());
82 SHOW_CALLBACK ("am callback 0x%x, opcode = %d, plugin = \"%s\" ", (int) pthread_self(), opcode, plug->name());
88 SHOW_CALLBACK ("am callback 0x%x, opcode = %d", (int) pthread_self().p, opcode);
90 SHOW_CALLBACK ("am callback 0x%x, opcode = %d", (int) pthread_self(), opcode);
96 case audioMasterAutomate:
97 SHOW_CALLBACK ("amc: audioMasterAutomate\n");
98 // index, value, returns 0
100 plug->set_parameter (index, opt);
104 case audioMasterVersion:
105 SHOW_CALLBACK ("amc: audioMasterVersion\n");
106 // vst version, currently 2 (0 for older)
109 case audioMasterCurrentId:
110 SHOW_CALLBACK ("amc: audioMasterCurrentId\n");
111 // returns the unique id of a plug that's currently loading
112 return vst_current_loading_id;
114 case audioMasterIdle:
115 SHOW_CALLBACK ("amc: audioMasterIdle\n");
116 #ifdef WINDOWS_VST_SUPPORT
117 fst_audio_master_idle();
120 effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
124 case audioMasterPinConnected:
125 SHOW_CALLBACK ("amc: audioMasterPinConnected\n");
126 // inquire if an input or output is beeing connected;
127 // index enumerates input or output counting from zero:
128 // value is 0 for input and != 0 otherwise. note: the
129 // return value is 0 for <true> such that older versions
130 // will always return true.
133 case audioMasterWantMidi:
134 SHOW_CALLBACK ("amc: audioMasterWantMidi\n");
135 // <value> is a filter which is currently ignored
137 plug->get_info()->n_inputs.set_midi (1);
141 case audioMasterGetTime:
142 SHOW_CALLBACK ("amc: audioMasterGetTime\n");
143 // returns const VstTimeInfo* (or 0 if not supported)
144 // <value> should contain a mask indicating which fields are required
145 // (see valid masks above), as some items may require extensive
150 framepos_t now = session->transport_frame();
152 _timeInfo.samplePos = now;
153 _timeInfo.sampleRate = session->frame_rate();
155 const TempoMetric& tm (session->tempo_map().metric_at (now));
157 if (value & (kVstTempoValid)) {
158 const Tempo& t (tm.tempo());
159 _timeInfo.tempo = t.beats_per_minute ();
160 _timeInfo.flags |= (kVstTempoValid);
162 if (value & (kVstTimeSigValid)) {
163 const Meter& m (tm.meter());
164 _timeInfo.timeSigNumerator = m.divisions_per_bar ();
165 _timeInfo.timeSigDenominator = m.note_divisor ();
166 _timeInfo.flags |= (kVstTimeSigValid);
168 if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
169 Timecode::BBT_Time bbt;
172 session->tempo_map().bbt_time_rt (now, bbt);
174 /* PPQ = pulse per quarter
175 * VST's "pulse" is our "division".
177 * 8 divisions per bar, 1 division = quarter, so 8 quarters per bar, ppq = 1
178 * 8 divisions per bar, 1 division = eighth, so 4 quarters per bar, ppq = 2
179 * 4 divisions per bar, 1 division = quarter, so 4 quarters per bar, ppq = 1
180 * 4 divisions per bar, 1 division = half, so 8 quarters per bar, ppq = 0.5
181 * 4 divisions per bar, 1 division = fifth, so (4 * 5/4) quarters per bar, ppq = 5/4
183 * general: divs_per_bar / (note_type / 4.0)
185 double ppq_scaling = tm.meter().note_divisor() / 4.0;
187 /* Note that this assumes constant meter/tempo throughout the session. Stupid VST */
188 double ppqBar = double(bbt.bars - 1) * tm.meter().divisions_per_bar();
189 double ppqBeat = double(bbt.beats - 1);
190 double ppqTick = double(bbt.ticks) / Timecode::BBT_Time::ticks_per_beat;
192 ppqBar *= ppq_scaling;
193 ppqBeat *= ppq_scaling;
194 ppqTick *= ppq_scaling;
196 if (value & (kVstPpqPosValid)) {
197 _timeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
198 _timeInfo.flags |= (kVstPpqPosValid);
201 if (value & (kVstBarsValid)) {
202 _timeInfo.barStartPos = ppqBar;
203 _timeInfo.flags |= (kVstBarsValid);
211 if (value & (kVstSmpteValid)) {
214 session->timecode_time (now, t);
216 _timeInfo.smpteOffset = (t.hours * t.rate * 60.0 * 60.0) +
217 (t.minutes * t.rate * 60.0) +
218 (t.seconds * t.rate) +
222 _timeInfo.smpteOffset *= 80.0; /* VST spec is 1/80th frames */
224 if (session->timecode_drop_frames()) {
225 if (session->timecode_frames_per_second() == 30.0) {
226 _timeInfo.smpteFrameRate = 5;
228 _timeInfo.smpteFrameRate = 4; /* 29.97 assumed, thanks VST */
231 if (session->timecode_frames_per_second() == 24.0) {
232 _timeInfo.smpteFrameRate = 0;
233 } else if (session->timecode_frames_per_second() == 24.975) {
234 _timeInfo.smpteFrameRate = 2;
235 } else if (session->timecode_frames_per_second() == 25.0) {
236 _timeInfo.smpteFrameRate = 1;
238 _timeInfo.smpteFrameRate = 3; /* 30 fps */
241 _timeInfo.flags |= (kVstSmpteValid);
244 if (session->transport_speed() != 0.0f) {
245 _timeInfo.flags |= (kVstTransportPlaying);
248 if (session->get_play_loop()) {
252 _timeInfo.samplePos = 0;
253 _timeInfo.sampleRate = AudioEngine::instance()->sample_rate();
256 return (intptr_t) &_timeInfo;
258 case audioMasterProcessEvents:
259 SHOW_CALLBACK ("amc: audioMasterProcessEvents\n");
260 // VstEvents* in <ptr>
261 if (plug->midi_buffer()) {
262 VstEvents* v = (VstEvents*)ptr;
263 for (int n = 0 ; n < v->numEvents; ++n) {
264 VstMidiEvent *vme = (VstMidiEvent*) (v->events[n]->dump);
265 if (vme->type == kVstMidiType) {
266 plug->midi_buffer()->push_back(vme->deltaFrames, 3, (uint8_t*)vme->midiData);
272 case audioMasterSetTime:
273 SHOW_CALLBACK ("amc: audioMasterSetTime\n");
274 // VstTimenfo* in <ptr>, filter in <value>, not supported
276 case audioMasterTempoAt:
277 SHOW_CALLBACK ("amc: audioMasterTempoAt\n");
278 // returns tempo (in bpm * 10000) at sample frame location passed in <value>
280 const Tempo& t (session->tempo_map().tempo_at (value));
281 return t.beats_per_minute() * 1000;
287 case audioMasterGetNumAutomatableParameters:
288 SHOW_CALLBACK ("amc: audioMasterGetNumAutomatableParameters\n");
291 case audioMasterGetParameterQuantization:
292 SHOW_CALLBACK ("amc: audioMasterGetParameterQuantization\n");
293 // returns the integer value for +1.0 representation,
294 // or 1 if full single float precision is maintained
295 // in automation. parameter index in <value> (-1: all, any)
298 case audioMasterIOChanged:
299 SHOW_CALLBACK ("amc: audioMasterIOChanged\n");
300 // numInputs and/or numOutputs has changed
303 case audioMasterNeedIdle:
304 SHOW_CALLBACK ("amc: audioMasterNeedIdle\n");
305 // plug needs idle calls (outside its editor window)
307 plug->state()->wantIdle = 1;
311 case audioMasterSizeWindow:
312 SHOW_CALLBACK ("amc: audioMasterSizeWindow\n");
313 // index: width, value: height
316 case audioMasterGetSampleRate:
317 SHOW_CALLBACK ("amc: audioMasterGetSampleRate\n");
319 return session->frame_rate();
323 case audioMasterGetBlockSize:
324 SHOW_CALLBACK ("amc: audioMasterGetBlockSize\n");
326 return session->get_block_size();
330 case audioMasterGetInputLatency:
331 SHOW_CALLBACK ("amc: audioMasterGetInputLatency\n");
334 case audioMasterGetOutputLatency:
335 SHOW_CALLBACK ("amc: audioMasterGetOutputLatency\n");
338 case audioMasterGetPreviousPlug:
339 SHOW_CALLBACK ("amc: audioMasterGetPreviousPlug\n");
340 // input pin in <value> (-1: first to come), returns cEffect*
343 case audioMasterGetNextPlug:
344 SHOW_CALLBACK ("amc: audioMasterGetNextPlug\n");
345 // output pin in <value> (-1: first to come), returns cEffect*
347 case audioMasterWillReplaceOrAccumulate:
348 SHOW_CALLBACK ("amc: audioMasterWillReplaceOrAccumulate\n");
349 // returns: 0: not supported, 1: replace, 2: accumulate
352 case audioMasterGetCurrentProcessLevel:
353 SHOW_CALLBACK ("amc: audioMasterGetCurrentProcessLevel\n");
354 // returns: 0: not supported,
355 // 1: currently in user thread (gui)
356 // 2: currently in audio thread (where process is called)
357 // 3: currently in 'sequencer' thread (midi, timer etc)
358 // 4: currently offline processing and thus in user thread
359 // other: not defined, but probably pre-empting user thread.
362 case audioMasterGetAutomationState:
363 SHOW_CALLBACK ("amc: audioMasterGetAutomationState\n");
364 // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
368 case audioMasterOfflineStart:
369 SHOW_CALLBACK ("amc: audioMasterOfflineStart\n");
372 case audioMasterOfflineRead:
373 SHOW_CALLBACK ("amc: audioMasterOfflineRead\n");
374 // ptr points to offline structure, see below. return 0: error, 1 ok
377 case audioMasterOfflineWrite:
378 SHOW_CALLBACK ("amc: audioMasterOfflineWrite\n");
382 case audioMasterOfflineGetCurrentPass:
383 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentPass\n");
386 case audioMasterOfflineGetCurrentMetaPass:
387 SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentMetaPass\n");
390 case audioMasterSetOutputSampleRate:
391 SHOW_CALLBACK ("amc: audioMasterSetOutputSampleRate\n");
392 // for variable i/o, sample rate in <opt>
395 case audioMasterGetSpeakerArrangement:
396 SHOW_CALLBACK ("amc: audioMasterGetSpeakerArrangement\n");
397 // (long)input in <value>, output in <ptr>
400 case audioMasterGetVendorString:
401 SHOW_CALLBACK ("amc: audioMasterGetVendorString\n");
402 // fills <ptr> with a string identifying the vendor (max 64 char)
403 strcpy ((char*) ptr, "Linux Audio Systems");
406 case audioMasterGetProductString:
407 SHOW_CALLBACK ("amc: audioMasterGetProductString\n");
408 // fills <ptr> with a string with product name (max 64 char)
409 strcpy ((char*) ptr, PROGRAM_NAME);
412 case audioMasterGetVendorVersion:
413 SHOW_CALLBACK ("amc: audioMasterGetVendorVersion\n");
414 // returns vendor-specific version
417 case audioMasterVendorSpecific:
418 SHOW_CALLBACK ("amc: audioMasterVendorSpecific\n");
419 // no definition, vendor specific handling
422 case audioMasterSetIcon:
423 SHOW_CALLBACK ("amc: audioMasterSetIcon\n");
424 // void* in <ptr>, format not defined yet
427 case audioMasterCanDo:
428 SHOW_CALLBACK ("amc: audioMasterCanDo\n");
429 // string in ptr, (const char*)ptr
430 for (int i = 0; i < vst_can_do_string_count; i++) {
431 if (! strcmp(vst_can_do_strings[i], (const char*)ptr)) {
437 case audioMasterGetLanguage:
438 SHOW_CALLBACK ("amc: audioMasterGetLanguage\n");
442 case audioMasterOpenWindow:
443 SHOW_CALLBACK ("amc: audioMasterOpenWindow\n");
444 // returns platform specific ptr
447 case audioMasterCloseWindow:
448 SHOW_CALLBACK ("amc: audioMasterCloseWindow\n");
449 // close window, platform specific handle in <ptr>
452 case audioMasterGetDirectory:
453 SHOW_CALLBACK ("amc: audioMasterGetDirectory\n");
454 // get plug directory, FSSpec on MAC, else char*
457 case audioMasterUpdateDisplay:
458 SHOW_CALLBACK ("amc: audioMasterUpdateDisplay\n");
459 // something has changed, update 'multi-fx' display
461 effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
465 case audioMasterBeginEdit:
466 SHOW_CALLBACK ("amc: audioMasterBeginEdit\n");
467 // begin of automation session (when mouse down), parameter index in <index>
470 case audioMasterEndEdit:
471 SHOW_CALLBACK ("amc: audioMasterEndEdit\n");
472 // end of automation session (when mouse up), parameter index in <index>
475 case audioMasterOpenFileSelector:
476 SHOW_CALLBACK ("amc: audioMasterOpenFileSelector\n");
477 // open a fileselector window with VstFileSelect* in <ptr>
481 SHOW_CALLBACK ("VST master dispatcher: undefed: %d\n", opcode);