All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
I/O related functions and classes

Various namespace-scope functions for I/O. More...

Classes

struct  HtkHeader
 A structure containing the HTK header. More...
 

Functions

template<typename Real >
bool ReadHtk (std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
 Extension of the HTK header. More...
 
template<typename Real >
bool WriteHtk (std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
 
template<class Real >
bool WriteSphinx (std::ostream &os, const MatrixBase< Real > &M)
 
template<typename Real >
std::ostream & operator<< (std::ostream &os, const MatrixBase< Real > &M)
 
template<typename Real >
std::istream & operator>> (std::istream &is, MatrixBase< Real > &M)
 
template<typename Real >
std::istream & operator>> (std::istream &is, Matrix< Real > &M)
 
template<typename Real >
bool SameDim (const MatrixBase< Real > &M, const MatrixBase< Real > &N)
 
template<typename Real >
std::ostream & operator<< (std::ostream &os, const VectorBase< Real > &rv)
 
template<typename Real >
std::istream & operator>> (std::istream &in, VectorBase< Real > &v)
 Input from a C++ stream. More...
 
template<typename Real >
std::istream & operator>> (std::istream &in, Vector< Real > &v)
 Input from a C++ stream. More...
 
template<typename Real >
std::ostream & operator<< (std::ostream &out, const PackedMatrix< Real > &M)
 
template<typename Real >
std::istream & operator>> (std::istream &is, PackedMatrix< Real > &M)
 

Detailed Description

Various namespace-scope functions for I/O.

end of "addtogroup matrix_group"

Output to a C++ stream.

Note that the << and >> operators are not recommended for normal use in Kaldi-style I/O (for that, use the Read and Write methods). The operators are mainly useful for logging and error output.

Non-binary by default (use Write for binary output).

Function Documentation

std::ostream & operator<< ( std::ostream &  os,
const VectorBase< Real > &  rv 
)

Definition at line 30 of file kaldi-vector-inl.h.

30  {
31  rv.Write(os, false);
32  return os;
33 }
std::ostream & operator<< ( std::ostream &  out,
const PackedMatrix< Real > &  M 
)

Definition at line 181 of file packed-matrix.h.

181  {
182  M.Write(os, false);
183  return os;
184 }
std::ostream & operator<< ( std::ostream &  os,
const MatrixBase< Real > &  M 
)
inline

Definition at line 41 of file kaldi-matrix-inl.h.

41  {
42  M.Write(os, false);
43  return os;
44 }
std::istream & operator>> ( std::istream &  in,
VectorBase< Real > &  v 
)

Input from a C++ stream.

Will automatically read text or binary data from the stream.

Definition at line 36 of file kaldi-vector-inl.h.

References VectorBase< Real >::Read().

36  {
37  rv.Read(is, false);
38  return is;
39 }
std::istream & operator>> ( std::istream &  in,
Vector< Real > &  v 
)

Input from a C++ stream.

Will automatically read text or binary data from the stream.

Definition at line 42 of file kaldi-vector-inl.h.

References Vector< Real >::Read().

42  {
43  rv.Read(is, false);
44  return is;
45 }
std::istream & operator>> ( std::istream &  is,
Matrix< Real > &  M 
)
inline

Definition at line 47 of file kaldi-matrix-inl.h.

References Matrix< Real >::Read().

47  {
48  M.Read(is, false);
49  return is;
50 }
std::istream & operator>> ( std::istream &  is,
MatrixBase< Real > &  M 
)
inline

Definition at line 54 of file kaldi-matrix-inl.h.

References MatrixBase< Real >::Read().

54  {
55  M.Read(is, false);
56  return is;
57 }
std::istream& kaldi::operator>> ( std::istream &  is,
PackedMatrix< Real > &  M 
)

Definition at line 187 of file packed-matrix.h.

187  {
188  M.Read(is, false);
189  return is;
190 }
bool ReadHtk ( std::istream &  is,
Matrix< Real > *  M_ptr,
HtkHeader *  header_ptr 
)

Extension of the HTK header.

Definition at line 2078 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT_IS_FLOATING_TYPE, KALDI_ERR, KALDI_SWAP2, KALDI_SWAP4, KALDI_VLOG, KALDI_WARN, kaldi::MachineIsLittleEndian(), HtkHeader::mNSamples, HtkHeader::mSampleKind, HtkHeader::mSamplePeriod, HtkHeader::mSampleSize, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), Matrix< Real >::Resize(), and MatrixBase< Real >::RowData().

Referenced by main(), HtkMatrixHolder::Read(), UnitTestHTKCompare1(), UnitTestHTKCompare2(), UnitTestHTKCompare3(), UnitTestHTKCompare4(), UnitTestHTKCompare5(), UnitTestHTKCompare6(), and kaldi::UnitTestHtkIo().

2079 {
2080  // check instantiated with double or float.
2082  Matrix<Real> &M = *M_ptr;
2083  HtkHeader htk_hdr;
2084 
2085  // TODO(arnab): this fails if the HTK file has CRC cheksum or is compressed.
2086  is.read((char*)&htk_hdr, sizeof(htk_hdr)); // we're being really POSIX here!
2087  if (is.fail()) {
2088  KALDI_WARN << "Could not read header from HTK feature file ";
2089  return false;
2090  }
2091 
2092  KALDI_SWAP4(htk_hdr.mNSamples);
2093  KALDI_SWAP4(htk_hdr.mSamplePeriod);
2094  KALDI_SWAP2(htk_hdr.mSampleSize);
2095  KALDI_SWAP2(htk_hdr.mSampleKind);
2096 
2097  bool has_checksum = false;
2098  {
2099  // See HParm.h in HTK code for sources of these things.
2100  enum BaseParmKind{
2101  Waveform, Lpc, Lprefc, Lpcepstra, Lpdelcep,
2102  Irefc, Mfcc, Fbank, Melspec, User, Discrete, Plp, Anon };
2103 
2104  const int32 IsCompressed = 02000, HasChecksum = 010000, HasVq = 040000,
2105  Problem = IsCompressed | HasVq;
2106  int32 base_parm = htk_hdr.mSampleKind & (077);
2107  has_checksum = (base_parm & HasChecksum) != 0;
2108  htk_hdr.mSampleKind &= ~HasChecksum; // We don't support writing with
2109  // checksum so turn it off.
2110  if (htk_hdr.mSampleKind & Problem)
2111  KALDI_ERR << "Code to read HTK features does not support compressed "
2112  "features, or features with VQ.";
2113  if (base_parm == Waveform || base_parm == Irefc || base_parm == Discrete)
2114  KALDI_ERR << "Attempting to read HTK features from unsupported type "
2115  "(e.g. waveform or discrete features.";
2116  }
2117 
2118  KALDI_VLOG(3) << "HTK header: Num Samples: " << htk_hdr.mNSamples
2119  << "; Sample period: " << htk_hdr.mSamplePeriod
2120  << "; Sample size: " << htk_hdr.mSampleSize
2121  << "; Sample kind: " << htk_hdr.mSampleKind;
2122 
2123  M.Resize(htk_hdr.mNSamples, htk_hdr.mSampleSize / sizeof(float));
2124 
2125  MatrixIndexT i;
2126  MatrixIndexT j;
2127  if (sizeof(Real) == sizeof(float)) {
2128  for (i = 0; i< M.NumRows(); i++) {
2129  is.read((char*)M.RowData(i), sizeof(float)*M.NumCols());
2130  if (is.fail()) {
2131  KALDI_WARN << "Could not read data from HTK feature file ";
2132  return false;
2133  }
2134  if (MachineIsLittleEndian()) {
2135  MatrixIndexT C = M.NumCols();
2136  for (j = 0; j < C; j++) {
2137  KALDI_SWAP4((M(i, j))); // The HTK standard is big-endian!
2138  }
2139  }
2140  }
2141  } else {
2142  float *pmem = new float[M.NumCols()];
2143  for (i = 0; i < M.NumRows(); i++) {
2144  is.read((char*)pmem, sizeof(float)*M.NumCols());
2145  if (is.fail()) {
2146  KALDI_WARN << "Could not read data from HTK feature file ";
2147  delete [] pmem;
2148  return false;
2149  }
2150  MatrixIndexT C = M.NumCols();
2151  for (j = 0; j < C; j++) {
2152  if (MachineIsLittleEndian()) // HTK standard is big-endian!
2153  KALDI_SWAP4(pmem[j]);
2154  M(i, j) = static_cast<Real>(pmem[j]);
2155  }
2156  }
2157  delete [] pmem;
2158  }
2159  if (header_ptr) *header_ptr = htk_hdr;
2160  if (has_checksum) {
2161  int16 checksum;
2162  is.read((char*)&checksum, sizeof(checksum));
2163  if (is.fail())
2164  KALDI_WARN << "Could not read checksum from HTK feature file ";
2165  // We ignore the checksum.
2166  }
2167  return true;
2168 }
#define KALDI_SWAP2(a)
Definition: kaldi-utils.h:110
OfflineFeatureTpl< MfccComputer > Mfcc
Definition: feature-mfcc.h:143
OfflineFeatureTpl< PlpComputer > Plp
Definition: feature-plp.h:167
int32 MatrixIndexT
Definition: matrix-common.h:96
OfflineFeatureTpl< FbankComputer > Fbank
int MachineIsLittleEndian()
Definition: kaldi-utils.h:79
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
Definition: kaldi-utils.h:134
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
#define KALDI_SWAP4(a)
Definition: kaldi-utils.h:103
bool WriteHtk ( std::ostream &  os,
const MatrixBase< Real > &  M,
HtkHeader  htk_hdr 
)

Definition at line 2178 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_SWAP2, KALDI_SWAP4, KALDI_WARN, kaldi::MachineIsLittleEndian(), HtkHeader::mNSamples, HtkHeader::mSampleKind, HtkHeader::mSamplePeriod, HtkHeader::mSampleSize, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::RowData().

Referenced by main(), UnitTestHTKCompare1(), UnitTestHTKCompare2(), UnitTestHTKCompare3(), UnitTestHTKCompare4(), UnitTestHTKCompare5(), UnitTestHTKCompare6(), kaldi::UnitTestHtkIo(), and HtkMatrixHolder::Write().

2179 {
2180  KALDI_ASSERT(M.NumRows() == static_cast<MatrixIndexT>(htk_hdr.mNSamples));
2181  KALDI_ASSERT(M.NumCols() == static_cast<MatrixIndexT>(htk_hdr.mSampleSize) /
2182  static_cast<MatrixIndexT>(sizeof(float)));
2183 
2184  KALDI_SWAP4(htk_hdr.mNSamples);
2185  KALDI_SWAP4(htk_hdr.mSamplePeriod);
2186  KALDI_SWAP2(htk_hdr.mSampleSize);
2187  KALDI_SWAP2(htk_hdr.mSampleKind);
2188 
2189  os.write((char*)&htk_hdr, sizeof(htk_hdr));
2190  if (os.fail()) goto bad;
2191 
2192  MatrixIndexT i;
2193  MatrixIndexT j;
2194  if (sizeof(Real) == sizeof(float) && !MachineIsLittleEndian()) {
2195  for (i = 0; i< M.NumRows(); i++) { // Unlikely to reach here ever!
2196  os.write((char*)M.RowData(i), sizeof(float)*M.NumCols());
2197  if (os.fail()) goto bad;
2198  }
2199  } else {
2200  float *pmem = new float[M.NumCols()];
2201 
2202  for (i = 0; i < M.NumRows(); i++) {
2203  const Real *rowData = M.RowData(i);
2204  for (j = 0;j < M.NumCols();j++)
2205  pmem[j] = static_cast<float> ( rowData[j] );
2206  if (MachineIsLittleEndian())
2207  for (j = 0;j < M.NumCols();j++)
2208  KALDI_SWAP4(pmem[j]);
2209  os.write((char*)pmem, sizeof(float)*M.NumCols());
2210  if (os.fail()) {
2211  delete [] pmem;
2212  goto bad;
2213  }
2214  }
2215  delete [] pmem;
2216  }
2217  return true;
2218 bad:
2219  KALDI_WARN << "Could not write to HTK feature file ";
2220  return false;
2221 }
#define KALDI_SWAP2(a)
Definition: kaldi-utils.h:110
int32 MatrixIndexT
Definition: matrix-common.h:96
int MachineIsLittleEndian()
Definition: kaldi-utils.h:79
#define KALDI_WARN
Definition: kaldi-error.h:130
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
#define KALDI_SWAP4(a)
Definition: kaldi-utils.h:103
bool WriteSphinx ( std::ostream &  os,
const MatrixBase< Real > &  M 
)

Definition at line 2230 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_SWAP4, KALDI_WARN, kaldi::MachineIsLittleEndian(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::RowData().

Referenced by main().

2231 {
2232  // CMUSphinx mfc file header contains count of the floats, followed
2233  // by the data in float little endian format.
2234 
2235  int size = M.NumRows() * M.NumCols();
2236  os.write((char*)&size, sizeof(int));
2237  if (os.fail()) goto bad;
2238 
2239  MatrixIndexT i;
2240  MatrixIndexT j;
2241  if (sizeof(Real) == sizeof(float) && MachineIsLittleEndian()) {
2242  for (i = 0; i< M.NumRows(); i++) { // Unlikely to reach here ever!
2243  os.write((char*)M.RowData(i), sizeof(float)*M.NumCols());
2244  if (os.fail()) goto bad;
2245  }
2246  } else {
2247  float *pmem = new float[M.NumCols()];
2248 
2249  for (i = 0; i < M.NumRows(); i++) {
2250  const Real *rowData = M.RowData(i);
2251  for (j = 0;j < M.NumCols();j++)
2252  pmem[j] = static_cast<float> ( rowData[j] );
2253  if (!MachineIsLittleEndian())
2254  for (j = 0;j < M.NumCols();j++)
2255  KALDI_SWAP4(pmem[j]);
2256  os.write((char*)pmem, sizeof(float)*M.NumCols());
2257  if (os.fail()) {
2258  delete [] pmem;
2259  goto bad;
2260  }
2261  }
2262  delete [] pmem;
2263  }
2264  return true;
2265 bad:
2266  KALDI_WARN << "Could not write to Sphinx feature file";
2267  return false;
2268 }
int32 MatrixIndexT
Definition: matrix-common.h:96
int MachineIsLittleEndian()
Definition: kaldi-utils.h:79
#define KALDI_WARN
Definition: kaldi-error.h:130
#define KALDI_SWAP4(a)
Definition: kaldi-utils.h:103