comparison examples/airharp/Plectrum.cpp @ 300:dbeed520b014 prerelease

Renamed projects to examples
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 27 May 2016 13:58:20 +0100
parents projects/airharp/Plectrum.cpp@40badaff5729
children
comparison
equal deleted inserted replaced
297:a3d83ebdf49b 300:dbeed520b014
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 }