Merge branch 'master' of https://scm.cri.ensmp.fr/git/Faustine
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / examples / faust-stk / glassHarmonica.dsp
1 declare name "Glass Harmonica";
2 declare description "Nonlinear Banded Waveguide Modeled Glass Harmonica";
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 = 3;
43
44 nMode(3) = 6;
45
46 modes(3,0) = 1.0;
47 basegains(3,0) = pow(0.999,1);
48 excitation(3,0) = 1*gain*gate/(nMode(3) - 1);
49
50 modes(3,1) = 2.32;
51 basegains(3,1) = pow(0.999,2);
52 excitation(3,1) = 1*gain*gate/(nMode(3) - 1);
53
54 modes(3,2) = 4.25;
55 basegains(3,2) = pow(0.999,3);
56 excitation(3,2) = 1*gain*gate/(nMode(3) - 1);
57
58 modes(3,3) = 6.63;
59 basegains(3,3) = pow(0.999,4);
60 excitation(3,3) = 1*gain*gate/(nMode(3) - 1);
61
62 modes(3,4) = 9.38;
63 basegains(3,4) = pow(0.999,5);
64 excitation(3,4) = 1*gain*gate/(nMode(3) - 1);
65
66 modes(3,5) = 0 : float;
67 basegains(3,5) = 0 : float;
68 excitation(3,5) = 0 : float;
69
70 //==================== SIGNAL PROCESSING ================
71
72 //----------------------- Nonlinear filter ----------------------------
73 //nonlinearities are created by the nonlinear passive allpass ladder filter declared in filter.lib
74
75 //nonlinear filter order
76 nlfOrder = 6;
77
78 //nonLinearModultor is declared in instrument.lib, it adapts allpassnn from filter.lib
79 //for using it with waveguide instruments
80 NLFM = nonLinearModulator((nonLinearity : smooth(0.999)),1,freq,
81 typeModulation,(frequencyMod : smooth(0.999)),nlfOrder);
82
83 //----------------------- Synthesis parameters computing and functions declaration ----------------------------
84
85 //the number of modes depends on the preset being used
86 nModes = nMode(preset);
87
88 //bow table parameters
89 tableOffset = 0;
90 tableSlope = 10 - (9*bowPressure);
91
92 delayLengthBase = SR/freq;
93
94 //delay lengths in number of samples
95 delayLength(x) = delayLengthBase/modes(preset,x);
96
97 //delay lines
98 delayLine(x) = delay(4096,delayLength(x));
99
100 //Filter bank: bandpass filters (declared in instrument.lib)
101 radius = 1 - PI*32/SR;
102 bandPassFilter(x) = bandPass(freq*modes(preset,x),radius);
103
104 //Delay lines feedback for bow table lookup control
105 baseGainApp = 0.8999999999999999 + (0.1*baseGain);
106 velocityInputApp = integrationConstant;
107 velocityInput = velocityInputApp + _*baseGainApp,par(i,(nModes-1),(_*baseGainApp)) :> +;
108
109 //Bow velocity is controled by an ADSR envelope
110 maxVelocity = 0.03 + 0.1*gain;
111 bowVelocity = maxVelocity*adsr(0.02,0.005,90,0.01,gate);
112
113 //stereoizer is declared in instrument.lib and implement a stereo spacialisation in function of
114 //the frequency period in number of samples
115 stereo = stereoizer(delayLengthBase);
116
117 //----------------------- Algorithm implementation ----------------------------
118
119 //Bow table lookup (bow is decalred in instrument.lib)
120 bowing = bowVelocity - velocityInput <: *(bow(tableOffset,tableSlope)) : /(nModes);
121
122 //One resonance
123 resonance(x) = + : + (excitation(preset,x)*select) : delayLine(x) : *(basegains(preset,x)) : bandPassFilter(x);
124
125 process =
126 //Bowed Excitation
127 (bowing*((select-1)*-1) <:
128 //nModes resonances with nModes feedbacks for bow table look-up
129 par(i,nModes,(resonance(i)~_)))~par(i,nModes,_) :> + :
130 //Signal Scaling and stereo
131 *(4) : NLFM : stereo : instrReverb;