Skip to content

Commit

Permalink
Instrument release time in milliseconds (#7217)
Browse files Browse the repository at this point in the history
Make instruments report their release time in milliseconds so that it becomes independent of the sample rate and sounds the same at any sample rate.

Technically this is done by removing the virtual keyword from `desiredReleaseFrames` so that it cannot be overridden anymore. The method now only serves to compute the number of frames from the given release time in milliseconds.

A new virtual method `desiredReleaseTimeMs` is added which instruments can override. The default returns 0 ms just like the default implementation previously returned 0 frames.

The method `computeReleaseTimeMsByFrameCount` is added for instruments that still use a hard coded release in frames. As of now this is only `SidInstrument`.

Add the helper method `getSampleRate` to `Instrument`.

Adjust several instruments to report their release times in milliseconds. The times are computed by taking the release in frames and assuming a sample rate of 44.1 kHz. In most cases the times are rounded to a "nice" next value, e.g.:
*  64 frames -> 1.5 ms (66 frames)
* 128 frames -> 3.0 ms (132 frames)
* 512 frames -> 12. ms (529 frames)
* 1000 frames -> 23 ms (1014 samples)

In parentheses the number of frames are shown which result from the rounded number of milliseconds when converted back assuming a sample rate of 44.1 kHz. The difference should not be noticeable in existing projects.

Remove the overrides for instruments that return the same value as the base class `Instrument` anyway. These are:
* GigPlayer
* Lb302
* Sf2Player

For `MonstroInstrument` the implementation is adjusted to behave in a very similar way. First the maximum of the envelope release times is computed. These are already available in milliseconds. Then the maximum of that value and 1.5 ms is taken and returned as the result.
  • Loading branch information
michaelgregorius authored Apr 24, 2024
1 parent 62e2a39 commit 71dd300
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 66 deletions.
28 changes: 22 additions & 6 deletions include/Instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#include "Plugin.h"
#include "TimePos.h"

#include <cmath>


namespace lmms
{

Expand Down Expand Up @@ -91,15 +94,26 @@ class LMMS_EXPORT Instrument : public Plugin
virtual f_cnt_t beatLen( NotePlayHandle * _n ) const;


// some instruments need a certain number of release-frames even
// if no envelope is active - such instruments can re-implement this
// method for returning how many frames they at least like to have for
// release
virtual f_cnt_t desiredReleaseFrames() const
// This method can be overridden by instruments that need a certain
// release time even if no envelope is active. It returns the time
// in milliseconds that these instruments would like to have for
// their release stage.
virtual float desiredReleaseTimeMs() const
{
return 0.f;
}

// Converts the desired release time in milliseconds to the corresponding
// number of frames depending on the sample rate.
f_cnt_t desiredReleaseFrames() const
{
return 0;
const sample_rate_t sampleRate = getSampleRate();

return static_cast<f_cnt_t>(std::ceil(desiredReleaseTimeMs() * sampleRate / 1000.f));
}

sample_rate_t getSampleRate() const;

virtual Flags flags() const
{
return Flag::NoFlags;
Expand Down Expand Up @@ -142,6 +156,8 @@ class LMMS_EXPORT Instrument : public Plugin
// desiredReleaseFrames() frames are left
void applyRelease( sampleFrame * buf, const NotePlayHandle * _n );

float computeReleaseTimeMsByFrameCount(f_cnt_t frames) const;


private:
InstrumentTrack * m_instrumentTrack;
Expand Down
4 changes: 2 additions & 2 deletions plugins/AudioFileProcessor/AudioFileProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class AudioFileProcessor : public Instrument

auto beatLen(NotePlayHandle* note) const -> int override;

f_cnt_t desiredReleaseFrames() const override
float desiredReleaseTimeMs() const override
{
return 128;
return 3.f;
}

gui::PluginView* instantiateView( QWidget * _parent ) override;
Expand Down
4 changes: 2 additions & 2 deletions plugins/BitInvader/BitInvader.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ class BitInvader : public Instrument

QString nodeName() const override;

f_cnt_t desiredReleaseFrames() const override
float desiredReleaseTimeMs() const override
{
return( 64 );
return 1.5f;
}

gui::PluginView * instantiateView( QWidget * _parent ) override;
Expand Down
18 changes: 3 additions & 15 deletions plugins/FreeBoy/FreeBoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,10 @@ QString FreeBoyInstrument::nodeName() const



/*f_cnt_t FreeBoyInstrument::desiredReleaseFrames() const
float FreeBoyInstrument::desiredReleaseTimeMs() const
{
const float samplerate = Engine::audioEngine()->processingSampleRate();
int maxrel = 0;
for( int i = 0 ; i < 3 ; ++i )
{
if( maxrel < m_voice[i]->m_releaseModel.value() )
maxrel = m_voice[i]->m_releaseModel.value();
}
return f_cnt_t( float(relTime[maxrel])*samplerate/1000.0 );
}*/

f_cnt_t FreeBoyInstrument::desiredReleaseFrames() const
{
return f_cnt_t( 1000 );
// Previous implementation was 1000 samples. At 44.1 kHz this is somewhat shy of 23. ms.
return 23.f;
}


Expand Down
2 changes: 1 addition & 1 deletion plugins/FreeBoy/FreeBoy.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FreeBoyInstrument : public Instrument

QString nodeName() const override;

f_cnt_t desiredReleaseFrames() const override;
float desiredReleaseTimeMs() const override;

gui::PluginView* instantiateView( QWidget * _parent ) override;

Expand Down
5 changes: 0 additions & 5 deletions plugins/GigPlayer/GigPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,6 @@ class GigInstrument : public Instrument

QString nodeName() const override;

f_cnt_t desiredReleaseFrames() const override
{
return 0;
}

Flags flags() const override
{
return Flag::IsSingleStreamed | Flag::IsNotBendable;
Expand Down
4 changes: 2 additions & 2 deletions plugins/Kicker/Kicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class KickerInstrument : public Instrument
return Flag::IsNotBendable;
}

f_cnt_t desiredReleaseFrames() const override
float desiredReleaseTimeMs() const override
{
return( 512 );
return 12.f;
}

gui::PluginView* instantiateView( QWidget * _parent ) override;
Expand Down
5 changes: 0 additions & 5 deletions plugins/Lb302/Lb302.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,6 @@ class Lb302Synth : public Instrument
return Flag::IsSingleStreamed;
}

f_cnt_t desiredReleaseFrames() const override
{
return 0; //4048;
}

gui::PluginView* instantiateView( QWidget * _parent ) override;

private:
Expand Down
7 changes: 3 additions & 4 deletions plugins/Monstro/Monstro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1326,13 +1326,12 @@ QString MonstroInstrument::nodeName() const
return monstro_plugin_descriptor.name;
}


f_cnt_t MonstroInstrument::desiredReleaseFrames() const
float MonstroInstrument::desiredReleaseTimeMs() const
{
return qMax( 64, qMax( m_env1_relF, m_env2_relF ) );
const auto maxEnvelope = std::max(m_env1_rel, m_env2_rel);
return std::max(1.5f, maxEnvelope);
}


gui::PluginView* MonstroInstrument::instantiateView( QWidget * _parent )
{
return( new gui::MonstroView( this, _parent ) );
Expand Down
2 changes: 1 addition & 1 deletion plugins/Monstro/Monstro.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ class MonstroInstrument : public Instrument

QString nodeName() const override;

f_cnt_t desiredReleaseFrames() const override;
float desiredReleaseTimeMs() const override;

gui::PluginView* instantiateView( QWidget * _parent ) override;

Expand Down
4 changes: 2 additions & 2 deletions plugins/Nes/Nes.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ class NesInstrument : public Instrument

QString nodeName() const override;

f_cnt_t desiredReleaseFrames() const override
float desiredReleaseTimeMs() const override
{
return( 8 );
return 0.2f;
}

gui::PluginView* instantiateView( QWidget * parent ) override;
Expand Down
4 changes: 2 additions & 2 deletions plugins/Patman/Patman.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class PatmanInstrument : public Instrument

QString nodeName() const override;

f_cnt_t desiredReleaseFrames() const override
float desiredReleaseTimeMs() const override
{
return( 128 );
return 3.f;
}

gui::PluginView* instantiateView( QWidget * _parent ) override;
Expand Down
5 changes: 0 additions & 5 deletions plugins/Sf2Player/Sf2Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ class Sf2Instrument : public Instrument

QString nodeName() const override;

f_cnt_t desiredReleaseFrames() const override
{
return 0;
}

Flags flags() const override
{
return Flag::IsSingleStreamed;
Expand Down
12 changes: 3 additions & 9 deletions plugins/Sid/SidInstrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,24 +221,18 @@ QString SidInstrument::nodeName() const
}




f_cnt_t SidInstrument::desiredReleaseFrames() const
float SidInstrument::desiredReleaseTimeMs() const
{
const float samplerate = Engine::audioEngine()->processingSampleRate();
int maxrel = 0;
for (const auto& voice : m_voice)
{
if( maxrel < voice->m_releaseModel.value() )
maxrel = (int)voice->m_releaseModel.value();
maxrel = std::max(maxrel, static_cast<int>(voice->m_releaseModel.value()));
}

return f_cnt_t( float(relTime[maxrel])*samplerate/1000.0 );
return computeReleaseTimeMsByFrameCount(relTime[maxrel]);
}




static int sid_fillbuffer(unsigned char* sidreg, reSID::SID *sid, int tdelta, short *ptr, int samples)
{
int total = 0;
Expand Down
2 changes: 1 addition & 1 deletion plugins/Sid/SidInstrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class SidInstrument : public Instrument

QString nodeName() const override;

f_cnt_t desiredReleaseFrames() const override;
float desiredReleaseTimeMs() const override;

gui::PluginView* instantiateView( QWidget * _parent ) override;

Expand Down
4 changes: 2 additions & 2 deletions plugins/TripleOscillator/TripleOscillator.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ class TripleOscillator : public Instrument

QString nodeName() const override;

f_cnt_t desiredReleaseFrames() const override
float desiredReleaseTimeMs() const override
{
return( 128 );
return 3.f;
}

gui::PluginView* instantiateView( QWidget * _parent ) override;
Expand Down
4 changes: 2 additions & 2 deletions plugins/Watsyn/Watsyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ class WatsynInstrument : public Instrument

QString nodeName() const override;

f_cnt_t desiredReleaseFrames() const override
float desiredReleaseTimeMs() const override
{
return( 64 );
return 1.5f;
}

gui::PluginView* instantiateView( QWidget * _parent ) override;
Expand Down
9 changes: 9 additions & 0 deletions src/core/Instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,16 @@ void Instrument::applyRelease( sampleFrame * buf, const NotePlayHandle * _n )
}
}

float Instrument::computeReleaseTimeMsByFrameCount(f_cnt_t frames) const
{
return frames / getSampleRate() * 1000.;
}


sample_rate_t Instrument::getSampleRate() const
{
return Engine::audioEngine()->processingSampleRate();
}


QString Instrument::fullDisplayName() const
Expand Down

0 comments on commit 71dd300

Please sign in to comment.