comparison examples/10-Instruments/airharp/Plectrum.cpp @ 464:8fcfbfb32aa0 prerelease

Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author Robert Jack <robert.h.jack@gmail.com>
date Mon, 20 Jun 2016 16:20:38 +0100
parents
children
comparison
equal deleted inserted replaced
463:c47709e8b5c9 464:8fcfbfb32aa0
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 }