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 2091 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().

2092 {
2093  // check instantiated with double or float.
2095  Matrix<Real> &M = *M_ptr;
2096  HtkHeader htk_hdr;
2097 
2098  // TODO(arnab): this fails if the HTK file has CRC cheksum or is compressed.
2099  is.read((char*)&htk_hdr, sizeof(htk_hdr)); // we're being really POSIX here!
2100  if (is.fail()) {
2101  KALDI_WARN << "Could not read header from HTK feature file ";
2102  return false;
2103  }
2104 
2105  KALDI_SWAP4(htk_hdr.mNSamples);
2106  KALDI_SWAP4(htk_hdr.mSamplePeriod);
2107  KALDI_SWAP2(htk_hdr.mSampleSize);
2108  KALDI_SWAP2(htk_hdr.mSampleKind);
2109 
2110  bool has_checksum = false;
2111  {
2112  // See HParm.h in HTK code for sources of these things.
2113  enum BaseParmKind{
2114  Waveform, Lpc, Lprefc, Lpcepstra, Lpdelcep,
2115  Irefc, Mfcc, Fbank, Melspec, User, Discrete, Plp, Anon };
2116 
2117  const int32 IsCompressed = 02000, HasChecksum = 010000, HasVq = 040000,
2118  Problem = IsCompressed | HasVq;
2119  int32 base_parm = htk_hdr.mSampleKind & (077);
2120  has_checksum = (base_parm & HasChecksum) != 0;
2121  htk_hdr.mSampleKind &= ~HasChecksum; // We don't support writing with
2122  // checksum so turn it off.
2123  if (htk_hdr.mSampleKind & Problem)
2124  KALDI_ERR << "Code to read HTK features does not support compressed "
2125  "features, or features with VQ.";
2126  if (base_parm == Waveform || base_parm == Irefc || base_parm == Discrete)
2127  KALDI_ERR << "Attempting to read HTK features from unsupported type "
2128  "(e.g. waveform or discrete features.";
2129  }
2130 
2131  KALDI_VLOG(3) << "HTK header: Num Samples: " << htk_hdr.mNSamples
2132  << "; Sample period: " << htk_hdr.mSamplePeriod
2133  << "; Sample size: " << htk_hdr.mSampleSize
2134  << "; Sample kind: " << htk_hdr.mSampleKind;
2135 
2136  M.Resize(htk_hdr.mNSamples, htk_hdr.mSampleSize / sizeof(float));
2137 
2138  MatrixIndexT i;
2139  MatrixIndexT j;
2140  if (sizeof(Real) == sizeof(float)) {
2141  for (i = 0; i< M.NumRows(); i++) {
2142  is.read((char*)M.RowData(i), sizeof(float)*M.NumCols());
2143  if (is.fail()) {
2144  KALDI_WARN << "Could not read data from HTK feature file ";
2145  return false;
2146  }
2147  if (MachineIsLittleEndian()) {
2148  MatrixIndexT C = M.NumCols();
2149  for (j = 0; j < C; j++) {
2150  KALDI_SWAP4((M(i, j))); // The HTK standard is big-endian!
2151  }
2152  }
2153  }
2154  } else {
2155  float *pmem = new float[M.NumCols()];
2156  for (i = 0; i < M.NumRows(); i++) {
2157  is.read((char*)pmem, sizeof(float)*M.NumCols());
2158  if (is.fail()) {
2159  KALDI_WARN << "Could not read data from HTK feature file ";
2160  delete [] pmem;
2161  return false;
2162  }
2163  MatrixIndexT C = M.NumCols();
2164  for (j = 0; j < C; j++) {
2165  if (MachineIsLittleEndian()) // HTK standard is big-endian!
2166  KALDI_SWAP4(pmem[j]);
2167  M(i, j) = static_cast<Real>(pmem[j]);
2168  }
2169  }
2170  delete [] pmem;
2171  }
2172  if (header_ptr) *header_ptr = htk_hdr;
2173  if (has_checksum) {
2174  int16 checksum;
2175  is.read((char*)&checksum, sizeof(checksum));
2176  if (is.fail())
2177  KALDI_WARN << "Could not read checksum from HTK feature file ";
2178  // We ignore the checksum.
2179  }
2180  return true;
2181 }
#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:133
#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 2191 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().

2192 {
2193  KALDI_ASSERT(M.NumRows() == static_cast<MatrixIndexT>(htk_hdr.mNSamples));
2194  KALDI_ASSERT(M.NumCols() == static_cast<MatrixIndexT>(htk_hdr.mSampleSize) /
2195  static_cast<MatrixIndexT>(sizeof(float)));
2196 
2197  KALDI_SWAP4(htk_hdr.mNSamples);
2198  KALDI_SWAP4(htk_hdr.mSamplePeriod);
2199  KALDI_SWAP2(htk_hdr.mSampleSize);
2200  KALDI_SWAP2(htk_hdr.mSampleKind);
2201 
2202  os.write((char*)&htk_hdr, sizeof(htk_hdr));
2203  if (os.fail()) goto bad;
2204 
2205  MatrixIndexT i;
2206  MatrixIndexT j;
2207  if (sizeof(Real) == sizeof(float) && !MachineIsLittleEndian()) {
2208  for (i = 0; i< M.NumRows(); i++) { // Unlikely to reach here ever!
2209  os.write((char*)M.RowData(i), sizeof(float)*M.NumCols());
2210  if (os.fail()) goto bad;
2211  }
2212  } else {
2213  float *pmem = new float[M.NumCols()];
2214 
2215  for (i = 0; i < M.NumRows(); i++) {
2216  const Real *rowData = M.RowData(i);
2217  for (j = 0;j < M.NumCols();j++)
2218  pmem[j] = static_cast<float> ( rowData[j] );
2219  if (MachineIsLittleEndian())
2220  for (j = 0;j < M.NumCols();j++)
2221  KALDI_SWAP4(pmem[j]);
2222  os.write((char*)pmem, sizeof(float)*M.NumCols());
2223  if (os.fail()) {
2224  delete [] pmem;
2225  goto bad;
2226  }
2227  }
2228  delete [] pmem;
2229  }
2230  return true;
2231 bad:
2232  KALDI_WARN << "Could not write to HTK feature file ";
2233  return false;
2234 }
#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 2243 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().

2244 {
2245  // CMUSphinx mfc file header contains count of the floats, followed
2246  // by the data in float little endian format.
2247 
2248  int size = M.NumRows() * M.NumCols();
2249  os.write((char*)&size, sizeof(int));
2250  if (os.fail()) goto bad;
2251 
2252  MatrixIndexT i;
2253  MatrixIndexT j;
2254  if (sizeof(Real) == sizeof(float) && MachineIsLittleEndian()) {
2255  for (i = 0; i< M.NumRows(); i++) { // Unlikely to reach here ever!
2256  os.write((char*)M.RowData(i), sizeof(float)*M.NumCols());
2257  if (os.fail()) goto bad;
2258  }
2259  } else {
2260  float *pmem = new float[M.NumCols()];
2261 
2262  for (i = 0; i < M.NumRows(); i++) {
2263  const Real *rowData = M.RowData(i);
2264  for (j = 0;j < M.NumCols();j++)
2265  pmem[j] = static_cast<float> ( rowData[j] );
2266  if (!MachineIsLittleEndian())
2267  for (j = 0;j < M.NumCols();j++)
2268  KALDI_SWAP4(pmem[j]);
2269  os.write((char*)pmem, sizeof(float)*M.NumCols());
2270  if (os.fail()) {
2271  delete [] pmem;
2272  goto bad;
2273  }
2274  }
2275  delete [] pmem;
2276  }
2277  return true;
2278 bad:
2279  KALDI_WARN << "Could not write to Sphinx feature file";
2280  return false;
2281 }
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