分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
Application Example using the ATK Real-Time API
下面是一个关于HTK的例子
- #include "stdafx.h"
- #include "dllSudx.h"
- typedef vector<STRING> TStringArray;
- struct ASampleSet
- {
- char name[256];
- TSoundTag tags[301];
- };
- typedef vector<ASAMPLESET> TSampleSetList;
- TStringArray sampleList;
- TSampleSetList sampleSetList;
- int vector_count = 0;
- int ListWavFiles(char* dir, TStringArray& fileList)
- {
- struct _finddata_t c_file;
- long hFile;
- char tmp[1024];
- int retVal = 0;
- sprintf(tmp, "%s//%s", dir, "*.wav");
- /* Find first .c file in current directory */
- if( (hFile = _findfirst( tmp, &c_file )) == -1L )
- return retVal;
- else
- {
- sprintf(tmp, "%s//%s", dir, c_file.name);
- fileList.push_back(tmp);
- retVal++;
- while( _findnext( hFile, &c_file ) == 0 )
- {
- sprintf(tmp, "%s//%s", dir, c_file.name);
- fileList.push_back(tmp);
- retVal++;
- }
- _findclose( hFile );
- }
- return retVal;
- }
- int ListVecFiles(char* dir, TStringArray& fileList)
- {
- struct _finddata_t c_file;
- long hFile;
- char tmp[1024];
- int retVal = 0;
- sprintf(tmp, "%s//%s", dir, "*.vec");
- /* Find first .c file in current directory */
- if( (hFile = _findfirst( tmp, &c_file )) == -1L )
- return retVal;
- else
- {
- sprintf(tmp, "%s//%s", dir, c_file.name);
- fileList.push_back(tmp);
- retVal++;
- while( _findnext( hFile, &c_file ) == 0 )
- {
- sprintf(tmp, "%s//%s", dir, c_file.name);
- fileList.push_back(tmp);
- retVal++;
- }
- _findclose( hFile );
- }
- return retVal;
- }
- int LoadVecFiles(TStringArray& fileList, TSampleSetList& sampleSetList)
- {
- int retVal = 0;
- for(int i=0; i<FILELIST.SIZE(); <声音文件名 printf(?使用方法:test printf(?参数错误/n?); { *if(argc!="4)" bVolumeFlag) bool , vecDirectory char* *VoiceName, VRecognize16BitWav(char int ; g_TestVectorFileName="test.vec" string } FALSE; : TRUE ? matchCount="0;" return matchCount++; i+="3;" pMatchResult[matchCount].dwMatchOffset="i;" pMatchResult[matchCount].ucScore="score;" &score)) bMode, pSampleVec, if(sudxCompareEx(pSrcVec+i, score; i++) matchCount<100; && i<nSrcLen-tailLen i="0;" for(int tailLen="64;" else 0; if(nSampleLen<300) if(nSampleLen<64) if(bMode="=TRUE)" tailLen; 输出匹配结果(得分、偏移量) pMatchResult) MATCHRESULT* 输出匹配结果个数 pnMaxMatch, int* 模式。="TRUE:快速模式;=FALSE:准确模式" BOOL 样本向量的个数 nSampleLen, 样本向量的首地址指针 TSoundTag* 信号源向量的长度(以TSoundTag为计量单位) nSrcLen, 待识别信号源向量的首地址指针 pSrcVec, sudxMatch(TSoundTag* 注意事项:pnMaxMatch的存储空间由应用程序分配,最大为100条记录。 函数返回:1:调用成功。0:调用失败 MATCHRESULT; dwMatchOffset; DWORD ucScore; char unsigned tagMATCHRESULT struct typedef retVal; ); fflush(stdout fileList[i].c_str()); [%s]/n?, file open not printf(?can fclose(fp); !/n?, OK set[%s] printf(?sample retVal++; sampleSetList.push_back(aset); strcpy(aset.name, short!/n?, too if(size!="80)" fp); 80, sizeof(TSoundTag), size="fread(aset.tags," aset; ASampleSet if(fp) ?rb?); fp="fopen(fileList[i].c_str()," FILE*> <样本目录> <音量标志>/n");
- printf("/t声音文件名是要被识别的声音文件,样本目录中包含已经预先处理好的样本/n"
- "/t 音量标志: 表示识别时是否采用音量相关的方式,为0表示识别时不管音量大小,为1表示识别时需要考虑音量大小/n"
- "Press any key continue...");
- getch();
- return -1;
- }*/
- //int volFlag = atol(argv[3]);
- if(bVolumeFlag)
- {
- sudxSetCompVolumeFlag(TRUE);
- printf("采用与音量相关的方式比较/n");
- }
- else
- {
- sudxSetCompVolumeFlag(FALSE);
- printf("采用与音量无关的方式比较/n");
- }
- TStringArray sampleList;
- int i = ListVecFiles(vecDirectory, sampleList);
- if(i<=0)
- {
- printf("No sample files in dir [%s]/n", vecDirectory);
- return -2;
- }
- TSampleSetList sampleSetList;
- int vector_count = LoadVecFiles(sampleList, sampleSetList);
- if(vector_count<=0)
- {
- printf("没有找到样本集合文件/n");
- return -3;
- }
- printf("Now start.../n");
- const int bufSampleSize = 49306; ////充值成功,你的账户余额为的 8k采样,8位长度编码的语音文件大小
- unsigned char * p8bitBuf = new unsigned char[ bufSampleSize];
- short *pFileBuf = new short[bufSampleSize*2]; //每次处理2M个16bit pcm样本,也就是4M byte数据。
- // char outFile[1000];
- // sprintf(outFile, "%s//%s.vec", "c:", VoiceName);
- // FILE* fout = fopen(outFile, "w+b"); //打开或创建向量文件
- FILE *fp = fopen(VoiceName, "rb");
- fseek(fp, 0L, SEEK_END);
- long size = ftell(fp);
- fseek(fp, 44L, SEEK_SET); //跳过wav头
- int old_clock = clock();
- // long segSize = fread(p8bitBuf, sizeof(char), bufSampleSize, fp);
- //alaw2linear( p8bitBuf , bufSampleSize , pFileBuf );
- long segSize = fread(pFileBuf, sizeof(short), bufSampleSize, fp);
- TSoundTag* pOut;
- int outSize, tailSize;
- short* pTail=NULL;
- int last_result[17];
- int iMax = sudxGetMaxInputSampleSize();
- int error = sudxCalcBuffer( pFileBuf, segSize, &pOut, &outSize, &pTail, &tailSize, last_result);
- if ( error > 0 )
- {
- if ( outSize > (64 + 1 ))
- {
- old_clock = clock();
- int compare_turn = outSize - 64 - 1;
- for(i=0; i<COMPARE_TURN; { int i++) for(int [%s]/n?, file if(tailSize tailSize="compare_turn+64-300-i;" sampleSetList[j].name); (i+1)*128, %d, pos sample at Match printf(?Quick ) FALSE) &(sampleSetList[j].tags[1]), sudxCompare(pOut+1+i, if( j++) j<vector_count; j="0;">300)
- {
- if(sudxCompare(pOut+1+i, sampleSetList[j].tags+1, TRUE))
- printf("精确匹配位置 %d, file[%s]/n", (i+1)*128, sampleSetList[j].name);
- else
- printf("!!Slow Not Match at sample pos %d, file[%s]/n", (i+1)*128, sampleSetList[j].name);
- }
- }
- else
- {
- printf("Not Match at sample pos %d, file [%s]/n", (i+1)*128, sampleSetList[j].name);
- }
- }
- }
- int time_passed = clock()-old_clock;
- printf("计算结束, 耗时%d ms/n", time_passed);
- printf("比较速度为每秒钟可查找%f秒音乐/n", double(compare_turn)*128.0/8000.0/(double(time_passed)/1000.0));
- }
- else
- {
- printf("输入的声音过短,不能比较/n");
- return -2;
- }
- }
- else
- {
- return -1;
- }
- return 0;
- }
- int VRecoInit(char * vecDirectory )
- {
- int i = ListVecFiles(vecDirectory, sampleList);
- if(i<=0)
- {
- printf("No sample files in dir [%s]/n", vecDirectory);
- return -2;
- }
- vector_count = LoadVecFiles(sampleList, sampleSetList);
- if(vector_count<=0)
- {
- printf("没有找到样本集合文件/n");
- return -3;
- }
- return 0;
- }
- ///
- int VRecognize(char *VoiceName, char* vecDirectory , bool bVolumeFlag, int * iMatchCount)
- {
- /*if(argc!=4)
- {
- printf("参数错误/n");
- printf("使用方法:test <声音文件名> <样本目录> <音量标志>/n");
- printf("/t声音文件名是要被识别的声音文件,样本目录中包含已经预先处理好的样本/n"
- "/t 音量标志: 表示识别时是否采用音量相关的方式,为0表示识别时不管音量大小,为1表示识别时需要考虑音量大小/n"
- "Press any key continue...");
- getch();
- return -1;
- }*/
- //int volFlag = atol(argv[3]);
- if(bVolumeFlag)
- {
- sudxSetCompVolumeFlag(TRUE);
- printf("采用与音量相关的方式比较/n");
- }
- else
- {
- sudxSetCompVolumeFlag(FALSE);
- printf("采用与音量无关的方式比较/n");
- }
- char gcaMsg[1000];
- sprintf(gcaMsg , "Now start...%s/n", VoiceName);
- printf( gcaMsg );
- fflush(stdout);
- const int bufSampleSize = 2048*1024; ////充值成功,你的账户余额为的 8k采样,8位长度编码的语音文件大小
- unsigned char * p8bitBuf = new unsigned char[ bufSampleSize];
- short *pFileBuf = new short[bufSampleSize]; //每次处理2M个16bit pcm样本,也就是4M byte数据。
- // char outFile[1000];
- // sprintf(outFile, "%s//%s.vec", "c:", VoiceName);
- // FILE* fout = fopen(outFile, "w+b"); //打开或创建向量文件
- try
- {
- sprintf(gcaMsg , "bf fopen...%s/n", VoiceName);
- printf( gcaMsg );
- fflush(stdout);
- FILE *fp = fopen(VoiceName, "rb");
- fseek(fp, 0L, SEEK_END);
- long size = ftell(fp);
- fseek(fp, 44L, SEEK_SET); //跳过wav头
- sprintf(gcaMsg , "af fopen...%s/n", VoiceName);
- printf( gcaMsg );
- fflush(stdout);
- int old_clock = clock();
- long segSize = fread(p8bitBuf, sizeof(char), bufSampleSize, fp);
- alaw2linear( p8bitBuf , bufSampleSize , pFileBuf );
- sprintf(gcaMsg , "Af alaw2linear...%s/n", VoiceName);
- printf( gcaMsg );
- fflush(stdout);
- // long segSize = fread(pFileBuf, sizeof(short), bufSampleSize, fp);
- TSoundTag* pOut;
- int outSize, tailSize;
- short* pTail=NULL;
- int last_result[17];
- //int iMax = sudxGetMaxInputSampleSize();
- int error = sudxCalcBuffer( pFileBuf, segSize, &pOut, &outSize, &pTail, &tailSize, last_result);
- if ( error > 0 )
- {
- sprintf(gcaMsg , "Af sudxCalcBuffer...%s/n", VoiceName);
- printf( gcaMsg );
- fflush(stdout);
- if ( outSize > (64 + 1 ))
- {
- old_clock = clock();
- int compare_turn = outSize - 64 - 1;
- for(int i=0; i<COMPARE_TURN; { int i++) for(int [%s]/n?, file if(tailSize tailSize="compare_turn+64-300-i;" sampleSetList[j].name); (i+1)*128, %d, pos sample at Match printf(?Quick ) FALSE) &(sampleSetList[j].tags[1]), sudxCompare(pOut+1+i, if( j++) j<vector_count; j="0;" iMatchCount)++; (*>300)
- {
- if(sudxCompare(pOut+1+i, sampleSetList[j].tags+1, TRUE))
- {
- (* iMatchCount) ++;
- printf("精确匹配位置 %d, file[%s]/n", (i+1)*128, sampleSetList[j].name);
- }
- else
- {
- // printf("!!Slow Not Match at sample pos %d, file[%s]/n", (i+1)*128, sampleSetList[j].name);
- }
- &nb
给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow