annotate examples/10-Instruments/airharp/Junction.cpp @ 556:ce391098f321 prerelease tip

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