Mercurial > hg > beaglert
comparison projects/airharp/Junction.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 * Excitation Junction for two waveguides | |
4 * | |
5 * Christian Heinrichs 04/2015 | |
6 * | |
7 */ | |
8 | |
9 #include "Junction.h" | |
10 #include "../include/Utilities.h" | |
11 | |
12 Junction::Junction() { | |
13 | |
14 setFrequency(440); | |
15 _dt = 1.0/44100.0; | |
16 | |
17 // initialize variables | |
18 for(int i=0;i<WG_BUFFER_SIZE;i++) { | |
19 _buffer_l[i] = 0; | |
20 _buffer_r[i] = 0; | |
21 } | |
22 _excitation = 0; | |
23 _lastPlectrumDisplacement = 0; | |
24 _readPtr = 0; | |
25 | |
26 } | |
27 | |
28 void Junction::update(float excitation, float left, float right) { | |
29 | |
30 // 1. advance delay buffer read pointer | |
31 | |
32 if(++_readPtr>=WG_BUFFER_SIZE) | |
33 _readPtr=0; | |
34 | |
35 // 2. add excitation sample into buffer | |
36 | |
37 _buffer_l[(_readPtr+_delay_l+WG_BUFFER_SIZE)%WG_BUFFER_SIZE] = excitation; | |
38 _buffer_r[(_readPtr+_delay_r+WG_BUFFER_SIZE)%WG_BUFFER_SIZE] = excitation; | |
39 | |
40 // 3. feed right input to left output and vice versa | |
41 | |
42 _buffer_l[_readPtr] += right; | |
43 _buffer_r[_readPtr] += left; | |
44 | |
45 // 4. store excitation value for later use | |
46 _excitation = excitation; | |
47 | |
48 } | |
49 | |
50 float Junction::getOutput(int direction) { | |
51 | |
52 if(direction = 0) | |
53 return _buffer_l[_readPtr]; | |
54 else | |
55 return _buffer_r[_readPtr]; | |
56 | |
57 } | |
58 | |
59 float Junction::getExcitationDisplacement() { | |
60 | |
61 // string displacement and excitation force | |
62 // use delayed value to account for excitation position | |
63 float in = _buffer_l[(_readPtr+_delay_l+WG_BUFFER_SIZE)%WG_BUFFER_SIZE] + _excitation; | |
64 | |
65 // integrate total force | |
66 float out = 0.00001 * in + 0.99999 * _lastPlectrumDisplacement; | |
67 | |
68 // store variable for next iteration | |
69 _lastPlectrumDisplacement = out; | |
70 | |
71 // multiply by delta time | |
72 return out * _dt; | |
73 | |
74 } | |
75 | |
76 void Junction::setPluckPosition(float pluckPos){ | |
77 | |
78 pluckPos = constrain(pluckPos,0,1); | |
79 _delay_l = pluckPos * _periodInSamples; | |
80 _delay_r = (1-pluckPos) * _periodInSamples; | |
81 | |
82 } | |
83 | |
84 void Junction::setPeriod(float period) { | |
85 | |
86 _periodInMilliseconds = period; | |
87 | |
88 } | |
89 | |
90 void Junction::setFrequency(float frequency) { | |
91 | |
92 _periodInMilliseconds = 1000.0/frequency; | |
93 _periodInSamples = (int)(_periodInMilliseconds * 44.1); | |
94 | |
95 } |