Score167Tests.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // --- Edison's Convert to score and dump to piano roll (core/NewtoneFx.h detectScore / dumpScore) - round 167c (§4)
  2. #include "core/NewtoneFx.h"
  3. TEST (score167_detects_the_melody)
  4. {
  5. using namespace lynx;
  6. // C4, E4, G4 (0.5 s each, 0.1 s apart), all sung 0.2 semitone sharp; E4 at half the level
  7. const double rate = 44100.0, sharp = 0.2;
  8. SampleData s;
  9. s.sampleRate = rate;
  10. s.left.assign ((std::size_t) (rate * 2.0), 0.0f);
  11. const int keys[] = { 60, 64, 67 };
  12. const float amps[] = { 0.5f, 0.25f, 0.5f };
  13. for (int k = 0; k < 3; ++k)
  14. {
  15. const auto from = (std::size_t) (rate * (0.1 + 0.6 * k)), to = from + (std::size_t) (rate * 0.5);
  16. const auto hz = 440.0 * std::pow (2.0, (keys[k] + sharp - 69) / 12.0);
  17. for (auto i = from; i < to; ++i)
  18. {
  19. // a few harmonics, like a voice
  20. const auto t = (double) (i - from) / rate;
  21. double x = 0.0;
  22. for (int h = 1; h <= 4; ++h)
  23. x += std::sin (2.0 * 3.141592653589793 * hz * h * t) / h;
  24. s.left[i] = (float) (amps[k] * 0.6 * x);
  25. }
  26. }
  27. double detune = 0.0;
  28. const auto notes = newtonefx::detectScore (s, &detune);
  29. CHECK (notes.size() == 3u);
  30. CHECK (std::abs (detune - sharp) < 0.1); // (round 190kj: FL's whole-sample periods add up to +0.07 st here)
  31. if (notes.size() == 3u)
  32. {
  33. for (int k = 0; k < 3; ++k)
  34. {
  35. CHECK (notes[(std::size_t) k].key == keys[k]); // the detune is taken out: the written keys
  36. CHECK (std::abs ((double) notes[(std::size_t) k].start / rate - (0.1 + 0.6 * k)) < 0.03);
  37. CHECK (std::abs ((double) notes[(std::size_t) k].end / rate - (0.6 + 0.6 * k)) < 0.03);
  38. }
  39. // volume = 0.5 + 0.5 x level: the loudest notes full, the quieter one about 0.75
  40. CHECK (notes[0].velocity > 0.95f && notes[2].velocity > 0.95f && std::abs (notes[1].velocity - 0.75f) < 0.05f);
  41. }
  42. // into the current pattern, the selected channel's notes replaced, the other channels' kept
  43. Song song;
  44. song.bpm = 120.0;
  45. song.patterns.resize (2);
  46. song.currentPattern = 1;
  47. Note old;
  48. old.channel = 2;
  49. old.key = 30;
  50. song.patterns[1].notes.push_back (old);
  51. old.channel = 0;
  52. song.patterns[1].notes.push_back (old);
  53. auto withShort = notes;
  54. newtonefx::DetectedNote tiny;
  55. tiny.start = 0;
  56. tiny.end = (std::int64_t) (rate * 0.01); // 0.01 s at 120 BPM = 19 ticks: left out (3 ticks at FL's PPQ 96)
  57. withShort.push_back (tiny);
  58. const auto count = newtonefx::dumpScore (song, 2, withShort, rate);
  59. CHECK (count == (int) notes.size());
  60. int ofTwo = 0, ofZero = 0;
  61. for (const auto& n : song.patterns[1].notes)
  62. (n.channel == 2 ? ofTwo : ofZero) += 1;
  63. CHECK (ofTwo == (int) notes.size() && ofZero == 1 && song.patterns[0].notes.empty());
  64. if (! notes.empty())
  65. {
  66. const auto& first = song.patterns[1].notes[0].channel == 2 ? song.patterns[1].notes[0] : song.patterns[1].notes[1];
  67. CHECK (first.start == std::llround ((double) notes[0].start / rate * 2.0 * ppq) && first.key == 60);
  68. }
  69. }