wave-reader-test.cc File Reference
#include <iostream>
#include "base/kaldi-math.h"
#include "feat/wave-reader.h"
#include "matrix/kaldi-matrix.h"
Include dependency graph for wave-reader-test.cc:

Go to the source code of this file.

Macros

#define BY(n, k)   ((char)((uint32)(n) >> (8 * (k)) & 0xFF))
 
#define WRD(n)   BY(n,0), BY(n,1)
 
#define DWRD(n)   BY(n,0), BY(n,1), BY(n,2), BY(n,3)
 

Functions

static void UnitTestStereo8K ()
 
static void UnitTestMono22K ()
 
static void UnitTestEndless1 ()
 
static void UnitTestEndless2 ()
 
static void UnitTest ()
 
int main ()
 

Macro Definition Documentation

◆ BY

#define BY (   n,
 
)    ((char)((uint32)(n) >> (8 * (k)) & 0xFF))

Definition at line 29 of file wave-reader-test.cc.

◆ DWRD

#define DWRD (   n)    BY(n,0), BY(n,1), BY(n,2), BY(n,3)

◆ WRD

#define WRD (   n)    BY(n,0), BY(n,1)

Function Documentation

◆ main()

int main ( )

Definition at line 208 of file wave-reader-test.cc.

References UnitTest().

208  {
209  try {
210  UnitTest();
211  std::cout << "LGTM\n";
212  return 0;
213  } catch (const std::exception &e) {
214  std::cerr << e.what();
215  return 1;
216  }
217 }
static void UnitTest()

◆ UnitTest()

static void UnitTest ( )
static

Definition at line 201 of file wave-reader-test.cc.

References UnitTestEndless1(), UnitTestEndless2(), UnitTestMono22K(), and UnitTestStereo8K().

Referenced by main().

201  {
203  UnitTestMono22K();
206 }
static void UnitTestEndless1()
static void UnitTestEndless2()
static void UnitTestMono22K()
static void UnitTestStereo8K()

◆ UnitTestEndless1()

static void UnitTestEndless1 ( )
static

Definition at line 127 of file wave-reader-test.cc.

References kaldi::AssertEqual(), WaveData::Data(), DWRD, WaveData::Read(), Matrix< Real >::Read(), and WRD.

Referenced by UnitTest().

127  {
128  const int hz = 8000;
129  const int byps = hz * 1 /* channels */ * 2 /* bytes/sample */;
130  const char file_data[] = {
131  'R', 'I', 'F', 'F',
132  DWRD(0), // File length unknown
133  'W', 'A', 'V', 'E',
134  'f', 'm', 't', ' ',
135  DWRD(18), // sizeof(struct WAVEFORMATEX)
136  WRD(1), // WORD wFormatTag;
137  WRD(1), // WORD nChannels;
138  DWRD(hz), // DWORD nSamplesPerSec;
139  DWRD(byps), // DWORD nAvgBytesPerSec;
140  WRD(2), // WORD nBlockAlign;
141  WRD(16), // WORD wBitsPerSample;
142  WRD(0), // WORD cbSize;
143  'd', 'a', 't', 'a',
144  DWRD(0), // 'data' chunk length unknown.
145  WRD(1), WRD(2), WRD(3)
146  };
147 
148  const char expect_mat[] = "[ 1 2 3 ]";
149 
150  // Read binary file data.
151  std::istringstream iws(std::string(file_data, sizeof file_data),
152  std::ios::in | std::ios::binary);
153  WaveData wave;
154  wave.Read(iws);
155 
156  // Read expected matrix.
157  std::istringstream ies(expect_mat, std::ios::in);
158  Matrix<BaseFloat> expected;
159  expected.Read(ies, false /* text */);
160 
161  AssertEqual(wave.Data(), expected);
162 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#define DWRD(n)
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
#define WRD(n)

◆ UnitTestEndless2()

static void UnitTestEndless2 ( )
static

Definition at line 164 of file wave-reader-test.cc.

References kaldi::AssertEqual(), WaveData::Data(), DWRD, WaveData::Read(), Matrix< Real >::Read(), and WRD.

Referenced by UnitTest().

164  {
165  const int hz = 8000;
166  const int byps = hz * 1 /* channels */ * 2 /* bytes/sample */;
167  const char file_data[] = {
168  'R', 'I', 'F', 'F',
169  DWRD(-1), // File length unknown
170  'W', 'A', 'V', 'E',
171  'f', 'm', 't', ' ',
172  DWRD(18), // sizeof(struct WAVEFORMATEX)
173  WRD(1), // WORD wFormatTag;
174  WRD(1), // WORD nChannels;
175  DWRD(hz), // DWORD nSamplesPerSec;
176  DWRD(byps), // DWORD nAvgBytesPerSec;
177  WRD(2), // WORD nBlockAlign;
178  WRD(16), // WORD wBitsPerSample;
179  WRD(0), // WORD cbSize;
180  'd', 'a', 't', 'a',
181  DWRD(-1), // 'data' chunk length unknown.
182  WRD(1), WRD(2), WRD(3)
183  };
184 
185  const char expect_mat[] = "[ 1 2 3 ]";
186 
187  // Read binary file data.
188  std::istringstream iws(std::string(file_data, sizeof file_data),
189  std::ios::in | std::ios::binary);
190  WaveData wave;
191  wave.Read(iws);
192 
193  // Read expected matrix.
194  std::istringstream ies(expect_mat, std::ios::in);
195  Matrix<BaseFloat> expected;
196  expected.Read(ies, false /* text */);
197 
198  AssertEqual(wave.Data(), expected);
199 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#define DWRD(n)
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
#define WRD(n)

◆ UnitTestMono22K()

static void UnitTestMono22K ( )
static

Definition at line 81 of file wave-reader-test.cc.

References kaldi::AssertEqual(), WaveData::Data(), WaveData::Duration(), DWRD, WaveData::Read(), Matrix< Real >::Read(), WaveData::SampFreq(), and WRD.

Referenced by UnitTest().

81  {
82  /* Reference file written with Adobe Audition (random data):
83 00000000 52 49 46 46 30 00 00 00 57 41 56 45 66 6d 74 20 |RIFF0...WAVEfmt |
84 00000010 12 00 00 00 01 00 01 00 22 56 00 00 44 ac 00 00 |........"V..D...|
85 00000020 02 00 10 00 00 00 64 61 74 61 0a 00 00 00 25 36 |......data....%6|
86 00000030 cb 41 1b 4d 04 4e 62 3d |.A.M.Nb=|
87  */
88 
89  const int hz = 22050;
90  const int byps = hz * 1 /* channels */ * 2 /* bytes/sample */;
91  const char file_data[] = {
92  'R', 'I', 'F', 'F',
93  DWRD(48), // File length after this point.
94  'W', 'A', 'V', 'E',
95  'f', 'm', 't', ' ',
96  DWRD(18), // sizeof(struct WAVEFORMATEX)
97  WRD(1), // WORD wFormatTag;
98  WRD(1), // WORD nChannels;
99  DWRD(hz), // DWORD nSamplesPerSec;
100  DWRD(byps), // DWORD nAvgBytesPerSec;
101  WRD(2), // WORD nBlockAlign;
102  WRD(16), // WORD wBitsPerSample;
103  WRD(0), // WORD cbSize;
104  'd', 'a', 't', 'a',
105  DWRD(10), // 'data' chunk length.
106  WRD(0), WRD(-1), WRD(-32768), WRD(32767), WRD(1)
107  };
108 
109  const char expect_mat[] = "[ 0 -1 -32768 32767 1 ]";
110 
111  // Read binary file data.
112  std::istringstream iws(std::string(file_data, sizeof file_data),
113  std::ios::in | std::ios::binary);
114  WaveData wave;
115  wave.Read(iws);
116 
117  // Read expected matrix.
118  std::istringstream ies(expect_mat, std::ios::in);
119  Matrix<BaseFloat> expected;
120  expected.Read(ies, false /* text */);
121 
122  AssertEqual(wave.SampFreq(), hz, 0);
123  AssertEqual(wave.Duration(), 5.0 /* samples */ / hz /* Hz */, 1E-6);
124  AssertEqual(wave.Data(), expected);
125 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
BaseFloat SampFreq() const
Definition: wave-reader.h:126
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#define DWRD(n)
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
BaseFloat Duration() const
Definition: wave-reader.h:129
#define WRD(n)

◆ UnitTestStereo8K()

static void UnitTestStereo8K ( )
static

Definition at line 33 of file wave-reader-test.cc.

References kaldi::AssertEqual(), WaveData::Data(), WaveData::Duration(), DWRD, WaveData::Read(), Matrix< Real >::Read(), WaveData::SampFreq(), and WRD.

Referenced by UnitTest().

33  {
34  /* Reference file written with Adobe Audition (random data):
35 00000000 52 49 46 46 32 00 00 00 57 41 56 45 66 6d 74 20 |RIFF2...WAVEfmt |
36 00000010 12 00 00 00 01 00 02 00 40 1f 00 00 00 7d 00 00 |........@....}..|
37 00000020 04 00 10 00 00 00 64 61 74 61 0c 00 00 00 00 00 |......data......|
38 00000030 31 51 ff 21 f4 63 38 4c 26 60 |1Q.!.c8L&`|
39  */
40 
41  const int hz = 8000;
42  const int byps = hz * 2 /* channels */ * 2 /* bytes/sample */;
43  const char file_data[] = {
44  'R', 'I', 'F', 'F',
45  DWRD(50), // File length after this point.
46  'W', 'A', 'V', 'E',
47  'f', 'm', 't', ' ',
48  DWRD(18), // sizeof(struct WAVEFORMATEX)
49  WRD(1), // WORD wFormatTag;
50  WRD(2), // WORD nChannels;
51  DWRD(hz), // DWORD nSamplesPerSec; 40 1f 00 00
52  DWRD(byps), // DWORD nAvgBytesPerSec; 00 7d 00 00
53  WRD(4), // WORD nBlockAlign;
54  WRD(16), // WORD wBitsPerSample;
55  WRD(0), // WORD cbSize;
56  'd', 'a', 't', 'a',
57  DWRD(12), // 'data' chunk length.
58  WRD(0), WRD(-1),
59  WRD(-32768), WRD(0),
60  WRD(32767), WRD(1)
61  };
62 
63  const char expect_mat[] = "[ 0 -32768 32767 \n -1 0 1 ]";
64 
65  // Read binary file data.
66  std::istringstream iws(std::string(file_data, sizeof file_data),
67  std::ios::in | std::ios::binary);
68  WaveData wave;
69  wave.Read(iws);
70 
71  // Read expected wave data.
72  std::istringstream ies(expect_mat, std::ios::in);
73  Matrix<BaseFloat> expected;
74  expected.Read(ies, false /* text */);
75 
76  AssertEqual(wave.SampFreq(), hz, 0);
77  AssertEqual(wave.Duration(), 3.0 /* samples */ / hz /* Hz */, 1E-6);
78  AssertEqual(wave.Data(), expected);
79 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
BaseFloat SampFreq() const
Definition: wave-reader.h:126
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#define DWRD(n)
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
BaseFloat Duration() const
Definition: wave-reader.h:129
#define WRD(n)