WaveData Class Reference

This class's purpose is to read in Wave files. More...

#include <wave-reader.h>

Collaboration diagram for WaveData:

Public Member Functions

 WaveData (BaseFloat samp_freq, const MatrixBase< BaseFloat > &data)
 
 WaveData ()
 
void Read (std::istream &is)
 Read() will throw on error. More...
 
void Write (std::ostream &os) const
 Write() will throw on error. os should be opened in binary mode. More...
 
const Matrix< BaseFloat > & Data () const
 
BaseFloat SampFreq () const
 
BaseFloat Duration () const
 
void CopyFrom (const WaveData &other)
 
void Clear ()
 
void Swap (WaveData *other)
 

Private Attributes

Matrix< BaseFloatdata_
 
BaseFloat samp_freq_
 

Static Private Attributes

static const uint32 kBlockSize = 1024 * 1024
 

Detailed Description

This class's purpose is to read in Wave files.

Definition at line 106 of file wave-reader.h.

Constructor & Destructor Documentation

◆ WaveData() [1/2]

WaveData ( BaseFloat  samp_freq,
const MatrixBase< BaseFloat > &  data 
)
inline

Definition at line 108 of file wave-reader.h.

109  : data_(data), samp_freq_(samp_freq) {}
BaseFloat samp_freq_
Definition: wave-reader.h:149
Matrix< BaseFloat > data_
Definition: wave-reader.h:148

◆ WaveData() [2/2]

WaveData ( )
inline

Definition at line 111 of file wave-reader.h.

References WaveInfo::Read().

111 : samp_freq_(0.0) {}
BaseFloat samp_freq_
Definition: wave-reader.h:149

Member Function Documentation

◆ Clear()

void Clear ( )
inline

Definition at line 136 of file wave-reader.h.

References data_, and WaveInfo::samp_freq_.

136  {
137  data_.Resize(0, 0);
138  samp_freq_ = 0.0;
139  }
BaseFloat samp_freq_
Definition: wave-reader.h:149
Matrix< BaseFloat > data_
Definition: wave-reader.h:148
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).

◆ CopyFrom()

void CopyFrom ( const WaveData other)
inline

Definition at line 131 of file wave-reader.h.

References data_, WaveData::data_, WaveInfo::samp_freq_, and WaveData::samp_freq_.

131  {
132  samp_freq_ = other.samp_freq_;
133  data_.CopyFromMat(other.data_);
134  }
BaseFloat samp_freq_
Definition: wave-reader.h:149
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
Matrix< BaseFloat > data_
Definition: wave-reader.h:148

◆ Data()

◆ Duration()

BaseFloat Duration ( ) const
inline

Definition at line 129 of file wave-reader.h.

References data_, and WaveInfo::samp_freq_.

Referenced by main(), UnitTestMono22K(), and UnitTestStereo8K().

129 { return data_.NumCols() / samp_freq_; }
BaseFloat samp_freq_
Definition: wave-reader.h:149
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Matrix< BaseFloat > data_
Definition: wave-reader.h:148

◆ Read()

void Read ( std::istream &  is)

Read() will throw on error.

It's valid to call Read() more than once– in this case it will destroy what was there before. "is" should be opened in binary mode.

Definition at line 272 of file wave-reader.cc.

References WaveInfo::BlockAlign(), data_, WaveInfo::DataBytes(), rnnlm::i, WaveInfo::IsStreamed(), rnnlm::j, KALDI_ERR, KALDI_SWAP2, KALDI_WARN, WaveInfo::NumChannels(), WaveInfo::Read(), WaveInfo::ReverseBytes(), and WaveInfo::SampFreq().

Referenced by main(), kaldi::TestOnlineAppendFeature(), kaldi::TestOnlineMfcc(), kaldi::TestOnlinePlp(), kaldi::TestOnlineTransform(), kaldi::UnitTestDiffSampleRate(), UnitTestEndless1(), UnitTestEndless2(), UnitTestHTKCompare1(), UnitTestHTKCompare2(), UnitTestHTKCompare3(), UnitTestHTKCompare4(), UnitTestHTKCompare5(), UnitTestHTKCompare6(), kaldi::UnitTestKeele(), kaldi::UnitTestKeeleNccfBallast(), UnitTestMono22K(), kaldi::UnitTestPenaltyFactor(), kaldi::UnitTestPitchExtractionSpeed(), kaldi::UnitTestPitchExtractorCompareKeele(), kaldi::UnitTestProcess(), UnitTestReadWave(), kaldi::UnitTestSnipEdges(), and UnitTestStereo8K().

272  {
273  const uint32 kBlockSize = 1024 * 1024;
274 
275  WaveInfo header;
276  header.Read(is);
277 
278  data_.Resize(0, 0); // clear the data.
279  samp_freq_ = header.SampFreq();
280 
281  std::vector<char> buffer;
282  uint32 bytes_to_go = header.IsStreamed() ? kBlockSize : header.DataBytes();
283 
284  // Once in a while header.DataBytes() will report an insane value;
285  // read the file to the end
286  while (is && bytes_to_go > 0) {
287  uint32 block_bytes = std::min(bytes_to_go, kBlockSize);
288  uint32 offset = buffer.size();
289  buffer.resize(offset + block_bytes);
290  is.read(&buffer[offset], block_bytes);
291  uint32 bytes_read = is.gcount();
292  buffer.resize(offset + bytes_read);
293  if (!header.IsStreamed())
294  bytes_to_go -= bytes_read;
295  }
296 
297  if (is.bad())
298  KALDI_ERR << "WaveData: file read error";
299 
300  if (buffer.size() == 0)
301  KALDI_ERR << "WaveData: empty file (no data)";
302 
303  if (!header.IsStreamed() && buffer.size() < header.DataBytes()) {
304  KALDI_WARN << "Expected " << header.DataBytes() << " bytes of wave data, "
305  << "but read only " << buffer.size() << " bytes. "
306  << "Truncated file?";
307  }
308 
309  uint16 *data_ptr = reinterpret_cast<uint16*>(&buffer[0]);
310 
311  // The matrix is arranged row per channel, column per sample.
312  data_.Resize(header.NumChannels(),
313  buffer.size() / header.BlockAlign());
314  for (uint32 i = 0; i < data_.NumCols(); ++i) {
315  for (uint32 j = 0; j < data_.NumRows(); ++j) {
316  int16 k = *data_ptr++;
317  if (header.ReverseBytes())
318  KALDI_SWAP2(k);
319  data_(j, i) = k;
320  }
321  }
322 }
static const uint32 kBlockSize
Definition: wave-reader.h:147
#define KALDI_SWAP2(a)
Definition: kaldi-utils.h:114
BaseFloat samp_freq_
Definition: wave-reader.h:149
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Matrix< BaseFloat > data_
Definition: wave-reader.h:148
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).

◆ SampFreq()

◆ Swap()

void Swap ( WaveData other)
inline

Definition at line 141 of file wave-reader.h.

References data_, WaveData::data_, WaveInfo::samp_freq_, WaveData::samp_freq_, and kaldi::swap().

141  {
142  data_.Swap(&(other->data_));
143  std::swap(samp_freq_, other->samp_freq_);
144  }
BaseFloat samp_freq_
Definition: wave-reader.h:149
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
void Swap(Matrix< Real > *other)
Swaps the contents of *this and *other. Shallow swap.
Matrix< BaseFloat > data_
Definition: wave-reader.h:148

◆ Write()

void Write ( std::ostream &  os) const

Write() will throw on error. os should be opened in binary mode.

Definition at line 332 of file wave-reader.cc.

References data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_ERR, KALDI_SWAP2, KALDI_WARN, kaldi::WriteUint16(), and kaldi::WriteUint32().

Referenced by main(), and WaveHolder::Write().

332  {
333  os << "RIFF";
334  if (data_.NumRows() == 0)
335  KALDI_ERR << "Error: attempting to write empty WAVE file";
336 
337  int32 num_chan = data_.NumRows(),
338  num_samp = data_.NumCols(),
339  bytes_per_samp = 2;
340 
341  int32 subchunk2size = (num_chan * num_samp * bytes_per_samp);
342  int32 chunk_size = 36 + subchunk2size;
343  WriteUint32(os, chunk_size);
344  os << "WAVE";
345  os << "fmt ";
346  WriteUint32(os, 16);
347  WriteUint16(os, 1);
348  WriteUint16(os, num_chan);
350  WriteUint32(os, static_cast<int32>(samp_freq_));
351  WriteUint32(os, static_cast<int32>(samp_freq_) * num_chan * bytes_per_samp);
352  WriteUint16(os, num_chan * bytes_per_samp);
353  WriteUint16(os, 8 * bytes_per_samp);
354  os << "data";
355  WriteUint32(os, subchunk2size);
356 
357  const BaseFloat *data_ptr = data_.Data();
358  int32 stride = data_.Stride();
359 
360  int num_clipped = 0;
361  for (int32 i = 0; i < num_samp; i++) {
362  for (int32 j = 0; j < num_chan; j++) {
363  int32 elem = static_cast<int32>(trunc(data_ptr[j * stride + i]));
364  int16 elem_16 = static_cast<int16>(elem);
365  if (elem < std::numeric_limits<int16>::min()) {
366  elem_16 = std::numeric_limits<int16>::min();
367  ++num_clipped;
368  } else if (elem > std::numeric_limits<int16>::max()) {
369  elem_16 = std::numeric_limits<int16>::max();
370  ++num_clipped;
371  }
372 #ifdef __BIG_ENDIAN__
373  KALDI_SWAP2(elem_16);
374 #endif
375  os.write(reinterpret_cast<char*>(&elem_16), 2);
376  }
377  }
378  if (os.fail())
379  KALDI_ERR << "Error writing wave data to stream.";
380  if (num_clipped > 0)
381  KALDI_WARN << "WARNING: clipped " << num_clipped
382  << " samples out of total " << num_chan * num_samp
383  << ". Reduce volume?";
384 }
#define KALDI_SWAP2(a)
Definition: kaldi-utils.h:114
BaseFloat samp_freq_
Definition: wave-reader.h:149
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
const Real * Data() const
Gives pointer to raw data (const).
Definition: kaldi-matrix.h:79
kaldi::int32 int32
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:70
float BaseFloat
Definition: kaldi-types.h:29
Matrix< BaseFloat > data_
Definition: wave-reader.h:148
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
static void WriteUint16(std::ostream &os, int16 i)
Definition: wave-reader.cc:100
static void WriteUint32(std::ostream &os, int32 i)
Definition: wave-reader.cc:86

Member Data Documentation

◆ data_

Matrix<BaseFloat> data_
private

Definition at line 148 of file wave-reader.h.

Referenced by WaveData::CopyFrom(), and WaveData::Swap().

◆ kBlockSize

const uint32 kBlockSize = 1024 * 1024
staticprivate

Definition at line 147 of file wave-reader.h.

◆ samp_freq_

BaseFloat samp_freq_
private

Definition at line 149 of file wave-reader.h.

Referenced by WaveData::CopyFrom(), and WaveData::Swap().


The documentation for this class was generated from the following files: