chris@164: /* chris@164: * chris@164: * Plectrum model for touching and plucking strings chris@164: * chris@164: * Christian Heinrichs 04/2015 chris@164: * chris@164: * [inspired by E. Berdahl's pluck~ abstraction for the FireFader] chris@164: * chris@164: */ chris@164: chris@164: #include "Plectrum.h" chris@164: chris@164: #include "../include/Utilities.h" chris@164: #include chris@164: #include chris@164: #include chris@164: chris@164: Plectrum::Plectrum() { chris@164: chris@164: _contact = 0; chris@164: _lastDistance = 0; chris@164: chris@164: } chris@164: chris@164: void Plectrum::setup(float spring, float damp, float hyst) { chris@164: chris@164: _spring = spring; chris@164: _damp = damp; chris@164: _hyst = hyst; chris@164: chris@164: } chris@164: chris@164: float Plectrum::update(float position, float stringPosition) { chris@164: chris@164: float distance = position - stringPosition; chris@164: chris@164: // Calculate spring/damp forces based on distance to string chris@164: chris@164: float springOut = distance * _spring; chris@164: chris@164: float dampOut = (distance - _lastDistance) * 44100; chris@164: chris@164: float out = springOut+dampOut; chris@164: chris@164: // If distance crosses zero, enable contact chris@164: chris@164: if((distance>0 && _lastDistance<=0)||(distance<0 && _lastDistance>=0)) chris@164: _contact = 1; chris@164: chris@164: // If distance exceeds hysteresis threshold, jump to zero (i.e. 'pluck') chris@164: chris@164: if(fabs(distance)>_hyst) chris@164: _contact = 0; chris@164: chris@164: // FIXME: contact doesn't switch back to zero if distance goes back in original direction chris@164: chris@164: _lastDistance = distance; chris@164: chris@164: return out * _contact; chris@164: chris@164: }