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/debug.h"
27 #include "ardour/session.h"
28 #include "ardour/tempo.h"
29 #include "ardour/plugin_insert.h"
30 #include "ardour/windows_vst_plugin.h"
31 #include "ardour/vestige/aeffectx.h"
32 #include "ardour/vst_types.h"
33 #ifdef WINDOWS_VST_SUPPORT
39 using namespace ARDOUR;
41 #define SHOW_CALLBACK(MSG) DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose (MSG " val = %1 idx = %2\n", index, value))
44 vst_ppq (const TempoMetric& tm, const Timecode::BBT_Time& bbt, double& ppqBar)
47 /* PPQ = pulse per quarter
48 * VST's "pulse" is our "division".
50 * 8 divisions per bar, 1 division = quarter, so 8 quarters per bar, ppq = 1
51 * 8 divisions per bar, 1 division = eighth, so 4 quarters per bar, ppq = 2
52 * 4 divisions per bar, 1 division = quarter, so 4 quarters per bar, ppq = 1
53 * 4 divisions per bar, 1 division = half, so 8 quarters per bar, ppq = 0.5
54 * 4 divisions per bar, 1 division = fifth, so (4 * 5/4) quarters per bar, ppq = 5/4
56 * general: divs_per_bar / (note_type / 4.0)
58 const double ppq_scaling = tm.meter().note_divisor() / 4.0;
60 /* Note that this assumes constant meter/tempo throughout the session. Stupid VST */
61 ppqBar = double(bbt.bars - 1) * tm.meter().divisions_per_bar();
62 double ppqBeat = double(bbt.beats - 1);
63 double ppqTick = double(bbt.ticks) / Timecode::BBT_Time::ticks_per_beat;
65 ppqBar *= ppq_scaling;
66 ppqBeat *= ppq_scaling;
67 ppqTick *= ppq_scaling;
69 return ppqBar + ppqBeat + ppqTick;
72 int Session::vst_current_loading_id = 0;
73 const char* Session::vst_can_do_strings[] = {
75 X_("sendVstTimeInfo"),
77 X_("sendVstMidiEvent"),
78 X_("receiveVstEvents"),
79 X_("receiveVstMidiEvent"),
82 X_("shellCategorycurID")
84 const int Session::vst_can_do_string_count = sizeof (vst_can_do_strings) / sizeof (char*);
86 intptr_t Session::vst_callback (
97 static VstTimeInfo _timeinfo; // only uses as fallback
98 VstTimeInfo* timeinfo;
101 if (effect && effect->user) {
102 plug = (VSTPlugin *) (effect->user);
103 session = &plug->session();
104 timeinfo = plug->timeinfo ();
105 DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("am callback 0x%1%2, opcode = %3%4, plugin = \"%5\"\n",
106 std::hex, (void*) DEBUG_THREAD_SELF,
107 std::dec, opcode, plug->name()));
111 timeinfo = &_timeinfo;
112 DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("am callback 0x%1%2, opcode = %3%4\n",
113 std::hex, (void*) DEBUG_THREAD_SELF,
119 case audioMasterAutomate:
120 SHOW_CALLBACK ("audioMasterAutomate");
121 // index, value, returns 0
123 plug->parameter_changed_externally (index, opt);
127 case audioMasterVersion:
128 SHOW_CALLBACK ("audioMasterVersion");
129 // vst version, currently 2 (0 for older)
132 case audioMasterCurrentId:
133 SHOW_CALLBACK ("audioMasterCurrentId");
134 // returns the unique id of a plug that's currently loading
135 return vst_current_loading_id;
137 case audioMasterIdle:
138 SHOW_CALLBACK ("audioMasterIdle");
139 #ifdef WINDOWS_VST_SUPPORT
140 fst_audio_master_idle();
143 effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
147 case audioMasterPinConnected:
148 SHOW_CALLBACK ("audioMasterPinConnected");
149 // inquire if an input or output is beeing connected;
150 // index enumerates input or output counting from zero:
151 // value is 0 for input and != 0 otherwise. note: the
152 // return value is 0 for <true> such that older versions
153 // will always return true.
156 // but ardour always connects all buffers, so we're good
161 if (plug->plugin_insert ()) {
163 const ChanMapping& map (plug->plugin_insert ()->input_map (plug->plugin_number ()));
164 map.get (DataType::AUDIO, index, &valid);
165 return valid ? 0 : 1;
167 if (index < plug->plugin()->numInputs) {
172 #if 0 // investigate, the outputs *are* connected to scratch buffers
173 if (plug->plugin_insert ()) {
175 const ChanMapping& map (plug->plugin_insert ()->output_map (plug->plugin_number ()));
176 map.get (DataType::AUDIO, index, &valid);
177 return valid ? 0 : 1;
180 if (index < plug->plugin()->numOutputs) {
189 case audioMasterWantMidi:
190 SHOW_CALLBACK ("audioMasterWantMidi");
191 // <value> is a filter which is currently ignored
192 if (plug && plug->get_info() != NULL) {
193 plug->get_info()->n_inputs.set_midi (1);
197 case audioMasterGetTime:
198 SHOW_CALLBACK ("audioMasterGetTime");
199 newflags = kVstNanosValid | kVstAutomationWriting | kVstAutomationReading;
201 timeinfo->nanoSeconds = g_get_monotonic_time () * 1000;
203 if (plug && session) {
204 framepos_t now = plug->transport_frame();
206 timeinfo->samplePos = now;
207 timeinfo->sampleRate = session->frame_rate();
209 if (value & (kVstTempoValid)) {
210 const Tempo& t (session->tempo_map().tempo_at_frame (now));
211 timeinfo->tempo = t.quarter_notes_per_minute ();
212 newflags |= (kVstTempoValid);
214 if (value & (kVstTimeSigValid)) {
215 const MeterSection& ms (session->tempo_map().meter_section_at_frame (now));
216 timeinfo->timeSigNumerator = ms.divisions_per_bar ();
217 timeinfo->timeSigDenominator = ms.note_divisor ();
218 newflags |= (kVstTimeSigValid);
220 if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
221 Timecode::BBT_Time bbt;
224 bbt = session->tempo_map().bbt_at_frame_rt (now);
227 /* exact quarter note */
228 double ppqBar = session->tempo_map().quarter_note_at_bbt_rt (bbt);
229 /* quarter note at frame position (not rounded to note subdivision) */
230 double ppqPos = session->tempo_map().quarter_note_at_frame_rt (now);
231 if (value & (kVstPpqPosValid)) {
232 timeinfo->ppqPos = ppqPos;
233 newflags |= kVstPpqPosValid;
236 if (value & (kVstBarsValid)) {
237 timeinfo->barStartPos = ppqBar;
238 newflags |= kVstBarsValid;
246 if (value & (kVstSmpteValid)) {
249 session->timecode_time (now, t);
251 timeinfo->smpteOffset = (t.hours * t.rate * 60.0 * 60.0) +
252 (t.minutes * t.rate * 60.0) +
253 (t.seconds * t.rate) +
257 timeinfo->smpteOffset *= 80.0; /* VST spec is 1/80th frames */
259 if (session->timecode_drop_frames()) {
260 if (session->timecode_frames_per_second() == 30.0) {
261 timeinfo->smpteFrameRate = 5;
263 timeinfo->smpteFrameRate = 4; /* 29.97 assumed, thanks VST */
266 if (session->timecode_frames_per_second() == 24.0) {
267 timeinfo->smpteFrameRate = 0;
268 } else if (session->timecode_frames_per_second() == 24.975) {
269 timeinfo->smpteFrameRate = 2;
270 } else if (session->timecode_frames_per_second() == 25.0) {
271 timeinfo->smpteFrameRate = 1;
273 timeinfo->smpteFrameRate = 3; /* 30 fps */
276 newflags |= (kVstSmpteValid);
279 if (session->actively_recording ()) {
280 newflags |= kVstTransportRecording;
283 if (plug->transport_speed () != 0.0f) {
284 newflags |= kVstTransportPlaying;
287 if (session->get_play_loop ()) {
288 newflags |= kVstTransportCycleActive;
289 Location * looploc = session->locations ()->auto_loop_location ();
291 timeinfo->cycleStartPos = session->tempo_map ().quarter_note_at_frame_rt (looploc->start ());
292 timeinfo->cycleEndPos = session->tempo_map ().quarter_note_at_frame_rt (looploc->end ());
294 newflags |= kVstCyclePosValid;
299 timeinfo->samplePos = 0;
300 timeinfo->sampleRate = AudioEngine::instance()->sample_rate();
303 if ((timeinfo->flags & (kVstTransportPlaying | kVstTransportRecording | kVstTransportCycleActive))
305 (newflags & (kVstTransportPlaying | kVstTransportRecording | kVstTransportCycleActive)))
307 newflags |= kVstTransportChanged;
310 timeinfo->flags = newflags;
311 return (intptr_t) timeinfo;
313 case audioMasterProcessEvents:
314 SHOW_CALLBACK ("audioMasterProcessEvents");
315 // VstEvents* in <ptr>
316 if (plug && plug->midi_buffer()) {
317 VstEvents* v = (VstEvents*)ptr;
318 for (int n = 0 ; n < v->numEvents; ++n) {
319 VstMidiEvent *vme = (VstMidiEvent*) (v->events[n]->dump);
320 if (vme->type == kVstMidiType) {
321 plug->midi_buffer()->push_back(vme->deltaFrames, 3, (uint8_t*)vme->midiData);
327 case audioMasterSetTime:
328 SHOW_CALLBACK ("audioMasterSetTime");
329 // VstTimenfo* in <ptr>, filter in <value>, not supported
331 case audioMasterTempoAt:
332 SHOW_CALLBACK ("audioMasterTempoAt");
333 // returns tempo (in bpm * 10000) at sample frame location passed in <value>
335 const Tempo& t (session->tempo_map().tempo_at_frame (value));
336 return t.quarter_notes_per_minute() * 1000;
342 case audioMasterGetNumAutomatableParameters:
343 SHOW_CALLBACK ("audioMasterGetNumAutomatableParameters");
346 case audioMasterGetParameterQuantization:
347 SHOW_CALLBACK ("audioMasterGetParameterQuantization");
348 // returns the integer value for +1.0 representation,
349 // or 1 if full single float precision is maintained
350 // in automation. parameter index in <value> (-1: all, any)
353 case audioMasterIOChanged:
354 SHOW_CALLBACK ("audioMasterIOChanged");
355 // numInputs and/or numOutputs has changed
358 case audioMasterNeedIdle:
359 SHOW_CALLBACK ("audioMasterNeedIdle");
360 // plug needs idle calls (outside its editor window)
362 plug->state()->wantIdle = 1;
366 case audioMasterSizeWindow:
367 SHOW_CALLBACK ("audioMasterSizeWindow");
368 if (plug && plug->state()) {
369 plug->state()->width = index;
370 plug->state()->height = value;
371 plug->state()->want_resize = 1;
375 case audioMasterGetSampleRate:
376 SHOW_CALLBACK ("audioMasterGetSampleRate");
378 return session->frame_rate();
382 case audioMasterGetBlockSize:
383 SHOW_CALLBACK ("audioMasterGetBlockSize");
385 return session->get_block_size();
389 case audioMasterGetInputLatency:
390 SHOW_CALLBACK ("audioMasterGetInputLatency");
393 case audioMasterGetOutputLatency:
394 SHOW_CALLBACK ("audioMasterGetOutputLatency");
397 case audioMasterGetPreviousPlug:
398 SHOW_CALLBACK ("audioMasterGetPreviousPlug");
399 // input pin in <value> (-1: first to come), returns cEffect*
402 case audioMasterGetNextPlug:
403 SHOW_CALLBACK ("audioMasterGetNextPlug");
404 // output pin in <value> (-1: first to come), returns cEffect*
406 case audioMasterWillReplaceOrAccumulate:
407 SHOW_CALLBACK ("audioMasterWillReplaceOrAccumulate");
408 // returns: 0: not supported, 1: replace, 2: accumulate
411 case audioMasterGetCurrentProcessLevel:
412 SHOW_CALLBACK ("audioMasterGetCurrentProcessLevel");
413 // returns: 0: not supported,
414 // 1: currently in user thread (gui)
415 // 2: currently in audio thread (where process is called)
416 // 3: currently in 'sequencer' thread (midi, timer etc)
417 // 4: currently offline processing and thus in user thread
418 // other: not defined, but probably pre-empting user thread.
421 case audioMasterGetAutomationState:
422 SHOW_CALLBACK ("audioMasterGetAutomationState");
423 // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
427 case audioMasterOfflineStart:
428 SHOW_CALLBACK ("audioMasterOfflineStart");
431 case audioMasterOfflineRead:
432 SHOW_CALLBACK ("audioMasterOfflineRead");
433 // ptr points to offline structure, see below. return 0: error, 1 ok
436 case audioMasterOfflineWrite:
437 SHOW_CALLBACK ("audioMasterOfflineWrite");
441 case audioMasterOfflineGetCurrentPass:
442 SHOW_CALLBACK ("audioMasterOfflineGetCurrentPass");
445 case audioMasterOfflineGetCurrentMetaPass:
446 SHOW_CALLBACK ("audioMasterOfflineGetCurrentMetaPass");
449 case audioMasterSetOutputSampleRate:
450 SHOW_CALLBACK ("audioMasterSetOutputSampleRate");
451 // for variable i/o, sample rate in <opt>
454 case audioMasterGetSpeakerArrangement:
455 SHOW_CALLBACK ("audioMasterGetSpeakerArrangement");
456 // (long)input in <value>, output in <ptr>
459 case audioMasterGetVendorString:
460 SHOW_CALLBACK ("audioMasterGetVendorString");
461 // fills <ptr> with a string identifying the vendor (max 64 char)
462 strcpy ((char*) ptr, "Linux Audio Systems");
465 case audioMasterGetProductString:
466 SHOW_CALLBACK ("audioMasterGetProductString");
467 // fills <ptr> with a string with product name (max 64 char)
468 strcpy ((char*) ptr, PROGRAM_NAME);
471 case audioMasterGetVendorVersion:
472 SHOW_CALLBACK ("audioMasterGetVendorVersion");
473 // returns vendor-specific version
476 case audioMasterVendorSpecific:
477 SHOW_CALLBACK ("audioMasterVendorSpecific");
478 // no definition, vendor specific handling
481 case audioMasterSetIcon:
482 SHOW_CALLBACK ("audioMasterSetIcon");
483 // void* in <ptr>, format not defined yet
486 case audioMasterCanDo:
487 SHOW_CALLBACK ("audioMasterCanDo");
488 // string in ptr, (const char*)ptr
489 for (int i = 0; i < vst_can_do_string_count; i++) {
490 if (! strcmp(vst_can_do_strings[i], (const char*)ptr)) {
496 case audioMasterGetLanguage:
497 SHOW_CALLBACK ("audioMasterGetLanguage");
501 case audioMasterOpenWindow:
502 SHOW_CALLBACK ("audioMasterOpenWindow");
503 // returns platform specific ptr
506 case audioMasterCloseWindow:
507 SHOW_CALLBACK ("audioMasterCloseWindow");
508 // close window, platform specific handle in <ptr>
511 case audioMasterGetDirectory:
512 SHOW_CALLBACK ("audioMasterGetDirectory");
513 // get plug directory, FSSpec on MAC, else char*
516 case audioMasterUpdateDisplay:
517 SHOW_CALLBACK ("audioMasterUpdateDisplay");
518 // something has changed, update 'multi-fx' display
520 effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
524 case audioMasterBeginEdit:
525 SHOW_CALLBACK ("audioMasterBeginEdit");
526 // begin of automation session (when mouse down), parameter index in <index>
529 case audioMasterEndEdit:
530 SHOW_CALLBACK ("audioMasterEndEdit");
531 // end of automation session (when mouse up), parameter index in <index>
534 case audioMasterOpenFileSelector:
535 SHOW_CALLBACK ("audioMasterOpenFileSelector");
536 // open a fileselector window with VstFileSelect* in <ptr>
540 DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("VST master dispatcher: undefed: %1\n", opcode));