diff --git a/include/instrument.h b/include/instrument.h index c9d38056e..1f1888f35 100644 --- a/include/instrument.h +++ b/include/instrument.h @@ -49,6 +49,7 @@ #include "plugin.h" +#include "mixer.h" // forward-declarations @@ -131,6 +132,11 @@ protected: return( m_instrumentTrack ); } + // instruments may use this to apply a soft fade out at the end of + // notes - method does this only if really less or equal + // desiredReleaseFrames() frames are left + void applyRelease( sampleFrame * buf, const notePlayHandle * _n ); + private: instrumentTrack * m_instrumentTrack; diff --git a/src/core/instrument.cpp b/src/core/instrument.cpp index 44b665ae9..4acce7022 100644 --- a/src/core/instrument.cpp +++ b/src/core/instrument.cpp @@ -28,6 +28,7 @@ #include "instrument.h" #include "instrument_track.h" #include "dummy_instrument.h" +#include "note_play_handle.h" instrument::instrument( instrumentTrack * _instrument_track, @@ -105,5 +106,25 @@ bool instrument::isFromTrack( const track * _track ) const +void instrument::applyRelease( sampleFrame * buf, const notePlayHandle * _n ) +{ + const fpp_t frames = _n->framesLeftForCurrentPeriod(); + const fpp_t fpp = engine::getMixer()->framesPerPeriod(); + const f_cnt_t fl = _n->framesLeft(); + if( fl <= desiredReleaseFrames()+fpp ) + { + for( fpp_t f = fl > desiredReleaseFrames() ? + ( tMax( fpp - desiredReleaseFrames(), 0 ) + + fl % fpp ) : 0; f < frames; ++f ) + { + const float fac = (float)( fl-f-1 ) / + desiredReleaseFrames(); + for( ch_cnt_t ch = 0; ch < DEFAULT_CHANNELS; ++ch ) + { + buf[f][ch] *= fac; + } + } + } +} #endif