Bug fixed for unix error "readlink /proc/self/fd/0" on MacOS.
[Faustine.git] / interpreter / preprocessor / faust-0.9.47mr3 / examples / faust-stk / modalBar.dsp
1 declare name "Modal Bar";
2 declare description "Nonlinear Modal percussive instruments";
3 declare author "Romain Michon (rmichon@ccrma.stanford.edu)";
4 declare copyright "Romain Michon";
5 declare version "1.0";
6 declare licence "STK-4.3"; // Synthesis Tool Kit 4.3 (MIT style license);
7 declare description "A number of different struck bar instruments. Presets numbers: 0->Marimba, 1->Vibraphone, 2->Agogo, 3->Wood1, 4->Reso, 5->Wood2, 6->Beats, 7->2Fix; 8->Clump";
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 stickHardness = hslider("h:Physical_and_Nonlinearity/v:Physical_Parameters/Stick_Hardness
19 [2][tooltip:A value between 0 and 1]",0.25,0,1,0.01);
20 reson = nentry("h:Physical_and_Nonlinearity/v:Physical_Parameters/Resonance
21 [2][tooltip:A value between 0 and 1]",1,0,1,1);
22 presetNumber = nentry("h:Physical_and_Nonlinearity/v:Physical_Parameters/Preset
23 [2][tooltip:0->Marimba, 1->Vibraphone, 2->Agogo, 3->Wood1, 4->Reso, 5->Wood2, 6->Beats, 7->2Fix; 8->Clump]",1,0,8,1);
24
25 typeModulation = nentry("h:Physical_and_Nonlinearity/v:Nonlinear_Filter_Parameters/Modulation_Type
26 [3][tooltip: 0=theta is modulated by the incoming signal; 1=theta is modulated by the averaged incoming signal;
27 2=theta is modulated by the squared incoming signal; 3=theta is modulated by a sine wave of frequency freqMod;
28 4=theta is modulated by a sine wave of frequency freq;]",0,0,4,1);
29 nonLinearity = hslider("h:Physical_and_Nonlinearity/v:Nonlinear_Filter_Parameters/Nonlinearity
30 [3][tooltip:Nonlinearity factor (value between 0 and 1)]",0,0,1,0.01);
31 frequencyMod = hslider("h:Physical_and_Nonlinearity/v:Nonlinear_Filter_Parameters/Modulation_Frequency
32 [3][unit:Hz][tooltip:Frequency of the sine wave for the modulation of theta (works if Modulation Type=3)]",220,20,1000,0.1);
33 nonLinAttack = hslider("h:Physical_and_Nonlinearity/v:Nonlinear_Filter_Parameters/Nonlinearity_Attack
34 [3][unit:s][Attack duration of the nonlinearity]",0.1,0,2,0.01);
35
36 vibratoFreq = hslider("h:Envelopes_and_Vibrato/v:Vibrato_Parameters/Vibrato_Freq
37 [4][unit:Hz]",6,1,15,0.1);
38 vibratoGain = hslider("h:Envelopes_and_Vibrato/v:Vibrato_Parameters/Vibrato_Gain
39 [4][tooltip:A value between 0 and 1]",0.1,0,1,0.01);
40
41 //==================== SIGNAL PROCESSING ================
42
43 //----------------------- Nonlinear filter ----------------------------
44 //nonlinearities are created by the nonlinear passive allpass ladder filter declared in filter.lib
45
46 //nonlinear filter order
47 nlfOrder = 6;
48
49 //nonLinearModultor is declared in instrument.lib, it adapts allpassnn from filter.lib
50 //for using it with waveguide instruments
51 NLFM = nonLinearModulator((nonLinearity : smooth(0.999)),1,freq,
52 typeModulation,(frequencyMod : smooth(0.999)),nlfOrder);
53
54 //----------------------- Synthesis parameters computing and functions declaration ----------------------------
55
56 //stereoizer is declared in instrument.lib and implement a stereo spacialisation in function of
57 //the frequency period in number of samples
58 stereo = stereoizer(SR/freq);
59
60 //check if the vibraphone is used
61 vibratoOn = presetNumber == 1;
62
63 //vibrato
64 vibrato = 1 + osc(vibratoFreq)*vibratoGain*vibratoOn;
65
66 //filter bank output gain
67 directGain = loadPreset(presetNumber,3,2);
68
69 //modal values for the filter bank
70 loadPreset = ffunction(float loadPreset (int,int,int), <modalBar.h>,"");
71
72 //filter bank using biquad filters
73 biquadBank = _ <: sum(i, 4, oneFilter(i))
74 with{
75 condition(x) = x<0 <: *(-x),((-(1))*-1)*x*freq :> +;
76 dampCondition = (gate < 1) & (reson != 1);
77
78 //the filter coefficients are interpolated when changing of preset
79 oneFilter(j,y) = (loadPreset(presetNumber,0,j : smooth(0.999)) : condition),
80 loadPreset(presetNumber,1,j : smooth(0.999))*(1-(gain*0.03*dampCondition)),
81 y*(loadPreset(presetNumber,2,j) : smooth(0.999)) : bandPassH;
82 };
83
84 //one pole filter with pole set at 0.9 for pre-filtering, onePole is declared in instrument.lib
85 sourceFilter = onePole(b0,a1)
86 with{
87 b0 = 1 - 0.9;
88 a1 = -0.9;
89 };
90
91 //excitation signal
92 excitation = counterSamples < (marmstk1TableSize*rate) : *(marmstk1Wave*gate)
93 with{
94 //readMarmstk1 and marmstk1TableSize are both declared in instrument.lib
95 marmstk1 = time%marmstk1TableSize : int : readMarmstk1;
96
97 dataRate(readRate) = readRate : (+ : decimal) ~ _ : *(float(marmstk1TableSize));
98
99 //the reading rate of the stick table is defined in function of the stickHardness
100 rate = 0.25*pow(4,stickHardness);
101
102 counterSamples = (*(gate)+1)~_ : -(1);
103 marmstk1Wave = rdtable(marmstk1TableSize,marmstk1,int(dataRate(rate)*gate));
104 };
105
106 process = excitation : sourceFilter : *(gain) <:
107 //resonance
108 (biquadBank <: -(*(directGain))) + (directGain*_) :
109 //vibrato for the vibraphone
110 *(vibrato) : NLFM*0.6 : stereo : instrReverb;