Skip to content

Commit 614bca7

Browse files
DomClarkzonkmachine
authored andcommitted
Make ppqPos in VST sync sample accurate
1 parent 3a94ed3 commit 614bca7

File tree

5 files changed

+36
-19
lines changed

5 files changed

+36
-19
lines changed

include/VstSyncController.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class VstSyncController : public QObject
3939
VstSyncController();
4040
~VstSyncController();
4141

42-
void setAbsolutePosition( int ticks );
42+
void setAbsolutePosition( double ticks );
4343

4444
void setPlaybackState( bool enabled )
4545
{
@@ -77,7 +77,7 @@ private slots:
7777
struct VstSyncData
7878
{
7979
bool isPlaying;
80-
float ppqPos;
80+
double ppqPos;
8181
int timeSigNumer;
8282
int timeSigDenom;
8383
bool isCycle;

include/VstSyncData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
struct VstSyncData
4343
{
4444
bool isPlaying;
45-
float ppqPos;
45+
double ppqPos;
4646
int timeSigNumer;
4747
int timeSigDenom;
4848
bool isCycle;

plugins/vst_base/RemoteVstPlugin.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ class RemoteVstPlugin : public RemotePluginClient
395395
// host to plugin synchronisation data structure
396396
struct in
397397
{
398-
float lastppqPos;
399-
float m_Timestamp;
398+
double lastppqPos;
399+
double m_Timestamp;
400400
int32_t m_lastFlags;
401401
} ;
402402

@@ -1605,10 +1605,20 @@ intptr_t RemoteVstPlugin::hostCallback( AEffect * _effect, int32_t _opcode,
16051605
}
16061606
else if( __plugin->m_vstSyncData->isPlaying )
16071607
{
1608-
__plugin->m_in->lastppqPos += (
1609-
__plugin->m_vstSyncData->hasSHM ?
1610-
__plugin->m_vstSyncData->m_bpm :
1611-
__plugin->m_bpm ) / (float)10340;
1608+
if( __plugin->m_vstSyncData->hasSHM )
1609+
{
1610+
__plugin->m_in->lastppqPos +=
1611+
__plugin->m_vstSyncData->m_bpm / 60.0
1612+
* __plugin->m_vstSyncData->m_bufferSize
1613+
/ __plugin->m_vstSyncData->m_sampleRate;
1614+
}
1615+
else
1616+
{
1617+
__plugin->m_in->lastppqPos +=
1618+
__plugin->m_bpm / 60.0
1619+
* __plugin->bufferSize()
1620+
/ __plugin->sampleRate();
1621+
}
16121622
_timeInfo.ppqPos = __plugin->m_in->lastppqPos;
16131623
}
16141624
// _timeInfo.ppqPos = __plugin->m_vstSyncData->ppqPos;

src/core/Song.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,6 @@ void Song::processNextBuffer()
265265
m_playPos[m_playMode].setTicks(
266266
tl->loopBegin().getTicks() );
267267

268-
m_vstSyncController.setAbsolutePosition(
269-
tl->loopBegin().getTicks() );
270268
m_vstSyncController.setPlaybackJumped( true );
271269

272270
emit updateSampleTracks();
@@ -293,8 +291,6 @@ void Song::processNextBuffer()
293291
int ticks = m_playPos[m_playMode].getTicks() +
294292
( int )( currentFrame / framesPerTick );
295293

296-
m_vstSyncController.setAbsolutePosition( ticks );
297-
298294
// did we play a whole tact?
299295
if( ticks >= MidiTime::ticksPerTact() )
300296
{
@@ -332,7 +328,6 @@ void Song::processNextBuffer()
332328
m_elapsedMilliSeconds =
333329
( ticks * 60 * 1000 / 48 ) / getTempo();
334330

335-
m_vstSyncController.setAbsolutePosition( ticks );
336331
m_vstSyncController.setPlaybackJumped( true );
337332
}
338333
}
@@ -354,7 +349,6 @@ void Song::processNextBuffer()
354349
m_elapsedMilliSeconds =
355350
( ticks * 60 * 1000 / 48 ) / getTempo();
356351

357-
m_vstSyncController.setAbsolutePosition( ticks );
358352
m_vstSyncController.setPlaybackJumped( true );
359353

360354
emit updateSampleTracks();
@@ -369,6 +363,16 @@ void Song::processNextBuffer()
369363
m_playPos[m_playMode].setCurrentFrame( currentFrame );
370364
}
371365

366+
if( framesPlayed == 0 )
367+
{
368+
// update VST sync position after we've corrected frame/
369+
// tick count but before actually playing any frames
370+
m_vstSyncController.setAbsolutePosition(
371+
m_playPos[m_playMode].getTicks()
372+
+ m_playPos[m_playMode].currentFrame()
373+
/ (double) framesPerTick );
374+
}
375+
372376
f_cnt_t framesToPlay =
373377
Engine::mixer()->framesPerPeriod() - framesPlayed;
374378

@@ -716,7 +720,10 @@ void Song::stop()
716720
m_playPos[m_playMode].setCurrentFrame( 0 );
717721

718722
m_vstSyncController.setPlaybackState( m_exporting );
719-
m_vstSyncController.setAbsolutePosition( m_playPos[m_playMode].getTicks() );
723+
m_vstSyncController.setAbsolutePosition(
724+
m_playPos[m_playMode].getTicks()
725+
+ m_playPos[m_playMode].currentFrame()
726+
/ (double) Engine::framesPerTick() );
720727

721728
// remove all note-play-handles that are active
722729
Engine::mixer()->clear();

src/core/VstSyncController.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ VstSyncController::~VstSyncController()
136136

137137

138138

139-
void VstSyncController::setAbsolutePosition( int ticks )
139+
void VstSyncController::setAbsolutePosition( double ticks )
140140
{
141141
#ifdef VST_SNC_LATENCY
142-
m_syncData->ppqPos = ( ( ticks + 0 ) / (float)48 ) - m_syncData->m_latency;
142+
m_syncData->ppqPos = ( ( ticks + 0 ) / 48.0 ) - m_syncData->m_latency;
143143
#else
144-
m_syncData->ppqPos = ( ( ticks + 0 ) / (float)48 );
144+
m_syncData->ppqPos = ( ( ticks + 0 ) / 48.0 );
145145
#endif
146146
}
147147

0 commit comments

Comments
 (0)