-
Notifications
You must be signed in to change notification settings - Fork 2
/
nr_readhtk.m
executable file
·109 lines (105 loc) · 3.64 KB
/
nr_readhtk.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function [d,fs,dt,tc,t]=nr_readhtk(file,duration)
%READHTK read an HTK parameter file [D,FP,DT,TC,T]=(FILE)
%
% duration: [start stop] or [start], has to be in miliseconds. eg.
% [10000 20000] or [10000]
% d is data, fp is frame period in seconds
% dt is data type, tc is full type code, t is a text version of the full typecode
% tc is the sum of the following values:
% 0 WAVEFORM
% 1 LPC
% 2 LPREFC
% 3 LPCEPSTRA
% 4 LPDELCEP
% 5 IREFC
% 6 MFCC
% 7 FBANK
% 8 MELSPEC
% 9 USER
% 10 DISCRETE
% 11 PLP
% 64 -E Includes energy terms
% 128 _N Suppress absolute energy
% 256 _D Include delta coefs
% 512 _A Include acceleration coefs
% 1024 _C Compressed
% 2048 _Z Zero mean static coefs
% 4096 _K CRC checksum (not implemented yet)
% 8192 _0 Include 0'th cepstral coef
% Copyright (C) Mike Brookes 1997
%
% This version modified to read HTK's compressed feature files
% 2005-05-18 [email protected]
%
% VOICEBOX home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You can obtain a copy of the GNU General Public License from
% ftp://prep.ai.mit.edu/pub/gnu/COPYING-2.0 or by writing to
% Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('duration','var') || isempty(duration),
duration = [];
end
fid=fopen(file,'r','b');
if fid < 0
error(sprintf('Cannot read from file %s',file));
end
nf=fread(fid,1,'long');
fp=fread(fid,1,'long')*1.E-7;
%fp=fread(fid,1,'long')*1.E-10;
fs=fp*1E3;
by=fread(fid,1,'short');
tc=fread(fid,1,'short');
hb=floor(tc*pow2(-14:-6));
hd=hb(9:-1:2)-2*hb(8:-1:1);
dt=tc-64*hb(9);
% hd(7)=1 CRC check
% hd(5)=1 compressed data
if ( dt == 0 ) && 0,
d=fread(fid,Inf,'short');
else
if (hd(5) == 1) && 0
% compressed data - first read scales
nf = nf - 4;
ncol = by / 2;
scales = fread(fid, ncol, 'float');
biases = fread(fid, ncol, 'float');
d = fread(fid,[ncol, nf], 'short');
d = repmat(1./scales,1,nf).*(d+repmat(biases,1,nf));
d = d.';
else
if ~isempty(duration)
% user specified the duration. first skip
start = (fs*duration(1)/1000);
FLOATSIZE = 4;
fseek(fid,(by/4)*ceil(start)*FLOATSIZE,'cof');
if length(duration)>1,
nf = ceil(fs*diff(duration)/1000);
else
nf = inf;
end
end
d=fread(fid,[by/4,nf],'float');
end
end;
fclose(fid);
if nargout > 2
hd(7)=0;
hd(5)=0;
ns=sum(hd);
kinds=['WAVEFORM ';'LPC ';'LPREFC ';'LPCEPSTRA ';'LPDELCEP ';'IREFC ';'MFCC ';'FBANK ';'MELSPEC ';'USER ';'DISCRETE ';'PLP ';'??? '];
kind=kinds(min(dt+1,size(kinds,1)),:);
cc='ENDACZK0';
t=[kind(1:min(find(kind==' '))-1) reshape(['_'*ones(1,ns);cc(hd>0)],1,2*ns)];
end