New directory tree, with preprocessor/ inside interpretor/.
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / examples / faust-stk / tunedBar.dsp
1 declare name "Tuned Bar";
2 declare description "Nonlinear Banded Waveguide Models";
3 declare author "Romain Michon";
4 declare copyright "Romain Michon (rmichon@ccrma.stanford.edu)";
5 declare version "1.0";
6 declare licence "STK-4.3"; // Synthesis Tool Kit 4.3 (MIT style license);
7 declare description "This instrument uses banded waveguide. For more information, see Essl, G. and Cook, P. Banded Waveguides: Towards Physical Modelling of Bar Percussion Instruments, Proceedings of the 1999 International Computer Music Conference.";
8
9 import("music.lib");
10 import("instrument.lib");
11
12 //==================== GUI SPECIFICATION ================
13
14 freq = nentry("h:Basic_Parameters/freq [1][unit:Hz] [tooltip:Tone frequency]",440,20,20000,1);
15 gain = nentry("h:Basic_Parameters/gain [1][tooltip:Gain (value between 0 and 1)]",0.8,0,1,0.01);
16 gate = button("h:Basic_Parameters/gate [1][tooltip:noteOn = 1, noteOff = 0]");
17
18 select = nentry("h:Physical_and_Nonlinearity/v:Physical_Parameters/Excitation_Selector
19 [2][tooltip:0=Bow; 1=Strike]",0,0,1,1);
20 integrationConstant = hslider("h:Physical_and_Nonlinearity/v:Physical_Parameters/Integration_Constant
21 [2][tooltip:A value between 0 and 1]",0,0,1,0.01);
22 baseGain = hslider("h:Physical_and_Nonlinearity/v:Physical_Parameters/Base_Gain
23 [2][tooltip:A value between 0 and 1]",1,0,1,0.01);
24 bowPressure = hslider("h:Physical_and_Nonlinearity/v:Physical_Parameters/Bow_Pressure
25 [2][tooltip:Bow pressure on the instrument (Value between 0 and 1)]",0.2,0,1,0.01);
26 bowPosition = hslider("h:Physical_and_Nonlinearity/v:Physical_Parameters/Bow_Position
27 [2][tooltip:Bow position on the instrument (Value between 0 and 1)]",0,0,1,0.01);
28
29 typeModulation = nentry("h:Physical_and_Nonlinearity/v:Nonlinear_Filter_Parameters/Modulation_Type
30 [3][tooltip: 0=theta is modulated by the incoming signal; 1=theta is modulated by the averaged incoming signal;
31 2=theta is modulated by the squared incoming signal; 3=theta is modulated by a sine wave of frequency freqMod;
32 4=theta is modulated by a sine wave of frequency freq;]",0,0,4,1);
33 nonLinearity = hslider("h:Physical_and_Nonlinearity/v:Nonlinear_Filter_Parameters/Nonlinearity
34 [3][tooltip:Nonlinearity factor (value between 0 and 1)]",0,0,1,0.01);
35 frequencyMod = hslider("h:Physical_and_Nonlinearity/v:Nonlinear_Filter_Parameters/Modulation_Frequency
36 [3][unit:Hz][tooltip:Frequency of the sine wave for the modulation of theta (works if Modulation Type=3)]",220,20,1000,0.1);
37 nonLinAttack = hslider("h:Physical_and_Nonlinearity/v:Nonlinear_Filter_Parameters/Nonlinearity_Attack
38 [3][unit:s][Attack duration of the nonlinearity]",0.1,0,2,0.01);
39
40 //==================== MODAL PARAMETERS ================
41
42 preset = 2;
43
44 nMode(2) = 4;
45
46 modes(2,0) = 1;
47 basegains(2,0) = pow(0.999,1);
48 excitation(2,0) = 1*gain*gate/nMode(2);
49
50 modes(2,1) = 4.0198391420;
51 basegains(2,1) = pow(0.999,2);
52 excitation(2,1) = 1*gain*gate/nMode(2);
53
54 modes(2,2) = 10.7184986595;
55 basegains(2,2) = pow(0.999,3);
56 excitation(2,2) = 1*gain*gate/nMode(2);
57
58 modes(2,3) = 18.0697050938;
59 basegains(2,3) = pow(0.999,4);
60 excitation(2,3) = 1*gain*gate/nMode(2);
61
62 //==================== SIGNAL PROCESSING ================
63
64 //----------------------- Nonlinear filter ----------------------------
65 //nonlinearities are created by the nonlinear passive allpass ladder filter declared in filter.lib
66
67 //nonlinear filter order
68 nlfOrder = 6;
69
70 //nonLinearModultor is declared in instrument.lib, it adapts allpassnn from filter.lib
71 //for using it with waveguide instruments
72 NLFM = nonLinearModulator((nonLinearity : smooth(0.999)),1,freq,
73 typeModulation,(frequencyMod : smooth(0.999)),nlfOrder);
74
75 //----------------------- Synthesis parameters computing and functions declaration ----------------------------
76
77 //the number of modes depends on the preset being used
78 nModes = nMode(preset);
79
80 //bow table parameters
81 tableOffset = 0;
82 tableSlope = 10 - (9*bowPressure);
83
84 delayLengthBase = SR/freq;
85
86 //delay lengths in number of samples
87 delayLength(x) = delayLengthBase/modes(preset,x);
88
89 //delay lines
90 delayLine(x) = delay(4096,delayLength(x));
91
92 //Filter bank: bandpass filters (declared in instrument.lib)
93 radius = 1 - PI*32/SR;
94 bandPassFilter(x) = bandPass(freq*modes(preset,x),radius);
95
96 //Delay lines feedback for bow table lookup control
97 baseGainApp = 0.8999999999999999 + (0.1*baseGain);
98 velocityInputApp = integrationConstant;
99 velocityInput = velocityInputApp + _*baseGainApp,par(i,(nModes-1),(_*baseGainApp)) :> +;
100
101 //Bow velocity is controled by an ADSR envelope
102 maxVelocity = 0.03 + 0.1*gain;
103 bowVelocity = maxVelocity*adsr(0.02,0.005,90,0.01,gate);
104
105 //stereoizer is declared in instrument.lib and implement a stereo spacialisation in function of
106 //the frequency period in number of samples
107 stereo = stereoizer(delayLengthBase);
108
109 //----------------------- Algorithm implementation ----------------------------
110
111 //Bow table lookup (bow is decalred in instrument.lib)
112 bowing = bowVelocity - velocityInput <: *(bow(tableOffset,tableSlope)) : /(nModes);
113
114 //One resonance
115 resonance(x) = + : + (excitation(preset,x)*select) : delayLine(x) : *(basegains(preset,x)) : bandPassFilter(x);
116
117 process =
118 //Bowed Excitation
119 (bowing*((select-1)*-1) <:
120 //nModes resonances with nModes feedbacks for bow table look-up
121 par(i,nModes,(resonance(i)~_)))~par(i,nModes,_) :> + :
122 //Signal Scaling and stereo
123 *(4) : NLFM : stereo : instrReverb;