Skip to content

Commit

Permalink
Implement VST latency reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
PhysSong committed Mar 1, 2024
1 parent 3a851e7 commit ac291c8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 7 deletions.
10 changes: 5 additions & 5 deletions include/aeffectx.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ class AEffect
int32_t numOutputs;
// flags 24-27
int32_t flags;
// Fill somewhere 28-2b
// Fill somewhere 28-2b 2c-2f
void *ptr1;
void *ptr2;
// 2c-2f
int latency;
// Zeroes 30-33 34-37
char empty3[4 + 4];
// latency(initial delay) 30-33
int initialDelay;
// Zeroes 34-37 38-3b
char empty2[4 + 4];
// 1.0f 3c-3f
float unknown_float;
// An object? pointer 40-43
Expand Down
4 changes: 4 additions & 0 deletions plugins/Vestige/Vestige.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ void VestigeInstrument::loadFile( const QString & _file )
return;
}

connect(m_plugin, &VstPlugin::latencyChanged, [this]() {
setLatency(m_plugin->latency());
});

if ( !(instrumentTrack() != nullptr && instrumentTrack()->isPreviewMode()))
{
m_plugin->createUI(nullptr);
Expand Down
32 changes: 32 additions & 0 deletions plugins/VstBase/RemoteVstPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ class RemoteVstPlugin : public RemotePluginClient
// has to be called as soon as input- or output-count changes
int updateInOutCount();

// called on latency change, returns true on change
void updateLatency(bool reply = false);

inline int cachedLatency() const
{
return m_latency;
}

inline void lockShm()
{
m_shmLock.lock();
Expand Down Expand Up @@ -481,6 +489,7 @@ class RemoteVstPlugin : public RemotePluginClient
bpm_t m_bpm;
double m_currentSamplePos;
int m_currentProgram;
int m_latency;

// host to plugin synchronisation data structure
struct in
Expand Down Expand Up @@ -718,6 +727,10 @@ bool RemoteVstPlugin::processMessage( const message & _m )
break;
}
#endif
case IdVstGetLatency:
updateLatency(true);
break;

default:
return RemotePluginClient::processMessage( _m );
}
Expand All @@ -738,6 +751,7 @@ void RemoteVstPlugin::init( const std::string & _plugin_file )
updateInOutCount();
updateBufferSize();
updateSampleRate();
updateLatency(true);

/* set program to zero */
/* i comment this out because it breaks dfx Geometer
Expand Down Expand Up @@ -1101,6 +1115,7 @@ void RemoteVstPlugin::process( const sampleFrame * _in, sampleFrame * _out )
#else
m_plugin->processReplacing(m_plugin, m_inputs, m_outputs, bufferSize());
#endif
updateLatency();

unlockShm();

Expand Down Expand Up @@ -1709,6 +1724,22 @@ int RemoteVstPlugin::updateInOutCount()




void RemoteVstPlugin::updateLatency(bool reply)
{
if (m_latency != m_plugin->initialDelay)
{
reply = true;
}
m_latency = m_plugin->initialDelay;
if (reply)
{
sendMessage(message(IdVstLatency).addInt(m_latency));
}
}



//#define DEBUG_CALLBACKS
#ifdef DEBUG_CALLBACKS
#define SHOW_CALLBACK __plugin->debugMessage
Expand Down Expand Up @@ -1853,6 +1884,7 @@ intptr_t RemoteVstPlugin::hostCallback( AEffect * _effect, int32_t _opcode,
case audioMasterIOChanged:
SHOW_CALLBACK( "amc: audioMasterIOChanged\n" );
// numInputs, numOutputs, and/or latency has changed
__plugin->updateLatency();
return __plugin->updateInOutCount();

#ifdef OLD_VST_SDK
Expand Down
5 changes: 4 additions & 1 deletion plugins/VstBase/VstPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ VstPlugin::VstPlugin( const QString & _plugin ) :
? ConfigManager::inst()->vstEmbedMethod()
: "headless" ),
m_version( 0 ),
m_currentProgram()
m_currentProgram(),
m_latency(0)
{
setSplittedChannels( true );

Expand Down Expand Up @@ -488,6 +489,8 @@ bool VstPlugin::processMessage( const message & _m )
}
break;
}
case IdVstLatency:
setLatency(_m.getInt(0));
default:
return RemotePlugin::processMessage( _m );
}
Expand Down
20 changes: 20 additions & 0 deletions plugins/VstBase/VstPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ class VSTBASE_EXPORT VstPlugin : public RemotePlugin, public JournallingObject

QString embedMethod() const;

inline int latency() const
{
return m_latency;
}

void setLatency(int latency)
{
if (m_latency != latency)
{
m_latency = latency;
emit latencyChanged();
}
}

void updateLatency();

signals:
void latencyChanged();

public slots:
void setTempo( lmms::bpm_t _bpm );
void updateSampleRate();
Expand Down Expand Up @@ -171,6 +190,7 @@ public slots:

QTimer m_idleTimer;

int m_latency;
} ;


Expand Down
4 changes: 3 additions & 1 deletion plugins/VstBase/communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ enum VstRemoteMessageIDs
IdVstIdleUpdate,
IdVstParameterDisplays,
IdVstParameterLabels,
IdVstGetLatency,

// remoteVstPlugin -> vstPlugin
IdVstFailedLoadingPlugin,
Expand All @@ -84,7 +85,8 @@ enum VstRemoteMessageIDs
IdVstPluginUniqueID,
IdVstSetParameter,
IdVstParameterCount,
IdVstParameterDump
IdVstParameterDump,
IdVstLatency

} ;

Expand Down
4 changes: 4 additions & 0 deletions plugins/VstEffect/VstEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ void VstEffect::openPlugin( const QString & _plugin )
return;
}

connect(m_plugin.data(), &VstPlugin::latencyChanged, [this]() {
setLatency(m_plugin->latency());
});

delete tf;

m_key.attributes["file"] = _plugin;
Expand Down

0 comments on commit ac291c8

Please sign in to comment.