You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.8KB

  1. #include "asserts.h"
  2. #include "MidiEditorContext.h"
  3. #include "MidiLock.h"
  4. #include "MidiSong.h"
  5. static void testReleaseSong()
  6. {
  7. MidiSongPtr song(std::make_shared<MidiSong>());
  8. MidiLocker l(song->lock);
  9. MidiEditorContext vp(song);
  10. {
  11. // vp._song = song;
  12. song->createTrack(0);
  13. auto track = song->getTrack(0);
  14. MidiNoteEventPtr ev = std::make_shared<MidiNoteEvent>();
  15. track->insertEvent(ev);
  16. assertEvCount(1); // one obj with two refs
  17. }
  18. song.reset(); // give up the one strong ref
  19. assertEvCount(0);
  20. }
  21. static void testEventAccess()
  22. {
  23. MidiSongPtr song(std::make_shared<MidiSong>());
  24. MidiLocker l(song->lock);
  25. song->createTrack(0);
  26. auto track = song->getTrack(0);
  27. MidiNoteEventPtr ev = std::make_shared<MidiNoteEvent>();
  28. ev->startTime = 100;
  29. ev->pitchCV = 40;
  30. track->insertEvent(ev);
  31. MidiEditorContext vp(song);
  32. vp.setStartTime(90);
  33. vp.setEndTime(110);
  34. vp.setPitchLow(0);
  35. vp.setPitchHi(80);
  36. auto its = vp.getEvents();
  37. assertEQ(std::distance(its.first, its.second), 1);
  38. assert(its.first != its.second);
  39. auto i = its.first;
  40. auto x = i->second->startTime;
  41. its.first++;
  42. }
  43. static void testEventFilter()
  44. {
  45. MidiSongPtr song(std::make_shared<MidiSong>());
  46. MidiLocker l(song->lock);
  47. song->createTrack(0);
  48. auto track = song->getTrack(0);
  49. MidiNoteEventPtr ev = std::make_shared<MidiNoteEvent>();
  50. ev->startTime = 100;
  51. ev->pitchCV = 40;
  52. track->insertEvent(ev);
  53. MidiNoteEventPtr ev2 = std::make_shared<MidiNoteEvent>();
  54. ev2->startTime = 102;
  55. ev2->pitchCV = 50;
  56. ev2->startTime = 100;
  57. track->insertEvent(ev2);
  58. assertEQ(track->size(), 2);
  59. MidiEditorContext vp(song);
  60. vp.setStartTime(90);
  61. vp.setEndTime(110);
  62. vp.setPitchLow(3);
  63. vp.setPitchHi(45);
  64. auto its = vp.getEvents();
  65. assertEQ(std::distance(its.first, its.second), 1);
  66. }
  67. static void testDemoSong()
  68. {
  69. MidiSongPtr song = MidiSong::makeTest(MidiTrack::TestContent::eightQNotes, 0);
  70. MidiTrackPtr track = song->getTrack(0);
  71. const int numNotes = 8; // track0 of test song has 8 notes
  72. const int numEvents = 8 + 1; // got an end event
  73. assertEQ(std::distance(track->begin(), track->end()), numEvents);
  74. MidiEditorContext viewport(song);
  75. viewport.setTimeRange(0, 8); // two measures
  76. // try a crazy wide range
  77. MidiNoteEvent note1;
  78. note1.setPitch(-10, 0);
  79. MidiNoteEvent note2;
  80. note2.setPitch(10, 0);
  81. viewport.setPitchRange(note1.pitchCV, note2.pitchCV);
  82. MidiEditorContext::iterator_pair it = viewport.getEvents();
  83. assertEQ(std::distance(it.first, it.second), numNotes);
  84. // try inclusive pitch range
  85. viewport.setPitchRange(PitchUtils::pitchToCV(3, 0), // pitch of first note
  86. PitchUtils::pitchToCV(3, 7));
  87. it = viewport.getEvents();
  88. assertEQ(std::distance(it.first, it.second), numNotes);
  89. // reduce the pitch range to lose the highest note.
  90. viewport.setPitchHi(PitchUtils::pitchToCV(3, 7) - .01f);
  91. it = viewport.getEvents();
  92. assertEQ(std::distance(it.first, it.second), numNotes-1);
  93. // reduce the pitch range to lose the lowest note.
  94. viewport.setPitchLow(PitchUtils::pitchToCV(3, 0) + .01f);
  95. it = viewport.getEvents();
  96. assertEQ(std::distance(it.first, it.second), numNotes - 2);
  97. // try inclusive pitch range, but only half the time
  98. viewport.setPitchRange(PitchUtils::pitchToCV(3, 0), // pitch of first note
  99. PitchUtils::pitchToCV(3, 7));
  100. viewport.setEndTime(viewport.startTime() + 4); // one measures
  101. it = viewport.getEvents();
  102. assertEQ(std::distance(it.first, it.second), numNotes /2);
  103. }
  104. void testMidiViewport()
  105. {
  106. assertEvCount(0);
  107. testReleaseSong();
  108. testEventAccess();
  109. testEventFilter();
  110. testDemoSong();
  111. assertEvCount(0);
  112. }