예제 #1
0
파일: rfs.c 프로젝트: karajrish/OS
int closeDir(int dirhandle){
	// Find the corresponding open file object and remove it
	int i;
	struct InCoreINode icn;
	struct DirEntry d;
	struct INode in;
	int inodeNo = isOpen(dirhandle, &icn);
	if( inodeNo==-1 ){
		printf("Directory not open!\n");
		return -1;
	}
	if( icn.ic_inode.i_nlinks==0 ){
		// readDirEntry(icn.ic_inode, )
		printf("Not a directory!%d\n", icn.ic_ino);
		return -1;
	}
	for(i=1; i<icn.ic_inode.i_nlinks; i++){
		readDirEntry(icn.ic_dev, &icn.ic_inode, i, &d);
		readINode(icn.ic_dev, d.d_entry.d_inode, &in);
		if( in.i_nlinks==0 )	closeFile(d.d_entry.d_inode);
		else					closeDir(d.d_entry.d_inode);
	}
	removeOpenFile(dirhandle);

}
예제 #2
0
파일: rfs.c 프로젝트: karajrish/OS
//============= TESTING APPLICATION USING THESE FS CALLS ==============
// Menu driven testing application for creation of fs, 
// and all file and directory related operations
int main(int argc, char** argv){
	int fd = openDevice(0);
	init_FS(fd);
	makeDir(fd, "Folder 1", 1, 1, 1);
	makeDir(fd, "Folder 2", 1, 1, 1);
	makeDir(fd, "Folder 3", 1, 1, 1);
	removeDir(fd, "Folder 2");
	int dirhandle = openDir(fd, "Folder 1");
	closeDir(dirhandle);
	shutdownDevice(0);
}
예제 #3
0
int train_hmm()
{
	AUD_Int8s  filePath[256] = { 0, };
	AUD_Int32s data;

	setbuf( stdout, NULL );
	setbuf( stdin, NULL );
	AUDLOG( "pls give training wav stream's folder path:\n" );
	filePath[0] = '\0';
	data = scanf( "%s", filePath );
	AUDLOG( "training folder path is: %s\n", filePath );

	entry *pEntry = NULL;
	dir   *pDir   = NULL;

	pDir = openDir( (const char*)filePath );
	while ( ( pEntry = scanDir( pDir ) ) )
	{
		// skip non wav file
		char suffix[256] = { 0, };
		for ( int i = 0; i < (int)strlen( pEntry->name ); i++ )
		{
			suffix[i] = tolower( pEntry->name[i] );
		}
		suffix[strlen( pEntry->name )] = '\0';

		if ( strstr( suffix, ".wav" ) == NULL )
		{
			continue;
		}

		char fileName[256] = { 0, };
		sprintf( fileName, "%s/%s", (const char*)filePath, pEntry->name );

		char hmmName[256] = { 0, };
		sprintf( hmmName, "%s/%s", WOV_KEYWORD_GMMHMMMODEL_DIR, pEntry->name );
		strcpy( hmmName + strlen( hmmName ) - 3, "hmm" );

		train_keyword_hmm( (const AUD_Int8s*)fileName, (AUD_Int8s*)hmmName );
	}
	closeDir( pDir );
	pDir = NULL;

	AUDLOG( "HMM training done\n" );

	return 0;
}
예제 #4
0
gxFileSystem::~gxFileSystem()
{
	while (dir_set.size())
		closeDir(*dir_set.begin());
}
예제 #5
0
AUD_Int32s perf_dtw_mfcc_vad()
{
	char            inputPath[256]   = { 0, };
	char            keywordPath[256] = { 0, };
	AUD_Double      threshold, minThreshold, maxThreshold, stepThreshold;
	AUD_Bool        isRecognized     = AUD_FALSE;
	AUD_Int32s      data;

	AUD_Error       error = AUD_ERROR_NONE;

	setbuf( stdin, NULL );
	AUDLOG( "pls give template wav stream's path:\n" );
	keywordPath[0] = '\0';
	data = scanf( "%s", keywordPath );
	AUDLOG( "template path is: %s\n", keywordPath );

	setbuf( stdin, NULL );
	AUDLOG( "pls give test wav stream's path:\n" );
	inputPath[0] = '\0';
	data = scanf( "%s", inputPath );
	AUDLOG( "test stream path is: %s\n", inputPath );

	setbuf( stdin, NULL );
	AUDLOG( "pls give lowest test threshold:\n" );
	data = scanf( "%lf", &minThreshold );
	AUDLOG( "lowest threshold is: %.2f\n", minThreshold );

	setbuf( stdin, NULL );
	AUDLOG( "pls give max test threshold:\n" );
	data = scanf( "%lf", &maxThreshold );
	AUDLOG( "max threshold is: %.2f\n", maxThreshold );

	setbuf( stdin, NULL );
	AUDLOG( "pls give test threshold step:\n" );
	data = scanf( "%lf", &stepThreshold );
	AUDLOG( "threshold step is: %.2f\n", stepThreshold );

	AUDLOG( "\n\n" );

	// file & directory operation, linux dependent
	entry   *pEntry        = NULL;
	dir     *pDir          = NULL;
	entry   *pKeywordEntry = NULL;
	dir     *pKeywordDir   = NULL;

	AUD_Int32s      i, j;

	AUD_Feature     keywordFeature;
	AUD_Int32s      keywordSampleNum = 0;
	AUD_Int32s      keywordWinNum    = 0;
	AUD_Int8s       keywordFile[256] = { 0, };
	AUD_Int8s       keywordName[256] = { 0, };
	AUD_Int32s      keywordID        = 0;

	AUD_DTWSession  dtwSession;
	AUD_Double      score             = 0.;
	AUD_Int32s      sampleNum;

	AUD_Int32s      keywordBufLen = 0;
	AUD_Int16s      *pKeywordBuf  = NULL;
	AUD_Int32s      bufLen        = 0;
	AUD_Int16s      *pBuf         = NULL;

	AUD_Int32s      ret;

	// init performance benchmark
	void *hBenchmark  = NULL;
	char logName[256] = { 0, };
	snprintf( logName, 256, "dtw-mfcc-vad-%d-%d-%d-%d", WOV_DTW_TYPE, WOV_DISTANCE_METRIC, WOV_DTWTRANSITION_STYLE, WOV_DTWSCORING_METHOD );
	ret = benchmark_init( &hBenchmark, (AUD_Int8s*)logName, keywords, sizeof(keywords) / sizeof(keywords[0]) );
	AUD_ASSERT( ret == 0 );

	for ( threshold = minThreshold; threshold < maxThreshold + stepThreshold; threshold += stepThreshold )
	{
		int frameStride = 0;

		ret = benchmark_newthreshold( hBenchmark, threshold );
		AUD_ASSERT( ret == 0 );

		AUDLOG( "***********************************************\n" );
		AUDLOG( "keyword training start...\n" );

		pKeywordDir = openDir( (const char*)keywordPath );
		keywordID  = 0;
		while ( ( pKeywordEntry = scanDir( pKeywordDir ) ) )
		{
			// train keyword
			AUD_Summary fileSummary;

			snprintf( (char*)keywordFile, 256, "%s/%s", (const char*)keywordPath, pKeywordEntry->name );

			ret = parseWavFromFile( keywordFile, &fileSummary );
			if ( ret < 0 )
			{
				continue;
			}
			AUD_ASSERT( fileSummary.channelNum == CHANNEL_NUM && fileSummary.bytesPerSample == BYTES_PER_SAMPLE && fileSummary.sampleRate == SAMPLE_RATE );

			keywordBufLen = fileSummary.dataChunkBytes + 100;
			pKeywordBuf   = (AUD_Int16s*)calloc( keywordBufLen, 1 );
			AUD_ASSERT( pKeywordBuf );

			keywordSampleNum = readWavFromFile( keywordFile, pKeywordBuf, keywordBufLen );
			if ( keywordSampleNum < 0 )
			{
				continue;
			}

			// front end processing
			 // pre-emphasis
			sig_preemphasis( pKeywordBuf, pKeywordBuf, keywordSampleNum );


			for ( j = 0; j * FRAME_STRIDE + FRAME_LEN <= keywordSampleNum; j++ )
			{
				;
			}
			frameStride = FRAME_STRIDE;

			AUDLOG( "pattern[%d: %s] valid frame number[%d]\n", keywordID, keywordFile, j );

			keywordWinNum                         = j;
			keywordFeature.featureMatrix.rows     = keywordWinNum - MFCC_DELAY;
			keywordFeature.featureMatrix.cols     = MFCC_FEATDIM;
			keywordFeature.featureMatrix.dataType = AUD_DATATYPE_INT32S;
			ret = createMatrix( &(keywordFeature.featureMatrix) );
			AUD_ASSERT( ret == 0 );

			keywordFeature.featureNorm.len      = keywordWinNum - MFCC_DELAY;
			keywordFeature.featureNorm.dataType = AUD_DATATYPE_INT64S;
			ret = createVector( &(keywordFeature.featureNorm) );
			AUD_ASSERT( ret == 0 );

			void *hTemplateMfccHandle = NULL;

			// init mfcc handle
			error = mfcc16s32s_init( &hTemplateMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, frameStride, SAMPLE_RATE, COMPRESS_TYPE );
			AUD_ASSERT( error == AUD_ERROR_NONE );

			// calc MFCC feature
			error = mfcc16s32s_calc( hTemplateMfccHandle, pKeywordBuf, keywordSampleNum, &keywordFeature );
			AUD_ASSERT( error == AUD_ERROR_NONE );

			// deinit mfcc handle
			error = mfcc16s32s_deinit( &hTemplateMfccHandle );
			AUD_ASSERT( error == AUD_ERROR_NONE );

			strncpy( (char*)keywordName, pKeywordEntry->name, 255 );
			ret = benchmark_newtemplate( hBenchmark, keywordName );
			AUD_ASSERT( ret == 0 );

			keywordID++;

			keywordBufLen  = 0;
			free( pKeywordBuf );
			pKeywordBuf = NULL;

			AUDLOG( "keyword training finish...\n" );

			// recognize
			pEntry = NULL;
			pDir   = NULL;
			pDir   = openDir( (const char*)inputPath );
			while ( ( pEntry = scanDir( pDir ) ) )
			{
				AUD_Int8s   inputStream[256] = { 0, };
				void        *hMfccHandle     = NULL;
				AUD_Summary fileSummary;

				snprintf( (char*)inputStream, 256, "%s/%s", (const char*)inputPath, pEntry->name );

				ret = parseWavFromFile( inputStream, &fileSummary );
				if ( ret < 0 )
				{
					continue;
				}
				AUD_ASSERT( fileSummary.channelNum == CHANNEL_NUM && fileSummary.bytesPerSample == BYTES_PER_SAMPLE && fileSummary.sampleRate == SAMPLE_RATE );
				AUDLOG( "input stream: %s\n", inputStream );

				bufLen  = fileSummary.dataChunkBytes;
				pBuf = (AUD_Int16s*)malloc( bufLen + 100 );
				AUD_ASSERT( pBuf );

				sampleNum = readWavFromFile( inputStream, pBuf, bufLen );
				AUD_ASSERT( sampleNum > 0 );

				// VAD
				AUD_Int16s *pValidBuf = (AUD_Int16s*)calloc( bufLen, 1 );
				AUD_ASSERT( pValidBuf );
				AUD_Int16s *pSpeechFlag  = (AUD_Int16s*)calloc( ( sampleNum / VAD_FRAME_LEN ) + 1, sizeof( AUD_Int16s ) );
				AUD_ASSERT( pSpeechFlag );

				void *hVadHandle = NULL;
				ret = sig_vadinit( &hVadHandle, pValidBuf, pSpeechFlag, bufLen, -1, -1 );
				AUD_ASSERT( ret == 0 );

				AUD_Int32s start = -1;
				for ( i = 0; i * VAD_FRAME_LEN + FRAME_LEN <= sampleNum; i++ )
				{
					ret = sig_vadupdate( hVadHandle, pBuf + i * VAD_FRAME_LEN, &start );
					if ( ret == 0 )
					{
						continue;
					}
					else
					{
						break;
					}
				}

				if ( ret == 0 )
				{
					continue;
				}

				AUD_Int32s validSampleNum = ret;
				sig_vaddeinit( &hVadHandle );

#if 1
				char vadFile[256] = { 0, };
				snprintf( vadFile, 255, "./vaded/%s", pEntry->name );
				ret = writeWavToFile( (AUD_Int8s*)vadFile, pValidBuf + start, validSampleNum );
				AUD_ASSERT( ret == 0 );
#endif

				// de-noise
				AUD_Int16s *pCleanBuf = (AUD_Int16s*)calloc( (validSampleNum + start) * BYTES_PER_SAMPLE, 1 );
				AUD_ASSERT( pCleanBuf );

				AUD_Int32s cleanLen = denoise_mmse( pValidBuf, pCleanBuf, (validSampleNum + start) );

#if 1
				char cleanFile[256] = { 0, };
				snprintf( cleanFile, 255, "./clean/%s", pEntry->name );
				ret = writeWavToFile( (AUD_Int8s*)cleanFile, pCleanBuf, cleanLen );
				AUD_ASSERT( ret == 0 );
#endif
				// front end processing
				 // pre-emphasis
				sig_preemphasis( pCleanBuf, pCleanBuf, cleanLen );

				for ( j = 0; j * FRAME_STRIDE + FRAME_LEN <= cleanLen; j++ )
				{
					;
				}
				frameStride = FRAME_LEN;

				bufLen = 0;
				free( pBuf );
				pBuf = NULL;
				free( pValidBuf );
				pValidBuf = NULL;
				free( pSpeechFlag );
				pSpeechFlag = NULL;

				AUD_Feature inFeature;
				inFeature.featureMatrix.rows     = j - MFCC_DELAY;
				inFeature.featureMatrix.cols     = MFCC_FEATDIM;
				inFeature.featureMatrix.dataType = AUD_DATATYPE_INT32S;
				ret = createMatrix( &(inFeature.featureMatrix) );
				AUD_ASSERT( ret == 0 );

				inFeature.featureNorm.len      = j - MFCC_DELAY;
				inFeature.featureNorm.dataType = AUD_DATATYPE_INT64S;
				ret = createVector( &(inFeature.featureNorm) );
				AUD_ASSERT( ret == 0 );

				// init mfcc handle
				error = mfcc16s32s_init( &hMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, frameStride, SAMPLE_RATE, COMPRESS_TYPE );
				AUD_ASSERT( error == AUD_ERROR_NONE );

				error = mfcc16s32s_calc( hMfccHandle, pCleanBuf, cleanLen, &inFeature );
				AUD_ASSERT( error == AUD_ERROR_NONE );

				// init dtw match
				dtwSession.dtwType        = WOV_DTW_TYPE;
				dtwSession.distType       = WOV_DISTANCE_METRIC;
				dtwSession.transitionType = WOV_DTWTRANSITION_STYLE;
				error = dtw_initsession( &dtwSession, &keywordFeature, inFeature.featureMatrix.rows );
				AUD_ASSERT( error == AUD_ERROR_NONE );

				error = dtw_updatefrmcost( &dtwSession, &inFeature );
				AUD_ASSERT( error == AUD_ERROR_NONE );

				error = dtw_match( &dtwSession, WOV_DTWSCORING_METHOD, &score, NULL );
				AUD_ASSERT( error == AUD_ERROR_NONE );

				error = dtw_deinitsession( &dtwSession );
				AUD_ASSERT( error == AUD_ERROR_NONE );

				// deinit mfcc handle
				error = mfcc16s32s_deinit( &hMfccHandle );
				AUD_ASSERT( error == AUD_ERROR_NONE );

				ret = destroyMatrix( &(inFeature.featureMatrix) );
				AUD_ASSERT( ret == 0 );

				ret = destroyVector( &(inFeature.featureNorm) );
				AUD_ASSERT( ret == 0 );

				AUDLOG( "score: %.2f\n", score );

				isRecognized = AUD_FALSE;
				if ( score <= threshold )
				{
					isRecognized = AUD_TRUE;
				}

				// insert log entry
				ret = benchmark_additem( hBenchmark, (AUD_Int8s*)pEntry->name, score, isRecognized );
				AUD_ASSERT( ret == 0 );

				cleanLen = 0;
				free( pCleanBuf );
				pCleanBuf = NULL;
			}
			closeDir( pDir );
			pDir = NULL;

			ret = benchmark_finalizetemplate( hBenchmark );
			AUD_ASSERT( ret == 0 );

			ret = destroyMatrix( &(keywordFeature.featureMatrix) );
			AUD_ASSERT( ret == 0 );

			ret = destroyVector( &(keywordFeature.featureNorm) );
			AUD_ASSERT( ret == 0 );
		}
		closeDir( pKeywordDir );
		pKeywordDir = NULL;

		ret = benchmark_finalizethreshold( hBenchmark );
		AUD_ASSERT( ret == 0 );
	}

	ret = benchmark_free( &hBenchmark );
	AUD_ASSERT( ret == 0 );

	AUDLOG( "DTW-MFCC VAD Benchmark finished\n" );

	return 0;
}
예제 #6
0
AUD_Int32s perf_dtw_mfcc()
{
	char            inputPath[256]   = { 0, };
	char            keywordPath[256] = { 0, };
	AUD_Double      threshold, minThreshold, maxThreshold, stepThreshold;
	AUD_Bool        isRecognized     = AUD_FALSE;
	AUD_Int32s      data;

	AUD_Error       error = AUD_ERROR_NONE;

	setbuf( stdin, NULL );
	AUDLOG( "pls give keyword wav stream's path:\n" );
	keywordPath[0] = '\0';
	data = scanf( "%s", keywordPath );
	AUDLOG( "keyword path is: %s\n", keywordPath );

	setbuf( stdin, NULL );
	AUDLOG( "pls give test wav stream's path:\n" );
	inputPath[0] = '\0';
	data = scanf( "%s", inputPath );
	AUDLOG( "test stream path is: %s\n", inputPath );

	setbuf( stdin, NULL );
	AUDLOG( "pls give min test threshold:\n" );
	data = scanf( "%lf", &minThreshold );
	AUDLOG( "min threshold is: %.2f\n", minThreshold );

	setbuf( stdin, NULL );
	AUDLOG( "pls give max test threshold:\n" );
	data = scanf( "%lf", &maxThreshold );
	AUDLOG( "max threshold is: %.2f\n", maxThreshold );

	setbuf( stdin, NULL );
	AUDLOG( "pls give test threshold step:\n" );
	data = scanf( "%lf", &stepThreshold );
	AUDLOG( "threshold step is: %.2f\n", stepThreshold );

	AUDLOG( "\n\n" );

	// file & directory operation, linux dependent
	entry   *pEntry        = NULL;
	dir     *pDir          = NULL;
	entry   *pKeywordEntry = NULL;
	dir     *pKeywordDir   = NULL;

	AUD_Feature     keywordFeature;
	AUD_Int32s      keywordSampleNum = 0;
	AUD_Int32s      keywordWinNum    = 0;
	AUD_Int8s       keywordFile[256] = { 0, };
	AUD_Int8s       keywordName[256] = { 0, };
	AUD_Int32s      keywordID        = 0;

	AUD_DTWSession  dtwSession;
	AUD_Double      score            = 0.;
	AUD_Int32s      sampleNum;

	AUD_Int32s      keywordBufLen = 0;
	AUD_Int16s      *pKeywordBuf  = NULL;
	AUD_Int32s      bufLen        = 0;
	AUD_Int16s      *pBuf         = NULL;

	AUD_Int32s      frameStride   = FRAME_STRIDE;

	AUD_Int32s      j;
	AUD_Int32s      ret;

	// init performance benchmark
	void *hBenchmark  = NULL;
	char logName[256] = { 0, };
	snprintf( logName, 256, "dtw-mfcc-%d-%d-%d-%d", WOV_DTW_TYPE, WOV_DISTANCE_METRIC, WOV_DTWTRANSITION_STYLE, WOV_DTWSCORING_METHOD );
	ret = benchmark_init( &hBenchmark, (AUD_Int8s*)logName, keywords, sizeof(keywords) / sizeof(keywords[0]) );
	AUD_ASSERT( ret == 0 );


	for ( threshold = minThreshold; threshold < maxThreshold + stepThreshold; threshold += stepThreshold )
	{
		ret = benchmark_newthreshold( hBenchmark, threshold );
		AUD_ASSERT( ret == 0 );

		AUDLOG( "***********************************************\n" );
		AUDLOG( "keyword training start...\n" );

		pKeywordDir = openDir( (const char*)keywordPath );
		keywordID  = 0;
		while ( ( pKeywordEntry = scanDir( pKeywordDir ) ) )
		{
			// train keyword
			keywordBufLen  = SAMPLE_RATE * BYTES_PER_SAMPLE * 10;
			pKeywordBuf = (AUD_Int16s*)calloc( keywordBufLen, 1 );
			AUD_ASSERT( pKeywordBuf );

			snprintf( (char*)keywordFile, 256, "%s/%s", (const char*)keywordPath, pKeywordEntry->name );

			keywordSampleNum = readWavFromFile( keywordFile, pKeywordBuf, keywordBufLen );
			if ( keywordSampleNum < 0 )
			{
				continue;
			}

			// front end processing
			 // pre-emphasis
			sig_preemphasis( pKeywordBuf, pKeywordBuf, keywordSampleNum );

			for ( j = 0; j * frameStride + FRAME_LEN <= keywordSampleNum; j++ )
			{
				;
			}
			AUDLOG( "pattern[%d: %s] valid frame number[%d]\n", keywordID, keywordFile, j );

			keywordWinNum                         = j;
			keywordFeature.featureMatrix.rows     = keywordWinNum - MFCC_DELAY;
			keywordFeature.featureMatrix.cols     = MFCC_FEATDIM;
			keywordFeature.featureMatrix.dataType = AUD_DATATYPE_INT32S;
			ret = createMatrix( &(keywordFeature.featureMatrix) );
			AUD_ASSERT( ret == 0 );

			keywordFeature.featureNorm.len      = keywordWinNum - MFCC_DELAY;
			keywordFeature.featureNorm.dataType = AUD_DATATYPE_INT64S;
			ret = createVector( &(keywordFeature.featureNorm) );
			AUD_ASSERT( ret == 0 );

			void *hKeywordMfccHandle = NULL;

			// init mfcc handle
			error = mfcc16s32s_init( &hKeywordMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, frameStride, SAMPLE_RATE, COMPRESS_TYPE );
			AUD_ASSERT( error == AUD_ERROR_NONE );

			// calc MFCC feature
			error = mfcc16s32s_calc( hKeywordMfccHandle, pKeywordBuf, keywordSampleNum, &keywordFeature );
			AUD_ASSERT( error == AUD_ERROR_NONE );

			// deinit mfcc handle
			error = mfcc16s32s_deinit( &hKeywordMfccHandle );
			AUD_ASSERT( error == AUD_ERROR_NONE );

			strncpy( (char*)keywordName, pKeywordEntry->name, 255 );
			ret = benchmark_newtemplate( hBenchmark, keywordName );
			AUD_ASSERT( ret == 0 );

			keywordID++;

			keywordBufLen  = 0;
			free( pKeywordBuf );
			pKeywordBuf    = NULL;

			AUDLOG( "keyword training finish...\n" );

			// recognize
			pEntry = NULL;
			pDir   = NULL;
			pDir   = openDir( (const char*)inputPath );
			
#if PERF_PROFILE
			AUD_Tick t;
#endif
			while ( ( pEntry = scanDir( pDir ) ) )
			{
				AUD_Int8s   inputStream[256] = { 0, };
				void        *hMfccHandle     = NULL;
				AUD_Summary fileSummary;

				snprintf( (char*)inputStream, 256, "%s/%s", (const char*)inputPath, pEntry->name );

				ret = parseWavFromFile( inputStream, &fileSummary );
				if ( ret < 0 )
				{
					continue;
				}

				AUD_ASSERT( fileSummary.channelNum == CHANNEL_NUM && fileSummary.bytesPerSample == BYTES_PER_SAMPLE && fileSummary.sampleRate == SAMPLE_RATE );
				AUDLOG( "input stream: %s\n", inputStream );

				bufLen = fileSummary.dataChunkBytes;
				pBuf   = (AUD_Int16s*)calloc( bufLen + 100, 1 );
				AUD_ASSERT( pBuf );

#if PERF_PROFILE
				t = -time_gettick();
#endif
				sampleNum = readWavFromFile( inputStream, pBuf, bufLen );
				AUD_ASSERT( sampleNum > 0 );
#if PERF_PROFILE
				t += time_gettick();
				AUDLOG( "PERF: read wav files takes %.2f ms\n", t / 1000. );
#endif

				// front end processing
				 // pre-emphasis
#if PERF_PROFILE
				t = -time_gettick();
#endif
				sig_preemphasis( pBuf, pBuf, sampleNum );
#if PERF_PROFILE
				t += time_gettick();
				AUDLOG( "PERF: pre-emphasis takes %.2f ms\n", t / 1000. );
#endif

				for ( j = 0; j * frameStride + FRAME_LEN <= sampleNum; j++ )
				{
					;
				}

#if PERF_PROFILE
				t = -time_gettick();
#endif
				AUD_Feature inFeature;
				inFeature.featureMatrix.rows     = j - MFCC_DELAY;
				inFeature.featureMatrix.cols     = MFCC_FEATDIM;
				inFeature.featureMatrix.dataType = AUD_DATATYPE_INT32S;
				ret = createMatrix( &(inFeature.featureMatrix) );
				AUD_ASSERT( ret == 0 );
#if PERF_PROFILE
				t += time_gettick();
				AUDLOG( "PERF: create feature matrix takes %.2f ms\n", t / 1000. );
#endif


#if PERF_PROFILE
				t = -time_gettick();
#endif
				inFeature.featureNorm.len      = j - MFCC_DELAY;
				inFeature.featureNorm.dataType = AUD_DATATYPE_INT64S;
				ret = createVector( &(inFeature.featureNorm) );
				AUD_ASSERT( ret == 0 );
#if PERF_PROFILE
				t += time_gettick();
				AUDLOG( "PERF: create feature norm takes %.2f ms\n", t / 1000. );
#endif


				// init mfcc handle
#if PERF_PROFILE
				t = -time_gettick();
#endif
				error = mfcc16s32s_init( &hMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, frameStride, SAMPLE_RATE, COMPRESS_TYPE );
				AUD_ASSERT( error == AUD_ERROR_NONE );
#if PERF_PROFILE
				t += time_gettick();
				AUDLOG( "PERF: mfcc init takes %.2f ms\n", t / 1000. );
#endif

#if PERF_PROFILE
				t = -time_gettick();
#endif
				error = mfcc16s32s_calc( hMfccHandle, pBuf, sampleNum, &inFeature );
				AUD_ASSERT( error == AUD_ERROR_NONE );
#if PERF_PROFILE
				t += time_gettick();
				AUDLOG( "PERF: mfcc calc takes %.2f ms\n", t / 1000. );
#endif

				// init dtw match
				dtwSession.dtwType        = WOV_DTW_TYPE;
				dtwSession.distType       = WOV_DISTANCE_METRIC;
				dtwSession.transitionType = WOV_DTWTRANSITION_STYLE;
				error = dtw_initsession( &dtwSession, &keywordFeature, inFeature.featureMatrix.rows );
				AUD_ASSERT( error == AUD_ERROR_NONE );

#if PERF_PROFILE
				t = -time_gettick();
#endif
				error = dtw_updatefrmcost( &dtwSession, &inFeature );
				AUD_ASSERT( error == AUD_ERROR_NONE );
#if PERF_PROFILE
				t += time_gettick();
				AUDLOG( "PERF: dtw update frame cost takes %.2f ms\n", t / 1000. );
#endif


#if PERF_PROFILE
				t = -time_gettick();
#endif
				error = dtw_match( &dtwSession, WOV_DTWSCORING_METHOD, &score, NULL );
				AUD_ASSERT( error == AUD_ERROR_NONE );
#if PERF_PROFILE
				t += time_gettick();
				AUDLOG( "PERF: dtw match takes %.2f ms\n", t / 1000. );
#endif

				error = dtw_deinitsession( &dtwSession );
				AUD_ASSERT( error == AUD_ERROR_NONE );

				// deinit mfcc handle
				error = mfcc16s32s_deinit( &hMfccHandle );
				AUD_ASSERT( error == AUD_ERROR_NONE );

				ret = destroyMatrix( &(inFeature.featureMatrix) );
				AUD_ASSERT( ret == 0 );

				ret = destroyVector( &(inFeature.featureNorm) );
				AUD_ASSERT( ret == 0 );

				isRecognized = AUD_FALSE;
				if ( score <= threshold )
				{
					isRecognized = AUD_TRUE;
				}

				AUDLOG( "score: %.2f\n", score );

				// insert log entry
				ret = benchmark_additem( hBenchmark, (AUD_Int8s*)pEntry->name, score, isRecognized );
				AUD_ASSERT( ret == 0 );

				bufLen  = 0;
				free( pBuf );
				pBuf = NULL;
			}
			closeDir( pDir );
			pDir = NULL;

			ret = benchmark_finalizetemplate( hBenchmark );
			AUD_ASSERT( ret == 0 );

			ret = destroyMatrix( &(keywordFeature.featureMatrix) );
			AUD_ASSERT( ret == 0 );

			ret = destroyVector( &(keywordFeature.featureNorm) );
			AUD_ASSERT( ret == 0 );
		}
		closeDir( pKeywordDir );
		pKeywordDir = NULL;

		ret = benchmark_finalizethreshold( hBenchmark );
		AUD_ASSERT( ret == 0 );
	}

	ret = benchmark_free( &hBenchmark );
	AUD_ASSERT( ret == 0 );

	AUDLOG( "DTW-MFCC Benchmark finished\n" );

	return 0;
}
예제 #7
0
AUD_Int32s wov_adapt_gmm_si()
{
	AUD_Error  error = AUD_ERROR_NONE;
	AUD_Int32s ret   = 0;
	AUD_Int8s  wavPath[256] = { 0, };
	AUD_Int32s data;

	setbuf( stdout, NULL );
	setbuf( stdin, NULL );
	AUDLOG( "pls give adapt wav stream's folder path:\n" );
	wavPath[0] = '\0';
	data = scanf( "%s", wavPath );
	AUDLOG( "adapt wav stream's folder path is: %s\n", wavPath );

	// step 1: read UBM model from file
	void *hUbm = NULL;
	FILE *fpUbm = fopen( WOV_UBM_GMMMODEL_FILE, "rb" );
	if ( fpUbm == NULL )
	{
		AUDLOG( "cannot open ubm model file: [%s]\n", WOV_UBM_GMMMODEL_FILE );
		return AUD_ERROR_IOFAILED;
	}
	error = gmm_import( &hUbm, fpUbm );
	AUD_ASSERT( error == AUD_ERROR_NONE );
	fclose( fpUbm );
	fpUbm = NULL;

	// AUDLOG( "ubm GMM as:\n" );
	// gmm_show( hUbm );

	AUD_Int32s i = 0, j = 0;
	entry      *pEntry = NULL;
	dir        *pDir   = NULL;

	AUD_Int32s totalWinNum = 0;
	pDir = openDir( (const char*)wavPath );
	if ( pDir == NULL )
	{
		AUDLOG( "cannot open folder: %s\n", wavPath );
		return -1;
	}

	while ( ( pEntry = scanDir( pDir ) ) )
	{
		AUD_Int8s   keywordFile[256] = { 0, };
		AUD_Summary fileSummary;
		AUD_Int32s  sampleNum = 0;

		snprintf( (char*)keywordFile, 256, "%s/%s", wavPath, pEntry->name );
		// AUDLOG( "%s\n", keywordFile );

		ret = parseWavFromFile( keywordFile, &fileSummary );
		if ( ret < 0 )
		{
			continue;
		}
		AUD_ASSERT( fileSummary.channelNum == CHANNEL_NUM && fileSummary.bytesPerSample == BYTES_PER_SAMPLE && fileSummary.sampleRate == SAMPLE_RATE );

		// request memeory for template
		sampleNum = fileSummary.dataChunkBytes / fileSummary.bytesPerSample;
		for ( j = 0; j * FRAME_STRIDE + FRAME_LEN <= sampleNum; j++ )
		{
			;
		}
		j = j - MFCC_DELAY;

		totalWinNum += j;
	}
	closeDir( pDir );
	pDir = NULL;

	AUD_Matrix featureMatrix;
	featureMatrix.rows     = totalWinNum;
	featureMatrix.cols     = MFCC_FEATDIM;
	featureMatrix.dataType = AUD_DATATYPE_INT32S;
	ret = createMatrix( &featureMatrix );
	AUD_ASSERT( ret == 0 );

	AUD_Int32s currentRow  = 0;
	pDir = openDir( (const char*)wavPath );
	while ( ( pEntry = scanDir( pDir ) ) )
	{
		AUD_Int8s   keywordFile[256] = { 0, };
		AUD_Summary fileSummary;
		AUD_Int32s  sampleNum        = 0;
		void        *hMfccHandle     = NULL;

		snprintf( (char*)keywordFile, 256, "%s/%s", wavPath, pEntry->name );
		// AUDLOG( "%s\n", keywordFile );

		ret = parseWavFromFile( keywordFile, &fileSummary );
		if ( ret < 0 )
		{
			continue;
		}
		AUD_ASSERT( fileSummary.channelNum == CHANNEL_NUM && fileSummary.bytesPerSample == BYTES_PER_SAMPLE && fileSummary.sampleRate == SAMPLE_RATE );

		AUD_Int32s bufLen = fileSummary.dataChunkBytes;
		AUD_Int16s *pBuf  = (AUD_Int16s*)calloc( bufLen, 1 );
		AUD_ASSERT( pBuf );

		sampleNum = readWavFromFile( (AUD_Int8s*)keywordFile, pBuf, bufLen );
		AUD_ASSERT( sampleNum > 0 );

		// pre-processing

		 // pre-emphasis
		sig_preemphasis( pBuf, pBuf, sampleNum );

		 // calc framing number
		for ( j = 0; j * FRAME_STRIDE + FRAME_LEN <= sampleNum; j++ )
		{
			;
		}

		 // XXX: select salient frames
		AUD_Feature feature;
		feature.featureMatrix.rows     = j - MFCC_DELAY;
		feature.featureMatrix.cols     = MFCC_FEATDIM;
		feature.featureMatrix.dataType = AUD_DATATYPE_INT32S;
		feature.featureMatrix.pInt32s  = featureMatrix.pInt32s + currentRow * feature.featureMatrix.cols;

		feature.featureNorm.len      = j - MFCC_DELAY;
		feature.featureNorm.dataType = AUD_DATATYPE_INT64S;
		ret = createVector( &(feature.featureNorm) );
		AUD_ASSERT( ret == 0 );

		error = mfcc16s32s_init( &hMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, FRAME_STRIDE, SAMPLE_RATE, COMPRESS_TYPE );
		AUD_ASSERT( error == AUD_ERROR_NONE );

		error = mfcc16s32s_calc( hMfccHandle, pBuf, sampleNum, &feature );
		AUD_ASSERT( error == AUD_ERROR_NONE );

		error = mfcc16s32s_deinit( &hMfccHandle );
		AUD_ASSERT( error == AUD_ERROR_NONE );

		free( pBuf );
		pBuf   = NULL;
		bufLen = 0;

		ret = destroyVector( &(feature.featureNorm) );
		AUD_ASSERT( ret == 0 );

		currentRow += feature.featureMatrix.rows;
	}
	closeDir( pDir );
	pDir = NULL;

	AUD_Matrix llrMatrix;
	llrMatrix.rows     = totalWinNum;
	llrMatrix.cols     = gmm_getmixnum( hUbm );
	llrMatrix.dataType = AUD_DATATYPE_DOUBLE;
	ret = createMatrix( &llrMatrix );
	AUD_ASSERT( ret == 0 );

	AUD_Double llr = 0.;
	for ( j = 0; j < featureMatrix.rows; j++ )
	{
		AUD_Vector componentLLR;
		componentLLR.len      = llrMatrix.cols;
		componentLLR.dataType = AUD_DATATYPE_DOUBLE;
		componentLLR.pDouble  = llrMatrix.pDouble + j * llrMatrix.cols;

		llr = gmm_llr( hUbm, &(featureMatrix), j, &componentLLR );
	}

	AUD_Vector sumLlr;
	sumLlr.len      = llrMatrix.cols;
	sumLlr.dataType = AUD_DATATYPE_DOUBLE;
	ret = createVector( &sumLlr );
	AUD_ASSERT( ret == 0 );

	AUD_Double *pSumLlr = sumLlr.pDouble;
	for ( j = 0; j < llrMatrix.cols; j++ )
	{
		pSumLlr[j] = llrMatrix.pDouble[j];
	}
	for ( i = 1; i < llrMatrix.rows; i++ )
	{
		for ( j = 0; j < llrMatrix.cols; j++ )
		{
			pSumLlr[j] = logadd( pSumLlr[j],  *(llrMatrix.pDouble + i * llrMatrix.cols + j) );
		}
	}

#if 0
	AUD_Vector bestIndex;
	bestIndex.len      = TOP_N;
	bestIndex.dataType = AUD_DATATYPE_INT32S;
	ret = createVector( &bestIndex );
	AUD_ASSERT( ret == 0 );

	// get top TOP_N component
	ret = sortVector( &sumLlr, &bestIndex );
	AUD_ASSERT( ret == 0 );
#else
	llr = pSumLlr[0];
	for ( j = 1; j < sumLlr.len; j++ )
	{
		llr = logadd( llr, pSumLlr[j] );
	}
	// AUDLOG( "llr: %.f\n", llr );

	AUD_Vector sortIndex;
	sortIndex.len      = sumLlr.len;
	sortIndex.dataType = AUD_DATATYPE_INT32S;
	ret = createVector( &sortIndex );
	AUD_ASSERT( ret == 0 );

	ret = sortVector( &sumLlr, &sortIndex );
	AUD_ASSERT( ret == 0 );

	int    num = 0;
	double val = 0.;
	for ( i = 0; i < sortIndex.len; i++ )
	{
		// ln( 0.001 ) ~= -7.
		val =  pSumLlr[sortIndex.pInt32s[i]] - llr + 7.;
		// AUDLOG( "%f, \n", val );
		if ( val < 0 )
		{
			break;
		}
		num++;
	}
	// AUDLOG( "\n" );
	AUD_ASSERT( num > 0 );

	AUDLOG( "computed component num: %d\n", num );

	num = AUD_MAX( num, TOP_N );
	AUDLOG( "normalized component num: %d\n", num );

	AUD_Vector bestIndex;
	bestIndex.len      = num;
	bestIndex.dataType = AUD_DATATYPE_INT32S;
	bestIndex.pInt32s  = sortIndex.pInt32s;
#endif

	int slash = '/';
	char *ptr = strrchr( (char*)wavPath, slash );
	ptr++;

	// select imposter GMM
	void      *hImposterGmm        = NULL;
	AUD_Int8s imposterGmmName[256] = { 0, };
	snprintf( (char*)imposterGmmName, 256, "%s-imposter", ptr );
	error = gmm_select( &hImposterGmm, hUbm, &bestIndex, 0 | GMM_INVERTSELECT_MASK, imposterGmmName );
	AUD_ASSERT( error == AUD_ERROR_NONE );

	gmm_show( hImposterGmm );

	// export gmm
	char imposterFile[256] = { 0 };
	snprintf( imposterFile, 256, "%s/%s-imposter.gmm", WOV_IMPOSTER_GMMMODEL_DIR, ptr );
	AUDLOG( "Export imposter GMM Model to: %s\n", imposterFile );
	FILE *fpImposterGmm = fopen( imposterFile, "wb" );
	AUD_ASSERT( fpImposterGmm );

	error = gmm_export( hImposterGmm, fpImposterGmm );
	AUD_ASSERT( error == AUD_ERROR_NONE );
	AUDLOG( "Export imposter GMM Model File Done\n" );

	fclose( fpImposterGmm );
	fpImposterGmm = NULL;

	error = gmm_free( &hImposterGmm );
	AUD_ASSERT( error == AUD_ERROR_NONE );

	// select keyword GMM
	void      *hAdaptedGmm        = NULL;
	AUD_Int8s adaptedGmmName[256] = { 0, };
	snprintf( (char*)adaptedGmmName, 256, "%s", ptr );
	// AUDLOG( "%s\n", adaptedGmmName );
	error = gmm_select( &hAdaptedGmm, hUbm, &bestIndex, 0, adaptedGmmName );
	AUD_ASSERT( error == AUD_ERROR_NONE );

	ret = destroyVector( &sumLlr );
	AUD_ASSERT( ret == 0 );

	ret = destroyMatrix( &llrMatrix );
	AUD_ASSERT( ret == 0 );

#if 0
	ret = destroyVector( &bestIndex );
	AUD_ASSERT( ret == 0 );
#else
	ret = destroyVector( &sortIndex );
	AUD_ASSERT( ret == 0 );
#endif

#if 1
	// adapt GMM
	error = gmm_adapt( hAdaptedGmm, &featureMatrix );
	AUD_ASSERT( error == AUD_ERROR_NONE );
#endif

	gmm_show( hAdaptedGmm );

	// export gmm
	char modelFile[256] = { 0 };
	snprintf( modelFile, 256, "%s/%s.gmm", WOV_KEYWORD_GMMMODEL_DIR, ptr );
	AUDLOG( "Export GMM Model to: %s\n", modelFile );
	FILE *fpGmm = fopen( modelFile, "wb" );
	AUD_ASSERT( fpGmm );

	error = gmm_export( hAdaptedGmm, fpGmm );
	AUD_ASSERT( error == AUD_ERROR_NONE );
	AUDLOG( "Export GMM Model File Done\n" );

	fclose( fpGmm );
	fpGmm = NULL;

	ret = destroyMatrix( &featureMatrix );
	AUD_ASSERT( ret == 0 );

	error = gmm_free( &hAdaptedGmm );
	AUD_ASSERT( error == AUD_ERROR_NONE );

	error = gmm_free( &hUbm );
	AUD_ASSERT( error == AUD_ERROR_NONE );

	AUDLOG( "keyword model adapt2 done\n" );

	return 0;
}