Mercurial > hg > beaglert
annotate examples/10-Instruments/airharp/Plectrum.cpp @ 556:ce391098f321 prerelease tip
THIS PROJECT HAS MOVED TO https://github.com/BelaPlatform/bela
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sat, 25 Jun 2016 20:21:00 +0100 |
parents | 8fcfbfb32aa0 |
children |
rev | line source |
---|---|
robert@464 | 1 /* |
robert@464 | 2 * |
robert@464 | 3 * Plectrum model for touching and plucking strings |
robert@464 | 4 * |
robert@464 | 5 * Christian Heinrichs 04/2015 |
robert@464 | 6 * |
robert@464 | 7 * [inspired by E. Berdahl's pluck~ abstraction for the FireFader] |
robert@464 | 8 * |
robert@464 | 9 */ |
robert@464 | 10 |
robert@464 | 11 #include "Plectrum.h" |
robert@464 | 12 |
robert@464 | 13 #include "../include/Utilities.h" |
robert@464 | 14 #include <cmath> |
robert@464 | 15 #include <stdio.h> |
robert@464 | 16 #include <cstdlib> |
robert@464 | 17 |
robert@464 | 18 Plectrum::Plectrum() { |
robert@464 | 19 |
robert@464 | 20 _contact = 0; |
robert@464 | 21 _lastDistance = 0; |
robert@464 | 22 |
robert@464 | 23 } |
robert@464 | 24 |
robert@464 | 25 void Plectrum::setup(float spring, float damp, float hyst) { |
robert@464 | 26 |
robert@464 | 27 _spring = spring; |
robert@464 | 28 _damp = damp; |
robert@464 | 29 _hyst = hyst; |
robert@464 | 30 |
robert@464 | 31 } |
robert@464 | 32 |
robert@464 | 33 float Plectrum::update(float position, float stringPosition) { |
robert@464 | 34 |
robert@464 | 35 float distance = position - stringPosition; |
robert@464 | 36 |
robert@464 | 37 // Calculate spring/damp forces based on distance to string |
robert@464 | 38 |
robert@464 | 39 float springOut = distance * _spring; |
robert@464 | 40 |
robert@464 | 41 float dampOut = (distance - _lastDistance) * 44100; |
robert@464 | 42 |
robert@464 | 43 float out = springOut+dampOut; |
robert@464 | 44 |
robert@464 | 45 // If distance crosses zero, enable contact |
robert@464 | 46 |
robert@464 | 47 if((distance>0 && _lastDistance<=0)||(distance<0 && _lastDistance>=0)) |
robert@464 | 48 _contact = 1; |
robert@464 | 49 |
robert@464 | 50 // If distance exceeds hysteresis threshold, jump to zero (i.e. 'pluck') |
robert@464 | 51 |
robert@464 | 52 if(fabs(distance)>_hyst) |
robert@464 | 53 _contact = 0; |
robert@464 | 54 |
robert@464 | 55 // FIXME: contact doesn't switch back to zero if distance goes back in original direction |
robert@464 | 56 |
robert@464 | 57 _lastDistance = distance; |
robert@464 | 58 |
robert@464 | 59 return out * _contact; |
robert@464 | 60 |
robert@464 | 61 } |