[89ea83]: mex / block_interface.c  Maximize  Restore  History

Download this file

285 lines (257 with data), 7.4 kB

  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
#include "block_interface.h"
#include <string.h>
void defaultSetter(biEntry* obj,const mxArray* in)
{
if(obj->var!=NULL)
mxDestroyArray(obj->var);
obj->var=mxDuplicateArray(in);
mexMakeArrayPersistent(obj->var);
}
mxArray* defaultGetter(biEntry* obj)
{
if(obj->var!=NULL)
return obj->var;
else
return mxCreateDoubleMatrix(0,0,mxREAL);
}
/*
* Filling the attributes dictionary
* */
static biEntry vars[] =
{
// Default setters and getters
{.name="Ls",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="Pos",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="BufCount",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="PlayChanList",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="RecChanList",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="PageList",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="PageNo",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="Skipped",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="BufLen",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="ClassId",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="AnaOverlap",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="SynOverlap",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="DispLoad",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="IsLoop",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
// Default setters and custom getters
{.name="Source",.var=NULL,.setter=defaultSetter,.getter=getSource},
{.name="EnqBufCount",.var=NULL,.setter=NULL,.getter=getEnqBufCount},
{.name="ToPlay",.var=NULL,.setter=defaultSetter,.getter=getToPlay},
};
biEntry* lookupEntry(const char* name, biEntry* dict,size_t dictLen)
{
for(size_t ii=0;ii<dictLen;ii++)
{
if(!strncmp(name,dict[ii].name,strlen(dict[ii].name)))
{
return &dict[ii];
}
}
return NULL;
}
void resetAll(biEntry* dict,size_t dictLen)
{
clearAll(dict,dictLen);
for(size_t ii=0;ii<dictLen;ii++)
{
if(!strcmp(dict[ii].name,"Ls"))
{
dict[ii].setter(&dict[ii],mxCreateDoubleScalar(-1.0));
}
else if(!strcmp(dict[ii].name,"Pos"))
{
dict[ii].setter(&dict[ii],mxCreateDoubleScalar(0.0));
}
else if(!strcmp(dict[ii].name,"BufCount"))
{
dict[ii].setter(&dict[ii],mxCreateDoubleScalar(3.0));
}
else if(!strcmp(dict[ii].name,"PageNo"))
{
dict[ii].setter(&dict[ii],mxCreateDoubleScalar(0.0));
}
else if(!strcmp(dict[ii].name,"Skipped"))
{
dict[ii].setter(&dict[ii],mxCreateDoubleScalar(0.0));
}
else if(!strcmp(dict[ii].name,"DispLoad"))
{
dict[ii].setter(&dict[ii],mxCreateDoubleScalar(1.0));
}
else if(!strcmp(dict[ii].name,"IsLoop"))
{
dict[ii].setter(&dict[ii],mxCreateDoubleScalar(0.0));
}
else if(!strcmp(dict[ii].name,"ClassId"))
{
dict[ii].setter(&dict[ii],mxCreateString("double"));
}
}
}
void clearAllWrapper()
{
clearAll(vars,ARRAYLEN(vars));
}
void clearAll(biEntry* dict, size_t dictLen)
{
for(size_t ii=0;ii<dictLen;ii++)
{
if(dict[ii].var!=NULL)
{
mxDestroyArray(dict[ii].var);
dict[ii].var=NULL;
}
}
}
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
static int atExitRegistered = 0;
if(!atExitRegistered)
{
atExitRegistered = 1;
mexAtExit(clearAllWrapper);
}
if(nrhs<1)
{
mexErrMsgTxt("BLOCK_INTERFACE: Not enough input arguments.");
}
const mxArray* mxCmd = prhs[0];
char command[COMMAND_LENGTH];
size_t comStrLen = mxGetNumberOfElements(mxCmd);
mxGetString(mxCmd,command,comStrLen+1);
if(!strncmp(command,"set",3))
{
if(nrhs<2)
{
mexErrMsgTxt("BLOCK_INTERFACE: Not enough arguments for the set method.");
}
biEntry* cmdStruct = lookupEntry(command+3,vars,ARRAYLEN(vars));
if(cmdStruct==NULL)
{
mexErrMsgTxt("BLOCK_INTERFACE: Unrecognized set command.");
}
cmdStruct->setter(cmdStruct,prhs[1]);
return;
}
else if(!strncmp(command,"get",3))
{
if(nrhs>1)
{
mexErrMsgTxt("BLOCK_INTERFACE: Too many input arguments for get method.");
}
biEntry* cmdStruct = lookupEntry(command+3,vars,ARRAYLEN(vars));
if(cmdStruct==NULL)
{
mexErrMsgTxt("BLOCK_INTERFACE: Unrecognized get command.");
}
plhs[0]=cmdStruct->getter(cmdStruct);
return;
}
else if(!strcmp(command,"reset"))
{
resetAll(vars,ARRAYLEN(vars));
return;
}
else if(!strcmp(command,"incPageNo"))
{
incPageNo();
return;
}
else if(!strcmp(command,"popPage"))
{
plhs[0] = popPage();
return;
}
else if(!strcmp(command,"pushPage"))
{
pushPage(prhs[1]);
return;
}
else if(!strcmp(command,"clearAll"))
{
clearAllWrapper();
return;
}
mexErrMsgTxt("BLOCK_INTERFACE: Unrecognized command.");
}
mxArray* getSource(biEntry* obj)
{
if(obj->var==NULL || mxIsNumeric(obj->var))
return mxCreateString("numeric");
else
return obj->var;
}
mxArray* getEnqBufCount(biEntry* obj)
{
double retVal = 0.0;
biEntry* cmdStruct = lookupEntry("PageList",vars,ARRAYLEN(vars));
if(cmdStruct->var!=NULL)
retVal = mxGetNumberOfElements(cmdStruct->var);
return mxCreateDoubleScalar(retVal);
}
void incPageNo()
{
biEntry* cmdStruct = lookupEntry("PageNo",vars,ARRAYLEN(vars));
if(cmdStruct->var==NULL)
{
cmdStruct->var = mxCreateDoubleScalar(1.0);
mexMakeArrayPersistent(cmdStruct->var);
}
else
{
double* pageNo = mxGetPr(cmdStruct->var);
*pageNo = *pageNo + 1.0;
}
}
mxArray* getToPlay(biEntry* obj)
{
if(obj->var!=NULL)
{
mxArray* duplicate = mxDuplicateArray(obj->var);
mxDestroyArray(obj->var);
obj->var = NULL;
return duplicate;
}
else
{
return defaultGetter(obj);
}
}
void pushPage(const mxArray* in)
{
biEntry* cmdStruct = lookupEntry("PageList",vars,ARRAYLEN(vars));
if(cmdStruct->var==NULL)
{
cmdStruct->setter(cmdStruct,in);
}
else
{
size_t M = mxGetM(cmdStruct->var);
size_t N = mxGetN(cmdStruct->var);
size_t maxMN = M>N?M:N;
mxArray* tmp = mxCreateDoubleMatrix(1,maxMN+1,mxREAL);
memcpy(mxGetPr(tmp),mxGetPr(cmdStruct->var),maxMN*sizeof(double));
memcpy(mxGetPr(tmp)+maxMN,mxGetPr(in),sizeof(double));
cmdStruct->setter(cmdStruct,tmp);
}
}
mxArray* popPage()
{
biEntry* cmdStruct = lookupEntry("PageList",vars,ARRAYLEN(vars));
if(cmdStruct->var==NULL)
{
return cmdStruct->getter(cmdStruct);
}
else
{
double retVal = mxGetScalar(cmdStruct->var);
size_t M = mxGetM(cmdStruct->var);
size_t N = mxGetN(cmdStruct->var);
size_t maxMN = M>N?M:N;
mxArray* tmp = mxCreateDoubleMatrix(1,maxMN-1,mxREAL);
memcpy(mxGetPr(tmp),mxGetPr(cmdStruct->var)+1,(maxMN-1)*sizeof(double));
cmdStruct->setter(cmdStruct,tmp);
return mxCreateDoubleScalar(retVal);
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks