/** * Export midi notes of data instance into midifile, needs external program midiconvert * * @param lpsFilename Name of file to export * @param iSst Pointer to instance to export * @param lpsFiletype Type of file to export * @return <code>O_K</code> if successful, a (negative) error code otherwise */ INT16 CGEN_PROTECTED CDlpFile_Midi_ExportMidi ( CDlpFile* _this, const char* lpsFilename, CDlpObject* iSrc, const char* lpsFiletype ) { char lpsTempFile[L_PATH]; char lpsCmdline [3*L_PATH] = ""; INT16 nErr =O_K; strcpy(lpsTempFile,dlp_tempnam(NULL,"dlabpro_midi_export")); sprintf(lpsCmdline,"midiconvert %s %s", lpsTempFile, lpsFilename); CData *idSrc = AS(CData,iSrc); CData_InsertRecs(idSrc, 0, 1, 1); CData_Dstore(idSrc, CData_GetDescr(idSrc,DESCR0),0,5); IF_NOK(CDlpFile_ExportAsciiFromData(_this,lpsTempFile,iSrc,"csv")) nErr = IERROR(iSrc,FIL_EXPORT,lpsTempFile,"csv",0); CData_DeleteRecs(idSrc,0,1); if (system(lpsCmdline)!=0) { nErr=IERROR(_this,FIL_EXEC,lpsCmdline,0,0); } if (remove(lpsTempFile)==-1) nErr=IERROR(_this,FIL_REMOVE,"temporary ",lpsTempFile,0); /* Clean up */ return nErr; }
/** * Initializes the instance from the interpreter command line. * <h3>Note</h3> * <p>You must setup the method invocation context <code>m_lpMic</code> (see * <a href="dlpinst.html"><code class="link">CDlpInstance</code></a>) before * calling this method. Otherwise it will do noting.</p> * * @param _this This instance * @param nRec First record to read * @param nComp First component to read * @param nCount Number of items to read * @return O_K if successfull, an error code otherwise */ INT16 CGEN_PUBLIC CData_InitializeEx(CData* _this, INT32 nRec, INT32 nComp, INT32 nCount) { INT32 nXXR = 0; INT32 nXC = 0; INT32 nR = 0; INT32 nC = 0; INT32 nCtr = 0; BOOL bRead = 0; const char* lpsToken = NULL; if (_this && !CDlpObject_MicGet(BASEINST(_this))) return IERROR(_this,ERR_GENERIC,"No method invocation context",0,0); if (!_this || CData_IsEmpty(_this)) { /* _this == NULL is ok, just remove the tokens between { and } */ do { lpsToken = MIC_NEXTTOKEN_FORCE; if (!lpsToken) return IERROR(_this,DATA_HERESCAN,"}",0,0); } while (dlp_strcmp(lpsToken,"}")!=0); return O_K; } /* Actual setup */ nXXR = CData_GetMaxRecs(_this); nXC = CData_GetNComps(_this); nR = nRec; nC = nComp; bRead = TRUE; for (;;) { lpsToken = MIC_NEXTTOKEN_FORCE; if (!lpsToken) return IERROR(_this,DATA_HERESCAN,"}",0,0); if (dlp_strcmp(lpsToken,"}")==0) break; if (!bRead) continue; if (nR==nXXR || (nCount>=0 && nCtr>=nCount)) { /* Ignore further initializers */ bRead = FALSE; continue; } /* Initialize cell value; skip cell on wildcard '*' initializer */ if (dlp_strcmp(lpsToken,"*")!=0) IF_NOK(dlp_sscanx(lpsToken,CData_GetCompType(_this,nC),CData_XAddr(_this,nR,nC))) IERROR(_this,DATA_BADINITIALIZER,lpsToken,(int)nR,(int)nC); nCtr++; nC++; if (nC==nXC) { nC=nComp; nR++; } } if (nCount>=0&&nCtr<nCount) return IERROR(_this,DATA_INITIALIZERS,"few" ,0,0); if (!bRead ) return IERROR(_this,DATA_INITIALIZERS,"many",0,0); return O_K; }
/** * Initialize one record from a token list. * * @param lpsInit Null ('\0') separated list of tokens. A double null "\0\0" * is expected as list terminator! * @param nRec Record index of first cell to initialize * @param nComp Component index of first cell to initialize * @return O_K if successfull, a negative error code otherwise */ INT16 CGEN_PUBLIC CData_InitializeRecordEx ( CData* _this, const char* lpsInit, INT32 nRec, INT32 nComp ) { const char* lpsToken = NULL; INT32 nXC = 0; INT32 nC = 0; nXC = CData_GetNComps(_this); nC = nComp; if (nC<0 || nC>=nXC ) return NOT_EXEC; if (nRec<0 || nRec>=CData_GetNRecs(_this)) return NOT_EXEC; for (lpsToken=lpsInit; *lpsToken && nC<nXC; lpsToken+=dlp_strlen(lpsToken)+1, nC++) if (dlp_strcmp(lpsToken,"*")!=0) IF_NOK(dlp_sscanx(lpsToken,CData_GetCompType(_this,nC),CData_XAddr(_this,nRec,nC))) IERROR(_this,DATA_BADINITIALIZER,lpsToken,(int)nRec,(int)nC); if (*lpsToken) return IERROR(_this,DATA_INITIALIZERS,"many",0,0); if (nC<nXC ) IERROR(_this,DATA_INITIALIZERS,"few" ,0,0); return O_K; }
INT16 CGEN_PROTECTED CCgen::OnClass() { char lpClassname[L_INPUTLINE+1]; GetNextDefToken(lpClassname); if (m_nAncestor > 1) return O_K; if (!dlp_strlen(lpClassname)) return IERROR(this,ERR_EXPECTAFTER,"name","CLASS:",0); char bad = 0; if (!m_bNoIdcheck) IF_NOK(dlp_is_valid_id(IDT_CLASS,lpClassname,&bad)) IERROR(this,ERR_BADIDCHAR,lpClassname,bad,0); if (m_nAncestor == 1) dlp_strcpy(m_lpsDlcParent,lpClassname); else dlp_strcpy(m_lpsClass,lpClassname); return O_K; }
/** * Import midi notes of a midifile into data, needs external program midiconvert * * @param lpsFilename Name of file to import * @param iDst Pointer to instance to import * @param lpsFiletype Type of file to import * @return <code>O_K</code> if successful, a (negative) error code otherwise */ INT16 CGEN_PROTECTED CDlpFile_Midi_ImportMidi ( CDlpFile* _this, const char* lpsFilename, CDlpObject* iDst, const char* lpsFiletype ) { char lpsTempFile[L_PATH]; char lpsCmdline [3*L_PATH] = ""; INT16 nErr =O_K; strcpy(lpsTempFile,dlp_tempnam(NULL,"dlabpro_midi_import")); sprintf(lpsCmdline,"midiconvert %s %s", lpsFilename, lpsTempFile); if (system(lpsCmdline)!=0) { nErr=IERROR(_this,FIL_EXEC,lpsCmdline,0,0); } else { CData *idDst = AS(CData,iDst); /*Prepare data*/ CData_Reset(iDst,TRUE); CData_AddComp(idDst,"CHAN",T_UCHAR); CData_AddComp(idDst,"VOL",T_UCHAR); CData_AddComp(idDst,"INST",T_UCHAR); CData_AddComp(idDst,"NOTE",T_UCHAR); CData_AddComp(idDst,"TIME",T_UINT); CData_AddComp(idDst,"LGTH",T_UINT); CData_AddComp(idDst,"VEL",T_UCHAR); /*import*/ IF_NOK(CDlpFile_ImportAsciiToData(_this,lpsTempFile,iDst,"csv")) nErr = IERROR(iDst,FIL_IMPORT,lpsTempFile,"csv",0); /*Set midifilter specific data descriptions and clean data*/ CData_SetDescr(idDst, DESCR0, CData_Dfetch(idDst,0,5)); CData_DeleteRecs(idDst,0,1); } if (remove(lpsTempFile)==-1) nErr=IERROR(_this,FIL_REMOVE,"temporary ",lpsTempFile,0); /* Clean up */ return nErr; }
INT16 CGmm_PrecalcD(CGmm* _this, BOOL bCleanup) #endif { INT32 i = 0; /* Triangular inv. cov. matrix cntr. */ INT32 c = 0; /* Index of inv. covariance matrix */ INT32 k = 0; /* Gaussian loop counter */ INT32 n = 0; /* Dimension loop counter */ INT32 m = 0; /* Dimension loop counter */ INT32 K = 0; /* Number of single Gaussians */ INT32 N = 0; /* Feature space dimensionality */ GMM_FTYPE nDelta1 = 0.; /* Class indep. term of delta const. */ #ifdef __TMS GMM_FTYPE *mean = (GMM_FTYPE*)CData_XAddr(AS(CData,_this->m_idMean),0,0); #endif /* Clean up precalculated data */ /* --------------------------------- */ dlp_free(_this->m_lpAlpha); /* Clear alpha vector */ dlp_free(_this->m_lpBeta ); /* Clear beta vectors */ dlp_free(_this->m_lpGamma); /* Clear gamma vector */ dlp_free(_this->m_lpDelta); /* Clear delta vector */ dlp_free(_this->m_lpI ); /* Clear inv. cov. pointer array */ dlp_free(_this->m_lpV ); /* Clear inverse variance array */ if(_this->m_idSse2Icov){ CData *idSse2Icov=AS(CData,_this->m_idSse2Icov); IDESTROY(idSse2Icov); /* Destroy inv.covs. for SSE2 opt. */ _this->m_idSse2Icov=NULL; } dlp_free(_this->m_lpSse2Buf); /* Clear aux. buffer for SSE2 opt. */ dlp_free(_this->m_lpLdlL); /* Clear L-matrix buffer for LDL */ dlp_free(_this->m_lpLdlD); /* Clear D-vector buffer for LDL */ _this->m_nN = 0; /* Clear feature space dimensionality*/ _this->m_nK = 0; /* Clear number of single Gaussians */ if (bCleanup) return O_K; /* When cleaning up that's it */ /* Initialize */ /* --------------------------------- */ K = CGmm_GetNGauss(_this); /* Get nuumber of single Gaussians */ N = CGmm_GetDim(_this); /* Get feature space dimensionality */ nDelta1 = -N/2*log(2*F_PI); /* Compute part of delta term */ /* Basic validation */ /* --------------------------------- */ IF_NOK(CGmm_CheckMean(_this)) DLPTHROW(GMM_NOTSETUP); /* Check mean vectors */ IF_NOK(CGmm_CheckIvar(_this)) DLPTHROW(GMM_NOTSETUP); /* Check inverse variance vectors */ IF_NOK(CGmm_CheckCdet(_this)) DLPTHROW(GMM_NOTSETUP); /* Check (co-)variance determinants */ /* Basic dimensions */ /* --------------------------------- */ _this->m_nN = N; /* Feature space dimensionality */ _this->m_nK = K; /* Number of single Gaussians */ /* Create inverse covariance matrix map */ /* --------------------------------- */ if (_this->m_idIcov) /* If using covariances */ { /* >> */ IF_NOK(CGmm_CheckIcov(_this)) DLPTHROW(GMM_NOTSETUP); /* Inverse covariance data corrupt */ _this->m_lpI = dlp_calloc(K,sizeof(GMM_FTYPE*)); /* Allocate map */ if (!_this->m_lpI) DLPTHROW(ERR_NOMEM); /* Out of memory */ for (k=0; k<K; k++) /* Loop over Gaussians */ { /* >> */ c=_this->m_idCmap ? (INT32)CData_Dfetch(AS(CData,_this->m_idCmap),k,0) : k;/* Cov. mat. idx. for Gaussian k */ I[k] = (GMM_FTYPE*)CData_XAddr(AS(CData,_this->m_idIcov),c,0); /* Store ptr. to inv. cov. matrix*/ } /* << */ } /* << */ /* Create inverse variance vector map */ /* --------------------------------- */ IF_NOK(CGmm_CheckIvar(_this)) DLPTHROW(GMM_NOTSETUP); /* Inverse covariance data corrupt */ _this->m_lpV = dlp_calloc(K,sizeof(GMM_FTYPE*)); /* Allocate map */ if (!_this->m_lpV) DLPTHROW(ERR_NOMEM); /* Out of memory */ for (k=0; k<K; k++) /* Loop over Gaussians */ V[k] = (GMM_FTYPE*)CData_XAddr(AS(CData,_this->m_idIvar),k,0); /* Store ptr. to inv. cov. matrix*/ /* LDL-factorization */ /* --------------------------------- */ if(_this->m_nLDL) /* LDL factorization used */ { /* >> */ /* TODO: GMM_TYPE is float => dlm_factldl in float */ /* ------------------------------- */ _this->m_lpLdlL = dlp_malloc(K*N*(N-1)/2*sizeof(GMM_FTYPE)); /* Alloc L matrix */ _this->m_lpLdlD = dlp_malloc(K*N*sizeof(GMM_FTYPE)); /* Alloc D vector */ if (!_this->m_lpLdlL || !_this->m_lpLdlD) DLPTHROW(ERR_NOMEM); /* Out of memory */ { FLOAT64 *lpAux1 = (FLOAT64 *)dlp_malloc(N*N*sizeof(FLOAT64)); /* Alloc auxilary matrix #1 */ FLOAT64 *lpAux2 = (FLOAT64 *)dlp_malloc(N*N*sizeof(FLOAT64)); /* Alloc auxilary matrix #2 */ if (!lpAux1 || !lpAux2) DLPTHROW(ERR_NOMEM); /* Out of memory */ for(k=0;k<K;k++) /* Loop over all Gaussians */ { /* >> */ for(n=0;n<N;n++) /* Loop over Gaussian dimension */ { /* >> */ lpAux1[n*N+n] = V[k][n]; /* Copy inverse variance values*/ for(m=0;m<n;m++) lpAux1[n*N+m] = lpAux1[m*N+n] = /* Copy inverse covariance val.*/ I?I[k][m*N-2*m-m*(m-1)/2+n-1]:0.; /* | */ } /* << */ dlm_factldl(lpAux1,lpAux2,N); /* Factorize matrix */ for(n=0;n<N;n++) /* Loop over Gaussian dimension */ { /* >> */ for(m=n+1;m<N;m++) Li(N,k,n,m) = lpAux1[n*N+m]; /* Store L matrix values */ D(N,k,n) = lpAux2[n*N+n]; /* Store D vector values */ } /* << */ if(_this->m_nLdlCoef>=0 && _this->m_nLdlCoef<N*(N-1)/2) { FLOAT64 nMinAbs; memcpy(lpAux1,&L(N,k,0),N*(N-1)/2); #if GMM_FTYPE_CODE == T_FLOAT qsort(lpAux1,N*(N-1)/2,sizeof(FLOAT64),cf_absfloat_down); #else qsort(lpAux1,N*(N-1)/2,sizeof(FLOAT64),cf_absdouble_down); #endif nMinAbs=fabs(lpAux1[_this->m_nLdlCoef]); for(n=0;n<N*(N-1)/2;n++) if(fabs(L(N,k,n))<=nMinAbs) L(N,k,n)=0.; } } /* << */ dlp_free(lpAux1); /* Free auxilary matrix #1 */ dlp_free(lpAux2); /* Free auxilary matrix #2 */ } } /* << */ /* Precalculate alpha and beta vectors */ /* --------------------------------- */ if(!_this->m_nLDL) /* No LDL factorization used */ { /* >> */ _this->m_lpAlpha = dlp_calloc(K,sizeof(GMM_FTYPE)); /* Allocate buffer */ _this->m_lpBeta = dlp_calloc(K*N,sizeof(GMM_FTYPE)); /* Allocate buffer */ if (!_this->m_lpAlpha || !_this->m_lpBeta) DLPTHROW(ERR_NOMEM); /* Out of memory */ for (k=0; k<K; k++) /* Loop over Gaussians */ for (n=0; n<N; n++) /* Loop over dimensions */ for (m=0; m<N; m++) /* Loop over dimensions */ if (m==n) /* Main diagonal (variances)?*/ { /* >> */ alpha[k] += V[k][n]*mu(k,m)*mu(k,n); /* Sum up alpha val. (n==m)*/ beta [k*N+n] += V[k][n]*mu(k,m); /* Sum up beta value (n==m)*/ } /* << */ else if (_this->m_lpI) /* Otherwise cov.(if present)*/ { /* >> */ if (m>n) i = n*N - 2*n - n*(n-1)/2 + m - 1; /* Calc index if I[n,m] in */ else i = m*N - 2*m - m*(m-1)/2 + n - 1; /* | triangular icov. mat. */ alpha[k] += I[k][i]*mu(k,m)*mu(k,n); /* Sum up alpha val. (n!=m)*/ beta [k*N+n] += I[k][i]*mu(k,m); /* Sum up beta value (n!=m)*/ } /* << */ } /* << */ else /* LDL factorization used */ { /* >> */ _this->m_lpAlpha = NULL; /* No alpha needed here */ _this->m_lpBeta = dlp_calloc(K*N,sizeof(GMM_FTYPE)); /* Allocate buffer */ if (!_this->m_lpBeta) DLPTHROW(ERR_NOMEM); /* Out of memory */ for(k=0;k<K;k++) for(n=0;n<N;n++) /* Loop over Gaussians & dimensions*/ { /* >> */ beta[k*N+n] = mu(k,n); /* Init beta with mean value */ for(m=n+1;m<N;m++) beta[k*N+n] += mu(k,m)*Li(N,k,n,m); /* Calculate beta from L*mu */ } /* << */ } /* << */ /* Allocate gamma vector */ /* --------------------------------- */ /* NOTE: If this fails there will just be no lazy computation -> no big deal*//* */ if (_this->m_idCmap && _this->m_idIcov && _this->m_lpI) /* Wanna do lazy computation? */ if (CData_GetDescr(AS(CData,_this->m_idIvar),0)==0.) /* Only if variances are tied too */ _this->m_lpGamma = dlp_calloc(K,sizeof(GMM_FTYPE)); /* Allocate buffer */ /* Precalculate delta vector */ /* --------------------------------- */ _this->m_lpDelta = dlp_calloc(K,sizeof(GMM_FTYPE)); /* Allocate buffer */ if (!_this->m_lpDelta) DLPTHROW(ERR_NOMEM); /* Out of memory */ for (k=0; k<K; k++) /* Loop over Gaussians */ delta[k] = nDelta1-(GMM_FTYPE)(0.5*log( /* Calculate delta[k] */ CData_Dfetch(AS(CData,_this->m_idCdet),k,0))); /* | */ /* SSE2 precalculated objects */ /* --------------------------------- */ #ifdef GMM_SSE2 /* Use SSE2? */ IFIELD_RESET(CData,"sse2_icov"); /* Create/reset SSE2 inv.cov. matrs. */ CGmm_Extract(_this,NULL,_this->m_idSse2Icov); /* ... and go get 'em */ _this->m_lpSse2Buf = dlp_calloc(2*N,sizeof(GMM_FTYPE)); /* Allocate auxilary buffer */ if (!_this->m_lpSse2Buf) DLPTHROW(ERR_NOMEM); /* Out of memory */ #endif /* #ifdef GMM_SSE2 */ /* Final checkup */ /* --------------------------------- */ #ifndef __NOXALLOC IF_NOK(CGmm_CheckPrecalc(_this)) DLPTHROW(GMM_NOTSETUP); /* Check precalculated data */ #endif return O_K; /* That's it folks ... */ DLPCATCH(GMM_NOTSETUP) /* On NOT_EXEC exception */ DLPCATCH(ERR_NOMEM) /* On ERR_NOMEM exception */ #if GMM_FTYPE_CODE == T_FLOAT CGmm_PrecalcF(_this,TRUE); /* - Free memory of precalc'd. data */ #else CGmm_PrecalcD(_this,TRUE); /* - Free memory of precalc'd. data */ #endif return NOT_EXEC; /* - Indicate failure */ }