annotate 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
rev   line source
chris@164 1 /*
chris@164 2 *
chris@164 3 * Plectrum model for touching and plucking strings
chris@164 4 *
chris@164 5 * Christian Heinrichs 04/2015
chris@164 6 *
chris@164 7 * [inspired by E. Berdahl's pluck~ abstraction for the FireFader]
chris@164 8 *
chris@164 9 */
chris@164 10
chris@164 11 #include "Plectrum.h"
chris@164 12
chris@164 13 #include "../include/Utilities.h"
chris@164 14 #include <cmath>
chris@164 15 #include <stdio.h>
chris@164 16 #include <cstdlib>
chris@164 17
chris@164 18 Plectrum::Plectrum() {
chris@164 19
chris@164 20 _contact = 0;
chris@164 21 _lastDistance = 0;
chris@164 22
chris@164 23 }
chris@164 24
chris@164 25 void Plectrum::setup(float spring, float damp, float hyst) {
chris@164 26
chris@164 27 _spring = spring;
chris@164 28 _damp = damp;
chris@164 29 _hyst = hyst;
chris@164 30
chris@164 31 }
chris@164 32
chris@164 33 float Plectrum::update(float position, float stringPosition) {
chris@164 34
chris@164 35 float distance = position - stringPosition;
chris@164 36
chris@164 37 // Calculate spring/damp forces based on distance to string
chris@164 38
chris@164 39 float springOut = distance * _spring;
chris@164 40
chris@164 41 float dampOut = (distance - _lastDistance) * 44100;
chris@164 42
chris@164 43 float out = springOut+dampOut;
chris@164 44
chris@164 45 // If distance crosses zero, enable contact
chris@164 46
chris@164 47 if((distance>0 && _lastDistance<=0)||(distance<0 && _lastDistance>=0))
chris@164 48 _contact = 1;
chris@164 49
chris@164 50 // If distance exceeds hysteresis threshold, jump to zero (i.e. 'pluck')
chris@164 51
chris@164 52 if(fabs(distance)>_hyst)
chris@164 53 _contact = 0;
chris@164 54
chris@164 55 // FIXME: contact doesn't switch back to zero if distance goes back in original direction
chris@164 56
chris@164 57 _lastDistance = distance;
chris@164 58
chris@164 59 return out * _contact;
chris@164 60
chris@164 61 }