在vc++6.0/2003/2005/2008中调用HTK
2009-04-27 20:06:39 来源:WEB开发网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];
更多精彩
赞助商链接