-
Notifications
You must be signed in to change notification settings - Fork 0
/
htkmfc.py
168 lines (146 loc) · 4.93 KB
/
htkmfc.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright (c) 2007 Carnegie Mellon University
#
# You may copy and modify this freely under the same terms as
# Sphinx-III
"""Read and write HTK feature files.
This module reads and writes the acoustic feature files used by HTK
"""
__author__ = "David Huggins-Daines <[email protected]>"
__version__ = "$Revision $"
from struct import unpack, pack
import numpy
LPC = 1
LPCREFC = 2
LPCEPSTRA = 3
LPCDELCEP = 4
IREFC = 5
MFCC = 6
FBANK = 7
MELSPEC = 8
USER = 9
DISCRETE = 10
PLP = 11
_E = 0000100 # has energy
_N = 0000200 # absolute energy supressed
_D = 0000400 # has delta coefficients
_A = 0001000 # has acceleration (delta-delta) coefficients
_C = 0002000 # is compressed
_Z = 0004000 # has zero mean static coefficients
_K = 0010000 # has CRC checksum
_O = 0020000 # has 0th cepstral coefficient
_V = 0040000 # has VQ data
_T = 0100000 # has third differential coefficients
def open(f, mode=None, veclen=13):
"""Open an HTK format feature file for reading or writing.
The mode parameter is 'rb' (reading) or 'wb' (writing)."""
if mode is None:
if hasattr(f, 'mode'):
mode = f.mode
else:
mode = 'rb'
if mode in ('r', 'rb'):
return HTKFeat_read(f) # veclen is ignored since it's in the file
elif mode in ('w', 'wb'):
return HTKFeat_write(f, veclen)
else:
raise Exception, "mode must be 'r', 'rb', 'w', or 'wb'"
class HTKFeat_read(object):
"Read HTK format feature files"
def __init__(self, filename=None):
self.swap = (unpack('=i', pack('>i', 42))[0] != 42)
if (filename != None):
self.open(filename)
def __iter__(self):
self.fh.seek(12,0)
return self
def open(self, filename):
self.filename = filename
self.fh = file(filename, "rb")
self.readheader()
def readheader(self):
self.fh.seek(0,0)
spam = self.fh.read(12)
self.nSamples, self.sampPeriod, self.sampSize, self.parmKind = \
unpack(">IIHH", spam)
# Get coefficients for compressed data
if self.parmKind & _C:
self.dtype = 'h'
self.veclen = self.sampSize / 2
if self.parmKind & 0x3f == IREFC:
self.A = 32767
self.B = 0
else:
self.A = numpy.fromfile(self.fh, 'f', self.veclen)
self.B = numpy.fromfile(self.fh, 'f', self.veclen)
if self.swap:
self.A = self.A.byteswap()
self.B = self.B.byteswap()
else:
self.dtype = 'f'
self.veclen = self.sampSize / 4
self.hdrlen = self.fh.tell()
def seek(self, idx):
self.fh.seek(self.hdrlen + idx * self.sampSize, 0)
def next(self):
vec = numpy.fromfile(self.fh, self.dtype, self.veclen)
if len(vec) == 0:
raise StopIteration
if self.swap:
vec = vec.byteswap()
# Uncompress data to floats if required
if self.parmKind & _C:
vec = (vec.astype('f') + self.B) / self.A
return vec
def readvec(self):
return self.next()
def getall(self):
self.seek(0)
data = numpy.fromfile(self.fh, self.dtype)
if self.parmKind & _K: # Remove and ignore checksum
data = data[:-1]
data = data.reshape(len(data)/self.veclen, self.veclen)
if self.swap:
data = data.byteswap()
# Uncompress data to floats if required
if self.parmKind & _C:
data = (data.astype('f') + self.B) / self.A
return data
class HTKFeat_write(object):
"Write Sphinx-II format feature files"
def __init__(self, filename=None,
veclen=13, sampPeriod=100000,
paramKind = (MFCC | _O)):
self.veclen = veclen
self.sampPeriod = sampPeriod
self.sampSize = veclen * 4
self.paramKind = paramKind
self.dtype = 'f'
self.filesize = 0
self.swap = (unpack('=i', pack('>i', 42))[0] != 42)
if (filename != None):
self.open(filename)
def __del__(self):
self.close()
def open(self, filename):
self.filename = filename
self.fh = file(filename, "wb")
self.writeheader()
def close(self):
self.writeheader()
def writeheader(self):
self.fh.seek(0,0)
self.fh.write(pack(">IIHH", self.filesize,
self.sampPeriod,
self.sampSize,
self.paramKind))
def writevec(self, vec):
if len(vec) != self.veclen:
raise Exception("Vector length must be %d" % self.veclen)
if self.swap:
numpy.array(vec, self.dtype).byteswap().tofile(self.fh)
else:
numpy.array(vec, self.dtype).tofile(self.fh)
self.filesize = self.filesize + self.veclen
def writeall(self, arr):
for row in arr:
self.writevec(row)