-
Notifications
You must be signed in to change notification settings - Fork 62
/
spats.lib
173 lines (153 loc) · 5.86 KB
/
spats.lib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//#################################### spats.lib ##########################################
// This library contains a collection of tools for sound spatialization.
// Its official prefix is `sp`.
//
// #### References
// * <https://github.com/grame-cncm/faustlibraries/blob/master/spats.lib>
//########################################################################################
/************************************************************************
************************************************************************
FAUST library file
Copyright (C) 2003-2012 GRAME, Centre National de Creation Musicale
----------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
EXCEPTION TO THE LGPL LICENSE : As a special exception, you may create a
larger FAUST program which directly or indirectly imports this library
file and still distribute the compiled code generated by the FAUST
compiler, or a modified version of this compiled code, under your own
copyright and license. This EXCEPTION TO THE LGPL LICENSE explicitly
grants you the right to freely choose the license for the resulting
compiled code. In particular the resulting compiled code has no obligation
to be LGPL or GPL. For example you are free to choose a commercial or
closed source license or any other license if you decide so.
************************************************************************
************************************************************************/
si = library("signals.lib");
ma = library("maths.lib");
declare name "Faust Spatialization Library";
declare version "1.1.0";
//-----------------------`(sp.)panner`------------------------
// A simple linear stereo panner.
// `panner` is a standard Faust function.
//
// #### Usage
//
// ```
// _ : panner(g) : _,_
// ```
//
// Where:
//
// * `g`: the panning (0-1)
//------------------------------------------------------------
panner(g) = _ <: *(1-g), *(g);
// TODO: need demo function for panner here
//---------------`(sp.)constantPowerPan`----------------------
// Apply the constant power pan rule to a stereo signal.
// The channels are not respatialized. Their gains are simply
// adjusted. A pan of 0 preserves the left channel and silences
// the right channel. A pan of 1 has the opposite effect.
// A pan value of 0.5 applies a gain of 0.5 to both channels.
//
// #### Usage
//
// ```
// _,_ : constantPowerPan(p) : _,_
// ```
//
// Where:
//
// * `p`: the panning (0-1)
//------------------------------------------------------------
declare dryWetMixer author "David Braun";
constantPowerPan(p, x, y) = x*gainLeft, y*gainRight
with {
theta = ma.PI*p/2.;
gainLeft = cos(theta)/sqrt(2.);
gainRight = sin(theta)/sqrt(2.);
};
//-----------------------`(sp.)spat`------------------------
// GMEM SPAT: n-outputs spatializer.
// `spat` is a standard Faust function.
//
// #### Usage
//
// ```
// _ : spat(N,r,d) : si.bus(N)
// ```
//
// Where:
//
// * `N`: number of outputs (a constant numerical expression)
// * `r`: rotation (between 0 et 1)
// * `d`: distance of the source (between 0 et 1)
//------------------------------------------------------
declare spat author "Laurent Pottier, revised by Romain Michon";
spat(N,a,d) = _ <: par(i, N, *(scaler(i, N, a, d) : si.smooth(0.9999)))
with {
scaler(i,N,a,d) = (d/2.0+0.5)
* sqrt(max(0.0, 1.0 - abs(fmod(a+0.5+float(N-i)/N, 1.0) - 0.5) * N * d));
};
//---------------`(sp.)stereoize`-------------
// Transform an arbitrary processor `p` into a stereo processor with 2 inputs
// and 2 outputs.
//
// #### Usage
//
// ```
// _,_ : stereoize(p) : _,_
// ```
//
// Where:
//
// * `p`: the arbitrary processor
//----------------------------------------
stereoize(p) = S(inputs(p), outputs(p))
with {
// degenerated processor with no outputs
S(n,0) = !,! : 0,0; // just in case, probably a rare case
// processors with no inputs
S(0,1) = !,! : p <: _,_; // add two fake inputs and split output
S(0,2) = !,! : p;
S(0,n) = !,! : p,p :> _,_; // we are sure this will work if n is odd
// processors with one input
S(1,1) = p,p; // add two fake inputs and split output
S(1,n) = p,p :> _,_; // we are sure this will work if n is odd
// processors with two inputs
S(2,1) = p <: _,_; // split the output
S(2,2) = p; // nothing to do, p is already stereo
// processors with inputs > 2 and outputs > 2
S(n,m) = _,_ <: p,p :> _,_; // we are sure this works if n or p are odd
};
// TODO: need demo function of spat here
//////////////////////////////////////////////////////////////////////////////////////////
// UNDOCUMENTED/DISMISSED ELEMENTS
//////////////////////////////////////////////////////////////////////////////////////////
// music.lib:
// The following functions could remain available but they would have to be
// factorized and reimplemented using the `par` function...
// bus2 = _,_;
// bus3 = _,_,_;
// bus4 = _,_,_,_;
// bus5 = _,_,_,_,_;
// bus6 = _,_,_,_,_,_;
// bus7 = _,_,_,_,_,_,_;
// bus8 = _,_,_,_,_,_,_,_;
// gain2(g) = *(g),*(g);
// gain3(g) = *(g),*(g),*(g);
// gain4(g) = *(g),*(g),*(g),*(g);
// gain5(g) = *(g),*(g),*(g),*(g),*(g);
// gain6(g) = *(g),*(g),*(g),*(g),*(g),*(g);
// gain7(g) = *(g),*(g),*(g),*(g),*(g),*(g),*(g);
// gain8(g) = *(g),*(g),*(g),*(g),*(g),*(g),*(g),*(g);