properly handle integer steps in plugin controls
[ardour.git] / libs / ardour / session_vst.cc
index b2e9b14c1a4acf1f87df3800ed972150fe51960b..46814ac4f19564d5673a406c46bdab8474489783 100644 (file)
 #endif
 #include <cstdio>
 
+#include "ardour/audioengine.h"
 #include "ardour/session.h"
 #include "ardour/tempo.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/debug.h"
 
 #include "i18n.h"
 
@@ -41,6 +47,20 @@ static int debug_callbacks = -1;
 
 using namespace ARDOUR;
 
+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,
@@ -59,21 +79,13 @@ intptr_t Session::vst_callback (
        }
 
        if (effect && effect->user) {
-               plug = (VSTPlugin *) (effect->user);
+               plug = (VSTPlugin *) (effect->user);
                session = &plug->session();
-#ifdef COMPILER_MSVC
-               SHOW_CALLBACK ("am callback 0x%x, opcode = %d, plugin = \"%s\" ", (int) pthread_self().p, opcode, plug->name());
-#else
-               SHOW_CALLBACK ("am callback 0x%x, opcode = %d, plugin = \"%s\" ", (int) pthread_self(), opcode, plug->name());
-#endif
+               SHOW_CALLBACK ("am callback 0x%p, opcode = %d, plugin = \"%s\" ", (void*) DEBUG_THREAD_SELF, opcode, plug->name());
        } else {
                plug = 0;
                session = 0;
-#ifdef COMPILER_MSVC
-               SHOW_CALLBACK ("am callback 0x%x, opcode = %d", (int) pthread_self().p, opcode);
-#else
-               SHOW_CALLBACK ("am callback 0x%x, opcode = %d", (int) pthread_self(), opcode);
-#endif
+               SHOW_CALLBACK ("am callback 0x%p, opcode = %d", (void*) DEBUG_THREAD_SELF, opcode);
        }
 
        switch(opcode){
@@ -89,18 +101,18 @@ intptr_t Session::vst_callback (
        case audioMasterVersion:
                SHOW_CALLBACK ("amc: audioMasterVersion\n");
                // vst version, currently 2 (0 for older)
-               return 2;
+               return 2400;
 
        case audioMasterCurrentId:
                SHOW_CALLBACK ("amc: audioMasterCurrentId\n");
-               // returns the unique id of a plug that's currently
-               // loading
-               return 0;
+               // returns the unique id of a plug that's currently loading
+               return vst_current_loading_id;
 
        case audioMasterIdle:
                SHOW_CALLBACK ("amc: audioMasterIdle\n");
-               // call application idle routine (this will
-               // call effEditIdle for all open editors too)
+#ifdef WINDOWS_VST_SUPPORT
+               fst_audio_master_idle();
+#endif
                if (effect) {
                        effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
                }
@@ -118,7 +130,7 @@ intptr_t Session::vst_callback (
        case audioMasterWantMidi:
                SHOW_CALLBACK ("amc: audioMasterWantMidi\n");
                // <value> is a filter which is currently ignored
-               if (plug) {
+               if (plug && plug->get_info() != NULL) {
                        plug->get_info()->n_inputs.set_midi (1);
                }
                return 0;
@@ -129,12 +141,13 @@ intptr_t Session::vst_callback (
                // <value> should contain a mask indicating which fields are required
                // (see valid masks above), as some items may require extensive
                // conversions
-               memset(&_timeInfo, 0, sizeof(_timeInfo));
+               _timeInfo.flags = 0;
+
                if (session) {
                        framepos_t now = session->transport_frame();
+
                        _timeInfo.samplePos = now;
                        _timeInfo.sampleRate = session->frame_rate();
-                       _timeInfo.flags = 0;
 
                        const TempoMetric& tm (session->tempo_map().metric_at (now));
 
@@ -143,55 +156,114 @@ intptr_t Session::vst_callback (
                                _timeInfo.tempo = t.beats_per_minute ();
                                _timeInfo.flags |= (kVstTempoValid);
                        }
-                       if (value & (kVstBarsValid)) {
+                       if (value & (kVstTimeSigValid)) {
                                const Meter& m (tm.meter());
                                _timeInfo.timeSigNumerator = m.divisions_per_bar ();
                                _timeInfo.timeSigDenominator = m.note_divisor ();
-                               _timeInfo.flags |= (kVstBarsValid);
+                               _timeInfo.flags |= (kVstTimeSigValid);
                        }
-                       if (value & (kVstPpqPosValid)) {
+                       if ((value & (kVstPpqPosValid)) || (value & (kVstBarsValid))) {
                                Timecode::BBT_Time bbt;
+
                                try {
                                        session->tempo_map().bbt_time_rt (now, bbt);
-                                       
-                                       /* Note that this assumes constant
-                                          meter/tempo throughout the session. We
-                                          can do better than this, because
-                                          progressive rock fans demand it.
-                                       */
+
+                                       /* 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)
+                                        */
+                                       double ppq_scaling =  tm.meter().note_divisor() / 4.0;
+
+                                       /* Note that this assumes constant meter/tempo throughout the session. Stupid VST */
                                        double 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;
-                                       // PPQ Pos
-                                       _timeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
-                                       _timeInfo.flags |= (kVstPpqPosValid);
+
+                                       ppqBar *= ppq_scaling;
+                                       ppqBeat *= ppq_scaling;
+                                       ppqTick *= ppq_scaling;
+
+                                       if (value & (kVstPpqPosValid)) {
+                                               _timeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
+                                               _timeInfo.flags |= (kVstPpqPosValid);
+                                       }
+
+                                       if (value & (kVstBarsValid)) {
+                                               _timeInfo.barStartPos = ppqBar;
+                                               _timeInfo.flags |= (kVstBarsValid);
+                                       }
+
                                } catch (...) {
                                        /* relax */
                                }
                        }
 
-                       _timeInfo.tempo = tm.tempo().beats_per_minute();
-                       _timeInfo.flags |= kVstTempoValid;
-                       
-                       // Bars
-                       // _timeInfo.barStartPos = ppqBar;
-                       // _timeInfo.flags |= kVstBarsValid;
-                       
-                       // Time Signature
-                       _timeInfo.timeSigNumerator = tm.meter().divisions_per_bar();
-                       _timeInfo.timeSigDenominator = tm.meter().note_divisor();
-                       _timeInfo.flags |= kVstTimeSigValid;
+                       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 */
+                                       }
+                               }
+                               _timeInfo.flags |= (kVstSmpteValid);
+                       }
 
                        if (session->transport_speed() != 0.0f) {
-                               _timeInfo.flags |= kVstTransportPlaying;
+                               _timeInfo.flags |= (kVstTransportPlaying);
                        }
+
+                       if (session->get_play_loop()) {
+                       }
+
+               } else {
+                       _timeInfo.samplePos = 0;
+                       _timeInfo.sampleRate = AudioEngine::instance()->sample_rate();
                }
 
-               return (long)&_timeInfo;
+               return (intptr_t) &_timeInfo;
 
        case audioMasterProcessEvents:
                SHOW_CALLBACK ("amc: audioMasterProcessEvents\n");
                // 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:
@@ -215,14 +287,14 @@ intptr_t Session::vst_callback (
 
        case audioMasterGetParameterQuantization:
                SHOW_CALLBACK ("amc: audioMasterGetParameterQuantization\n");
-               // 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)
+               // 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 ("amc: audioMasterIOChanged\n");
-              // numInputs and/or numOutputs has changed
+               // numInputs and/or numOutputs has changed
                return 0;
 
        case audioMasterNeedIdle:
@@ -262,16 +334,16 @@ intptr_t Session::vst_callback (
 
        case audioMasterGetPreviousPlug:
                SHOW_CALLBACK ("amc: audioMasterGetPreviousPlug\n");
-              // input pin in <value> (-1: first to come), returns cEffect*
+               // input pin in <value> (-1: first to come), returns cEffect*
                return 0;
 
        case audioMasterGetNextPlug:
                SHOW_CALLBACK ("amc: audioMasterGetNextPlug\n");
-              // output pin in <value> (-1: first to come), returns cEffect*
+               // output pin in <value> (-1: first to come), returns cEffect*
 
        case audioMasterWillReplaceOrAccumulate:
                SHOW_CALLBACK ("amc: audioMasterWillReplaceOrAccumulate\n");
-              // returns: 0: not supported, 1: replace, 2: accumulate
+               // returns: 0: not supported, 1: replace, 2: accumulate
                return 0;
 
        case audioMasterGetCurrentProcessLevel:
@@ -293,10 +365,10 @@ intptr_t Session::vst_callback (
        case audioMasterOfflineStart:
                SHOW_CALLBACK ("amc: audioMasterOfflineStart\n");
                return 0;
-               
+
        case audioMasterOfflineRead:
                SHOW_CALLBACK ("amc: audioMasterOfflineRead\n");
-              // ptr points to offline structure, see below. return 0: error, 1 ok
+               // ptr points to offline structure, see below. return 0: error, 1 ok
                return 0;
 
        case audioMasterOfflineWrite:
@@ -307,7 +379,7 @@ intptr_t Session::vst_callback (
        case audioMasterOfflineGetCurrentPass:
                SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentPass\n");
                return 0;
-               
+
        case audioMasterOfflineGetCurrentMetaPass:
                SHOW_CALLBACK ("amc: audioMasterOfflineGetCurrentMetaPass\n");
                return 0;
@@ -351,7 +423,12 @@ intptr_t Session::vst_callback (
 
        case audioMasterCanDo:
                SHOW_CALLBACK ("amc: audioMasterCanDo\n");
-               // string in ptr, see below
+               // 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: