如何在vc 6 0/2003/2005/2008中调用ATK

本文详细介绍了一个基于HTK的实时API应用实例,通过列举代码片段展示了如何处理和识别音视频文件,包括列表化.wav和.vec文件,加载样本集,以及进行信号源向量的匹配和比较。文章还涉及了音量标志的设置及其对识别过程的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

Application Example using the ATK Real-Time API  

 

下面是一个关于HTK的例子

 

 

  1. #include "stdafx.h"    
  2.    
  3.    
  4. #include "dllSudx.h"    
  5. typedef vector<STRING>  TStringArray;   
  6.    
  7. struct ASampleSet   
  8. {   
  9.     char name[256];   
  10.     TSoundTag tags[301];   
  11. };   
  12.    
  13. typedef vector<ASAMPLESET>  TSampleSetList;   
  14.    
  15. TStringArray sampleList;   
  16. TSampleSetList sampleSetList;   
  17. int vector_count = 0;   
  18.    
  19. int ListWavFiles(char* dir, TStringArray& fileList)   
  20. {   
  21.     struct _finddata_t c_file;   
  22.     long hFile;   
  23.     char tmp[1024];   
  24.     int retVal = 0;   
  25.    
  26.     sprintf(tmp, "%s//%s", dir, "*.wav");   
  27.        
  28.     /* Find first .c file in current directory */   
  29.     if( (hFile = _findfirst( tmp, &c_file )) == -1L )   
  30.         return retVal;   
  31.     else   
  32.     {   
  33.         sprintf(tmp, "%s//%s", dir, c_file.name);   
  34.         fileList.push_back(tmp);   
  35.         retVal++;   
  36.         while( _findnext( hFile, &c_file ) == 0 )   
  37.         {   
  38.             sprintf(tmp, "%s//%s", dir, c_file.name);   
  39.             fileList.push_back(tmp);   
  40.             retVal++;   
  41.         }   
  42.         _findclose( hFile );   
  43.     }   
  44.    
  45.     return retVal;   
  46. }   
  47.    
  48.    
  49. int ListVecFiles(char* dir, TStringArray& fileList)   
  50. {   
  51.     struct _finddata_t c_file;   
  52.     long hFile;   
  53.     char tmp[1024];   
  54.     int retVal = 0;   
  55.    
  56.     sprintf(tmp, "%s//%s", dir, "*.vec");   
  57.        
  58.     /* Find first .c file in current directory */   
  59.     if( (hFile = _findfirst( tmp, &c_file )) == -1L )   
  60.         return retVal;   
  61.     else   
  62.     {   
  63.         sprintf(tmp, "%s//%s", dir, c_file.name);   
  64.         fileList.push_back(tmp);   
  65.         retVal++;   
  66.         while( _findnext( hFile, &c_file ) == 0 )   
  67.         {   
  68.             sprintf(tmp, "%s//%s", dir, c_file.name);   
  69.             fileList.push_back(tmp);   
  70.             retVal++;   
  71.         }   
  72.         _findclose( hFile );   
  73.     }   
  74.    
  75.     return retVal;   
  76. }   
  77.    
  78.    
  79. int LoadVecFiles(TStringArray& fileList, TSampleSetList& sampleSetList)   
  80. {   
  81.     int retVal = 0;   
  82.    
  83.     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");   
  84.         printf("/t声音文件名是要被识别的声音文件,样本目录中包含已经预先处理好的样本/n"   
  85.                         "/t 音量标志: 表示识别时是否采用音量相关的方式,为0表示识别时不管音量大小,为1表示识别时需要考虑音量大小/n"   
  86.                         "Press any key continue...");   
  87.         getch();   
  88.         return -1;   
  89.     }*/   
  90.    
  91.     //int volFlag = atol(argv[3]);    
  92.     if(bVolumeFlag)   
  93.     {   
  94.         sudxSetCompVolumeFlag(TRUE);   
  95.         printf("采用与音量相关的方式比较/n");   
  96.     }   
  97.     else   
  98.     {   
  99.         sudxSetCompVolumeFlag(FALSE);   
  100.         printf("采用与音量无关的方式比较/n");   
  101.     }   
  102.    
  103.     TStringArray sampleList;   
  104.     int i = ListVecFiles(vecDirectory, sampleList);   
  105.     if(i<=0)   
  106.     {   
  107.         printf("No sample files in dir [%s]/n", vecDirectory);   
  108.         return -2;   
  109.     }   
  110.        
  111.     TSampleSetList sampleSetList;   
  112.     int vector_count = LoadVecFiles(sampleList, sampleSetList);   
  113.     if(vector_count<=0)   
  114.     {   
  115.         printf("没有找到样本集合文件/n");   
  116.         return -3;   
  117.     }   
  118.    
  119.    
  120.     printf("Now start.../n");   
  121.     const int bufSampleSize = 49306; ////充值成功,你的账户余额为的 8k采样,8位长度编码的语音文件大小    
  122.     unsigned char * p8bitBuf = new unsigned   char[ bufSampleSize];   
  123.     short *pFileBuf = new short[bufSampleSize*2];   //每次处理2M个16bit pcm样本,也就是4M byte数据。    
  124.    
  125.    
  126. //  char outFile[1000];    
  127. //  sprintf(outFile, "%s//%s.vec", "c:", VoiceName);    
  128. //  FILE* fout = fopen(outFile, "w+b");     //打开或创建向量文件    
  129.    
  130.     FILE *fp = fopen(VoiceName, "rb");   
  131.     fseek(fp, 0L, SEEK_END);   
  132.     long size = ftell(fp);   
  133.     fseek(fp, 44L, SEEK_SET);   //跳过wav头     
  134.    
  135.     int old_clock = clock();   
  136. //  long segSize = fread(p8bitBuf, sizeof(char), bufSampleSize, fp);    
  137.     //alaw2linear( p8bitBuf , bufSampleSize , pFileBuf );    
  138.    
  139.     long segSize = fread(pFileBuf, sizeof(short), bufSampleSize, fp);   
  140.     TSoundTag* pOut;   
  141.     int outSize, tailSize;   
  142.     short* pTail=NULL;   
  143.    
  144.     int last_result[17];   
  145.      int iMax = sudxGetMaxInputSampleSize();   
  146.    
  147.     int error = sudxCalcBuffer( pFileBuf, segSize, &pOut, &outSize, &pTail, &tailSize, last_result);   
  148.     if ( error > 0 )   
  149.     {   
  150.         if ( outSize > (64 + 1 ))   
  151.         {   
  152.             old_clock = clock();   
  153.    
  154.             int compare_turn = outSize - 64 - 1;   
  155.             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)   
  156.                         {   
  157.                             if(sudxCompare(pOut+1+i, sampleSetList[j].tags+1, TRUE))   
  158.                                 printf("精确匹配位置 %d, file[%s]/n", (i+1)*128, sampleSetList[j].name);   
  159.                             else   
  160.                                 printf("!!Slow Not Match at sample pos %d, file[%s]/n", (i+1)*128, sampleSetList[j].name);   
  161.                         }   
  162.                     }   
  163.                     else   
  164.                     {   
  165.                         printf("Not Match at sample pos %d, file [%s]/n", (i+1)*128, sampleSetList[j].name);   
  166.                     }   
  167.    
  168.                 }   
  169.             }   
  170.             int time_passed = clock()-old_clock;   
  171.             printf("计算结束, 耗时%d ms/n", time_passed);   
  172.             printf("比较速度为每秒钟可查找%f秒音乐/n"double(compare_turn)*128.0/8000.0/(double(time_passed)/1000.0));   
  173.    
  174.         }   
  175.         else   
  176.         {   
  177.             printf("输入的声音过短,不能比较/n");   
  178.             return -2;   
  179.         }   
  180.    
  181.     }   
  182.     else    
  183.     {   
  184.         return -1;   
  185.     }   
  186.     return 0;   
  187. }   
  188.    
  189.    
  190. int VRecoInit(char * vecDirectory )   
  191. {   
  192.    
  193.        
  194.     int i = ListVecFiles(vecDirectory, sampleList);   
  195.     if(i<=0)   
  196.     {   
  197.         printf("No sample files in dir [%s]/n", vecDirectory);   
  198.         return -2;   
  199.     }   
  200.        
  201.        
  202.      vector_count = LoadVecFiles(sampleList, sampleSetList);   
  203.     if(vector_count<=0)   
  204.     {   
  205.         printf("没有找到样本集合文件/n");   
  206.         return -3;   
  207.     }   
  208.    
  209.     return 0;   
  210.    
  211.    
  212.    
  213.    
  214. }   
  215. ///    
  216. int VRecognize(char *VoiceName, char* vecDirectory , bool bVolumeFlag,  int * iMatchCount)   
  217. {   
  218.     /*if(argc!=4)  
  219.     {  
  220.         printf("参数错误/n");  
  221.         printf("使用方法:test <声音文件名> <样本目录> <音量标志>/n");  
  222.         printf("/t声音文件名是要被识别的声音文件,样本目录中包含已经预先处理好的样本/n"  
  223.                         "/t 音量标志: 表示识别时是否采用音量相关的方式,为0表示识别时不管音量大小,为1表示识别时需要考虑音量大小/n"  
  224.                         "Press any key continue...");  
  225.         getch();  
  226.         return -1;  
  227.     }*/   
  228.    
  229.     //int volFlag = atol(argv[3]);    
  230.     if(bVolumeFlag)   
  231.     {   
  232.         sudxSetCompVolumeFlag(TRUE);   
  233.         printf("采用与音量相关的方式比较/n");   
  234.     }   
  235.     else   
  236.     {   
  237.         sudxSetCompVolumeFlag(FALSE);   
  238.         printf("采用与音量无关的方式比较/n");   
  239.     }   
  240.    
  241.     char gcaMsg[1000];   
  242.     sprintf(gcaMsg , "Now start...%s/n", VoiceName);   
  243.     printf( gcaMsg );   
  244.     fflush(stdout);   
  245.     const int bufSampleSize = 2048*1024; ////充值成功,你的账户余额为的 8k采样,8位长度编码的语音文件大小    
  246.     unsigned char * p8bitBuf = new unsigned   char[ bufSampleSize];   
  247.     short *pFileBuf = new short[bufSampleSize]; //每次处理2M个16bit pcm样本,也就是4M byte数据。    
  248.    
  249.    
  250.    
  251. //  char outFile[1000];    
  252. //  sprintf(outFile, "%s//%s.vec", "c:", VoiceName);    
  253. //  FILE* fout = fopen(outFile, "w+b");     //打开或创建向量文件    
  254.    
  255.     try   
  256.     {   
  257.     
  258.        sprintf(gcaMsg , "bf fopen...%s/n", VoiceName);   
  259.        printf( gcaMsg );   
  260.        fflush(stdout);   
  261.            
  262.         FILE *fp = fopen(VoiceName, "rb");   
  263.         fseek(fp, 0L, SEEK_END);   
  264.         long size = ftell(fp);   
  265.         fseek(fp, 44L, SEEK_SET);   //跳过wav头     
  266.    
  267.    
  268.         sprintf(gcaMsg , "af fopen...%s/n", VoiceName);   
  269.         printf( gcaMsg );   
  270.         fflush(stdout);   
  271.        
  272.    
  273.         int old_clock = clock();   
  274.         long segSize = fread(p8bitBuf, sizeof(char), bufSampleSize, fp);   
  275.         alaw2linear( p8bitBuf , bufSampleSize , pFileBuf );   
  276.    
  277.         sprintf(gcaMsg , "Af alaw2linear...%s/n", VoiceName);   
  278.         printf( gcaMsg );   
  279.         fflush(stdout);   
  280.        
  281.         //  long segSize = fread(pFileBuf, sizeof(short), bufSampleSize, fp);    
  282.         TSoundTag* pOut;   
  283.         int outSize, tailSize;   
  284.         short* pTail=NULL;   
  285.    
  286.         int last_result[17];   
  287.         //int iMax = sudxGetMaxInputSampleSize();    
  288.    
  289.            
  290.         int error = sudxCalcBuffer( pFileBuf, segSize, &pOut, &outSize, &pTail, &tailSize, last_result);   
  291.    
  292.         if ( error > 0 )   
  293.         {   
  294.         sprintf(gcaMsg , "Af sudxCalcBuffer...%s/n", VoiceName);   
  295.         printf( gcaMsg );   
  296.         fflush(stdout);   
  297.    
  298.             if ( outSize > (64 + 1 ))   
  299.             {   
  300.                 old_clock = clock();   
  301.    
  302.                 int compare_turn = outSize - 64 - 1;   
  303.                 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)   
  304.                             {   
  305.                                 if(sudxCompare(pOut+1+i, sampleSetList[j].tags+1, TRUE))   
  306.                                 {   
  307.    
  308.                                     (* iMatchCount) ++;   
  309.                                     printf("精确匹配位置 %d, file[%s]/n", (i+1)*128, sampleSetList[j].name);   
  310.                                 }   
  311.                                 else   
  312.                                 {   
  313.                                 //  printf("!!Slow Not Match at sample pos %d, file[%s]/n", (i+1)*128, sampleSetList[j].name);    
  314.                                 }   
  315.                       &nb

    给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
    这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值