35 using namespace Nsound;
41 static const char *
THIS_FILE =
"Buffer_UnitTest.cc";
54 cout <<
TEST_HEADER <<
"Testing Buffer::operator<<(float64) ...";
58 for(
float64 i = 0.0; i < 100.0; i += 1.1)
64 for(
float64 i = 0.0; i < 100.0; i += 1.1)
66 if( ::fabs(b1[index++] - i) >
GAMMA)
69 <<
"Output did not match expected values!"
78 cout << TEST_HEADER <<
"Testing Buffer::Buffer(const Buffer &) ...";
82 if(b1 != b2 || b2 != b1)
85 <<
"Output did not match expected values!"
89 diff.
plot(
"data - gold");
100 cout << TEST_HEADER <<
"Testing Buffer::operator=(const Buffer &) ...";
106 if(b3 != b1 || b1 != b3)
109 <<
"Output did not match expected values!"
113 diff.
plot(
"data - gold");
124 cout << TEST_HEADER <<
"Testing Buffer::operator<<(const Buffer &) ...";
131 if(!(b4 == b3) || !(b3 == b4))
134 <<
"Output did not match expected values!"
138 diff.
plot(
"data - gold");
149 cout << TEST_HEADER <<
"Testing Buffer::operator[](uint32) ...";
153 for(
uint32 i = 0; i < size; ++i)
155 if(::fabs(b4[i] - b3[i]) >
GAMMA)
158 <<
"Output did not match expected values!"
162 diff.
plot(
"data - gold");
174 cout << TEST_HEADER <<
"Testing Buffer::operator+,-,*,/ ...";
178 if(1 + b4 * 2 != 2 * b3 + 1)
181 <<
"Output did not match expected values!"
187 if(1 - b4 / 2 != -1 * b3 / 2 + 1)
190 <<
"Output did not match expected values!"
196 if(2.0 / b4 != 4.0 * (0.5 / b4))
199 <<
"Output did not match expected values!"
210 for(
uint32 i = 0; i < n_samples; ++i)
215 <<
"b4 += b3 _SHOULD_ be all zeros!"
223 cout << TEST_HEADER <<
"Testing Buffer::abs() ...";
230 <<
"b4.getLength() != 0"
237 for(
uint32 i = 0; i < 100; ++i)
249 <<
"b4.abs() is broken"
256 cout << TEST_HEADER <<
"Testing Buffer::reverse() ...";
263 <<
"b4.getLength() != 0"
268 for(
float64 f = 0.0; f < 100.0; f += 1.0)
276 for(
float64 f = 99.0; f >= 0.0; f -= 1.0)
278 if(::fabs(b4[index++] - f) >
GAMMA)
281 <<
"b4.reverse() borken"
293 cout << TEST_HEADER <<
"Testing Buffer::subbuffer() ...";
299 for(
uint32 i = 0; i < chunk * 2 + 1; ++i)
311 <<
"subbuf.getLength() = "
326 <<
"subbuf.getLength() = "
341 <<
"subbuf.getLength() = "
349 test_size = chunk + 10;
351 for(
uint32 i = 0; i < 10; ++i)
358 <<
"subbuf.getLength() = "
369 for(
uint32 i = 0; i < 10; ++i)
371 if(static_cast<uint32>(subbuf[i]) != i)
374 <<
"b3.subbuffer() error!"
384 for(
uint32 i = 0; i < 3; ++i)
386 if(static_cast<uint32>(subbuf[i]) != i + 3)
389 <<
"b3.subbuffer() error!"
402 <<
"subbuf.getLength() = "
417 <<
"subbuf.getLength() _SHOULD_ be 10, found "
423 for(
uint32 i = 0; i < 10; ++i)
425 if(static_cast<uint32>(subbuf[i]) != i)
428 <<
"b3.subbuffer() error!"
439 <<
"subbuf.getLength() = "
446 for(
uint32 i = 0; i < 5; ++i)
448 if(static_cast<uint32>(subbuf[i]) != i + 5)
464 cout << TEST_HEADER <<
"Testing Buffer::smooth() ...";
468 b4 <<
"gold/Filter_noise.wav";
478 <<
"b4.smooth() error!"
486 <<
"b4.smooth() error!"
493 cout << TEST_HEADER <<
"Testing Buffer::pow() ...";
499 for(
uint32 i = 0; i < 5; ++i)
504 <<
"Buffer::pow() error!"
505 <<
" 4.0 != " << b4[i]
513 for(
uint32 i = 0; i < 5; ++i)
518 <<
"Buffer::pow() error!"
519 <<
" 2.0 != " << b4[i]
525 b4 = (b4 ^ 2.0) + (b4 ^ 2.0);
527 for(
uint32 i = 0; i < 5; ++i)
532 <<
"Buffer::pow() error!"
533 <<
" 8.0 != " << b4[i]
541 cout << TEST_HEADER <<
"Testing Buffer operators with different length ...";
554 <<
"result.getLength() != b5.getLength(), "
580 <<
"result.getLength() != b6.getLength(), "
588 for(
uint32 i = 0; i < b5.getLength(); ++i)
616 cout << TEST_HEADER <<
"Testing Buffer::add() ...";
622 cout << TEST_HEADER <<
"Testing Buffer::circular_iterator ...";
624 b5 = sine.
drawLine(1.0, 1.0, 100.0);
628 for(
uint32 i = 0; i < b5.getLength(); ++i)
633 if(itor != b5.cbegin())
636 <<
"circular_iterator::operator++ is broken!"
643 for(
uint32 i = 0; i < b5.getLength(); ++i)
648 if(itor != b5.cbegin())
651 <<
"circular_iterator::operator-- is broken!"
658 for(
uint32 i = 0; i < 3; ++i)
663 if(itor != (b5.cbegin() + 3))
666 <<
"circular_iterator::operator++ is broken!"
673 for(
uint32 i = 0; i < 3; ++i)
678 if(itor != (b5.cbegin() - 3))
681 <<
"circular_iterator::operator-- is broken!"
688 cout << TEST_HEADER <<
"Testing Buffer advanced operators ...";
694 data(data > 0.5) = 0.5;
695 data(data < -0.5) = -0.5;
701 Buffer gold(
"gold/Buffer_out1.wav");
708 <<
"Output did not match expected values!"
711 diff.
plot(
"gold - data");
735 gold =
Buffer(
"gold/Buffer_out2.wav");
742 <<
"Output did not match expected values!"
745 diff.
plot(
"gold - data");
754 cout << SUCCESS << endl;
760 const std::string & filename,
761 unsigned int line_number,
762 unsigned int n_samples,
764 unsigned int a_length,
765 unsigned int b_length,
766 const std::string & message)
899 for(
uint32 i = 0; i < 12; ++i)
901 final_case[i] =
false;
904 for(
uint32 run = 0; run < 2; ++run)
913 for(
uint32 i = 0; i < test_size; ++i)
921 for(
uint32 i = 0; i < test_size * 5 + 7; ++i)
928 for(
uint32 i = 0; i < test_size; ++i)
936 for(
uint32 i = 0; i < test_size * 5 + 7; ++i)
947 uint32 offset_end = a_length + test_size * 6 + 7;
949 for(
uint32 n_samples = 0; n_samples < offset_end; ++n_samples)
951 for(
uint32 offset = 0; offset < offset_end; ++offset)
953 bool current_run[12];
955 for(
uint32 i = 0; i < 12; ++i)
957 current_run[i] =
false;
961 result.
add(b,offset,n_samples);
971 if(n_samples == 0 && a_length < offset)
973 current_run[0] =
true;
976 if(result_length != offset + b_length)
979 n_samples, offset, a_length, b_length,
980 "result length error");
984 for(
uint32 i = 0; i < a_length; ++i)
989 n_samples, offset, a_length, b_length,
990 "result prefix error");
995 for(
uint32 i = a_length + 1; i < offset; ++i)
1000 n_samples, offset, a_length, b_length,
1001 "result gap error");
1006 for(
uint32 i = offset + 1; i < result_length; ++i)
1008 if(result[i] != 1.0)
1011 n_samples, offset, a_length, b_length,
1012 "result b value error");
1027 else if(n_samples != 0
1028 && a_length < offset
1029 && n_samples <= b_length)
1031 current_run[1] =
true;
1033 if(result_length != offset + n_samples)
1036 n_samples, offset, a_length, b_length,
1037 "result length error");
1041 for(
uint32 i = 0; i < a_length; ++i)
1043 if(result[i] != 1.0)
1046 n_samples, offset, a_length, b_length,
1047 "result prefix error");
1052 for(
uint32 i = a_length + 1; i < offset; ++i)
1054 if(result[i] != 0.0)
1057 n_samples, offset, a_length, b_length,
1058 "result gap error");
1063 for(
uint32 i = offset + 1; i < result_length; ++i)
1065 if(result[i] != 1.0)
1068 n_samples, offset, a_length, b_length,
1069 "result b value error");
1084 else if(n_samples != 0
1085 && a_length < offset
1086 && n_samples > b_length)
1088 current_run[2] =
true;
1091 if(result_length != offset + n_samples)
1094 n_samples, offset, a_length, b_length,
1095 "result length error");
1099 for(
uint32 i = 0; i < a_length; ++i)
1101 if(result[i] != 1.0)
1104 n_samples, offset, a_length, b_length,
1105 "result prefix error");
1110 for(
uint32 i = a_length + 1; i < offset; ++i)
1112 if(result[i] != 0.0)
1115 n_samples, offset, a_length, b_length,
1116 "result gap error");
1121 for(
uint32 i = offset + 1; i < offset + b_length; ++i)
1123 if(result[i] != 1.0)
1126 n_samples, offset, a_length, b_length,
1127 "result b value error");
1132 for(
uint32 i = offset + b_length + 1; i < result_length; ++i)
1134 if(result[i] != 0.0)
1137 n_samples, offset, a_length, b_length,
1138 "result tail error");
1152 else if(n_samples == 0
1153 && a_length >= offset
1154 && a_length < offset + b_length)
1156 current_run[3] =
true;
1159 if(result_length != offset + b_length)
1162 n_samples, offset, a_length, b_length,
1163 "result length error");
1167 for(
uint32 i = 0; i < offset; ++i)
1169 if(result[i] != 1.0)
1172 n_samples, offset, a_length, b_length,
1173 "result prefix error");
1178 for(
uint32 i = offset + 1; i < a_length; ++i)
1180 if(result[i] != 2.0)
1183 n_samples, offset, a_length, b_length,
1184 "result overlap error");
1189 for(
uint32 i = a_length + 1; i < result_length; ++i)
1191 if(result[i] != 1.0)
1194 n_samples, offset, a_length, b_length,
1195 "result tail error");
1211 else if(n_samples != 0
1212 && a_length >= offset
1213 && a_length <= offset + n_samples
1214 && n_samples <= b_length)
1216 current_run[4] =
true;
1219 if(result_length != offset + n_samples)
1222 n_samples, offset, a_length, b_length,
1223 "result length error");
1227 for(
uint32 i = 0; i < offset; ++i)
1229 if(result[i] != 1.0)
1232 n_samples, offset, a_length, b_length,
1233 "result prefix error");
1238 for(
uint32 i = offset + 1; i < a_length; ++i)
1240 if(result[i] != 2.0)
1243 n_samples, offset, a_length, b_length,
1244 "result overlap error");
1249 for(
uint32 i = a_length + 1; i < result_length; ++i)
1251 if(result[i] != 1.0)
1254 n_samples, offset, a_length, b_length,
1255 "result tail error");
1270 else if(n_samples != 0
1271 && a_length >= offset
1272 && a_length > offset + n_samples
1273 && n_samples < b_length)
1275 current_run[5] =
true;
1278 if(result_length != a_length)
1281 n_samples, offset, a_length, b_length,
1282 "result length error");
1286 for(
uint32 i = 0; i < offset; ++i)
1288 if(result[i] != 1.0)
1291 n_samples, offset, a_length, b_length,
1292 "result prefix error");
1297 for(
uint32 i = offset + 1; i < offset + n_samples; ++i)
1299 if(result[i] != 2.0)
1302 n_samples, offset, a_length, b_length,
1303 "result overlap error");
1308 for(
uint32 i = a_length + 1; i < result_length; ++i)
1310 if(result[i] != 1.0)
1313 n_samples, offset, a_length, b_length,
1314 "result tail error");
1329 else if(n_samples != 0
1330 && a_length >= offset
1331 && a_length <= offset + b_length
1332 && n_samples > b_length)
1334 current_run[6] =
true;
1337 if(result_length != offset + n_samples)
1340 n_samples, offset, a_length, b_length,
1341 "result length error");
1345 for(
uint32 i = 0; i < offset; ++i)
1347 if(result[i] != 1.0)
1350 n_samples, offset, a_length, b_length,
1351 "result prefix error");
1356 for(
uint32 i = offset + 1; i < a_length; ++i)
1358 if(result[i] != 2.0)
1361 n_samples, offset, a_length, b_length,
1362 "result overlap error");
1367 for(
uint32 i = a_length + 1; i < offset + b_length; ++i)
1369 if(result[i] != 1.0)
1372 n_samples, offset, a_length, b_length,
1373 "result b value error");
1378 for(
uint32 i = offset + b_length + 1; i < result_length; ++i)
1380 if(result[i] != 0.0)
1383 n_samples, offset, a_length, b_length,
1384 "result tail error");
1396 else if(n_samples == 0
1397 && a_length >= offset + b_length)
1399 current_run[7] =
true;
1402 if(result_length != a_length)
1405 n_samples, offset, a_length, b_length,
1406 "result length error");
1410 for(
uint32 i = 0; i < offset; ++i)
1412 if(result[i] != 1.0)
1415 n_samples, offset, a_length, b_length,
1416 "result prefix error");
1421 for(
uint32 i = offset + 1; i < offset + b_length; ++i)
1423 if(result[i] != 2.0)
1426 n_samples, offset, a_length, b_length,
1427 "result overlap error");
1432 for(
uint32 i = offset + b_length + 1; i < result_length; ++i)
1434 if(result[i] != 1.0)
1437 n_samples, offset, a_length, b_length,
1438 "result a value error");
1452 else if(n_samples != 0
1453 && a_length >= offset + n_samples
1454 && n_samples < b_length)
1456 current_run[8] =
true;
1459 if(result_length != a_length)
1462 n_samples, offset, a_length, b_length,
1463 "result length error");
1467 for(
uint32 i = 0; i < offset; ++i)
1469 if(result[i] != 1.0)
1472 n_samples, offset, a_length, b_length,
1473 "result prefix error");
1478 for(
uint32 i = offset + 1; i < offset + n_samples; ++i)
1480 if(result[i] != 2.0)
1483 n_samples, offset, a_length, b_length,
1484 "result overlap error");
1489 for(
uint32 i = offset + n_samples + 1; i < result_length; ++i)
1491 if(result[i] != 1.0)
1494 n_samples, offset, a_length, b_length,
1495 "result a value error");
1509 else if(n_samples != 0
1510 && a_length >= offset + n_samples
1511 && n_samples >= b_length)
1513 current_run[9] =
true;
1516 if(result_length != a_length)
1519 n_samples, offset, a_length, b_length,
1520 "result length error");
1524 for(
uint32 i = 0; i < offset; ++i)
1526 if(result[i] != 1.0)
1529 n_samples, offset, a_length, b_length,
1530 "result prefix error");
1535 for(
uint32 i = offset + 1; i < offset + b_length; ++i)
1537 if(result[i] != 2.0)
1540 n_samples, offset, a_length, b_length,
1541 "result overlap error");
1546 for(
uint32 i = offset + b_length + 1; i < result_length; ++i)
1548 if(result[i] != 1.0)
1551 result.
plot(
"result");
1554 n_samples, offset, a_length, b_length,
1555 "result a value error");
1569 else if(n_samples != 0
1570 && a_length > offset + b_length
1571 && a_length < offset + n_samples)
1573 current_run[10] =
true;
1576 if(result_length != offset + n_samples)
1579 n_samples, offset, a_length, b_length,
1580 "result length error");
1584 for(
uint32 i = 0; i < offset; ++i)
1586 if(result[i] != 1.0)
1589 n_samples, offset, a_length, b_length,
1590 "result prefix error");
1595 for(
uint32 i = offset + 1; i < offset + b_length; ++i)
1597 if(result[i] != 2.0)
1600 n_samples, offset, a_length, b_length,
1601 "result overlap error");
1606 for(
uint32 i = offset + b_length + 1; i < a_length; ++i)
1608 if(result[i] != 1.0)
1611 n_samples, offset, a_length, b_length,
1612 "result a value error");
1617 for(
uint32 i = a_length + 1; i < result_length; ++i)
1619 if(result[i] != 0.0)
1622 n_samples, offset, a_length, b_length,
1623 "result tail error");
1640 for(
uint32 i = 0; i < 11; ++i)
1643 final_case[i] =
true;
1649 cout <<
"cases not hit: ";
1651 for(
uint32 i = 0; i < 11; ++i)
1654 cout << i + 1 <<
" ";
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
static void show()
Acutally draw the plots to the screen.
void reportError(const std::string &filename, unsigned int line_number, unsigned int n_samples, unsigned int offset, unsigned int a_length, unsigned int b_length, const std::string &message)
void plot(const std::string &title="Buffer") const
Requires matplotlib. Creates a plot of this Buffer.
A circulator iterator for class Buffer.
Buffer getAbs() const
Modifies a copy of the Buffer by making any negative value positive.
uint32 getLength() const
Returns the number of samples in the Buffer.
#define TEST_ERROR_HEADER
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
void smooth(uint32 n_passes, uint32 n_samples_per_average)
Applies a moving average filter to smooth this Buffer.
static Buffer ones(const uint32 n_samples)
Returns a Buffer full of ones of length n_samples.
void reverse()
Reverses the samples in this Buffer.
void abs()
Modifies the Buffer by making any negative value positive.
A Buffer for storing audio samples.
std::vector< boolean > BooleanVector
float64 getMin() const
Returns the minimum sample value in the Buffer.
static const char * THIS_FILE
static void setDefaultSampleSize(uint32 size)
Buffer drawLine(const float64 &duration, const float64 &litude_start, const float64 &litude_finish) const
This method draws a linear line beteween 2 points.
float64 getMax() const
Returns the maximum sample value in the Buffer.
void add(const Buffer &buffer, uint32 offset=0, uint32 n_samples=0)
This method adds buffer to *this.
static const float64 GAMMA