diff --git a/include/Engine.h b/include/Engine.h index 2f940dfbf..54ada4062 100644 --- a/include/Engine.h +++ b/include/Engine.h @@ -31,6 +31,7 @@ #include "export.h" +#include "lmms_basics.h" class BBTrackContainer; class DummyTrackContainer; @@ -100,6 +101,9 @@ public: { return s_framesPerTick; } + + static float framesPerTick(sample_rate_t sample_rate); + static void updateFramesPerTick(); static inline LmmsCore * inst() diff --git a/src/core/Engine.cpp b/src/core/Engine.cpp index a53abbe5d..ee8aef708 100644 --- a/src/core/Engine.cpp +++ b/src/core/Engine.cpp @@ -103,6 +103,12 @@ void LmmsCore::destroy() delete ConfigManager::inst(); } +float LmmsCore::framesPerTick(sample_rate_t sample_rate) +{ + return sample_rate * 60.0f * 4 / + DefaultTicksPerTact / s_song->getTempo(); +} + diff --git a/src/tracks/SampleTrack.cpp b/src/tracks/SampleTrack.cpp index ad1ae1b81..23decb1f0 100644 --- a/src/tracks/SampleTrack.cpp +++ b/src/tracks/SampleTrack.cpp @@ -626,13 +626,16 @@ bool SampleTrack::play( const MidiTime & _start, const fpp_t _frames, { TrackContentObject * tco = getTCO( i ); SampleTCO * sTco = dynamic_cast( tco ); - float framesPerTick = Engine::framesPerTick(); + if( _start >= sTco->startPosition() && _start < sTco->endPosition() ) { if( sTco->isPlaying() == false ) { - f_cnt_t sampleStart = framesPerTick * ( _start - sTco->startPosition() ); - f_cnt_t tcoFrameLength = framesPerTick * ( sTco->endPosition() - sTco->startPosition() ); + auto bufferFramesPerTick = Engine::framesPerTick (sTco->sampleBuffer ()->sampleRate ()); + f_cnt_t sampleStart = bufferFramesPerTick * ( _start - sTco->startPosition() ); + + f_cnt_t tcoFrameLength = bufferFramesPerTick * ( sTco->endPosition() - sTco->startPosition() ); + f_cnt_t sampleBufferLength = sTco->sampleBuffer()->frames(); //if the Tco smaller than the sample length we play only until Tco end //else we play the sample to the end but nothing more