void mfccrasta_init( Mfcc_rasta **phMrasta, AUD_Int32s mfccLen ) { Mfcc_rasta *pMrasta = NULL; pMrasta = (Mfcc_rasta*)calloc( sizeof(Mfcc_rasta), 1 ); if ( pMrasta == NULL ) { *phMrasta = NULL; return; } AUD_Int32s i; for ( i = 0; i < RASTA_BUFLEN; i++ ) { pMrasta->pXState[i] = (AUD_Int32s*)calloc( mfccLen * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pMrasta->pXState[i] ); } pMrasta->ys = (AUD_Int32s*)calloc( mfccLen * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pMrasta->ys ); pMrasta->b[0] = -0.2; pMrasta->b[1] = -0.1; pMrasta->b[2] = 0.; pMrasta->b[3] = -pMrasta->b[1]; pMrasta->b[4] = -pMrasta->b[0]; pMrasta->a = 0.98; pMrasta->mfccLen = mfccLen; *phMrasta = pMrasta; return; }
AUD_Int32s showMatrix( AUD_Matrix *pMatrix ) { AUD_ASSERT( pMatrix ); AUD_Int32s i, j; AUDLOG( "###### matrix rows: %d, cols: %d, dataType: %d ######\n", pMatrix->rows, pMatrix->cols, pMatrix->dataType ); switch ( pMatrix->dataType ) { case AUD_DATATYPE_INT16S: for ( i = 0; i < pMatrix->rows; i++ ) { for ( j = 0; j < pMatrix->cols; j++ ) { AUDLOG( "%d, ", *( pMatrix->pInt16s + i * pMatrix->cols + j ) ); } AUDLOG( "\n" ); } break; case AUD_DATATYPE_INT32S: for ( i = 0; i < pMatrix->rows; i++ ) { for ( j = 0; j < pMatrix->cols; j++ ) { AUDLOG( "%d, ", *( pMatrix->pInt32s + i * pMatrix->cols + j ) ); } AUDLOG( "\n" ); } break; case AUD_DATATYPE_INT64S: for ( i = 0; i < pMatrix->rows; i++ ) { for ( j = 0; j < pMatrix->cols; j++ ) { AUDLOG( "%lld, ", *( pMatrix->pInt64s + i * pMatrix->cols + j ) ); } AUDLOG( "\n" ); } break; case AUD_DATATYPE_DOUBLE: for ( i = 0; i < pMatrix->rows; i++ ) { for ( j = 0; j < pMatrix->cols; j++ ) { AUDLOG( "%f, ", *( pMatrix->pDouble + i * pMatrix->cols + j ) ); } AUDLOG( "\n" ); } break; default: AUD_ASSERT( 0 ); break; } AUDLOG( "\n" ); return 0; }
// XXX: bad practice, betray memory management principle, need refine AUD_Int32s readMatrixFromFile( AUD_Matrix *pMatrix, AUD_Int8s *pFileName ) { AUD_ASSERT( pMatrix && pFileName ); FILE *fp = NULL; AUD_Int32s ret = 0, data; fp = fopen( (const char*)pFileName, "rb" ); if ( !fp ) { AUDLOG( "open file %s for read failed", pFileName ); return AUD_ERROR_IOFAILED; } data = fread( &( pMatrix->dataType ), sizeof( pMatrix->dataType ), 1, fp ); data = fread( &( pMatrix->rows ), sizeof( pMatrix->rows ), 1, fp ); data = fread( &( pMatrix->cols ), sizeof( pMatrix->cols ), 1, fp ); ret = createMatrix( pMatrix ); AUD_ASSERT( ret == 0 ); data = fread( (void*)( pMatrix->pInt16s ), SIZEOF_TYPE( pMatrix->dataType ) * pMatrix->rows * pMatrix->cols, 1, fp ); fclose( fp ); fp = NULL; return 0; }
AUD_Error gmm_free( void **phGmmHandle ) { AUD_ASSERT( phGmmHandle != NULL && *phGmmHandle != NULL ); AUD_Int32s ret = 0; GmmModel *pState = (GmmModel*)(*phGmmHandle); ret = destroyMatrix( &(pState->means) ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &(pState->cvars) ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &(pState->weights) ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &(pState->dets) ); if ( ret == -1 ) { AUDLOG( "this GMM has no determinant information\n" ); } free( pState ); pState = NULL; *phGmmHandle = NULL; return AUD_ERROR_NONE; }
/* mean & variance normalization of matrix, along specific dimension( 1: row; 2: col ) */ AUD_Int32s mvnMatrix( AUD_Matrix *pMatrix, AUD_Int32s rowOrCol ) { AUD_ASSERT( pMatrix ); AUD_ASSERT( pMatrix->dataType == AUD_DATATYPE_INT32S ); AUD_Int32s normalizeStep, normalizeStride, normalizeVectors, normalizeLen; if ( rowOrCol == 1 ) { normalizeStep = pMatrix->cols; normalizeStride = 1; normalizeVectors = pMatrix->rows; normalizeLen = pMatrix->cols; } else if ( rowOrCol == 2 ) { normalizeStep = 1; normalizeStride = pMatrix->cols; normalizeVectors = pMatrix->cols; normalizeLen = pMatrix->rows; } else { AUD_ASSERT( 0 ); } AUD_Double mean = 0., var = 0., stdvar = 0.; AUD_Int32s *pData = pMatrix->pInt32s; AUD_Int32s i, j; for ( i = 0; i < normalizeVectors; i++ ) { // calc mean mean = 0.; for ( j = 0; j < normalizeLen; j++ ) { mean += pData[i * normalizeStep + j * normalizeStride]; } mean /= normalizeLen; // calc variance var = 0.; for ( j = 0; j < normalizeLen; j++ ) { var += pow( (pData[i * normalizeStep + j * normalizeStride] - mean), 2.0 ); } var /= normalizeLen; stdvar = sqrt( var ); // normalize AUD_Double tmp = 0.; for ( j = 0; j < normalizeLen; j++ ) { tmp = (pData[i * normalizeStep + j * normalizeStride] - mean) / stdvar; pData[i * normalizeStep + j * normalizeStride] = (AUD_Int32s)round( tmp * 32768.0 ); } } return 0; }
AUD_Int32s cloneVector( AUD_Vector *pDstVector, AUD_Vector *pSrcVector ) { AUD_ASSERT( pDstVector && pSrcVector ); AUD_ASSERT( pDstVector->len == pSrcVector->len && pSrcVector->len > 0 ); AUD_ASSERT( pDstVector->dataType == pSrcVector->dataType ); memmove( pDstVector->pInt16s, pSrcVector->pInt16s, pDstVector->len * SIZEOF_TYPE( pDstVector->dataType ) ); return 0; }
AUD_Int32s cloneMatrix( AUD_Matrix *pDstMatrix, AUD_Matrix *pSrcMatrix ) { AUD_ASSERT( pDstMatrix && pSrcMatrix ); AUD_ASSERT( pDstMatrix->rows == pSrcMatrix->rows && pDstMatrix->cols == pSrcMatrix->cols ); AUD_ASSERT( pDstMatrix->dataType == pSrcMatrix->dataType ); memmove( pDstMatrix->pInt16s, pSrcMatrix->pInt16s, pDstMatrix->rows * pDstMatrix->cols * SIZEOF_TYPE( pDstMatrix->dataType ) ); return 0; }
AUD_Int32s mergeVector( AUD_Vector *pDstVector, AUD_Vector *pSrcVector1, AUD_Vector *pSrcVector2 ) { AUD_ASSERT( pDstVector && pSrcVector1 && pSrcVector2 ); AUD_ASSERT( pDstVector->len == ( pSrcVector1->len + pSrcVector2->len ) ); AUD_ASSERT( pSrcVector1->len > 0 && pSrcVector2->len > 0 ); AUD_ASSERT( pDstVector->dataType == pSrcVector1->dataType && pDstVector->dataType == pSrcVector2->dataType ); AUD_Int32s firstSize = pSrcVector1->len * SIZEOF_TYPE( pSrcVector1->dataType ); memmove( pDstVector->pInt16s, pSrcVector1->pInt16s, firstSize ); memmove( (AUD_Int8s*)pDstVector->pInt16s + firstSize, pSrcVector2->pInt16s, pSrcVector2->len * SIZEOF_TYPE( pSrcVector2->dataType ) ); return 0; }
AUD_Int32s cleanMatrix( AUD_Matrix *pMatrix ) { AUD_ASSERT( pMatrix ); AUD_ASSERT( pMatrix->rows > 0 && pMatrix->cols > 0 ); AUD_ASSERT( pMatrix->pInt16s ); AUD_Int32s eleSize = SIZEOF_TYPE( pMatrix->dataType ); AUD_ASSERT( eleSize != -1 ); memset( pMatrix->pInt16s, 0, pMatrix->rows * pMatrix->cols * eleSize ); return 0; }
AUD_Int32s mergeMatrix( AUD_Matrix *pDstMatrix, AUD_Matrix *pSrcMatrix1, AUD_Matrix *pSrcMatrix2 ) { AUD_ASSERT( pDstMatrix && pSrcMatrix1 && pSrcMatrix2 ); AUD_ASSERT( pDstMatrix->rows == (pSrcMatrix1->rows + pSrcMatrix2->rows) ); AUD_ASSERT( pDstMatrix->cols == pSrcMatrix1->cols && pDstMatrix->cols == pSrcMatrix2->cols ); AUD_ASSERT( pDstMatrix->dataType == pSrcMatrix1->dataType && pDstMatrix->dataType == pSrcMatrix2->dataType ); AUD_Int32s firstSize = pSrcMatrix1->rows * pSrcMatrix1->cols * SIZEOF_TYPE( pSrcMatrix1->dataType ); memmove( pDstMatrix->pInt16s, pSrcMatrix1->pInt16s, firstSize ); memmove( (AUD_Int8s*)pDstMatrix->pInt16s + firstSize, pSrcMatrix2->pInt16s, pSrcMatrix2->rows * pSrcMatrix2->cols * SIZEOF_TYPE( pSrcMatrix2->dataType ) ); return 0; }
PRIVATE AUD_ERR_T aud_BtOpen(HAL_AIF_FREQ_T sampleRate, BOOL voice, BOOL mono) { HAL_AIF_CONFIG_T aifCfg; HAL_AIF_SERIAL_CFG_T serialCfg; AUD_ERR_T errStatus = AUD_ERR_NO; if (voice) { AUD_ASSERT(sampleRate == HAL_AIF_FREQ_8000HZ, "AUD: BT: Voice mode only supports 8kHz frequency."); AUD_ASSERT(mono == TRUE, "AUD: BT: Voice mode is mono only."); // TODO add a bunch of assert/check serialCfg.mode = HAL_SERIAL_MODE_VOICE;//HAL_SERIAL_MODE_I2S; serialCfg.aifIsMaster = TRUE; serialCfg.lsbFirst = FALSE; serialCfg.polarity = FALSE; serialCfg.rxDelay = HAL_AIF_RX_DELAY_2; // <-- 2 !!! serialCfg.txDelay = HAL_AIF_TX_DELAY_1;//HAL_AIF_TX_DELAY_ALIGN; serialCfg.rxMode = HAL_AIF_RX_MODE_STEREO_MONO_FROM_L; serialCfg.txMode = HAL_AIF_TX_MODE_MONO_STEREO_CHAN_L; serialCfg.fs = 8000; serialCfg.bckLrckRatio = 31; serialCfg.invertBck = FALSE; serialCfg.outputHalfCycleDelay = FALSE; serialCfg.inputHalfCycleDelay = FALSE; serialCfg.enableBckOutGating = FALSE; aifCfg.interface = HAL_AIF_IF_SERIAL; // FIXME ? aifCfg.serialCfg = &serialCfg; aifCfg.channelNb = HAL_AIF_MONO; aifCfg.sampleRate = HAL_AIF_FREQ_8000HZ; } else { aifCfg.interface = HAL_AIF_IF_PARALLEL_STEREO; aifCfg.serialCfg = NULL; aifCfg.sampleRate = sampleRate; aifCfg.channelNb = mono ? HAL_AIF_MONO : HAL_AIF_STEREO; } errStatus = hal_AifOpen(&aifCfg); g_audBtState.playing = TRUE; SXS_TRACE(TSTDOUT,"AUD: BT: hal_AifOpen %d ",errStatus); return errStatus; }
AUD_Error gmm_export( void *hGmmHandle, FILE *fp ) { AUD_ASSERT( hGmmHandle && fp ); GmmModel *pState = (GmmModel*)hGmmHandle; // write gmm name (void)fwrite( pState->arGmmName, 1, MAX_GMMNAME_LENGTH, fp ); // write num Mix (void)fwrite( &(pState->numMix), sizeof(AUD_Int32s), 1, fp ); // write feature dimension (void)fwrite( &(pState->width), sizeof(AUD_Int32s), 1, fp ); // write feature step (void)fwrite( &(pState->step), sizeof(AUD_Int32s), 1, fp ); // write weights (void)fwrite( pState->weights.pDouble, sizeof(AUD_Double), pState->weights.len, fp ); // write means (void)fwrite( pState->means.pInt32s, sizeof(AUD_Int32s), pState->means.rows * pState->means.cols, fp ); // write diagonal covariance (void)fwrite( pState->cvars.pInt64s, sizeof(AUD_Int64s), pState->cvars.rows * pState->cvars.cols, fp ); // AUDLOG( "exported GMM\n" ); // gmm_show( pState ); return AUD_ERROR_NONE; }
AUD_Int32s pushCircularMatrix( AUD_CircularMatrix *pCircularMatrix, void *pArray ) { AUD_ASSERT( pCircularMatrix && pArray ); pCircularMatrix->curPosition = ( pCircularMatrix->curPosition + 1 ) % pCircularMatrix->matrix.rows; if ( ( pCircularMatrix->curPosition == pCircularMatrix->matrix.rows - 1 ) && ( pCircularMatrix->isFull == AUD_FALSE ) ) { pCircularMatrix->isFull = AUD_TRUE; } AUD_Int8s *pDst = GET_MATRIX_ELEPOINTER( &(pCircularMatrix->matrix), pCircularMatrix->curPosition, 0 ); AUD_ASSERT( pDst ); memcpy( pDst, pArray, SIZEOF_TYPE( pCircularMatrix->matrix.dataType ) * pCircularMatrix->matrix.cols ); return 0; }
int main( int argc, char* argv[] ) { AUD_Int32s ret = 0; AUD_Int32s data = 0; AUD_Int32s i; AUD_Int32s caseNo = -1; AUD_Int32s testNum = (AUD_Int32s)( sizeof(allTests) / sizeof(allTests[0]) ); setbuf( stdout, NULL ); setbuf( stdin, NULL ); AUDLOG( "pls select desired test case with index:\n" ); for ( i = 0; i < testNum; i++ ) { AUDLOG( "\t%d - %s\n", i, allTests[i].pName ); } data = scanf( "%d", &caseNo ); AUD_ASSERT( caseNo >= 0 && caseNo < testNum ); AUDLOG( "chosen test case is: %d\n", caseNo ); ret = allTests[caseNo].fFunc(); return ret; }
void mfccdd_init( Mfcc_deltadelta **phMdd, AUD_Int32s mfccLen ) { Mfcc_deltadelta *pMdd = NULL; AUD_Int32s i; pMdd = (Mfcc_deltadelta*)calloc( sizeof(Mfcc_deltadelta), 1 ); if ( pMdd == NULL ) { *phMdd = NULL; return; } for ( i = 0; i < DDELTA_BUFLEN; i++ ) { pMdd->pState[i] = (AUD_Int32s*)calloc( mfccLen * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pMdd->pState[i] ); } pMdd->c[0] = 0.04; pMdd->c[1] = 0.04; pMdd->c[2] = 0.01; pMdd->c[3] = -0.04; pMdd->c[4] = -0.1; pMdd->c[5] = -0.04; pMdd->c[6] = 0.01; pMdd->c[7] = 0.04; pMdd->c[8] = 0.04; pMdd->mfccLen = mfccLen; *phMdd = pMdd; return; }
AUD_Int32s showVector( AUD_Vector *pVector ) { AUD_ASSERT( pVector ); AUDLOG( "###### vector len: %d, dataType: %d ######\n", pVector->len, pVector->dataType ); switch ( pVector->dataType ) { case AUD_DATATYPE_INT16S: for ( AUD_Int32s i = 0; i < pVector->len; i++ ) { AUDLOG( "%d, ", *( pVector->pInt16s + i ) ); } AUDLOG( "\n" ); break; case AUD_DATATYPE_INT32S: for ( AUD_Int32s i = 0; i < pVector->len; i++ ) { AUDLOG( "%d, ", *( pVector->pInt32s + i ) ); } AUDLOG( "\n" ); break; case AUD_DATATYPE_INT64S: for ( AUD_Int32s i = 0; i < pVector->len; i++ ) { AUDLOG( "%lld, ", *( pVector->pInt64s + i ) ); } AUDLOG( "\n" ); break; case AUD_DATATYPE_DOUBLE: for ( AUD_Int32s i = 0; i < pVector->len; i++ ) { AUDLOG( "%f, ", *( pVector->pDouble + i ) ); } AUDLOG( "\n" ); break; default: AUD_ASSERT( 0 ); break; } AUDLOG( "\n" ); return 0; }
AUD_Int32s gmm_getmixnum( void *hGmmHandle ) { AUD_ASSERT( hGmmHandle != NULL ); GmmModel *pState = (GmmModel*)hGmmHandle; return pState->numMix; }
// ============================================================================= // aud_NullStreamStart // ----------------------------------------------------------------------------- /// Start the playing of a stream. /// /// This function outputs the /// audio on the audio interface specified as a parameter, on the output selected in /// the #aud_Setup function. \n /// In normal operation, when /// the buffer runs out, the playing will wrap at the beginning. Here, there are two ways /// you can handle your buffer: HAL can call a user function at the middle or /// at the end of the playback or, depending /// on your application, you can simply poll the playing state using the "reached half" /// and "reached end functions" TODO Ask if needed, and then implement ! /// /// @param stream Stream to play. Handler called at the middle and end of the buffer /// are defined there. /// @param cfg The configuration set applied to the audio interface /// @return #AUD_ERR_RESOURCE_BUSY when the driver is busy with another audio /// command, /// #AUD_ERR_NO it can execute the command. // ============================================================================= PUBLIC AUD_ERR_T aud_NullStreamStart( SND_ITF_T itf, CONST HAL_AIF_STREAM_T* stream, CONST AUD_DEVICE_CFG_T * cfg) { AUD_ASSERT(FALSE, "Attempt to use an inexisting audio device, %s, %d", __FILE__, __LINE__); return HAL_ERR_NO; }
// matrix AUD_Int32s createMatrix( AUD_Matrix *pMatrix ) { AUD_ASSERT( pMatrix ); AUD_ASSERT( pMatrix->rows > 0 && pMatrix->cols > 0 ); AUD_Int32s eleSize = SIZEOF_TYPE( pMatrix->dataType ); AUD_ASSERT( eleSize != -1 ); // since union member share the value, do just allocate one of them is OK pMatrix->pInt16s = (AUD_Int16s*)calloc( pMatrix->rows * pMatrix->cols * eleSize, 1 ); if ( pMatrix->pInt16s == NULL ) { return -1; } return 0; }
AUD_Int32s transposeMatrix( AUD_Matrix *pSrcMatrix, AUD_Matrix *pDstMatrix ) { AUD_ASSERT( pSrcMatrix && pDstMatrix ); AUD_ASSERT( pSrcMatrix->rows > 0 && pSrcMatrix->cols > 0 ); AUD_ASSERT( pDstMatrix->rows > 0 && pDstMatrix->cols > 0 ); AUD_ASSERT( pSrcMatrix->cols == pDstMatrix->rows ); AUD_ASSERT( pSrcMatrix->rows == pDstMatrix->cols ); AUD_ASSERT( pSrcMatrix->dataType == pDstMatrix->dataType ); AUD_Int32s i, j; switch ( pSrcMatrix->dataType ) { case AUD_DATATYPE_INT16S: for ( i = 0; i < pSrcMatrix->rows; i++ ) { for ( j = 0; j < pSrcMatrix->cols; j++ ) { *( pDstMatrix->pInt16s + j * pDstMatrix->cols + i ) = *( pSrcMatrix->pInt16s + i * pSrcMatrix->cols + j ); } } break; case AUD_DATATYPE_INT32S: for ( i = 0; i < pSrcMatrix->rows; i++ ) { for ( j = 0; j < pSrcMatrix->cols; j++ ) { *( pDstMatrix->pInt32s + j * pDstMatrix->cols + i ) = *( pSrcMatrix->pInt32s + i * pSrcMatrix->cols + j ); } } break; case AUD_DATATYPE_INT64S: for ( i = 0; i < pSrcMatrix->rows; i++ ) { for ( j = 0; j < pSrcMatrix->cols; j++ ) { *( pDstMatrix->pInt64s + j * pDstMatrix->cols + i ) = *( pSrcMatrix->pInt64s + i * pSrcMatrix->cols + j ); } } break; case AUD_DATATYPE_DOUBLE: for ( i = 0; i < pSrcMatrix->rows; i++ ) { for ( j = 0; j < pSrcMatrix->cols; j++ ) { *( pDstMatrix->pDouble + j * pDstMatrix->cols + i ) = *( pSrcMatrix->pDouble + i * pSrcMatrix->cols + j ); } } break; default: AUD_ASSERT( 0 ); break; } AUDLOG( "\n" ); return 0; }
// ============================================================================= // aud_NullTone // ----------------------------------------------------------------------------- // Manage the playing of a tone: DTMF or Comfort Tone. /// /// Outputs a DTMF or comfort tone /// /// When the audio output is enabled, a DTMF or a comfort tones can be output /// as well. This function starts the output of a tone generated in the audio /// module. \n /// You can call this function several times without calling the /// #aud_ToneStop function or the #aud_TonePause function in /// between, if you just need to change the attenuation or the tone type. \n /// If the function returns #AUD_ERR_RESOURCE_BUSY, it means that the driver is /// busy with an other audio command. /// /// @param tone The tone to generate /// @param attenuation The attenuation level of the tone generator /// @param cfg The configuration set applied to the audio interface /// @param start If \c TRUE, starts the playing of the tone. /// If \c FALSE, stops the tone. /// /// @return #AUD_ERR_RESOURCE_BUSY, if the driver is busy with another audio command, /// #AUD_ERR_NO otherwise // ============================================================================= PUBLIC AUD_ERR_T aud_NullTone( SND_ITF_T itf, CONST SND_TONE_TYPE_T tone, CONST AUD_DEVICE_CFG_T* cfg, CONST BOOL start) { AUD_ASSERT(FALSE, "Attempt to use an inexisting audio device, %s, %d", __FILE__, __LINE__); return AUD_ERR_NO; return HAL_ERR_NO; }
AUD_Error gmm_getname( void *hGmmHandle, AUD_Int8s arGmmName[256] ) { AUD_ASSERT( hGmmHandle != NULL ); GmmModel *pState = (GmmModel*)hGmmHandle; strcpy( (char*)arGmmName, (char*)(pState->arGmmName) ); return AUD_ERROR_NONE; }
AUD_Int32s sortVector( AUD_Vector *pVector, AUD_Vector *pSortedIdx ) { AUD_ASSERT( pVector && pSortedIdx ); AUD_ASSERT( pSortedIdx->len <= pVector->len && pSortedIdx->len > 0 ); AUD_ASSERT( pVector->dataType == AUD_DATATYPE_DOUBLE ); AUD_ASSERT( pSortedIdx->dataType == AUD_DATATYPE_INT32S ); AUD_ASSERT( pVector->dataType == AUD_DATATYPE_DOUBLE ); AUD_Int32s i, j, k; pSortedIdx->pInt32s[0] = 0; for ( i = 1; i < pVector->len; i++ ) { for ( j = 0; j < AUD_MIN( pSortedIdx->len, i ); j++ ) { if ( pVector->pDouble[i] > pVector->pDouble[pSortedIdx->pInt32s[j]] ) { for ( k = pSortedIdx->len - 2; k >= j; k-- ) { pSortedIdx->pInt32s[k + 1] = pSortedIdx->pInt32s[k]; } pSortedIdx->pInt32s[j] = i; break; } } if ( j == i && j < pSortedIdx->len ) { pSortedIdx->pInt32s[j] = i; } } #if 0 AUDLOG( "\n" ); for ( i = 0; i < pSortedIdx->len; i++ ) { AUDLOG( "%f, ", pVector->pDouble[pSortedIdx->pInt32s[i]] ); } AUDLOG( "\n" ); #endif return 0; }
AUD_Int32s copyMatrixRow( AUD_Matrix *pDstMatrix, AUD_Int32s dstRow, AUD_Matrix *pSrcMatrix, AUD_Int32s srcRow ) { AUD_ASSERT( pDstMatrix && pSrcMatrix ); AUD_ASSERT( pDstMatrix->dataType == pSrcMatrix->dataType ); AUD_ASSERT( pDstMatrix->cols == pSrcMatrix->cols ); AUD_ASSERT( dstRow < pDstMatrix->rows && dstRow >= 0 ); AUD_ASSERT( srcRow < pSrcMatrix->rows && srcRow >= 0 ); AUD_Int32s cols = pDstMatrix->cols; switch ( pDstMatrix->dataType ) { case AUD_DATATYPE_INT16S: memmove( pDstMatrix->pInt16s + dstRow * cols, pSrcMatrix->pInt16s + srcRow * cols, cols * SIZEOF_TYPE( pDstMatrix->dataType ) ); break; case AUD_DATATYPE_INT32S: memmove( pDstMatrix->pInt32s + dstRow * cols, pSrcMatrix->pInt32s + srcRow * cols, cols * SIZEOF_TYPE( pDstMatrix->dataType ) ); break; case AUD_DATATYPE_INT64S: memmove( pDstMatrix->pInt64s + dstRow * cols, pSrcMatrix->pInt64s + srcRow * cols, cols * SIZEOF_TYPE( pDstMatrix->dataType ) ); break; case AUD_DATATYPE_DOUBLE: memmove( pDstMatrix->pDouble + dstRow * cols, pSrcMatrix->pDouble + srcRow * cols, cols * SIZEOF_TYPE( pDstMatrix->dataType ) ); break; default: AUD_ASSERT( 0 ); break; } return 0; }
// get min distance to cluster using Euclidean distance AUD_Double calc_nearestdist( AUD_Matrix *pFeatMatrix, AUD_Int32s rowIndex, AUD_Matrix *pCluster ) { AUD_ASSERT( pFeatMatrix != NULL ); AUD_ASSERT( pCluster != NULL ); AUD_ASSERT( pFeatMatrix->dataType == AUD_DATATYPE_INT32S ); AUD_ASSERT( pCluster->dataType == AUD_DATATYPE_INT32S ); AUD_ASSERT( rowIndex >= 0 && rowIndex < pFeatMatrix->rows ); AUD_ASSERT( pFeatMatrix->cols == pCluster->cols && pCluster->cols > 0 ); AUD_Int32s i; AUD_Double minDistance = -1.; AUD_Double curDistance; AUD_Int32s *pVec = pFeatMatrix->pInt32s + rowIndex * pFeatMatrix->cols; for ( i = 0; i < pCluster->rows; i++ ) { curDistance = euclideanDist( pVec, pCluster->pInt32s + i * pCluster->cols, pCluster->cols ); if ( curDistance < minDistance || minDistance < 0 ) { minDistance = curDistance; } } return minDistance; }
// vector AUD_Int32s createVector( AUD_Vector *pVector ) { AUD_ASSERT( pVector && pVector->len > 0 ); // since union member share the value, do just allocate one of them is OK pVector->pInt16s = (AUD_Int16s*)calloc( pVector->len * SIZEOF_TYPE( pVector->dataType ), 1 ); if ( pVector->pInt16s == NULL ) { return -1; } return 0; }
AUD_Int32s destroyVector( AUD_Vector *pVector ) { AUD_ASSERT( pVector ); if ( pVector->pInt16s == NULL ) { return -1; } // since union member share the value, do just free one of them is OK free( (void*)(pVector->pInt16s) ); pVector->pInt16s = NULL; pVector->len = 0; pVector->dataType = AUD_DATATYPE_INVALID; return 0; }
/* compute probability for Mixture of Gaussians */ AUD_Double gmm_prob( void *hGmmHandle, AUD_Matrix *pFeatMatrix, AUD_Int32s rowIndex, AUD_Vector *pComponentProb ) { AUD_ASSERT( hGmmHandle && pFeatMatrix ); AUD_ASSERT( pFeatMatrix->dataType == AUD_DATATYPE_INT32S ); AUD_ASSERT( rowIndex < pFeatMatrix->rows ); AUD_ASSERT( !pComponentProb || ( pComponentProb && pComponentProb->dataType == AUD_DATATYPE_DOUBLE ) ); GmmModel *pState = (GmmModel*)hGmmHandle; AUD_Int32s i; AUD_ASSERT( pFeatMatrix->cols == pState->width ); AUD_Double *llr = (AUD_Double*)calloc( pState->numMix * sizeof(AUD_Double), 1 ); AUD_ASSERT( llr ); AUD_Double maxLLR = 0.; for ( i = 0; i < pState->numMix; i++ ) { loggauss( pFeatMatrix->pInt32s + rowIndex * pFeatMatrix->cols, pState->means.pInt32s + i * pState->step, pState->cvars.pInt64s + i * pState->step, pState->width, &llr[i], (pState->dets.pDouble)[i] ); if ( pComponentProb != NULL ) { pComponentProb->pDouble[i] = exp( llr[i] ); } if ( i == 0 ) { maxLLR = llr[i]; } else { maxLLR = AUD_MAX( maxLLR, llr[i] ); } } AUD_Double expDiffSum = 0., totalLLR, prob; for ( i = 0; i < pState->numMix; i++ ) { expDiffSum += exp( llr[i] - maxLLR ); } totalLLR = log( expDiffSum ) + maxLLR; prob = exp( totalLLR ); // AUDLOG( "totalLLR[%f], prob[%f]\n", totalLLR, prob ); free( llr ); llr = NULL; return prob; }
AUD_Int32s destroyMatrix( AUD_Matrix *pMatrix ) { AUD_ASSERT( pMatrix ); if ( pMatrix->pInt16s == NULL ) { return -1; } // since union member share the value, do just free one of them is OK free( (void*)(pMatrix->pInt16s) ); pMatrix->pInt16s = NULL; pMatrix->rows = 0; pMatrix->cols = 0; pMatrix->dataType = AUD_DATATYPE_INVALID; return 0; }
void mfcc_init( Mfcc **phMfcc, AUD_Int32s winSize, AUD_Float fs, AUD_Float fl, AUD_Float fh, AUD_Int32s fbLen, AUD_Float melMul, AUD_Float melDiv, AUD_Int32s mfccLen, AUD_AmpCompress compressType ) { Mfcc *pMfcc = NULL; AUD_Int32s fftOrder, i; pMfcc = (Mfcc*)calloc( sizeof(Mfcc), 1 ); if ( pMfcc == NULL ) { *phMfcc = NULL; return; } *phMfcc = pMfcc; pMfcc->fftLen = winSize / 2; pMfcc->fbLen = fbLen; pMfcc->mfccLen = mfccLen; pMfcc->compressType = compressType; melfb_init( &(pMfcc->pFb), &fftOrder, winSize, fs, fl, fh, fbLen, melMul, melDiv ); pMfcc->fbBuffer = (AUD_Int32s*)calloc( fbLen * sizeof(AUD_Int32s), 1 ); if ( pMfcc->fbBuffer == NULL ) { free( pMfcc ); *phMfcc = NULL; return; } dct_init( &(pMfcc->pDct), 1, mfccLen, fbLen, 15 ); for ( i = 0; i < RAW_BUFLEN; i++ ) { pMfcc->pState[i] = (AUD_Int32s*)calloc( mfccLen * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pMfcc->pState[i] ); } return; }