wave-reader-test.cc
Go to the documentation of this file.
1 // feat/wave-reader-test.cc
2 
3 // Copyright 2017 Smart Action LLC (kkm)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include <iostream>
21 
22 #include "base/kaldi-math.h"
23 #include "feat/wave-reader.h"
24 #include "matrix/kaldi-matrix.h"
25 
26 using namespace kaldi;
27 
28 // Ugly macros to package bytes in wave file order (low-endian).
29 #define BY(n,k) ((char)((uint32)(n) >> (8 * (k)) & 0xFF))
30 #define WRD(n) BY(n,0), BY(n,1)
31 #define DWRD(n) BY(n,0), BY(n,1), BY(n,2), BY(n,3)
32 
33 static void UnitTestStereo8K() {
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 }
80 
81 static void UnitTestMono22K() {
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 }
126 
127 static void UnitTestEndless1() {
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 }
163 
164 static void UnitTestEndless2() {
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 }
200 
201 static void UnitTest() {
203  UnitTestMono22K();
206 }
207 
208 int main() {
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 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
static void UnitTestEndless1()
BaseFloat SampFreq() const
Definition: wave-reader.h:126
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
static void UnitTest()
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
static void UnitTestEndless2()
int main()
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
static void UnitTestMono22K()
#define WRD(n)
static void UnitTestStereo8K()