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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
//
// C++ Interface: %{MODULE}
//
// Description:
//
//
// Author: %{AUTHOR} <%{EMAIL}>, (C) %{YEAR}
//
// Copyright: See COPYING file that comes with this distribution
//
//
#ifndef TNet_Features_h
#define TNet_Features_h
//*****************************************************************************
//*****************************************************************************
// Standard includes
//
#include <list>
#include <queue>
#include <string>
//*****************************************************************************
//*****************************************************************************
// Specific includes
//
#include "Common.h"
#include "Matrix.h"
#include "StkStream.h"
#include "Types.h"
#include "Timer.h"
// we need these for reading and writing
#define UINT_16 unsigned short
#define UINT_32 unsigned
#define INT_16 short
#define INT_32 int
#define FLOAT_32 float
#define DOUBLE_64 double
#define PARAMKIND_WAVEFORM 0
#define PARAMKIND_LPC 1
#define PARAMKIND_LPREFC 2
#define PARAMKIND_LPCEPSTRA 3
#define PARAMKIND_LPDELCEP 4
#define PARAMKIND_IREFC 5
#define PARAMKIND_MFCC 6
#define PARAMKIND_FBANK 7
#define PARAMKIND_MELSPEC 8
#define PARAMKIND_USER 9
#define PARAMKIND_DISCRETE 10
#define PARAMKIND_PLP 11
#define PARAMKIND_ANON 12
#define PARAMKIND_E 0000100 /// has energy
#define PARAMKIND_N 0000200 /// absolute energy suppressed
#define PARAMKIND_D 0000400 /// has delta coefficients
#define PARAMKIND_A 0001000 /// has acceleration coefficients
#define PARAMKIND_C 0002000 /// is compressed
#define PARAMKIND_Z 0004000 /// has zero mean static coef.
#define PARAMKIND_K 0010000 /// has CRC checksum
#define PARAMKIND_0 0020000 /// has 0'th cepstral coef.
#define PARAMKIND_V 0040000 /// has VQ codebook index
#define PARAMKIND_T 0100000 /// has triple delta coefficients
//*****************************************************************************
//*****************************************************************************
// Code ...
//
namespace TNet
{
/** **************************************************************************
** **************************************************************************
*/
class FileListElem
{
private:
std::string mLogical; ///< Logical file name representation
std::string mPhysical; ///< Pysical file name representation
float mWeight;
public:
FileListElem(const std::string & rFileName);
~FileListElem() {}
const std::string &
Logical() const { return mLogical; }
const std::string &
Physical() const { return mPhysical; }
const float&
Weight() const { return mWeight; }
};
/** *************************************************************************
* @brief
*/
class FeatureRepository
{
public:
/**
* @brief HTK parameter file header (see HTK manual)
*/
struct HtkHeader
{
int mNSamples;
int mSamplePeriod;
short mSampleSize;
short mSampleKind;
HtkHeader()
: mNSamples(0),mSamplePeriod(100000),mSampleSize(0),mSampleKind(12)
{ }
};
/**
* @brief Extension of the HTK header
*/
struct HtkHeaderExt
{
int mHeaderSize;
int mVersion;
int mSampSize;
};
/**
* @brief Normalization file type
*/
enum CNFileType
{
CNF_Mean,
CNF_Variance,
CNF_VarScale
};
static int
ReadParmKind(const char *pStr, bool checkBrackets);
static int
ParmKind2Str(unsigned parmKind, char *pOutstr);
static void
ReadCepsNormFile(
const char* pFileName,
char** lastFile,
BaseFloat** vecBuff,
int sampleKind,
CNFileType type,
int coefs);
static const char mpParmKindNames[13][16];
//////////////////////////////////////////////////////////////////////////////
// PUBLIC SECTION
//////////////////////////////////////////////////////////////////////////////
public:
/// Iterates through the list of feature file records
typedef std::list<FileListElem>::iterator ListIterator;
// some params for loading features
bool mSwapFeatures;
int mStartFrameExt;
int mEndFrameExt;
int mTargetKind;
int mDerivOrder;
int* mDerivWinLengths;
const char* mpCvgFile;
//:TODO: get rid of these
const char* mpCmnPath;
const char* mpCmnMask;
const char* mpCvnPath;
const char* mpCvnMask;
int mTrace;
// Constructors and destructors
/**
* @brief Default constructor that creates an empty repository
*/
FeatureRepository() : mDerivWinLengths(NULL), mpCvgFile(NULL),
mpCmnPath(NULL), mpCmnMask(NULL), mpCvnPath(NULL), mpCvnMask(NULL),
mTrace(0),
mpLastFileName(NULL), mLastFileName(""), mpLastCmnFile (NULL),
mpLastCvnFile (NULL), mpLastCvgFile (NULL), mpCmn(NULL),
mpCvn(NULL), mpCvg(NULL), mpA(NULL), mpB(NULL),
mTimeOpen(0), mTimeSeek(0), mTimeRead(0), mTimeNormalize(0)
{
mInputQueueIterator = mInputQueue.end();
}
/**
* @brief Copy constructor which copies filled repository
*/
FeatureRepository(const FeatureRepository& ori)
: mDerivWinLengths(NULL), mpCvgFile(NULL),
mpCmnPath(NULL), mpCmnMask(NULL), mpCvnPath(NULL), mpCvnMask(NULL),
mTrace(0),
mpLastFileName(NULL), mLastFileName(""), mpLastCmnFile (NULL),
mpLastCvnFile (NULL), mpLastCvgFile (NULL), mpCmn(NULL),
mpCvn(NULL), mpCvg(NULL), mpA(NULL), mpB(NULL),
mTimeOpen(0), mTimeSeek(0), mTimeRead(0), mTimeNormalize(0)
{
//copy all the data from the input queue
mInputQueue = ori.mInputQueue;
//initialize like the original
Init(
ori.mSwapFeatures,
ori.mStartFrameExt,
ori.mEndFrameExt,
ori.mTargetKind,
ori.mDerivOrder,
ori.mDerivWinLengths,
ori.mpCmnPath,
ori.mpCmnMask,
ori.mpCvnPath,
ori.mpCvnMask,
ori.mpCvgFile);
//set on the end
mInputQueueIterator = mInputQueue.end();
//copy default header values
mHeader = ori.mHeader;
}
/**
* @brief Destroys the repository
*/
~FeatureRepository()
{
if (NULL != mpA) {
free(mpA);
}
if (NULL != mpB) {
free(mpB);
}
//remove all entries
mInputQueue.clear();
if(mTrace&4) {
std::cout << "[FeatureRepository -- open:" << mTimeOpen << "s seek:" << mTimeSeek << "s read:" << mTimeRead << "s normalize:" << mTimeNormalize << "s]\n";
}
}
/**
* @brief Initializes the object using the given parameters
*
* @param swap Boolean value specifies whether to swap bytes
* when reading file or not.
* @param extLeft Features read from file are extended with extLeft
* initial frames. Normally, these frames are
* repetitions of the first feature frame in file
* (with its derivative, if derivatives are preset in
* the file). However, if segment of feature frames
* is extracted according to range specification, the
* true feature frames from beyond the segment boundary
* are used, wherever it is possible. Note that value
* of extLeft can be also negative. In such case
* corresponding number of initial frames is discarded.
* @param extRight The paramerer is complementary to parameter extLeft
* and has obvious meaning. (Controls extensions over
* the last frame, last frame from file is repeated
* only if necessary).
* @param targetKind The parameters is used to check whether
* pHeader->mSampleKind match to requited targetKind
* and to control suppression of 0'th cepstral or
* energy coefficients accorging to modifiers _E, _0,
* and _N. Modifiers _D, _A and _T are ignored;
* Computation of derivatives is controled by parameters
* derivOrder and derivWinLen. Value PARAMKIND_ANON
* ensures that function do not result in targetKind
* mismatch error and cause no _E or _0 suppression.
* @param derivOrder Final features will be augmented with their
* derivatives up to 'derivOrder' order. If 'derivOrder'
* is negative value, no new derivatives are appended
* and derivatives that already present in feature file
* are preserved. Straight features are considered
* to be of zero order. If some derivatives are already
* present in feature file, these are not computed
* again, only higher order derivatives are appended
* if required. Note, that HTK feature file cannot
* contain higher order derivatives (e.g. double delta)
* without containing lower ones (e.g. delta).
* Derivative present in feature file that are of
* higher order than is required are discarded.
* Derivatives are computed in the final stage from
* (extracted segment of) feature frames possibly
* extended by repeated frames. Derivatives are
* computed using the same formula that is employed
* also by HTK tools. Lengths of windows used for
* computation of derivatives are passed in parameter
* derivWinLen. To compute derivatives for frames close
* to boundaries, frames before the first and after the
* last frame (of the extracted segment) are considered
* to be (yet another) repetitions of the first and the
* last frame, respectively. If the segment of frames
* is extracted according to range specification and
* parameters extLeft and extLeft are set to zero, the
* first and the last frames of the segment are
* considered to be repeated, eventough the true feature
* frames from beyond the segment boundary can be
* available in the file. Therefore, segment extracted
* from features that were before augmented with
* derivatives will differ
* from the same segment augmented with derivatives by
* this function. Difference will be of course only on
* boundaries and only in derivatives. This "incorrect"
* behavior was chosen to fully simulate behavior of
* HTK tools. To obtain more correct computation of
* derivatives, use parameters extLeft and extRight,
* which correctly extend segment with the true frames
* (if possible) and in resulting feature matrix ignore
* first extLeft and last extRight frames. For this
* purpose, both extLeft and extRight should be set to
* sum of all values in the array derivWinLen.
* @param pDerivWinLen Array of size derivOrder specifying lengths of
* windows used for computation of derivatives.
* Individual values represents one side context
* used in the computation. The each window length is
* therefore twice the value from array plus one.
* Value at index zero specify window length for first
* order derivatives (delta), higher indices
* corresponds to higher order derivatives.
* @param pCmnPath Cepstral mean normalization path
* @param pCmnMask Cepstral mean normalization mask
* @param pCvnPath Cepstral variance normalization path
* @param pCvnMask Cepstral variance normalization mask
* @param pCvgFile Global variance file to be parsed
*
* The given parameters are necessary for propper feature extraction
*/
void
Init(
bool swap,
int
|