-
Notifications
You must be signed in to change notification settings - Fork 3
/
safpy_build.py
243 lines (189 loc) · 7.21 KB
/
safpy_build.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import os.path
import sys
from cffi import FFI
ffibuilder = FFI()
home_dir = os.path.expanduser('~')
this_dir = os.path.abspath(os.path.dirname(__file__))
# define saf_path here, assumes SAF parallel to safpy by default.
saf_path = os.path.join(this_dir, ".", "Spatial_Audio_Framework")
# Populate these
c_header_source = ""
include_dirs = []
libraries = []
library_dirs = []
saf_performance_lib = []
extra_link_args = []
# Sensible default, please adjust if needed.
# In case of doubt, you can also check numpy.show_config() for its BLAS
print("Detected platform: " + sys.platform)
if sys.platform == "darwin":
print("SAFPY using default Apple Accelerate")
extra_link_args.extend(['-Wl,-framework', '-Wl,Accelerate'])
elif sys.platform == "linux":
print("SAFPY using default OpenBLAS/LAPACKE")
saf_performance_lib.extend(["openblas", "lapacke"])
elif sys.platform == "win32":
print("SAFPY using default OpenBLAS/LAPACKE")
saf_performance_lib.extend(["libopenblas"]) # should include lapacke
else:
print("No defaults.")
# cdef() expects a single string declaring the C types, functions and
# globals needed to use the shared object. It must be in valid C syntax.
ffibuilder.cdef("""
#define SAF_VERSION ...
""")
if sys.platform != "win32":
ffibuilder.cdef("""
typedef float _Complex float_complex;
typedef double _Complex double_complex;
""")
else:
ffibuilder.cdef("""
typedef struct
{
float _Val[2];
} float_complex;
""")
ffibuilder.cdef("""
typedef enum _AFSTFT_FDDATA_FORMAT{
AFSTFT_BANDS_CH_TIME, /**< nBands x nChannels x nTimeHops */
AFSTFT_TIME_CH_BANDS /**< nTimeHops x nChannels x nBands */
}AFSTFT_FDDATA_FORMAT;
void *malloc(size_t size);
void free(void *ptr);
/**
* 2-D malloc (contiguously allocated, so use free() as usual to deallocate)
*/
void** malloc2d(size_t dim1, size_t dim2, size_t data_size);
/**
* 2-D calloc (contiguously allocated, so use free() as usual to deallocate)
*/
void** calloc2d(size_t dim1, size_t dim2, size_t data_size);
/**
* 3-D malloc (contiguously allocated, so use free() as usual to deallocate)
*/
void*** malloc3d(size_t dim1, size_t dim2, size_t dim3, size_t data_size);
/**
* 3-D calloc (contiguously allocated, so use free() as usual to deallocate)
*/
void*** calloc3d(size_t dim1, size_t dim2, size_t dim3, size_t data_size);
long double factorial(int n);
void getSHreal(/* Input Arguments */
int order,
float* dirs_rad,
int nDirs,
/* Output Arguments */
float* Y);
void getSHreal_recur(/* Input Arguments */
int order,
float* dirs_rad,
int nDirs,
/* Output Arguments */
float* Y);
void getSHreal_part
(
int order_start,
int order_end,
float* dirs_rad,
int nDirs,
float* Y);
void getSHcomplex(/* Input Arguments */
int order,
float* dirs_rad,
int nDirs,
/* Output Arguments */
float_complex* Y);
void generateVBAPgainTable3D(/* Input arguments */
float* ls_dirs_deg,
int L,
int az_res_deg,
int el_res_deg,
int omitLargeTriangles,
int enableDummies,
float spread,
/* Output arguments */
float** gtable,
int* N_gtable,
int* nTriangles);
void afSTFT_create(void ** const phSTFT,
int nCHin,
int nCHout,
int hopsize,
int lowDelayMode,
int hybridmode,
AFSTFT_FDDATA_FORMAT format);
void afSTFT_destroy(void ** const phSTFT);
int afSTFT_getNBands(void * const hSTFT);
void afSTFT_getCentreFreqs(void * const hSTFT,
float fs,
int nBands,
float* freqVector);
int afSTFT_getProcDelay(void * const hSTFT);
void afSTFT_clearBuffers(void * const hSTFT);
void afSTFT_forward(void * const hSTFT,
float** dataTD,
int framesize,
float_complex*** dataFD);
void afSTFT_backward(void * const hSTFT,
float_complex*** dataFD,
int framesize,
float** dataTD);
void afSTFT_forward_flat(void * const hSTFT,
float* dataTD,
int framesize,
float_complex* dataFD);
void afSTFT_backward_flat(void * const hSTFT,
float_complex* dataFD,
int framesize,
float* dataTD);
void latticeDecorrelator_create(void ** phDecor,
float fs,
int hopsize,
float * freqVector,
int nBands,
int nCH,
int * orders,
float * freqCutoffs,
int nCutoffs,
int maxDelay,
int lookupOffset,
float enComp_coeff);
void latticeDecorrelator_destroy(void ** phDecor);
void latticeDecorrelator_reset(void * hDecor);
void latticeDecorrelator_apply(void * hDecor,
float_complex *** inFrame,
int nTimeSlots,
float_complex *** decorFrame);
void transientDucker_create(void ** phDucker,
int nCH,
int nBands);
void transientDucker_destroy(void ** phDucker);
void transientDucker_apply(void * hDucker,
float_complex *** inFrame,
int nTimeSlots,
float alpha,
float beta,
float_complex *** residualFrame,
float_complex *** transientFrame);
""")
# set_source() gives the name of the python extension module to
# produce, and some C source code as a string. This C code needs
# to make the declarated functions, types and globals available,
# so it is often just the "#include".
c_header_source += f"""
#include "{saf_path}/framework/include/saf.h" // the C header of the lib
"""
libraries.append(saf_path + "/build/framework/saf") # lib name, for the linker
libraries.extend(saf_performance_lib)
library_dirs.extend([this_dir, saf_path])
print("Compiling _safpy with:")
print(f"C_Header_Source: {c_header_source}")
print(f"include_dirs: {include_dirs}")
print(f"libraries: {libraries}")
print(f"library_dirs: {library_dirs}")
print(f"extra_link_args: {extra_link_args}")
ffibuilder.set_source("_safpy", c_header_source, include_dirs=include_dirs,
libraries=libraries, library_dirs=library_dirs,
extra_link_args=extra_link_args)
if __name__ == "__main__":
ffibuilder.compile(verbose=True)