WaveAnalysisTests.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Spectral waveform analysis (feature/spectral-wave): band mix, peaks/RMS, mip levels, disk-cache format.
  2. // Included at the end of tests.cpp.
  3. #include "core/WaveAnalysis.h"
  4. namespace
  5. {
  6. std::vector<float> waveSine (double hz, float amplitude, std::size_t frames, double rate = 48000.0)
  7. {
  8. std::vector<float> v (frames);
  9. for (std::size_t i = 0; i < frames; ++i)
  10. v[i] = amplitude * (float) std::sin (2.0 * 3.14159265358979323846 * hz * (double) i / rate);
  11. return v;
  12. }
  13. std::vector<float> waveNoise (float amplitude, std::size_t frames)
  14. {
  15. std::vector<float> v (frames);
  16. std::uint32_t x = 0x12345678u;
  17. for (auto& s : v)
  18. {
  19. x ^= x << 13;
  20. x ^= x >> 17;
  21. x ^= x << 5;
  22. s = amplitude * ((float) (x & 0xffffff) / (float) 0x800000 - 1.0f);
  23. }
  24. return v;
  25. }
  26. lynx::wave::BandMix waveMixOf (const std::vector<float>& v)
  27. {
  28. const auto s = lynx::wave::analyse (v.data(), nullptr, v.size(), 48000.0);
  29. // skip the first 0.1 s (filter settling)
  30. const auto p = s.query (4800.0, (double) v.size(), -1);
  31. return lynx::wave::bandMix (p.low, p.mid, p.high);
  32. }
  33. } // namespace
  34. TEST (wave_band_mix_follows_the_spectrum)
  35. {
  36. const auto frames = (std::size_t) 48000;
  37. const auto low = waveMixOf (waveSine (60.0, 0.8f, frames));
  38. const auto mid = waveMixOf (waveSine (1000.0, 0.8f, frames));
  39. const auto high = waveMixOf (waveSine (9000.0, 0.8f, frames));
  40. const auto noise = waveMixOf (waveNoise (0.8f, frames));
  41. CHECK (low.low > 0.95f && low.mid < 0.05f && low.high < 0.01f);
  42. CHECK (mid.mid > 0.95f && mid.low < 0.05f && mid.high < 0.05f);
  43. CHECK (high.high > 0.9f && high.low < 0.01f);
  44. // white noise: energy grows with bandwidth, so mostly "high", some "mid", little "low"
  45. CHECK (noise.high > noise.mid && noise.mid > noise.low && noise.mid > 0.05f && noise.low < 0.1f);
  46. CHECK (std::abs (noise.low + noise.mid + noise.high - 1.0f) < 1.0e-4f);
  47. // bass + hats mixed at musical levels: both colours present
  48. auto mix = waveSine (55.0, 0.6f, frames);
  49. const auto hats = waveNoise (0.08f, frames);
  50. for (std::size_t i = 0; i < frames; ++i)
  51. mix[i] += hats[i];
  52. const auto both = waveMixOf (mix);
  53. CHECK (both.low > 0.6f && both.high > 0.1f && both.high < 0.3f); // measured: 0.84 / 0.01 / 0.15
  54. // silence is neutral, never NaN
  55. const auto silent = lynx::wave::bandMix (0.0f, 0.0f, 0.0f);
  56. CHECK (silent.mid == 1.0f && silent.low == 0.0f && silent.high == 0.0f);
  57. }
  58. TEST (wave_spectral_colours_are_warm_to_cool)
  59. {
  60. using namespace lynx::wave;
  61. const auto warm = spectralColour (BandMix { 1.0f, 0.0f, 0.0f });
  62. const auto neutral = spectralColour (BandMix { 0.0f, 1.0f, 0.0f });
  63. const auto cool = spectralColour (BandMix { 0.0f, 0.0f, 1.0f });
  64. CHECK (warm.r > 0.9f && warm.r > warm.g && warm.g > warm.b);
  65. CHECK (neutral.g > neutral.r && neutral.g > neutral.b);
  66. CHECK (cool.b > 0.9f && cool.b > cool.r);
  67. // a blend stays bright and between its anchors
  68. const auto half = spectralColour (BandMix { 0.5f, 0.0f, 0.5f });
  69. CHECK (std::max ({ half.r, half.g, half.b }) >= 0.89f);
  70. CHECK (half.r < warm.r + 1e-6f && half.b > warm.b && half.b < cool.b + 1e-6f);
  71. for (float l = 0.0f; l <= 1.0f; l += 0.1f)
  72. for (float h = 0.0f; l + h <= 1.0f; h += 0.1f)
  73. {
  74. const auto c = spectralColour (BandMix { l, 1.0f - l - h, h });
  75. CHECK (c.r >= 0.0f && c.r <= 1.0f && c.g >= 0.0f && c.g <= 1.0f && c.b >= 0.0f && c.b <= 1.0f);
  76. }
  77. }
  78. TEST (wave_peaks_rms_and_levels)
  79. {
  80. using namespace lynx::wave;
  81. const auto frames = (std::size_t) 200000;
  82. const auto v = waveSine (440.0, 0.5f, frames);
  83. const auto s = analyse (v.data(), nullptr, frames, 48000.0);
  84. CHECK (s.channels == 1 && s.frames == frames && s.bucket == minBucket && s.scale == 1.0f);
  85. CHECK (std::abs (s.peak - 0.5f) < 1e-3f);
  86. CHECK (s.levels.size() > 5 && s.levels.back().size() == 1);
  87. CHECK (s.levels[0].size() == (frames + minBucket - 1) / minBucket);
  88. for (std::size_t l = 1; l < s.levels.size(); ++l)
  89. CHECK (s.levels[l].size() == (s.levels[l - 1].size() + levelFactor - 1) / levelFactor);
  90. // any zoom: min/max/RMS of a sine (RMS = A / sqrt 2)
  91. for (const double span : { 1000.0, 5000.0, 60000.0, 190000.0 })
  92. {
  93. const auto p = s.query (1000.0, 1000.0 + span, 0);
  94. CHECK (p.valid);
  95. CHECK (std::abs (p.hi - 0.5f) < 0.01f && std::abs (p.lo + 0.5f) < 0.01f);
  96. CHECK (std::abs (p.rms - 0.5f / std::sqrt (2.0f)) < 0.01f);
  97. }
  98. // closer than one column: interpolated, still within the sine's range and continuous
  99. float previous = -1.0f;
  100. for (double f = 50000.0; f < 50000.0 + 4.0 * minBucket; f += 4.0)
  101. {
  102. const auto p = s.query (f, f + 4.0, 0);
  103. CHECK (p.valid && p.hi <= 0.51f && p.lo >= -0.51f && p.rms > 0.3f);
  104. if (previous >= 0.0f)
  105. CHECK (std::abs (p.rms - previous) < 0.02f);
  106. previous = p.rms;
  107. }
  108. // past the end: invalid; a range overlapping the end is clamped
  109. CHECK (! s.query ((double) frames, (double) frames + 100.0, 0).valid);
  110. CHECK (s.query ((double) frames - 10.0, (double) frames + 1000.0, 0).valid);
  111. CHECK (! analyse (nullptr, nullptr, 0, 48000.0).query (0.0, 10.0, -1).valid);
  112. CHECK (s.memoryBytes() < frames / minBucket * sizeof (Column) * 3 / 2 + 4096);
  113. }
  114. TEST (wave_stereo_and_loud_float_files)
  115. {
  116. using namespace lynx::wave;
  117. const auto frames = (std::size_t) 20000;
  118. const std::vector<float> silent (frames, 0.0f);
  119. auto loud = waveSine (100.0, 2.0f, frames); // float file over 0 dBFS
  120. const auto s = analyse (silent.data(), loud.data(), frames, 44100.0);
  121. CHECK (s.channels == 2 && std::abs (s.scale - 2.0f) < 0.01f);
  122. const auto l = s.query (0.0, (double) frames, 0), r = s.query (0.0, (double) frames, 1), both = s.query (0.0, (double) frames, -1);
  123. CHECK (l.rms < 1e-3f && l.hi < 1e-3f);
  124. CHECK (std::abs (r.hi - 2.0f) < 0.02f && std::abs (r.lo + 2.0f) < 0.02f);
  125. CHECK (std::abs (both.hi - 2.0f) < 0.02f && both.rms > l.rms && both.rms < r.rms);
  126. // the same pointer twice is mono
  127. CHECK (analyse (loud.data(), loud.data(), frames, 44100.0).channels == 1);
  128. // cancelled: empty
  129. std::atomic<bool> cancel { true };
  130. CHECK (analyse (loud.data(), nullptr, frames, 44100.0, &cancel).levels.empty());
  131. }
  132. TEST (wave_disk_cache_round_trip)
  133. {
  134. using namespace lynx::wave;
  135. const auto v = waveNoise (0.7f, 77777);
  136. const auto s = analyse (v.data(), nullptr, v.size(), 44100.0);
  137. const auto bytes = serialise (s);
  138. Summary back;
  139. CHECK (deserialise (bytes.data(), bytes.size(), back));
  140. CHECK (back.frames == s.frames && back.channels == s.channels && back.bucket == s.bucket && back.sampleRate == s.sampleRate);
  141. CHECK (back.levels.size() == s.levels.size());
  142. bool same = true;
  143. for (std::size_t l = 0; l < s.levels.size(); ++l)
  144. same = same && std::memcmp (back.levels[l].data(), s.levels[l].data(), s.levels[l].size() * sizeof (Column)) == 0;
  145. CHECK (same);
  146. const auto a = s.query (100.0, 30000.0, 0), b = back.query (100.0, 30000.0, 0);
  147. CHECK (a.hi == b.hi && a.rms == b.rms && a.high == b.high);
  148. // damaged files are rejected (truncated, extra bytes, other version, wrong magic)
  149. Summary junk;
  150. CHECK (! deserialise (bytes.data(), bytes.size() - 1, junk));
  151. auto longer = bytes;
  152. longer.push_back (0);
  153. CHECK (! deserialise (longer.data(), longer.size(), junk));
  154. auto other = bytes;
  155. other[4] ^= 0xff;
  156. CHECK (! deserialise (other.data(), other.size(), junk));
  157. other = bytes;
  158. other[0] ^= 0xff;
  159. CHECK (! deserialise (other.data(), other.size(), junk));
  160. CHECK (! deserialise (nullptr, 0, junk));
  161. }