Mercurial > hg > beaglert
comparison projects/airharp/Plectrum.cpp @ 164:40badaff5729 heavy-updated
- added more pd/heavy examples
- removed heavy-specific flags from Makefile
- added air-harp cpp example project
| author | chnrx <chris.heinrichs@gmail.com> | 
|---|---|
| date | Thu, 03 Dec 2015 16:19:33 +0000 | 
| parents | |
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| 163:20b52283c7b4 | 164:40badaff5729 | 
|---|---|
| 1 /* | |
| 2 * | |
| 3 * Plectrum model for touching and plucking strings | |
| 4 * | |
| 5 * Christian Heinrichs 04/2015 | |
| 6 * | |
| 7 * [inspired by E. Berdahl's pluck~ abstraction for the FireFader] | |
| 8 * | |
| 9 */ | |
| 10 | |
| 11 #include "Plectrum.h" | |
| 12 | |
| 13 #include "../include/Utilities.h" | |
| 14 #include <cmath> | |
| 15 #include <stdio.h> | |
| 16 #include <cstdlib> | |
| 17 | |
| 18 Plectrum::Plectrum() { | |
| 19 | |
| 20 _contact = 0; | |
| 21 _lastDistance = 0; | |
| 22 | |
| 23 } | |
| 24 | |
| 25 void Plectrum::setup(float spring, float damp, float hyst) { | |
| 26 | |
| 27 _spring = spring; | |
| 28 _damp = damp; | |
| 29 _hyst = hyst; | |
| 30 | |
| 31 } | |
| 32 | |
| 33 float Plectrum::update(float position, float stringPosition) { | |
| 34 | |
| 35 float distance = position - stringPosition; | |
| 36 | |
| 37 // Calculate spring/damp forces based on distance to string | |
| 38 | |
| 39 float springOut = distance * _spring; | |
| 40 | |
| 41 float dampOut = (distance - _lastDistance) * 44100; | |
| 42 | |
| 43 float out = springOut+dampOut; | |
| 44 | |
| 45 // If distance crosses zero, enable contact | |
| 46 | |
| 47 if((distance>0 && _lastDistance<=0)||(distance<0 && _lastDistance>=0)) | |
| 48 _contact = 1; | |
| 49 | |
| 50 // If distance exceeds hysteresis threshold, jump to zero (i.e. 'pluck') | |
| 51 | |
| 52 if(fabs(distance)>_hyst) | |
| 53 _contact = 0; | |
| 54 | |
| 55 // FIXME: contact doesn't switch back to zero if distance goes back in original direction | |
| 56 | |
| 57 _lastDistance = distance; | |
| 58 | |
| 59 return out * _contact; | |
| 60 | |
| 61 } | 
