summaryrefslogtreecommitdiff
path: root/libs/ardour/session_vst.cc
blob: a6321b2031578106def6a29842ec28761734d512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
    Copyright (C) 2004

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef COMPILER_MSVC
#include <stdbool.h>
#endif
#include <cstdio>

#include "ardour/audioengine.h"
#include "ardour/debug.h"
#include "ardour/session.h"
#include "ardour/tempo.h"
#include "ardour/plugin_insert.h"
#include "ardour/windows_vst_plugin.h"
#include "ardour/vestige/aeffectx.h"
#include "ardour/vst_types.h"
#ifdef WINDOWS_VST_SUPPORT
#include <fst.h>
#endif

#include "pbd/i18n.h"

using namespace ARDOUR;

#define SHOW_CALLBACK(MSG) DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose (MSG " val = %1 idx = %2\n", index, value))

static double
vst_ppq (const TempoMetric& tm, const Timecode::BBT_Time& bbt, double& ppqBar)
{

	/* PPQ = pulse per quarter
	 * VST's "pulse" is our "division".
	 *
	 * 8 divisions per bar, 1 division = quarter, so 8 quarters per bar, ppq = 1
	 * 8 divisions per bar, 1 division = eighth, so  4 quarters per bar, ppq = 2
	 * 4 divisions per bar, 1 division = quarter, so  4 quarters per bar, ppq = 1
	 * 4 divisions per bar, 1 division = half, so 8 quarters per bar, ppq = 0.5
	 * 4 divisions per bar, 1 division = fifth, so (4 * 5/4) quarters per bar, ppq = 5/4
	 *
	 * general: divs_per_bar / (note_type / 4.0)
	 */
	const double ppq_scaling =  tm.meter().note_divisor() / 4.0;

	/* Note that this assumes constant meter/tempo throughout the session. Stupid VST */
	ppqBar = double(bbt.bars - 1) * tm.meter().divisions_per_bar();
	double ppqBeat = double(bbt.beats - 1);
	double ppqTick = double(bbt.ticks) / Timecode::BBT_Time::ticks_per_beat;

	ppqBar *= ppq_scaling;
	ppqBeat *= ppq_scaling;
	ppqTick *= ppq_scaling;

	return ppqBar + ppqBeat + ppqTick;
}

int Session::vst_current_loading_id = 0;
const char* Session::vst_can_do_strings[] = {
	X_("supplyIdle"),
	X_("sendVstTimeInfo"),
	X_("sendVstEvents"),
	X_("sendVstMidiEvent"),
	X_("receiveVstEvents"),
	X_("receiveVstMidiEvent"),
	X_("supportShell"),
	X_("shellCategory"),
	X_("shellCategorycurID")
};
const int Session::vst_can_do_string_count = sizeof (vst_can_do_strings) / sizeof (char*);

intptr_t Session::vst_callback (
	AEffect* effect,
	int32_t opcode,
	int32_t index,
	intptr_t value,
	void* ptr,
	float opt
	)
{
	VSTPlugin* plug;
	Session* session;
	static VstTimeInfo _timeinfo; // only uses as fallback
	VstTimeInfo* timeinfo;
	int32_t newflags = 0;

	if (effect && effect->user) {
		plug = (VSTPlugin *) (effect->user);
		session = &plug->session();
		timeinfo = plug->timeinfo ();
		DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("am callback 0x%1%2, opcode = %3%4, plugin = \"%5\"\n",
					std::hex, (void*) DEBUG_THREAD_SELF,
					std::dec, opcode, plug->name()));
	} else {
		plug = 0;
		session = 0;
		timeinfo = &_timeinfo;
		DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("am callback 0x%1%2, opcode = %3%4\n",
					std::hex, (void*) DEBUG_THREAD_SELF,
					std::dec, opcode));
	}

	switch(opcode){

	case audioMasterAutomate:
		SHOW_CALLBACK ("audioMasterAutomate");
		// index, value, returns 0
		if (plug) {
			plug->parameter_changed_externally (index, opt);
		}
		return 0;

	case audioMasterVersion:
		SHOW_CALLBACK ("audioMasterVersion");
		// vst version, currently 2 (0 for older)
		return 2400;

	case audioMasterCurrentId:
		SHOW_CALLBACK ("audioMasterCurrentId");
		// returns the unique id of a plug that's currently loading
		return vst_current_loading_id;

	case audioMasterIdle:
		SHOW_CALLBACK ("audioMasterIdle");
#ifdef WINDOWS_VST_SUPPORT
		fst_audio_master_idle();
#endif
		if (effect) {
			effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
		}
		return 0;

	case audioMasterPinConnected:
		SHOW_CALLBACK ("audioMasterPinConnected");
		// inquire if an input or output is beeing connected;
		// index enumerates input or output counting from zero:
		// value is 0 for input and != 0 otherwise. note: the
		// return value is 0 for <true> such that older versions
		// will always return true.
		if (!plug) {
			// we don't know.
			// but ardour always connects all buffers, so we're good
			return 0;
		}
		switch (value) {
			case 0:
				if (plug->plugin_insert ()) {
					bool valid;
					const ChanMapping& map (plug->plugin_insert ()->input_map (plug->plugin_number ()));
					map.get (DataType::AUDIO, index, &valid);
					return valid ? 0 : 1;
				}
				if (index < plug->plugin()->numInputs) {
					return 0;
				}
				break;
			case 1:
#if 0 // investigate, the outputs *are* connected to scratch buffers
				if (plug->plugin_insert ()) {
					bool valid;
					const ChanMapping& map (plug->plugin_insert ()->output_map (plug->plugin_number ()));
					map.get (DataType::AUDIO, index, &valid);
					return valid ? 0 : 1;
				}
#endif
				if (index < plug->plugin()->numOutputs) {
					return 0;
				}
				break;
			default:
				break;
		}
		return 1;

	case audioMasterWantMidi:
		SHOW_CALLBACK ("audioMasterWantMidi");
		// <value> is a filter which is currently ignored
		if (plug && plug->get_info() != NULL) {
			plug->get_info()->n_inputs.set_midi (1);
		}
		return 0;

	case audioMasterGetTime:
		SHOW_CALLBACK ("audioMasterGetTime");
		newflags = kVstNanosValid | kVstAutomationWriting | kVstAutomationReading;

		timeinfo->nanoSeconds = g_get_monotonic_time () * 1000;

		if (plug && session) {
			framepos_t now = plug->transport_frame();

			timeinfo->samplePos = now;
			timeinfo->sampleRate = session->frame_rate();

			if (value & (kVstTempoValid)) {
				const Tempo& t (session->tempo_map().tempo_at_frame (now));
				timeinfo->tempo = t.quarter_notes_per_minute ();
				newflags |= (kVstTempoValid);
			}
			if (value & (kVstTimeSigValid)) {
				const MeterSection& ms (session->tempo_map().meter_section_at_frame (now));
				timeinfo->timeSigNumerator = ms.divisions_per_bar ();
				timeinfo->timeSigDenominator = ms.note_divisor ();
				newflags |= (kVstTimeSigValid);
			}
			if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
				Timecode::BBT_Time bbt;

				try {
					bbt = session->tempo_map().bbt_at_frame_rt (now);
					bbt.beats = 1;
					bbt.ticks = 0;
					/* exact quarter note */
					double ppqBar = session->tempo_map().quarter_note_at_bbt_rt (bbt);
					/* quarter note at frame position (not rounded to note subdivision) */
					double ppqPos = session->tempo_map().quarter_note_at_frame_rt (now);
					if (value & (kVstPpqPosValid)) {
						timeinfo->ppqPos = ppqPos;
						newflags |= kVstPpqPosValid;
					}

					if (value & (kVstBarsValid)) {
						timeinfo->barStartPos = ppqBar;
						newflags |= kVstBarsValid;
					}

				} catch (...) {
					/* relax */
				}
			}

			if (value & (kVstSmpteValid)) {
				Timecode::Time t;

				session->timecode_time (now, t);

				timeinfo->smpteOffset = (t.hours * t.rate * 60.0 * 60.0) +
					(t.minutes * t.rate * 60.0) +
					(t.seconds * t.rate) +
					(t.frames) +
					(t.subframes);

				timeinfo->smpteOffset *= 80.0; /* VST spec is 1/80th frames */

				if (session->timecode_drop_frames()) {
					if (session->timecode_frames_per_second() == 30.0) {
						timeinfo->smpteFrameRate = 5;
					} else {
						timeinfo->smpteFrameRate = 4; /* 29.97 assumed, thanks VST */
					}
				} else {
					if (session->timecode_frames_per_second() == 24.0) {
						timeinfo->smpteFrameRate = 0;
					} else if (session->timecode_frames_per_second() == 24.975) {
						timeinfo->smpteFrameRate = 2;
					} else if (session->timecode_frames_per_second() == 25.0) {
						timeinfo->smpteFrameRate = 1;
					} else {
						timeinfo->smpteFrameRate = 3; /* 30 fps */
					}
				}
				newflags |= (kVstSmpteValid);
			}

			if (session->actively_recording ()) {
				newflags |= kVstTransportRecording;
			}

			if (plug->transport_speed () != 0.0f) {
				newflags |= kVstTransportPlaying;
			}

			if (session->get_play_loop ()) {
				newflags |= kVstTransportCycleActive;
				Location * looploc = session->locations ()->auto_loop_location ();
				if (looploc) try {
					timeinfo->cycleStartPos = session->tempo_map ().quarter_note_at_frame_rt (looploc->start ());
					timeinfo->cycleEndPos = session->tempo_map ().quarter_note_at_frame_rt (looploc->end ());

					newflags |= kVstCyclePosValid;
				} catch (...) { }
			}

		} else {
			timeinfo->samplePos = 0;
			timeinfo->sampleRate = AudioEngine::instance()->sample_rate();
		}

		if ((timeinfo->flags & (kVstTransportPlaying | kVstTransportRecording | kVstTransportCycleActive))
		    !=
		    (newflags        & (kVstTransportPlaying | kVstTransportRecording | kVstTransportCycleActive)))
		{
			newflags |= kVstTransportChanged;
		}

		timeinfo->flags = newflags;
		return (intptr_t) timeinfo;

	case audioMasterProcessEvents:
		SHOW_CALLBACK ("audioMasterProcessEvents");
		// VstEvents* in <ptr>
		if (plug && plug->midi_buffer()) {
			VstEvents* v = (VstEvents*)ptr;
			for (int n = 0 ; n < v->numEvents; ++n) {
				VstMidiEvent *vme = (VstMidiEvent*) (v->events[n]->dump);
				if (vme->type == kVstMidiType) {
					plug->midi_buffer()->push_back(vme->deltaFrames, 3, (uint8_t*)vme->midiData);
				}
			}
		}
		return 0;

	case audioMasterSetTime:
		SHOW_CALLBACK ("audioMasterSetTime");
		// VstTimenfo* in <ptr>, filter in <value>, not supported

	case audioMasterTempoAt:
		SHOW_CALLBACK ("audioMasterTempoAt");
		// returns tempo (in bpm * 10000) at sample frame location passed in <value>
		if (session) {
			const Tempo& t (session->tempo_map().tempo_at_frame (value));
			return t.quarter_notes_per_minute() * 1000;
		} else {
			return 0;
		}
		break;

	case audioMasterGetNumAutomatableParameters:
		SHOW_CALLBACK ("audioMasterGetNumAutomatableParameters");
		return 0;

	case audioMasterGetParameterQuantization:
		SHOW_CALLBACK ("audioMasterGetParameterQuantization");
		// returns the integer value for +1.0 representation,
		// or 1 if full single float precision is maintained
		// in automation. parameter index in <value> (-1: all, any)
		return 0;

	case audioMasterIOChanged:
		SHOW_CALLBACK ("audioMasterIOChanged");
		// numInputs and/or numOutputs has changed
		return 0;

	case audioMasterNeedIdle:
		SHOW_CALLBACK ("audioMasterNeedIdle");
		// plug needs idle calls (outside its editor window)
		if (plug) {
			plug->state()->wantIdle = 1;
		}
		return 0;

	case audioMasterSizeWindow:
		SHOW_CALLBACK ("audioMasterSizeWindow");
		if (plug && plug->state()) {
			plug->state()->width = index;
			plug->state()->height = value;
			plug->state()->want_resize = 1;
		}
		return 0;

	case audioMasterGetSampleRate:
		SHOW_CALLBACK ("audioMasterGetSampleRate");
		if (session) {
			return session->frame_rate();
		}
		return 0;

	case audioMasterGetBlockSize:
		SHOW_CALLBACK ("audioMasterGetBlockSize");
		if (session) {
			return session->get_block_size();
		}
		return 0;

	case audioMasterGetInputLatency:
		SHOW_CALLBACK ("audioMasterGetInputLatency");
		return 0;

	case audioMasterGetOutputLatency:
		SHOW_CALLBACK ("audioMasterGetOutputLatency");
		return 0;

	case audioMasterGetPreviousPlug:
		SHOW_CALLBACK ("audioMasterGetPreviousPlug");
		// input pin in <value> (-1: first to come), returns cEffect*
		return 0;

	case audioMasterGetNextPlug:
		SHOW_CALLBACK ("audioMasterGetNextPlug");
		// output pin in <value> (-1: first to come), returns cEffect*

	case audioMasterWillReplaceOrAccumulate:
		SHOW_CALLBACK ("audioMasterWillReplaceOrAccumulate");
		// returns: 0: not supported, 1: replace, 2: accumulate
		return 0;

	case audioMasterGetCurrentProcessLevel:
		SHOW_CALLBACK ("audioMasterGetCurrentProcessLevel");
		// returns: 0: not supported,
		// 1: currently in user thread (gui)
		// 2: currently in audio thread (where process is called)
		// 3: currently in 'sequencer' thread (midi, timer etc)
		// 4: currently offline processing and thus in user thread
		// other: not defined, but probably pre-empting user thread.
		return 0;

	case audioMasterGetAutomationState:
		SHOW_CALLBACK ("audioMasterGetAutomationState");
		// returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
		// offline
		return 0;

	case audioMasterOfflineStart:
		SHOW_CALLBACK ("audioMasterOfflineStart");
		return 0;

	case audioMasterOfflineRead:
		SHOW_CALLBACK ("audioMasterOfflineRead");
		// ptr points to offline structure, see below. return 0: error, 1 ok
		return 0;

	case audioMasterOfflineWrite:
		SHOW_CALLBACK ("audioMasterOfflineWrite");
		// same as read
		return 0;

	case audioMasterOfflineGetCurrentPass:
		SHOW_CALLBACK ("audioMasterOfflineGetCurrentPass");
		return 0;

	case audioMasterOfflineGetCurrentMetaPass:
		SHOW_CALLBACK ("audioMasterOfflineGetCurrentMetaPass");
		return 0;

	case audioMasterSetOutputSampleRate:
		SHOW_CALLBACK ("audioMasterSetOutputSampleRate");
		// for variable i/o, sample rate in <opt>
		return 0;

	case audioMasterGetSpeakerArrangement:
		SHOW_CALLBACK ("audioMasterGetSpeakerArrangement");
		// (long)input in <value>, output in <ptr>
		return 0;

	case audioMasterGetVendorString:
		SHOW_CALLBACK ("audioMasterGetVendorString");
		// fills <ptr> with a string identifying the vendor (max 64 char)
		strcpy ((char*) ptr, "Linux Audio Systems");
		return 0;

	case audioMasterGetProductString:
		SHOW_CALLBACK ("audioMasterGetProductString");
		// fills <ptr> with a string with product name (max 64 char)
		strcpy ((char*) ptr, PROGRAM_NAME);
		return 0;

	case audioMasterGetVendorVersion:
		SHOW_CALLBACK ("audioMasterGetVendorVersion");
		// returns vendor-specific version
		return 900;

	case audioMasterVendorSpecific:
		SHOW_CALLBACK ("audioMasterVendorSpecific");
		// no definition, vendor specific handling
		return 0;

	case audioMasterSetIcon:
		SHOW_CALLBACK ("audioMasterSetIcon");
		// void* in <ptr>, format not defined yet
		return 0;

	case audioMasterCanDo:
		SHOW_CALLBACK ("audioMasterCanDo");
		// string in ptr,  (const char*)ptr
		for (int i = 0; i < vst_can_do_string_count; i++) {
			if (! strcmp(vst_can_do_strings[i], (const char*)ptr)) {
				return 1;
			}
		}
		return 0;

	case audioMasterGetLanguage:
		SHOW_CALLBACK ("audioMasterGetLanguage");
		// see enum
		return 0;

	case audioMasterOpenWindow:
		SHOW_CALLBACK ("audioMasterOpenWindow");
		// returns platform specific ptr
		return 0;

	case audioMasterCloseWindow:
		SHOW_CALLBACK ("audioMasterCloseWindow");
		// close window, platform specific handle in <ptr>
		return 0;

	case audioMasterGetDirectory:
		SHOW_CALLBACK ("audioMasterGetDirectory");
		// get plug directory, FSSpec on MAC, else char*
		return 0;

	case audioMasterUpdateDisplay:
		SHOW_CALLBACK ("audioMasterUpdateDisplay");
		// something has changed, update 'multi-fx' display
		if (effect) {
			effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
		}
		return 0;

	case audioMasterBeginEdit:
		SHOW_CALLBACK ("audioMasterBeginEdit");
		// begin of automation session (when mouse down), parameter index in <index>
		return 0;

	case audioMasterEndEdit:
		SHOW_CALLBACK ("audioMasterEndEdit");
		// end of automation session (when mouse up),     parameter index in <index>
		return 0;

	case audioMasterOpenFileSelector:
		SHOW_CALLBACK ("audioMasterOpenFileSelector");
		// open a fileselector window with VstFileSelect* in <ptr>
		return 0;

	default:
		DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("VST master dispatcher: undefed: %1\n", opcode));
		break;
	}

	return 0;
}