File: TestMultiChannelAudioFileWriter.cxx

package info (click to toggle)
clam 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 20,981
  • sloc: cpp: 92,504; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (341 lines) | stat: -rw-r--r-- 8,753 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <cppunit/extensions/HelperMacros.h>
#include "cppUnitHelper.hxx"
#include "MultiChannelAudioFileReader.hxx"
#include "MultiChannelAudioFileWriter.hxx"
#include "similarityHelper.hxx"
#include "CLAM_Math.hxx"
#include "OSDefines.hxx"

namespace CLAMTest
{
	class MultiChannelAudioFileWriterFunctionalTest;

	CPPUNIT_TEST_SUITE_REGISTRATION( MultiChannelAudioFileWriterFunctionalTest );

	class MultiChannelAudioFileWriterFunctionalTest
		: public CppUnit::TestFixture
	{
		CPPUNIT_TEST_SUITE( MultiChannelAudioFileWriterFunctionalTest );
		CPPUNIT_TEST( testDo_PCM_WritesRightAKnownSignal );
		CPPUNIT_TEST( testDo_DoubleWriting_Is_Not_Allowed );
		CPPUNIT_TEST( testDo_PCM_WritesTheSameThatWasRead );
		CPPUNIT_TEST( testDo_OggVorbis_WritesTheSameThatWasRead );
		CPPUNIT_TEST_SUITE_END();

	protected:
		std::string mPathToTestData;
	public:

		void setUp()
		{
			mPathToTestData = GetTestDataDirectory();

		}

		void tearDown()
		{
		}

	private:
		
		void testDo_PCM_WritesRightAKnownSignal()
		{
			CLAM::MultiChannelAudioFileWriterConfig cfgWriter;
			cfgWriter.SetTargetFile( "twosines-stereo.wav" );
			CLAM::MultiChannelAudioFileWriter procWriter(cfgWriter);

			CPPUNIT_ASSERT_EQUAL( true, procWriter.Configure( cfgWriter ) );

			std::vector<CLAM::Audio> samples(2);
			samples[0].SetSize( 256 );
			samples[1].SetSize( 256 );

			procWriter.Start();

			int  framesSynth = 0;
			CLAM::TData srate = 44100.;
			CLAM::TData f0 = 100;
			CLAM::TData f1 = 400;

			while ( framesSynth < 100 )
			{
				for ( int i = 0; i < 256; i++ )
				{
					samples[0].GetBuffer()[i] = 0.0;
					samples[1].GetBuffer()[i] = 0.0;
				}
				procWriter.Do(samples);
				framesSynth++;
				
			}
			
			// cosinus starts at zero ( -pi/2 intial phase )
			CLAM::TData phi0 = -M_PI / 2.0;
			CLAM::TData phi1 = -M_PI / 2.0;
			CLAM::TData dphi0 = (2.0*M_PI*f0) / srate;
			CLAM::TData dphi1 = (2.0*M_PI*f1) / srate;

			while( framesSynth < 200 )
			{

				for ( int i = 0; i < 256; i++ )
				{
					samples[0].GetBuffer()[i] = 0.5 * cos( phi0 );
					samples[1].GetBuffer()[i] = 0.5 * cos( phi1 );
					phi0 += dphi0;
					phi1 += dphi1;
				}

				procWriter.Do(samples);
				framesSynth++;
			}


			procWriter.Stop();

		}

		void testDo_DoubleWriting_Is_Not_Allowed()
		{
			CLAM::MultiChannelAudioFileWriterConfig cfgWriter;
			cfgWriter.SetTargetFile( "twosines-stereo.wav" );

			CLAM::MultiChannelAudioFileWriter procWriter;
			CLAM::MultiChannelAudioFileWriter procWriter2;
			
			procWriter.Configure( cfgWriter );

			CPPUNIT_ASSERT_EQUAL( false, procWriter2.Configure( cfgWriter ) );

		}

		void testDo_PCM_WritesTheSameThatWasRead()
		{
			CLAM::MultiChannelAudioFileReaderConfig cfgReader;
			cfgReader.SetSourceFile( mPathToTestData+"test-stereo-decoding.wav" );
			CLAM::MultiChannelAudioFileReader procReader(cfgReader);

			CLAM::MultiChannelAudioFileWriterConfig cfgWriter;
			cfgWriter.SetTargetFile( "test-stereo-decoding-copy.wav" );
			cfgWriter.SetNChannels( procReader.GetHeader().GetChannels() );
			cfgWriter.SetSampleRate( procReader.GetHeader().GetSampleRate() );
			// Not yet supported
//			cfgWriter.SetFormat( procReader.GetHeader().GetFormat() );
//			cfgWriter.SetEncoding( procReader.GetHeader().GetEncoding() );
//			cfgWriter.SetEndianess( procReader.GetHeader().GetEndianess() );

			CLAM::MultiChannelAudioFileWriter procWriter;
			CPPUNIT_ASSERT_EQUAL( true,
					      procWriter.Configure( cfgWriter ) );

			std::vector<CLAM::Audio> samples(2);
			samples[0].SetSize( 256 );
			samples[1].SetSize( 256 );


			procReader.Start();
			procWriter.Start();

			int  framesRead = 0;

			while( procReader.Do(samples) )
			{
				framesRead++;
				procWriter.Do(samples);
			}

			procReader.Stop();
			procWriter.Stop();

			// Once written to disk, now we recover it, and 
			// check it is the same frame by frame
			
			CLAM::MultiChannelAudioFileReader procReader2;
			cfgReader.SetSourceFile( "test-stereo-decoding-copy.wav" );
			CPPUNIT_ASSERT_EQUAL( true, procReader2.Configure( cfgReader ) );

			std::vector<CLAM::Audio> samples2(2);
			samples2[0].SetSize( 256 );
			samples2[1].SetSize( 256 );

			
			procReader.Start();
			procReader2.Start();

			int framesChecked = 0;

			while( procReader.Do(samples) && procReader2.Do(samples2) )
			{
				framesChecked++;
				double simLeft = evaluateSimilarity( samples[0].GetBuffer(), 
								     samples2[0].GetBuffer() );

				CPPUNIT_ASSERT
					(  simLeft >= 0.9999 );

				double simRight = evaluateSimilarity( samples[1].GetBuffer(),
								      samples2[1].GetBuffer() );
				CPPUNIT_ASSERT
					( simRight >= 0.9999 );

			}

			procReader.Stop();
			procReader2.Stop();		

			CPPUNIT_ASSERT_EQUAL( framesRead,
					      framesChecked );
			
		}

		void testDo_OggVorbis_WritesTheSameThatWasRead()
		{
			
			CLAM::MultiChannelAudioFileReaderConfig cfgReader;
			cfgReader.SetSourceFile( mPathToTestData+"ElvisStereo.wav" );
			CLAM::MultiChannelAudioFileReader procReader;
			CPPUNIT_ASSERT_EQUAL( true,
					      procReader.Configure( cfgReader ) );		

			CLAM::MultiChannelAudioFileWriterConfig cfgWriter;
			cfgWriter.SetTargetFile( "ElvisStereo-copy.ogg" );
			cfgWriter.SetSampleRate( procReader.GetHeader().GetSampleRate());
			cfgWriter.SetNChannels( procReader.GetHeader().GetChannels());
			CLAM::MultiChannelAudioFileWriter procWriter;
			CPPUNIT_ASSERT_EQUAL( true,
					      procWriter.Configure( cfgWriter ) );

			std::vector<CLAM::Audio> samples(2);
			samples[0].SetSize( 4096 );
			samples[1].SetSize( 4096 );


			procReader.Start();
			procWriter.Start();

			CLAM::TSize framesRead = 0;

			while( procReader.Do(samples) )
			{
				framesRead++;

				procWriter.Do(samples);
			}

			procReader.Stop();
			procWriter.Stop();

			// Once written to disk, now we recover it, and 
			// check it is the same frame by frame
			
			CLAM::MultiChannelAudioFileReader procReader2;
			cfgReader.SetSourceFile( "ElvisStereo-copy.ogg" );
			CPPUNIT_ASSERT_EQUAL( true, procReader2.Configure( cfgReader ) );

			std::vector<CLAM::Audio> samples2(2);
			samples2[0].SetSize( 4096 );
			samples2[1].SetSize( 4096 );

			procReader.Start();
			procReader2.Start();

			int framesChecked = 0;

			double maxSimLeft = -1e20;
			int    maxSimLeftFrame = 0;
			double minSimLeft = 1e20;
			int    minSimLeftFrame = 0;
			double averageSimLeft = 0.0;

			double maxSimRight = -1e20;
			int    maxSimRightFrame = 0;
			double minSimRight = 1e20;
			int    minSimRightFrame = 0;
			double averageSimRight = 0.0;


			while( procReader.Do(samples) && procReader2.Do(samples2) )
			{
				framesChecked++;
				double simLeft = evaluateSimilarity( samples[0].GetBuffer(), 
								     samples2[0].GetBuffer() );

				double simRight = evaluateSimilarity( samples[1].GetBuffer(),
								      samples2[1].GetBuffer() );


				if ( simLeft > maxSimLeft )
				{
					maxSimLeft = simLeft;
					maxSimLeftFrame = framesChecked;
				}
				if ( simLeft < minSimLeft )
				{
					minSimLeft = simLeft;
					minSimLeftFrame = framesChecked;
				}

				averageSimLeft += simLeft;


				if ( simRight > maxSimRight )
				{
					maxSimRight = simRight;
					maxSimRightFrame = framesChecked;
				}
				if ( simRight < minSimRight )
				{
					minSimRight = simRight;
					minSimRightFrame = framesChecked;
				}

				averageSimRight += simRight;


				CPPUNIT_ASSERT
					(  simLeft >= 0.9 );

				CPPUNIT_ASSERT
					( simRight >= 0.9 );

			}

			procReader.Stop();
			procReader2.Stop();		

			averageSimLeft *= (1.0/double(framesChecked));
			averageSimRight *= (1.0/double(framesChecked));
			
			/*
			std::cout << std::endl;
			std::cout << "test_Do_OggVorbis" << std::endl;
			std::cout << "Maximum Left similarity: " << maxSimLeft << " at " << maxSimLeftFrame;
			std::cout << std::endl;
			std::cout << "Minimum Left similarity: " << minSimLeft << " at " << minSimLeftFrame;
			std::cout << std::endl;
			std::cout << "Average Left similarity: " << averageSimLeft  << std::endl;

			std::cout << "Maximum Right similarity: " << maxSimRight << " at " << maxSimRightFrame;
			std::cout << std::endl;
			std::cout << "Minimum Right similarity: " << minSimRight << " at " << minSimRightFrame;
			std::cout << std::endl;
			std::cout << "Average Right similarity: " << averageSimRight  << std::endl;
			*/

			CPPUNIT_ASSERT( fabs( maxSimLeft -  0.999595) < 1e-2 );
			CPPUNIT_ASSERT( fabs( minSimLeft -  0.980736) < 1e-2 );
			CPPUNIT_ASSERT( fabs( averageSimLeft - 0.99788 ) < 1e-2 );

			CPPUNIT_ASSERT( fabs( maxSimRight - 0.999595) < 1e-2 );
			CPPUNIT_ASSERT( fabs( minSimRight - 0.980736) < 1e-2 );
			CPPUNIT_ASSERT( fabs( averageSimRight - 0.997888) < 1e-2 );

			CPPUNIT_ASSERT_EQUAL( framesRead,
					      framesChecked );
			
		}


	};
}