annotate projects/airharp/Junction.cpp @ 191:b3a306da03e0

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