Don't use VST ->user points, prefer host-reserved ptr
[ardour.git] / libs / ardour / session_vst.cc
index 4cce5219ac11248a95e7ba637170764752dcdc61..3950a294781ee9b7b6d8a739e14e23e61e6e2811 100644 (file)
 #include <fst.h>
 #endif
 
-#include "i18n.h"
+#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"),
@@ -98,8 +69,8 @@ intptr_t Session::vst_callback (
        VstTimeInfo* timeinfo;
        int32_t newflags = 0;
 
-       if (effect && effect->user) {
-               plug = (VSTPlugin *) (effect->user);
+       if (effect && effect->ptr1) {
+               plug = (VSTPlugin *) (effect->ptr1);
                session = &plug->session();
                timeinfo = plug->timeinfo ();
                DEBUG_TRACE (PBD::DEBUG::VSTCallbacks, string_compose ("am callback 0x%1%2, opcode = %3%4, plugin = \"%5\"\n",
@@ -206,17 +177,15 @@ intptr_t Session::vst_callback (
                        timeinfo->samplePos = now;
                        timeinfo->sampleRate = session->frame_rate();
 
-                       const TempoMetric& tm (session->tempo_map().metric_at (now));
-
                        if (value & (kVstTempoValid)) {
-                               const Tempo& t (tm.tempo());
-                               timeinfo->tempo = t.beats_per_minute ();
+                               const Tempo& t (session->tempo_map().tempo_at_frame (now));
+                               timeinfo->tempo = t.quarter_notes_per_minute ();
                                newflags |= (kVstTempoValid);
                        }
                        if (value & (kVstTimeSigValid)) {
-                               const Meter& m (tm.meter());
-                               timeinfo->timeSigNumerator = m.divisions_per_bar ();
-                               timeinfo->timeSigDenominator = m.note_divisor ();
+                               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))) {
@@ -224,10 +193,12 @@ intptr_t Session::vst_callback (
 
                                try {
                                        bbt = session->tempo_map().bbt_at_frame_rt (now);
-
-                                       double ppqBar;
-                                       double ppqPos = vst_ppq (tm, bbt, ppqBar);
-
+                                       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;
@@ -288,14 +259,8 @@ intptr_t Session::vst_callback (
                                newflags |= kVstTransportCycleActive;
                                Location * looploc = session->locations ()->auto_loop_location ();
                                if (looploc) try {
-                                       double ppqBar;
-                                       Timecode::BBT_Time bbt;
-
-                                       bbt = session->tempo_map ().bbt_at_frame_rt (looploc->start ());
-                                       timeinfo->cycleStartPos = vst_ppq (tm, bbt, ppqBar);
-
-                                       bbt = session->tempo_map ().bbt_at_frame_rt (looploc->end ());
-                                       timeinfo->cycleEndPos = vst_ppq (tm, bbt, ppqBar);
+                                       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 (...) { }
@@ -339,7 +304,7 @@ intptr_t Session::vst_callback (
                // 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.beats_per_minute() * 1000;
+                       return t.quarter_notes_per_minute() * 1000;
                } else {
                        return 0;
                }
@@ -372,9 +337,14 @@ intptr_t Session::vst_callback (
        case audioMasterSizeWindow:
                SHOW_CALLBACK ("audioMasterSizeWindow");
                if (plug && plug->state()) {
-                       plug->state()->width = index;
-                       plug->state()->height = value;
-                       plug->state()->want_resize = 1;
+                       if (plug->state()->width != index || plug->state()->height != value) {
+                               plug->state()->width = index;
+                               plug->state()->height = value;
+#ifndef NDEBUG
+                               printf ("audioMasterSizeWindow %d %d\n", plug->state()->width, plug->state()->height);
+#endif
+                               plug->VSTSizeWindow (); /* EMIT SIGNAL */
+                       }
                }
                return 0;
 
@@ -472,7 +442,7 @@ intptr_t Session::vst_callback (
                SHOW_CALLBACK ("audioMasterGetProductString");
                // fills <ptr> with a string with product name (max 64 char)
                strcpy ((char*) ptr, PROGRAM_NAME);
-               return 0;
+               return 1;
 
        case audioMasterGetVendorVersion:
                SHOW_CALLBACK ("audioMasterGetVendorVersion");