Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 5098dadf authored by hhakim's avatar hhakim
Browse files

Integrate matfaust.Faust.load_native for loading .mat files (using C++ recent...

Integrate matfaust.Faust.load_native for loading .mat files (using C++ recent code instead of matlab Faust.load).
parent 23c65653
No related branches found
No related tags found
No related merge requests found
......@@ -253,7 +253,7 @@ classdef Faust
F.matrix = FaustCore(factors, scale, optCopy, F.isReal, F.dev, F.dtype);
elseif(ischar(varargin{1}))
% init a Faust from file
F = matfaust.Faust.load(varargin{:});
F = matfaust.Faust.load_native(varargin{:});
elseif(isa(varargin{1}, 'matfaust.Faust'))
% create a Faust from another but not with the same
% handle to set inside the FaustCore object (matrix)
......@@ -2371,6 +2371,44 @@ classdef Faust
end
F = matfaust.Faust(out_factors, varargin{1:end});
end
%================================================================
%> Loads a Faust from a .mat file (native C++ version).
%===
%>
%> @b Example
%> @code
%> import matfaust.*
%> F = rand(10,10)
%> F.save('my_faust.mat')
%> F2 = matfaust.Faust.load_native('my_faust.mat')
%> F3 = Faust('my_faust.mat')
%> % F == F2 == F3
%> @endcode
%>
%> <p> @b See @b also Faust.Faust
%================================================================
function F = load_native(filepath)
file_type = mexFaustReal('get_mat_file_type', filepath);
is_real = true;
dtype = 'double';
switch(file_type)
case 0
% single
core_obj = mexFaustRealFloat('read_from_mat_file', filepath);
dtype = 'float'
case 1
% double
core_obj = mexFaustReal('read_from_mat_file', filepath);
case 2
core_obj = mexFaustCplx('read_from_mat_file', filepath);
is_real = false;
otherwise
error('Invalid file type from .matfile')
end
F = matfaust.Faust(core_obj, is_real, 'cpu', dtype);
end
end
end
......@@ -45,5 +45,9 @@
#define __MEX_FAUST_SAVE__
template <typename SCALAR, FDevice DEV>
void faust_save(const mxArray **prhs, const int nrhs, mxArray **plhs, const int nlhs);
template <typename SCALAR, FDevice DEV>
void faust_restore(const mxArray **prhs, const int nrhs, mxArray **plhs, const int nlhs);
template <typename SCALAR, FDevice DEV>
void faust_get_mat_file_type(const mxArray **prhs, const int nrhs, mxArray **plhs, const int nlhs);
#include "faust_save.hpp"
#endif
#include "class_handle.hpp"
#include "faust_TransformHelper.h"
template <typename SCALAR, FDevice DEV>
void extract_filepath(const mxArray* fp_arr, char* fp, size_t fp_max_len)
{
if(mxGetString(fp_arr, fp, fp_max_len) || strlen(fp) == 0)
{
mexErrMsgTxt("The filepath is not valid.");
}
}
template <typename SCALAR, FDevice DEV>
void faust_save(const mxArray **prhs, const int nrhs, mxArray **plhs, const int nlhs)
{
Faust::TransformHelper<SCALAR,DEV>* core_ptr = convertMat2Ptr<Faust::TransformHelper<SCALAR,DEV> >(prhs[1]);
const size_t MAX_PATH_SIZE=512;
const size_t MAX_PATH_SIZE = 512;
char filepath[MAX_PATH_SIZE];
// prhs[2] must be the filepath
if(nrhs != 3){
mexErrMsgTxt("The number of arguments for the save function is not valid. Must be one.");
return;
}
if(mxGetString(prhs[2], filepath, sizeof(filepath)) || strlen(filepath) == 0){
mexErrMsgTxt("The filepath is not valid.");
return;
}
// printf("save: filepath = %s, nprhs = %d\n", filepath, nprhs);
try
{
core_ptr->save_mat_file(filepath/*, mxGetScalar(prhs[1])*/);
}
catch(exception& e)
{
mexErrMsgTxt("Failed to save the file.");
}
if(nrhs != 3){
mexErrMsgTxt("The number of arguments for the save function is not valid. Must be one.");
return;
}
extract_filepath<SCALAR, DEV>(prhs[2], filepath, MAX_PATH_SIZE);
// printf("save: filepath = %s, nprhs = %d\n", filepath, nprhs);
try
{
core_ptr->save_mat_file(filepath/*, mxGetScalar(prhs[1])*/);
}
catch(exception& e)
{
mexErrMsgTxt("Failed to save the file.");
}
}
template <typename SCALAR, FDevice DEV>
void faust_restore(const mxArray **prhs, const int nrhs, mxArray **plhs, const int nlhs)
{
const size_t MAX_PATH_SIZE=512;
char filepath[MAX_PATH_SIZE];
if(nrhs != 2){
mexErrMsgTxt("The number of arguments for the load function is not valid. Must be one.");
return;
}
extract_filepath<SCALAR, DEV>(prhs[1], filepath, MAX_PATH_SIZE);
auto th = new Faust::TransformHelper<SCALAR,DEV>();
th->read_from_mat_file(filepath);
plhs[0] = convertPtr2Mat<Faust::TransformHelper<SCALAR,DEV> >(th);
}
template <typename SCALAR, FDevice DEV>
void faust_get_mat_file_type(const mxArray **prhs, const int nrhs, mxArray **plhs, const int nlhs)
{
const size_t MAX_PATH_SIZE=512;
char filepath[MAX_PATH_SIZE];
if(nrhs != 2){
mexErrMsgTxt("The number of arguments for the get_mat_file_type function is not valid. Must be one.");
return;
}
extract_filepath<SCALAR, DEV>(prhs[1], filepath, MAX_PATH_SIZE);
int file_type = Faust::TransformHelper<SCALAR,DEV>::get_mat_file_type(filepath);
plhs[0] = mxCreateDoubleScalar(file_type);
}
......@@ -187,6 +187,10 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
faust_is_all_dense<SCALAR, DEV>(prhs, nrhs, plhs, nlhs);
else if (!strcmp("save", cmd))
faust_save<SCALAR, DEV>(prhs, nrhs, plhs, nlhs);
else if (!strcmp("read_from_mat_file", cmd))
faust_restore<SCALAR, DEV>(prhs, nrhs, plhs, nlhs);
else if (!strcmp("get_mat_file_type", cmd))
faust_get_mat_file_type<SCALAR, DEV>(prhs, nrhs, plhs, nlhs);
else if(!strcmp("conj", cmd))
faust_conj<SCALAR,DEV>(prhs, nrhs, plhs, nlhs);
else if(!strcmp("transpose", cmd))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment