DAT_File_Access.cpp
资源名称:VQ-DHMM.rar [点击查看]
上传用户:avbj512
上传日期:2013-09-18
资源大小:6239k
文件大小:10k
源码类别:
DSP编程
开发平台:
Visual C++
- // DAT_File_Access.cpp:
- // Implementation of the DAT_File_Access Module.
- // That is Data Structure & Access Function of .dat File format.
- //
- // Created 2001/08, By DongMing, MDSR.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "DAT_File_Access.h"
- #include "kwspot.h"
- extern PRO_CONFIG u_Pro_Config;
- //////////////////////////////////////////////////////////////////////
- // API functions
- //////////////////////////////////////////////////////////////////////
- // 函数名称:dfa_Feature_Get_File_Info
- // 函数功能:从.DAT格式的Feature文件读入文件头信息
- // 函数性质:API
- // 输入参数:
- // sz_Feature_File_Name,Feature文件的文件名
- // 输出参数:
- // pu_Feature_File_Head,输出的文件头信息的存放缓冲区
- // 返回值:
- // 0 表示成功;
- // 备注:关于FEATURE_FILE_HEAD的定义,参见本模块.H文件
- int dfa_Feature_Get_File_Info(char * sz_Feature_File_Name, FEATURE_FILE_HEAD * pu_Feature_File_Head)
- {
- FILE * fp_Feature_File;
- fp_Feature_File = fopen(sz_Feature_File_Name, "rb");
- ASSERT(fp_Feature_File != NULL);
- ASSERT(sizeof(FEATURE_FILE_HEAD) == 200);
- fread(pu_Feature_File_Head, sizeof(FEATURE_FILE_HEAD), 1, fp_Feature_File);
- fclose(fp_Feature_File);
- return 0;
- }
- //////////////////////////////////////////////////////////////////////
- // 函数名称:dfa_Feature_Get_File_Total_Frame_Num
- // 函数功能:读出.DAT格式的Feature文件中的前若干句子的总帧数
- // 函数性质:API
- // 输入参数:
- // sz_Feature_File_Name,Feature文件的文件名
- // n_Sentence_Num,要统计的句子数
- // 输出参数:
- // 无
- // 返回值:
- // 统计出的总帧数
- // 备注:在类如VQ训练准备数据的场合,应该使用这个API,效率会比较高
- int dfa_Feature_Get_File_Total_Frame_Num(char * sz_Feature_File_Name, int n_Sentence_Num)
- {
- int nResult;
- FILE * fp_Feature_File;
- long l_Frame_Num;
- int n_Sentence_Index;
- fp_Feature_File = fopen(sz_Feature_File_Name, "rb");
- ASSERT(fp_Feature_File != NULL);
- fseek(fp_Feature_File, sizeof(FEATURE_FILE_HEAD), SEEK_SET);
- nResult = 0;
- for (n_Sentence_Index = 0; n_Sentence_Index < n_Sentence_Num; n_Sentence_Index++)
- {
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- nResult += l_Frame_Num;
- }
- fclose(fp_Feature_File);
- return(nResult);
- }
- //////////////////////////////////////////////////////////////////////
- // 函数名称:dfa_Feature_Get_File_Total_Frame_Num
- // 函数功能:读出.DAT格式的Feature文件中的待训练句子的总帧数
- // 函数性质:API
- // 输入参数:
- // sz_Feature_File_Name,Feature文件的文件名
- // n_Sentence_Num,要统计的句子数
- // n_Sentence_Start_Index, 开始的句子书。
- // 输出参数:
- // 无
- // 返回值:
- // 统计出的总帧数
- // 备注:在类如VQ训练准备数据的场合,应该使用这个API,效率会比较高
- int dfa_Feature_Get_File_Total_Frame_Num(char * sz_Feature_File_Name, int n_Sentence_Num, int n_Sentence_Start_Index)
- {
- int nResult;
- FILE * fp_Feature_File;
- long l_Frame_Num;
- int n_Sentence_Index;
- fp_Feature_File = fopen(sz_Feature_File_Name, "rb");
- ASSERT(fp_Feature_File != NULL);
- fseek(fp_Feature_File, sizeof(FEATURE_FILE_HEAD), SEEK_SET);
- fseek(fp_Feature_File, sizeof(long) * 2 * n_Sentence_Start_Index, SEEK_CUR);
- nResult = 0;
- for (n_Sentence_Index = 0; n_Sentence_Index < n_Sentence_Num; n_Sentence_Index++)
- {
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- if (l_Frame_Num > 0)
- nResult += l_Frame_Num;
- }
- fclose(fp_Feature_File);
- return(nResult);
- }
- //////////////////////////////////////////////////////////////////////
- // 函数名称:dfa_Feature_Get_File_Total_Frame_Num
- // 函数功能:读出.DAT格式的Feature文件中的待训练句子的总帧数
- // 函数性质:API
- // 输入参数:
- // sz_Feature_File_Name,Feature文件的文件名
- // n_Sentence_Num,要统计的句子数
- // n_Sentence_Start_Index, 开始的句子书。
- // 输出参数:
- // 无
- // 返回值:
- // 统计出的总帧数
- // 备注:在类如VQ训练准备数据的场合,应该使用这个API,效率会比较高
- int dfa_Feature_Get_Silence_Total_Frame_Num(char * sz_Feature_File_Name, int n_Sentence_Num, int n_Sentence_Start_Index)
- {
- int nResult;
- FILE * fp_Feature_File;
- long l_Frame_Num;
- int n_Sentence_Index;
- /**/fp_Feature_File = fopen(sz_Feature_File_Name, "rb");
- ASSERT(fp_Feature_File != NULL);
- fseek(fp_Feature_File, sizeof(FEATURE_FILE_HEAD), SEEK_SET);
- fseek(fp_Feature_File, sizeof(long) * 2 * n_Sentence_Start_Index, SEEK_CUR);
- nResult = 0;
- for (n_Sentence_Index = 0; n_Sentence_Index < n_Sentence_Num; n_Sentence_Index++)
- {
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- if (l_Frame_Num > 0)
- nResult += l_Frame_Num;
- }
- fclose(fp_Feature_File);
- //nResult = n_Sentence_Num * RELAX_FRAME * 2;
- return(nResult);
- }
- //////////////////////////////////////////////////////////////////////
- // 函数名称:dfa_Feature_Get_Sentence_Frame_Num
- // 函数功能:读出.DAT格式的Feature文件中的某个句子的帧数
- // 函数性质:API
- // 输入参数:
- // sz_Feature_File_Name,Feature文件的文件名
- // n_Sentence_Index,要统计的句子索引号
- // 输出参数:
- // 无
- // 返回值:
- // 该句子的帧数
- // 备注:
- int dfa_Feature_Get_Sentence_Frame_Num(char * sz_Feature_File_Name, int n_Sentence_Index)
- {
- FILE * fp_Feature_File;
- long l_Frame_Num;
- fp_Feature_File = fopen(sz_Feature_File_Name, "rb");
- ASSERT(fp_Feature_File != NULL);
- fseek(fp_Feature_File, sizeof(FEATURE_FILE_HEAD) + sizeof(SENTENCE_INDEX_BLOCK) * n_Sentence_Index, SEEK_SET);
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- fclose(fp_Feature_File);
- return(l_Frame_Num);
- }
- //////////////////////////////////////////////////////////////////////
- // 函数名称:dfa_Feature_Get_Sentence_Frame_Num
- // 函数功能:读出.DAT格式的Feature文件中的某个句子的帧数
- // 函数性质:API
- // 输入参数:
- // sz_Feature_File_Name,Feature文件的文件名
- // n_Sentence_Index,要统计的句子索引号
- // 输出参数:
- // 无
- // 返回值:
- // 该句子的帧数
- // 备注:
- int dfa_Feature_Get_Silence_Frame_Num(char * sz_Feature_File_Name, int n_Sentence_Index)
- {
- FILE * fp_Feature_File;
- long l_Frame_Num;
- fp_Feature_File = fopen(sz_Feature_File_Name, "rb");
- ASSERT(fp_Feature_File != NULL);
- /**/
- fseek(fp_Feature_File, sizeof(FEATURE_FILE_HEAD) + sizeof(SENTENCE_INDEX_BLOCK) * n_Sentence_Index, SEEK_SET);
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- fclose(fp_Feature_File);
- //l_Frame_Num = 2 * RELAX_FRAME;
- return(l_Frame_Num);
- }
- //////////////////////////////////////////////////////////////////////
- // 函数名称:dfa_Feature_Read_A_Sentence
- // 函数功能:读出.DAT格式的Feature文件中的某个句子的特征
- // 函数性质:API
- // 输入参数:
- // sz_Feature_File_Name,Feature文件的文件名
- // n_Sentence_Index,要读取特征的句子索引号
- // n_Feature_Dim,特征的维数
- // 输出参数:
- // d2dda,存放特征的缓冲区
- // 返回值:
- // 读出的特征的帧数
- // 备注:关于DYNA_2DIM_DOUBLE_ARRAY,参见DXDXA模块
- // 按照调试需要,读出特征的同时,
- // 将对一阶MFCC差分倍乘系数u_Pro_Config.d_Feature_DT_MFCC_Coefficient
- int dfa_Feature_Read_A_Sentence(char * sz_Feature_File_Name, int n_Sentence_Index, int n_Feature_Dim, DYNA_2DIM_DOUBLE_ARRAY d2dda)
- {
- FILE * fp_Feature_File;
- long l_Offset, l_Frame_Num;
- int n_Frame_Index;
- float f_Tmp;
- FEATURE_FILE_HEAD filehead;
- fp_Feature_File = fopen(sz_Feature_File_Name, "rb");
- ASSERT(fp_Feature_File != NULL);
- fread(&filehead, sizeof(FEATURE_FILE_HEAD), 1, fp_Feature_File);
- ASSERT( filehead.sFeatureOrder == u_Pro_Config.n_Feature_Dim);
- fseek(fp_Feature_File, sizeof(SENTENCE_INDEX_BLOCK) * n_Sentence_Index, SEEK_CUR);
- fread(&l_Offset, sizeof(long), 1, fp_Feature_File);
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- fseek(fp_Feature_File, l_Offset, SEEK_SET);
- for (n_Frame_Index = 0; n_Frame_Index < l_Frame_Num; n_Frame_Index++)
- {
- for (int i = 0; i < n_Feature_Dim; i++)
- {
- fread(&f_Tmp, sizeof(float), 1, fp_Feature_File);
- d2dda[n_Frame_Index][i] = f_Tmp;
- float temp = d2dda[n_Frame_Index][i] * 32768L/4;
- ASSERT((temp >= short(MIN_SHORT_VALUE)) && (temp <= MAX_SHORT_VALUE));
- }
- }
- fclose(fp_Feature_File);
- return(l_Frame_Num);
- }
- //////////////////////////////////////////////////////////////////////
- // 函数名称:dfa_Feature_Read_A_Silence
- // 函数功能:读出.DAT格式的Feature文件中的某个句子的静音部分特征
- // 函数性质:API
- // 输入参数:
- // sz_Feature_File_Name,Feature文件的文件名
- // n_Sentence_Index,要读取特征的句子索引号
- // n_Feature_Dim,特征的维数
- // 输出参数:
- // d2dda,存放特征的缓冲区
- // 返回值:
- // 读出的特征的帧数
- // 备注:关于DYNA_2DIM_DOUBLE_ARRAY,参见DXDXA模块
- // 按照调试需要,读出特征的同时,
- // 将对一阶MFCC差分倍乘系数u_Pro_Config.d_Feature_DT_MFCC_Coefficient
- int dfa_Feature_Read_A_Silence(char * sz_Feature_File_Name, int n_Sentence_Index, int n_Feature_Dim, DYNA_2DIM_DOUBLE_ARRAY d2dda)
- {
- FILE * fp_Feature_File;
- long l_Offset, l_Frame_Num;
- int n_Frame_Index;
- float ff_Tmp;
- fp_Feature_File = fopen(sz_Feature_File_Name, "rb");
- ASSERT(fp_Feature_File != NULL);
- fseek(fp_Feature_File, sizeof(FEATURE_FILE_HEAD) + sizeof(SENTENCE_INDEX_BLOCK) * n_Sentence_Index, SEEK_SET);
- fread(&l_Offset, sizeof(long), 1, fp_Feature_File);
- fread(&l_Frame_Num, sizeof(long), 1, fp_Feature_File);
- fseek(fp_Feature_File, l_Offset, SEEK_SET);
- for (n_Frame_Index = 0; n_Frame_Index < l_Frame_Num; n_Frame_Index++)
- {
- for (int i = 0; i < n_Feature_Dim; i++)
- {
- fread(&ff_Tmp, sizeof(float), 1, fp_Feature_File);
- d2dda[n_Frame_Index][i] = ff_Tmp;
- float temp = d2dda[n_Frame_Index][i] * 32768L/4;
- ASSERT((temp >= short(MIN_SHORT_VALUE)) && (temp <= MAX_SHORT_VALUE));
- }
- }
- fclose(fp_Feature_File);
- return(l_Frame_Num);
- }