Skip to content

Commit

Permalink
Mallets - Add random knob function (#6466)
Browse files Browse the repository at this point in the history
* Mallets - Add random knob function

Implement randomness for the instrument. When Random is applied, Hardness and Position of instrument 1 - 9, or Modulator and Crossfade on instrument 10 (Tubular Bells), are nudged on every note to liven up the sound. With the modulated knobs placed at their center values, Random at max will select from the full range of possible values.

Co-authored-by: saker <[email protected]>
Co-authored-by: Dominic Clark <[email protected]>
  • Loading branch information
3 people authored Sep 29, 2023
1 parent 8fb9c3e commit 7dc0052
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
44 changes: 38 additions & 6 deletions plugins/Stk/Mallets/Mallets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ MalletsInstrument::MalletsInstrument( InstrumentTrack * _instrument_track ):
m_strikeModel( true, this, tr( "Bowed" ) ),
m_presetsModel(this),
m_spreadModel(0, 0, 255, 1, this, tr( "Spread" )),
m_randomModel(0.0f, 0.0f, 1.0f, 0.01f, this, tr("Randomness")),
m_versionModel( MALLETS_PRESET_VERSION, 0, MALLETS_PRESET_VERSION, this, "" ),
m_isOldVersionModel( false, this, "" ),
m_filesMissing( !QDir( ConfigManager::inst()->stkDir() ).exists() ||
Expand Down Expand Up @@ -155,6 +156,7 @@ void MalletsInstrument::saveSettings( QDomDocument & _doc, QDomElement & _this )

m_presetsModel.saveSettings( _doc, _this, "preset" );
m_spreadModel.saveSettings( _doc, _this, "spread" );
m_randomModel.saveSettings(_doc, _this, "randomness");
m_versionModel.saveSettings( _doc, _this, "version" );
m_isOldVersionModel.saveSettings( _doc, _this, "oldversion" );
}
Expand Down Expand Up @@ -189,6 +191,7 @@ void MalletsInstrument::loadSettings( const QDomElement & _this )

m_presetsModel.loadSettings( _this, "preset" );
m_spreadModel.loadSettings( _this, "spread" );
m_randomModel.loadSettings(_this, "randomness");
m_isOldVersionModel.loadSettings( _this, "oldversion" );

// To maintain backward compatibility
Expand Down Expand Up @@ -284,7 +287,7 @@ void MalletsInstrument::playNote( NotePlayHandle * _n,
}

int p = m_presetsModel.value();

const float freq = _n->frequency();
if (!_n->m_pluginData)
{
Expand All @@ -293,6 +296,29 @@ void MalletsInstrument::playNote( NotePlayHandle * _n,
m_isOldVersionModel.value() ? 100.0 : 200.0;
const float vel = _n->getVolume() / velocityAdjust;

const float random = m_randomModel.value();
float hardness = m_hardnessModel.value();
float position = m_positionModel.value();
float modulator = m_modulatorModel.value();
float crossfade = m_crossfadeModel.value();

if (p < 9)
{
hardness += random * static_cast<float>(fast_rand() % 128) - 64.0;
hardness = std::clamp(hardness, 0.0f, 128.0f);

position += random * static_cast<float>(fast_rand() % 64) - 32.0;
position = std::clamp(position, 0.0f, 64.0f);
}
else if (p == 9)
{
modulator += random * static_cast<float>(fast_rand() % 128) - 64.0;
modulator = std::clamp(modulator, 0.0f, 128.0f);

crossfade += random * static_cast<float>(fast_rand() % 128) - 64.0;
crossfade = std::clamp(crossfade, 0.0f, 128.0f);
}

// critical section as STK is not thread-safe
static QMutex m;
m.lock();
Expand All @@ -301,8 +327,8 @@ void MalletsInstrument::playNote( NotePlayHandle * _n,
_n->m_pluginData = new MalletsSynth( freq,
vel,
m_stickModel.value(),
m_hardnessModel.value(),
m_positionModel.value(),
hardness,
position,
m_vibratoGainModel.value(),
m_vibratoFreqModel.value(),
p,
Expand All @@ -315,8 +341,8 @@ void MalletsInstrument::playNote( NotePlayHandle * _n,
vel,
p,
m_lfoDepthModel.value(),
m_modulatorModel.value(),
m_crossfadeModel.value(),
modulator,
crossfade,
m_lfoSpeedModel.value(),
m_adsrModel.value(),
(uint8_t) m_spreadModel.value(),
Expand Down Expand Up @@ -412,6 +438,11 @@ MalletsInstrumentView::MalletsInstrumentView( MalletsInstrument * _instrument,
m_spreadKnob->move( 190, 140 );
m_spreadKnob->setHintText( tr( "Spread:" ), "" );

m_randomKnob = new Knob(KnobType::Vintage32, this);
m_randomKnob->setLabel(tr("Random"));
m_randomKnob->move(190, 190);
m_randomKnob->setHintText(tr("Random:"), "");

// try to inform user about missing Stk-installation
if( _instrument->m_filesMissing && getGUI() != nullptr )
{
Expand Down Expand Up @@ -467,7 +498,7 @@ QWidget * MalletsInstrumentView::setupModalBarControls( QWidget * _parent )
m_stickKnob->setLabel( tr( "Stick mix" ) );
m_stickKnob->move( 190, 90 );
m_stickKnob->setHintText( tr( "Stick mix:" ), "" );

return( widget );
}

Expand Down Expand Up @@ -565,6 +596,7 @@ void MalletsInstrumentView::modelChanged()
// m_strikeLED->setModel( &inst->m_strikeModel );
m_presetsCombo->setModel( &inst->m_presetsModel );
m_spreadKnob->setModel( &inst->m_spreadModel );
m_randomKnob->setModel(&inst->m_randomModel);
}


Expand Down
2 changes: 2 additions & 0 deletions plugins/Stk/Mallets/Mallets.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class MalletsInstrument : public Instrument

ComboBoxModel m_presetsModel;
FloatModel m_spreadModel;
FloatModel m_randomModel;
IntModel m_versionModel;
BoolModel m_isOldVersionModel;

Expand Down Expand Up @@ -255,6 +256,7 @@ public slots:

ComboBox * m_presetsCombo;
Knob * m_spreadKnob;
Knob * m_randomKnob;
};


Expand Down

0 comments on commit 7dc0052

Please sign in to comment.