forked from ltfat/ltfat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltfatmex.m
563 lines (477 loc) · 16.4 KB
/
ltfatmex.m
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
function ltfatmex(varargin)
%LTFATMEX Compile Mex/Oct interfaces
% Usage: ltfatmex;
% ltfatmex(...);
%
% `ltfatmex` compiles the C backend in order to speed up the execution of
% the toolbox. The C backend is linked to Matlab and Octave through Mex
% and Octave C++ interfaces.
% Please see INSTALL-Matlab or INSTALL-Octave for the requirements.
%
% The action of `ltfatmex` is determined by one of the following flags:
%
% 'compile' Compile stuff. This is the default.
%
% 'clean' Removes the compiled functions.
%
% 'test' Run some small tests that verify that the compiled
% functions work.
%
% The target to work on is determined by on of the following flags.
%
% General LTFAT:
%
% 'lib' Perform action on the LTFAT C library.
%
% 'mex' Perform action on the mex / oct interfaces.
%
% 'pbc' Perform action on the PolyBoolClipper code for use with MULACLAB
%
% 'auto' Choose automatically which targets to work on from the
% previous ones based on the operation system etc. This is
% the default.
%
% Block-processing framework related:
%
% 'playrec' Perform action on the playrec code for use with real-time
% block streaming framework.
%
% 'java' Perform compilation of JAVA classes into the bytecode.
% The classes makes the GUI for the blockproc. framework.
%
% Other:
%
% 'verbose' Print action details.
%
% 'debug' Build a debug version. This will disable compiler
% optimizations and include debug symbols.
%
% AUTHOR : Peter L. Søndergaard.
% TESTING: NA
% REFERENCE: NA
% Verify that comp_pgauss is in path
if ~exist('comp_pgauss','file')
disp(' ');
disp('--- LTFAT - The Linear Time Frequency Analysis toolbox. ---');
disp(' ')
disp('To start the toolbox, call LTFATSTART as the first command.');
disp(' ');
return;
end;
bp=mfilename('fullpath');
bp=bp(1:end-length(mfilename));
definput.flags.target={'auto','lib','mex','pbc','playrec','java','blockproc'};
% This has to be also defined
definput.flags.comptarget={'release','debug'};
definput.flags.command={'compile','clean','test'};
definput.flags.verbosity={'noverbose','verbose'};
s = which('ltfatarghelper');
if strcmpi(mexext,s(end-numel(mexext)+1:end))
% We must avoid calling corrupt ltfatarghelper.mexext
% The following must not fail
try
ltfatarghelper({},struct(),{});
catch
if any(strcmpi('verbose',varargin))
fprintf('Removing corrupt ltfatarghelper.%s',mexext);
end
delete(s);
% Now there is just a ltfatarghelper.m
end
end
[flags,kv]=ltfatarghelper({},definput,varargin);
% Remember the current directory.
curdir=pwd;
% Compile backend lib?
do_lib = flags.do_lib || flags.do_auto;
% Compile MEX/OCT interfaces?
do_mex = flags.do_mex || flags.do_auto;
% Compile MEX PolyBoolClipper.mex
do_pbc = flags.do_pbc || flags.do_auto;
% Compile MEX playrec.mex... using Portaudio library.
% (relevant only for the bloc processing framework)
do_playrec = flags.do_playrec || flags.do_blockproc;
% Compile Java classes containing GUI for the bloc proc. framework.
do_java = flags.do_java || flags.do_blockproc;
if isoctave
extname='oct';
ext='oct';
else
extname='mex';
ext=mexext;
end;
fftw_lib_names = {'fftw3', 'fftw3f' };
% Check if we are on Windows
if ispc
makefilename='Makefile_mingw';
make_exe = 'mingw32-make';
sharedExt = 'dll';
%fftw_lib_names = {'fftw3', 'fftw3f' };
% The pre-compiled Octave for Windows comes only in 32bit version (3.6.4)
% We use different Makefiles
if isoctave
makefilename='Makefile_mingwoct';
end
end;
% Check if we are on Unix-type system
if isunix
makefilename='Makefile_unix';
make_exe = 'make';
sharedExt = 'so';
end;
% Check if we are on Mac
if ismac
makefilename='Makefile_mac';
make_exe = 'make';
sharedExt = 'dylib';
end;
clear mex;
% -------------- Handle cleaning --------------------------------
if flags.do_clean
if do_lib
disp('========= Cleaning libltfat ===============');
cd([bp,'libltfat',filesep,'ltfatcompat']);
callmake(make_exe,makefilename,'target','clean',flags.verbosity);
%[status,result]=system([make_exe, ' -f ',makefilename,' clean']);
%disp('Done.');
end;
if do_mex
fprintf('========= Cleaning %s interfaces =========\n', upper(extname));
cd([bp,extname]);
callmake(make_exe,makefilename,'target','clean','ext',ext,flags.verbosity);
%[status,result]=system([make_exe, ' -f ',makefilename,' clean',...
% ' EXT=',ext]);
end;
if do_pbc
% Use the correct makefile
makefilename_pbc = makefilename;
if isoctave
if ~strcmpi(makefilename(end-2:end),ext)
makefilename_pbc = [makefilename,ext];
end
end
disp('========= Cleaning PolyBoolClipper ====================');
cd([bp,'thirdparty',filesep,'polyboolclipper']);
clear mex;
callmake(make_exe,makefilename_pbc,'target','clean','ext',mexext,flags.verbosity);
%[status,result]=system([make_exe, ' -f ',makefilename,' clean',' EXT=',mexext]);
end;
if do_playrec
% Use the correct makefile
if isoctave
if ~strcmpi(makefilename(end-2:end),ext)
makefilename = [makefilename,ext];
end
end
disp('========= Cleaning PLAYREC ================');
cd([bp,'thirdparty',filesep,'Playrec']);
clear mex;
%[status,result]=system([make_exe, ' -f ',makefilename,' clean',' EXT=',mexext]);
callmake(make_exe,makefilename,'target','clean','ext',mexext,flags.verbosity);
end;
if do_java
disp('========= Cleaning JAVA ================');
cd([bp,'blockproc',filesep,'java']);
%[status,result]=system([make_exe,' clean']);
callmake(make_exe,[],'target','clean',flags.verbosity);
end;
cd(curdir);
end;
% -------------- Handle compiling --------------------------------
if flags.do_compile
if do_lib
disp('========= Compiling libltfat ==============');
cd([bp,'libltfat',filesep,'ltfatcompat']);
clear mex;
dfftw = ['-l',fftw_lib_names{1}];
sfftw = ['-l',fftw_lib_names{2}];
if ispc && ~isoctave
fftw_lib_found_names = searchfor(bp,fftw_lib_names,sharedExt);
if ~isempty(fftw_lib_found_names)
dfftw = ['-l:',fftw_lib_found_names{1}];
sfftw = ['-l:',fftw_lib_found_names{2}];
end
end
% DFFTW and SFFTW are not used in the unix_makefile
[status,result] = callmake(make_exe,makefilename,'matlabroot','arch',...
'dfftw',dfftw,'sfftw',sfftw,flags.verbosity,...
'comptarget',flags.comptarget);
if(~status)
disp('Done.');
else
error('Failed to build LTFAT libs:\n %s',result);
end
%end;
end;
if do_mex
fprintf('========= Compiling %s interfaces ========\n', upper(extname));
clear mex;
cd([bp,extname]);
dfftw = ['-l',fftw_lib_names{1}];
sfftw = ['-l',fftw_lib_names{2}];
if ~isoctave
fftw_lib_found_names = searchfor(bp,fftw_lib_names,sharedExt);
if ~isempty(fftw_lib_found_names)
if ~ismac
dfftw = ['-l:',fftw_lib_found_names{1}];
sfftw = ['-l:',fftw_lib_found_names{2}];
else
% We need a full path here.
dfftw = [binDirPath(),filesep,fftw_lib_found_names{1}];
sfftw = [binDirPath(),filesep,fftw_lib_found_names{2}];
end
end
end
[status,result] = callmake(make_exe,makefilename,'matlabroot','arch',...
'ext',ext,'dfftw',dfftw,'sfftw',sfftw,...
flags.verbosity,...
'comptarget',flags.comptarget);
if(~status)
disp('Done.');
else
error('Failed to build %s interfaces: %s \n',upper(extname),result);
end
end;
if do_pbc
makefilename_pbc = makefilename;
if isoctave
if ~strcmpi(makefilename(end-2:end),ext)
makefilename_pbc = [makefilename,ext];
end
end
disp('========= Compiling PolyBoolClipper ===================');
% Compile PolyBoolClipper mex file for use with mulaclab
cd([bp,'thirdparty',filesep,'polyboolclipper']);
clear mex;
[status,result] = callmake(make_exe,makefilename_pbc,'matlabroot','arch',...
'ext',ext,flags.verbosity);
if(~status)
disp('Done.');
else
error('Failed to build PlyBoolClipper:\n %s',result);
end
end;
if do_playrec
disp('========= Compiling PLAYREC ===============');
cd([bp,'thirdparty',filesep,'Playrec']);
clear mex;
% Compile the Playrec (interface to portaudio) for the real-time block-
% stream processing
portaudioLib = '-lportaudio';
binArchPath = binDirPath();
playrecRelPath = ['thirdparty',filesep,'Playrec'];
foundPAuser = [];
if ispc
foundPAuser = dir([bp,playrecRelPath,filesep,'*portaudio*',sharedExt,'*']);
end
foundPAmatlab = [];
if ~isoctave
% Check if portaudio library is present in the Matlab installation
foundPAmatlab = dir([binArchPath,filesep,'*portaudio*',sharedExt,'*']);
end
if ~isempty(foundPAuser)
if numel(foundPAuser)>1
error('Ambiguous portaudio libraries in %s. Please leave just one.',playrecRelPath);
end
foundPAuser = foundPAuser(1).name;
elseif ~isempty(foundPAmatlab)
if numel(foundPAmatlab)>1
if ispc
%This should not happen on Windows
%Use the first one on Linux
error('Ambiguous portaudio libraries in %s.',binArchPath);
end
end
foundPAmatlab = foundPAmatlab(1).name;
else
if ispc && isoctave || ispc
error(['Portaudio not found. Please download Portaudio http://www.portaudio.com\n',...
'and build it as a shared library and copy it to the\n',...
'%s directory. \n'],playrecPath);
end
end
if isoctave
if ~strcmpi(makefilename(end-2:end),ext)
makefilename = [makefilename,ext];
end
end
doPAuser = ~isempty(foundPAuser);
doPAmatlab = ~isempty(foundPAmatlab) && ~doPAuser;
if doPAmatlab
if ismac
% Full path is needed on MAC since
% clang does not understand -l: prefix.
portaudioLib = [binArchPath,filesep,foundPAmatlab];
else
portaudioLib = ['-l:',foundPAmatlab];
end
fprintf(' ...using %s from Matlab installation.\n',foundPAmatlab);
elseif doPAuser
portaudioLib = ['-l:',foundPAuser];
fprintf(' ...using %s from ltfat%s%s.\n',...
foundPAuser,filesep,playrecRelPath);
end
[status,result] = callmake(make_exe,makefilename,'matlabroot','arch',...
'ext',mexext,'portaudio',portaudioLib,'extra','HAVE_PORTAUDIO',...
flags.verbosity);
if(~status)
disp('Done.');
else
error('Failed to build PLAYREC:\n %s',result);
end
end;
if do_java
disp('========= Compiling JAVA classes ===================');
% Compile the JAVA classes
cd([bp,'blockproc',filesep,'java']);
clear mex;
[status,result] = callmake(make_exe,'Makefile',flags.verbosity);
if(~status)
disp('Done.');
else
error('Failed to build JAVA classes:\n %s',result);
end
end;
end;
% -------------- Handle testing ---------------------------------------
if flags.do_test
if do_mex
fprintf('========= Testing %s interfaces ==========\n', extname);
fprintf('1.: Test if comp_pgauss.%s was compiled: ',ext);
fname=['comp_pgauss.',ext];
if exist(fname,'file')
disp('SUCCESS.');
else
disp('FAILED.');
end;
fprintf('2.: Test if pgauss executes: ');
pgauss(100);
% If the execution of the script makes it here, we know that pgauss
% did not crash the system, so we can just print success. Same story
% with the following entries.
disp('SUCCESS.');
fprintf('3.: Test if fftreal executes: ');
fftreal(randn(10,1),10);
disp('SUCCESS.');
fprintf('4.: Test if dgt executes: ');
dgt(randn(12,1),randn(12,1),3,4);
disp('SUCCESS.');
end;
end;
% Jump back to the original directory.
cd(curdir);
function status = filesExist(filenames)
if(~iscell(filenames))
filenames={filenames};
end
for ii=1:length(filenames)
filename = filenames{ii};
if(~exist(filename,'file'))
error('%s: File %s not found.',mfilename,filename);
end
end
function found_files=searchfor(bp,files,sharedExt)
found_names = {};
if ispc
for ii=1:numel(files)
% Search the ltfat/mex lib
L = dir([bp,'mex',filesep,'*',files{ii},'*.',sharedExt]);
if isempty(L)
error(['%s: %s could not be found in ltfat/mex subdir.',...
' Please download the FFTW dlls and install them.'],...
upper(mfilename),files{ii});
end
found_files{ii} = L(1).name;
fprintf(' ...using %s from ltfat/mex.\n',L(1).name);
end
elseif isunix
for ii=1:numel(files)
L = dir([binDirPath(),filesep,'*',files{ii},'*.',sharedExt,'*']);
if isempty(L)
error('%s: Matlab FFTW libs were not found. Strange.',...
upper(mfilename));
end
found_files{ii} = L(1).name;
fprintf(' ...using %s from Matlab installation.\n',...
found_files{ii});
end
end;
function path=binDirPath()
path = [matlabroot,filesep,'bin',filesep,computer('arch')];
function [status,result]=callmake(make_exe,makefilename,varargin)
%CALLMAKE
% Usage: callmake(make_exe,makefilename);
% callmake(make_exe,makefilename,'matlabroot',matlabroot,...);
%
% `callmake(make_exe,makefilename)` is a platform independent wrapper for
% calling the make command `make_exe` on `makefilename` file. When
% `makefilename` is missing or is empty, the default `Makefile` file is
% used.
%
% `callmake(...,'target',target)` used `target` from the makefile.
%
% Flags:
%
% matlabroot: Pass MATLABROOT=matlabroot variable to the makefile.
%
% arch: Pass ARCH=computer('arch') variable to the makefile.
%
% Key-value parameters:
%
% ext: Pass EXT variable to the makefile.
%
% portaudio: Pass PORTAUDIO variable to the makefile.
%
% dfftw: Pass DFFTW variable to the makefile.
%
% sfftw: Pass SFFTW variable to the makefile.
if nargin < 2 || isempty(makefilename)
systemCommand = make_exe;
else
systemCommand = [make_exe, ' -f ',makefilename];
end
definput.flags.matlabroot={'none','matlabroot'};
definput.flags.arch={'none','arch'};
definput.keyvals.ext=[];
definput.keyvals.dfftw=[];
definput.keyvals.sfftw=[];
definput.keyvals.target=[];
definput.keyvals.comptarget=[];
definput.keyvals.portaudio=[];
definput.keyvals.extra=[];
definput.flags.verbosity={'noverbose','verbose'};
[flags,kv]=ltfatarghelper({},definput,varargin);
if flags.do_matlabroot
systemCommand = [systemCommand, ' MATLABROOT=','"',matlabroot,'"'];
end
if flags.do_arch
systemCommand = [systemCommand, ' ARCH=',computer('arch')];
end
if ~isempty(kv.ext)
systemCommand = [systemCommand, ' EXT=',kv.ext];
end
if ~isempty(kv.dfftw)
systemCommand = [systemCommand, ' DFFTW=',kv.dfftw];
end
if ~isempty(kv.sfftw)
systemCommand = [systemCommand, ' SFFTW=',kv.sfftw];
end
if ~isempty(kv.portaudio)
systemCommand = [systemCommand, ' PORTAUDIO=',kv.portaudio];
end
if ~isempty(kv.comptarget) && ~strcmpi(kv.comptarget,'release')
systemCommand = [systemCommand, ' COMPTARGET=',kv.comptarget];
end
if ~isempty(kv.extra)
systemCommand = [systemCommand, ' ',kv.extra,'=1'];
end
if ~isempty(kv.target)
systemCommand = [systemCommand,' ',kv.target];
end
if flags.do_verbose
fprintf('Calling:\n %s\n\n',systemCommand);
end
[status,result]=system(systemCommand);
if flags.do_verbose && ~isoctave
disp(result);
end